子弹处理部分不太会,子弹生成之后,不会执行移动方法,跟随着按键一起动
发表在Python图书答疑
2018-12-17
《Python从入门到项目实践》第25章 飞机大战游戏
是否精华
是
否
版块置顶:
是
否
pygame pygame.locals * bullet play enemy width = height = screen = pygame.display.set_mode([width, height]) pygame.display.set_caption() title_image = pygame.image.load() bullet_img = pygame.image.load().convert_alpha() pygame.display.set_icon(title_image) background_image = pygame.image.load() Bullet(pygame.sprite.Sprite): (,bullet_img, init_pos): pygame.sprite.Sprite.() .image = bullet_img .rect = .image.get_rect() .rect.midbottom = init_pos .speed = .mask = pygame.mask.from_surface(.image) move(): .rect.top -= .speed reset(,init_pos): .rect.left,.rect.top = init_pos my_plane(): (): .rect = pygame.Rect(,,,) .plan_image = pygame.image.load().convert_alpha() .speed = .bullets = pygame.sprite.Group() draw(): screen.blit(.plan_image,(.rect.x,.rect.y)) shoot(, bullet_img): bullet = Bullet(bullet_img, .rect.midtop) .bullets.add(bullet) moveUp(): .rect.top <= : .rect.top = : .rect.top -= .speed moveDown(): .rect.top >= : .rect.top = : .rect.top += .speed moveLeft(): .rect.left <= -: .rect.left = -: .rect.left -= .speed moveRight(): .rect.right >= : .rect.right = : .rect.right += .speed main(): pygame.init() player = my_plane() width = height = screen = pygame.display.set_mode([width, height]) pygame.display.set_caption() title_image = pygame.image.load() pygame.display.set_icon(title_image) background_image = pygame.image.load() : event pygame.event.get(): event.type == QUIT: () key_pressed = pygame.key.get_pressed() key_pressed[K_w] key_pressed[K_UP]: player.moveUp() key_pressed[K_s] key_pressed[K_DOWN]: player.moveDown() key_pressed[K_a] key_pressed[K_LEFT]: player.moveLeft() key_pressed[K_d] key_pressed[K_RIGHT]: player.moveRight() bullet player.bullets: bullet.move() bullet.rect.bottom < : player.bullets.remove(bullet) screen.blit(background_image, (, )) bulleter = Bullet(bullet_img, player.rect.midtop) player.draw() player.shoot(bullet_img) player.bullets.draw(screen) bulleter.move() pygame.display.update() __name__ == : main()
于2018-12-17 11:44:46编辑