from random import random
import pygame
import constants
from game.bullet import Bullet
class Plane(pygame.sprite.Sprite):
plane_images = []
destroy_images = []
down_sound_src = None
active = True
bullets = pygame.sprite.Group()
def __init__(self,srceen,speed = None):
super().__init__()
self.screen = srceen
self.img_list = []
self._destroy_list = []
self.down_sound = None
self.load_src()
self.speed = speed or 10
self.rect = self.img_list[0].get_rect()
self.plane_w, self.plane_h = self.img_list[0].get_size()
self.width,self. height = self.screen.get_size()
self.rect.top = int(self.height / 2)
self.rect.left = int((self.width - self.plane_w) / 2)
def load_src(self):
for img in self.plane_images:
self.img_list.append(pygame.image.load(img))
for img in self.destroy_images:
self._destroy_list.append(pygame.image.load(img))
if self.down_sound_src:
self.down_sound = pygame.mixer.Sound(self.down_sound_src)
@property
def image(self):
return self.img_list[0]
def blit_me(self):
self.screen.blit(self.image,self.rect)
def move_up(self):
self.rect.top -= self.speed
这是plane的前半部分
2020-09-04 17:55:39
def move_down(self):
self.rect.top += self.speed
def move_left(self):
self.rect.left -= self.speed
def move_right(self):
self.rect.left += self.speed
def broken_down(self):
if self.down_sound:
self.down_sound.play()
for img in self._destroy_list:
self.screen.blit(img,self.rect)
self.active = False
def shoot(self):
bullet = Bullet(self.screen,self,15)
self.bullets.add(bullet)
class OurPlane(Plane):
plane_images = constants.OUR_PLANE_IMG_List
destroy_images = constants.Destroy_Image_list
down_sound_src = None
def update(self, war):
if war.frame % 5 :
self.screen.blit(self.img_list[0],self.rect)
else :
self.screen.blit(self.img_list[1], self.rect)
rest = pygame.sprite.spritecollide(self,war.Enemy_Group2,False)
if rest:
war.status = war.Over
war.Enemy_Group2.empty()
war.Enemy_Group.empty()
self.broken_down()
def move_up(self):
super().move_up()
if self.rect.top <= 0 :
self.rect.top = 0
plane的中半部分
2020-09-04 17:56:13
def move_down(self):
super().move_down()
if self.rect.top >= self.height - self.plane_h :
self.rect.top = self.height - self.plane_h
def move_lef(self):
super().move_left()
if self.rect.left <= 0:
self.rect.left = 0
def move_right(self):
super().move_right()
if self.rect.left >= self.width - self.plane_w:
self.rect.left = self.width - self.plane_w
class Enemy_P(Plane):
plane_images = constants.Enemy_P_I
destroy_images = constants.Enemy_P_D_List
down_sound_src = constants.Enemy_P_S
def __init__(self,screen,speed):
super().__init__(screen,speed)
self.init_pos()
def init_pos(self):
self.rect.left = random.randint(0, self.width - self.plane_w)
self.rect.top = random.randint(-5 * self.plane_h, -self.plane_h)
def update(self, *args):
super().move_down()
self.blit_me()
if self.rect.top >= self.height:
active = False
self.reset()
# self.kill()
def reset(self):
self.active = True
self.init_pos()
def Broken_Down(self):
super().broken_down()
self.reset()
后半部分,还需要其他代码吗?
2020-09-04 17:57:12
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星