记得刚学python那会,作过一个飞机大战小项目,这个项目非常经典,可以帮助初学者提高动手能力,今天把它分享出来。
一、项目介绍
先放几张图片
二、项目实现
1.首先安装库
pipinstallpygame
2.主要python代码
importpygame frompygame.localsimport* importrandom #https://blog.csdn.net/qq_36079986/article/details/110395731 classHeroPlane(object):#创建一个飞机的英雄类 def__init__(self,screen_temp):#始化方法 self.x=200 self.y=500 self.image=pygame.image.load('./img/hero1.png') self.screen=screen_temp#因为在display方法中使用到了screen方法#但是在类中没有也不能调用#所以说以传参的方法传递到类中 self.bullet_list=[] defdisplay(self):#创建一个展现飞机的方法 herp_x1,herp_y1,herp_x2,herp_y2,herp_x3,herp_y3=-100,-100,-100,-100,-100,-100 self.screen.blit(self.image,(self.x,self.y)) forbulletinself.bullet_list:###遍历子弹类对象,赋值给bullet herp_x1,herp_y1,herp_x2,herp_y2,herp_x3,herp_y3=bullet.display()######bullet对象调用display方法,显示子弹 bullet.move() ifself.bullet_list[0].y<0: delself.bullet_list[0] returnherp_x1,herp_y1,herp_x2,herp_y2,herp_x3,herp_y3 defmove_left(self): self.x-=50 defmove_right(self): self.x+=50 defmove_up(self): self.y-=50 defmove_down(self): self.y+=50 deffire(self): print(len(self.bullet_list),'个子弹##########################################') self.bullet_list.append(HeroBullet(self.screen,self.x,self.y)) classHeroBullet(object): def__init__(self,screen_temp,x_temp,y_temp): self.x=x_temp self.y=y_temp self.image=pygame.image.load('./img/bullet.png') self.screen=screen_temp defdisplay(self): self.screen.blit(self.image,(self.x-22,self.y-22)) self.screen.blit(self.image,(self.x+39,self.y-22)) self.screen.blit(self.image,(self.x+100,self.y-22)) returnself.x-22,self.y-22,self.x+39,self.y-22,self.x+100,self.y-22 defmove(self): self.y-=1 classEnemyPlane(object): def__init__(self,screen_temp): self.ra=random.randint(0,2) self.x=random.randint(0,400) self.y=0 self.image=pygame.image.load('./img/enemy1.png') self.screen=screen_temp self.bullet_list=[] if0<=self.x<=540: ifself.ra==0: self.flag="left" elifself.ra==1: self.flag="right" else: self.flag='middle' defdisplay(self): self.screen.blit(self.image,(self.x,self.y)) defmove(self): ifself.x<0: self.flag="left" elifself.x>450: self.flag='right' ifself.flag=="left": self.x+=2 elifself.flag=='right': self.x-=2 self.y+=0.1 deffire(self): if(random.randint(0,50)==1): self.bullet_list.append(EnemyBullet(self.screen,self.x,self.y)) #print(len(self.bullet_list),'颗敌方子弹') forbulletinself.bullet_list: bullet.display() bullet.move() ifbullet.y>700: delbullet classEnemyBullet(object): def__init__(self,screen_temp,x,y): self.x=x+30 self.y=y+90 self.image=pygame.image.load('./img/bullet-1.gif') self.screen=screen_temp defdisplay(self): self.screen.blit(self.image,(self.x,self.y)) defmove(self): self.y+=10 defkey_control(hero_temp): foreventinpygame.event.get():#判断输入类型 ifevent.type==QUIT:#判断是否是点击了退出按钮 print("exit") exit() elifevent.type==KEYDOWN:#判断是否是按下了键#如果是键盘事件(键盘按下事件) ifevent.key==K_aorevent.key==K_LEFT: hero_temp.move_left() elifevent.key==K_dorevent.key==K_RIGHT: hero_temp.move_right() elifevent.key==K_worevent.key==K_UP: hero_temp.move_up() elifevent.key==K_sorevent.key==K_DOWN: hero_temp.move_down() elifevent.key==K_SPACE:#检测按键是否是空格键 hero_temp.fire() defmain(): screen=pygame.display.set_mode((480,650),0,32)#使用screen资源来创建背景#返回值是一个资源 pygame.display.set_caption("aircraftgame")#设置窗口标题 background=pygame.image.load('./img/background.png')##./当前路径../上一级路径/根目录 hero=HeroPlane(screen) enemy_list=[] j=0 whileTrue: screen.blit(background,(0,0))#资源中的blit方法能够加载游戏的画面 herp_x1,herp_y1,herp_x2,herp_y2,herp_x3,herp_y3=hero.display()#####刷新 iflen(enemy_list)<=10: enemy_list.append(EnemyPlane(screen)) ifenemy_list[0].y>=700: delenemy_list[0] forenemy_eachinenemy_list: #enemy_each.fire() enemy_each.move() enemy_each.display() #print(len(enemy_list),'个敌机') if(enemy_each.x<=herp_x1-10<=enemy_each.x+80andenemy_each.y-10<=herp_y1<=enemy_each.y+90)or(enemy_each.x-10<=herp_x2<=enemy_each.x+80andenemy_each.y-10<=herp_y2<=enemy_each.y+90)or(enemy_each.x-10<=herp_x3<=enemy_each.x+80andenemy_each.y-10<=herp_y3<=enemy_each.y+90): enemy_list.remove(enemy_each) #print(len(enemy_list),'个敌机') print('jizhon') #print(hero.x,type(hero.x)) ifj%100==5: print(len(enemy_list),'个敌机') pygame.display.update()###加载游戏 key_control(hero)#调用键盘事件函数 j+=1 #print(herp_x1,herp_y1,herp_x2,herp_y2,herp_x3,herp_y3) if__name__=='__main__': main()
三、说明:代码功能都在注释里面,我就不做过多解释了;与本项目有关的图片数据我传到资源里面了,大家可以免费下载,我把代码和数据都打包到一起了,大家下载下来导入库就可以直接一些了。
四、完整版代码及数据 下载地址(下载就能用)