|
玩法你可以控制飞机左右移动,躲避敌机子弹,同时发射自己的炮弹,将敌人击落!部署方案:1、代码如下图;2、将代码保存到一个python中,比如planeFight.py;3、在你的电脑中安装python环境,然后使用命令:“pyplaneFight.py”运行这个文件即可;代码importpygameimportrandom#初始化Pygamepygame.init()#设置屏幕尺寸screen_width=800screen_height=600screen=pygame.display.set_mode((screen_width,screen_height))#设置pygame.display.set_caption("飞机大战")#加载并缩放图片player_image=pygame.image.load("player.png")player_image=pygame.transform.scale(player_image,(50,50))enemy_image=pygame.image.load("enemy.png")enemy_image=pygame.transform.scale(enemy_image,(50,50))bullet_image=pygame.image.load("bullet.png")bullet_image=pygame.transform.scale(bullet_image,(10,30))#设置字体font=pygame.font.Font(None,36)#玩家类classPlayer(pygame.sprite.Sprite):def__init__(self):super(Player,self).__init__()self.image=player_imageself.rect=self.image.get_rect()self.rect.center=(screen_width//2,screen_height-50)self.speed=5self.lives=3#初始生命值self.score=0#初始分数defupdate(self):keys=pygame.key.get_pressed()ifkeys[pygame.K_LEFT]andself.rect.left>0:self.rect.x-=self.speedifkeys[pygame.K_RIGHT]andself.rect.rightscreen_height:self.rect.x=random.randint(0,screen_width-self.rect.width)self.rect.y=random.randint(-100,-40)self.speed=random.randint(1,3)#子弹类classBullet(pygame.sprite.Sprite):def__init__(self,x,y):super(Bullet,self).__init__()self.image=bullet_imageself.rect=self.image.get_rect()self.rect.center=(x,y)self.speed=5defupdate(self):self.rect.y-=self.speedifself.rect.bottom
|
|