为什么我的飞机飞到底部没有消失,我调用的只是父类move_down啊
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | import random import pygame import constants from game.bullet import Bullet class Plane(pygame.sprite.Sprite): """ 飞机的基类 """ # # 飞机的图片 # plane_images = [] # # 飞机爆炸的图片 # destroy_images = [] # # 坠毁的音乐地址 # down_sound_src = None # # 飞机的状态:True 活得,False 死的 # active = True # # 该飞机发射的子弹精灵组 bullets = pygame.sprite.Group() def __init__( self ,screen, speed = None ): super ().__init__() self .screen = screen # 加载静态资源 self .img_list = [] self ._destroy_img_list = [] self .down_souund = 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.left = int (( self .width - self .plane_w) / 2 ) self .rect.top = int ( self .height / 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_img_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 def move_down( self ): """飞机向下移动""" self .rect.top + = self .speed def move_left( self ): """飞机向左移动""" self .rect.left - = self .speed def move_right( self ): """ 飞机向右移动 """ self .rect.right + = self .speed def broken_down( self ): """飞机坠毁效果""" #1.播放坠毁音乐 if self .down_sound: self .down_sound.play() #2.播放坠毁的动画 for img in self ._destory_img_list: self .screen.blit(img, self .rect) #3.坠毁后 self .active = False def shoot( self ): """飞机发射子弹""" bullet = Bullet( self .screen, self , 30 ) self .bullets.add(bullet) class OurPlane(Plane): """我方的飞机""" #飞机的图片 plane_images = constants.OUR_PLANE_IMG_LIST #飞机爆炸的图片 destroy_images = constants.OUR_DESTROY_IMG_LIST #坠毁的音乐地址 down_sound_src = None def update( self ,frame): """更新飞机动画的效果""" if frame % 5 : self .screen.blit( self .img_list[ 0 ], self .rect) else : self .screen.blit( self .img_list[ 1 ], self .rect) def move_up( self ): """向上移动,超出范围之后,拉回来""" super ().move_up() if self .rect.top < = 0 : self .rect.top = 0 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_left( 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 SmallEnemyPlane(OurPlane): """敌方小型的飞机""" # 飞机的图片 plane_images = constants.SMALL_ENEMY_PLANE_IMG_LIST # 飞机爆炸的图片 destroy_images = constants.SMALL_ENEMY_DESTROY_IMG_LIST # 坠毁的音乐地址 down_sound_src = constants.SMALL_ENEMY_DOWN_SOUND 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() #超出范围后如何处理 #1.重用 if self .rect.top > = self .height: self .active = False #self.kill() self .reset() # todo 2.多线程、多进程 def reset( self ): """重置飞机的状态,达到复用的效果""" self .active = True # 改变飞机的随机位置 self .init_pos() |
3
收起
正在回答 回答被采纳积分+1
1.Python零基础入门
- 参与学习 人
- 提交作业 2727 份
- 解答问题 8160 个
想要进入Python Web、爬虫、人工智能等高薪领域,你需要掌握本阶段的Python基础知识,课程安排带你高效学习轻松入门,学完你也能听得懂Python工程师的行业梗。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧