菜鸡源码,专注精品下载!
当前位置:首页 > 建站教程 > 建站知识

Python飞机小游戏源码及打包成品

发布时间:2024-01-05  栏目:建站知识   浏览:   分类:python教程 飞机游戏源码 飞机大战源码 Python源码 python打包

以下是一个简单的Python打飞机小游戏源码,使用了pygame库。你可以将这段代码复制到一个名为"plane_game.py"的文件中,然后使用Python运行它。为了打包成成品,你可以使用PyInstaller库将游戏打包成一个可执行文件。首先确保已经安装了pyinstaller库,然后在命令行中输入以下命令: ```bash pip install pyinstaller pyinstaller --onefile plane_game.py ``` 这将在dist文件夹中生成一个名为"plane_game.exe"的可执行文件。你可以将这个文件分发给其他人,他们无需安装Python或pygame库就可以运行游戏。

Python源码

用的pygame库,自带的random和os。程序运行需要的图片,声音和字体下载链接: https://pan.baidu.com/s/1KItG2usXOM_xcxcdHIixaw 提取码: qmwe

importpygameimportrandomimportosFPS=60WIDTH=500HEIGHT=600BLACK=(0,0,0)WHITE=(255,255,255)GREEN=(0,255,0)RED=(255,0,0)YELLOW=(255,255,0)#游戏初始化&创建视窗pygame.init()pygame.mixer.init()screen=pygame.display.set_mode((WIDTH,HEIGHT))pygame.display.set_caption("第一个游戏")clock=pygame.time.Clock()#载入图片background_img=pygame.image.load(os.path.join("img","background.png")).convert()player_img=pygame.image.load(os.path.join("img","player.png")).convert()player_mini_img=pygame.transform.scale(player_img,(25,19))player_mini_img.set_colorkey(BLACK)pygame.display.set_icon(player_mini_img)bullet_img=pygame.image.load(os.path.join("img","bullet.png")).convert()rock_imgs=[]foriinrange(7):rock_imgs.append(pygame.image.load(os.path.join("img",f"rock{i}.png")).convert())expl_anim={}expl_anim['lg']=[]expl_anim['sm']=[]expl_anim['player']=[]foriinrange(9):expl_img=pygame.image.load(os.path.join("img",f"expl{i}.png")).convert()expl_img.set_colorkey(BLACK)expl_anim['lg'].append(pygame.transform.scale(expl_img,(75,75)))expl_anim['sm'].append(pygame.transform.scale(expl_img,(30,30)))player_expl_img=pygame.image.load(os.path.join("img",f"player_expl{i}.png")).convert()player_expl_img.set_colorkey(BLACK)expl_anim['player'].append(player_expl_img)power_imgs={}power_imgs['shield']=pygame.image.load(os.path.join("img","shield.png")).convert()power_imgs['gun']=pygame.image.load(os.path.join("img","gun.png")).convert()#载入音乐shoot_sound=pygame.mixer.Sound(os.path.join("sound","shoot.wav"))gun_sound=pygame.mixer.Sound(os.path.join("sound","pow1.wav"))shield_sound=pygame.mixer.Sound(os.path.join("sound","pow0.wav"))die_sound=pygame.mixer.Sound(os.path.join("sound","rumble.ogg"))expl_sounds=[pygame.mixer.Sound(os.path.join("sound","expl0.wav")),pygame.mixer.Sound(os.path.join("sound","expl1.wav"))]pygame.mixer.music.load(os.path.join("sound","background.ogg"))pygame.mixer.music.set_volume(0.4)font_name=os.path.join("font.ttf")defdraw_text(surf,text,size,x,y):font=pygame.font.Font(font_name,size)text_surface=font.render(text,True,WHITE)text_rect=text_surface.get_rect()text_rect.centerx=xtext_rect.top=ysurf.blit(text_surface,text_rect)defnew_rock():r=Rock()all_sprites.add(r)rocks.add(r)defdraw_health(surf,hp,x,y):ifhp<0:hp=0BAR_LENGTH=100BAR_HEIGHT=10fill=(hp/100)*BAR_LENGTHoutline_rect=pygame.Rect(x,y,BAR_LENGTH,BAR_HEIGHT)fill_rect=pygame.Rect(x,y,fill,BAR_HEIGHT)pygame.draw.rect(surf,GREEN,fill_rect)pygame.draw.rect(surf,WHITE,outline_rect,2)defdraw_lives(surf,lives,img,x,y):foriinrange(lives):img_rect=img.get_rect()img_rect.x=x+32*iimg_rect.y=ysurf.blit(img,img_rect)defdraw_init():screen.blit(background_img,(0,0))draw_text(screen,'太空生存战!',64,WIDTH/2,HEIGHT/4)draw_text(screen,'←→移动飞船空格键发射子弹~',22,WIDTH/2,HEIGHT/2)draw_text(screen,'按任意键开始游戏',18,WIDTH/2,HEIGHT*3/4)pygame.display.update()waiting=Truewhilewaiting:clock.tick(FPS)#取得输入foreventinpygame.event.get():ifevent.type==pygame.QUIT:pygame.quit()returnTrueelifevent.type==pygame.KEYUP:waiting=FalsereturnFalseclassPlayer(pygame.sprite.Sprite):def__init__(self):pygame.sprite.Sprite.__init__(self)self.image=pygame.transform.scale(player_img,(50,38))self.image.set_colorkey(BLACK)self.rect=self.image.get_rect()self.radius=20#pygame.draw.circle(self.image,RED,self.rect.center,self.radius)self.rect.centerx=WIDTH/2self.rect.bottom=HEIGHT-10self.speedx=8self.health=100self.lives=3self.hidden=Falseself.hide_time=0self.gun=1self.gun_time=0defupdate(self):now=pygame.time.get_ticks()ifself.gun>1andnow-self.gun_time>5000:self.gun-=1self.gun_time=nowifself.hiddenandnow-self.hide_time>1000:self.hidden=Falseself.rect.centerx=WIDTH/2self.rect.bottom=HEIGHT-10key_pressed=pygame.key.get_pressed()ifkey_pressed[pygame.K_d]:self.rect.x+=self.speedxifkey_pressed[pygame.K_a]:self.rect.x-=self.speedxifself.rect.right>WIDTH:self.rect.right=WIDTHifself.rect.left<0:self.rect.left=0defshoot(self):ifnot(self.hidden):ifself.gun==1:bullet=Bullet(self.rect.centerx,self.rect.top)all_sprites.add(bullet)bullets.add(bullet)shoot_sound.play()elifself.gun>=2:bullet1=Bullet(self.rect.left,self.rect.centery)bullet2=Bullet(self.rect.right,self.rect.centery)all_sprites.add(bullet1)all_sprites.add(bullet2)bullets.add(bullet1)bullets.add(bullet2)shoot_sound.play()defhide(self):self.hidden=Trueself.hide_time=pygame.time.get_ticks()self.rect.center=(WIDTH/2,HEIGHT+500)defgunup(self):self.gun+=1self.gun_time=pygame.time.get_ticks()classRock(pygame.sprite.Sprite):def__init__(self):pygame.sprite.Sprite.__init__(self)self.image_ori=random.choice(rock_imgs)self.image_ori.set_colorkey(BLACK)self.image=self.image_ori.copy()self.rect=self.image.get_rect()self.radius=int(self.rect.width*0.85/2)#pygame.draw.circle(self.image,RED,self.rect.center,self.radius)self.rect.x=random.randrange(0,WIDTH-self.rect.width)self.rect.y=random.randrange(-180,-100)self.speedy=random.randrange(2,10)self.speedx=random.randrange(-3,3)self.total_degree=0self.rot_degree=random.randrange(-3,3)defrotate(self):self.total_degree+=self.rot_degreeself.total_degree=self.total_degree%360self.image=pygame.transform.rotate(self.image_ori,self.total_degree)center=self.rect.centerself.rect=self.image.get_rect()self.rect.center=centerdefupdate(self):self.rotate()self.rect.y+=self.speedyself.rect.x+=self.speedxifself.rect.top>HEIGHTorself.rect.left>WIDTHorself.rect.right<0:self.rect.x=random.randrange(0,WIDTH-self.rect.width)self.rect.y=random.randrange(-100,-40)self.speedy=random.randrange(2,10)self.speedx=random.randrange(-3,3)classBullet(pygame.sprite.Sprite):def__init__(self,x,y):pygame.sprite.Sprite.__init__(self)self.image=bullet_imgself.image.set_colorkey(BLACK)self.rect=self.image.get_rect()self.rect.centerx=xself.rect.bottom=yself.speedy=-10defupdate(self):self.rect.y+=self.speedyifself.rect.bottom<0:self.kill()classExplosion(pygame.sprite.Sprite):def__init__(self,center,size):pygame.sprite.Sprite.__init__(self)self.size=sizeself.image=expl_anim[self.size][0]self.rect=self.image.get_rect()self.rect.center=centerself.frame=0self.last_update=pygame.time.get_ticks()self.frame_rate=50defupdate(self):now=pygame.time.get_ticks()ifnow-self.last_update>self.frame_rate:self.last_update=nowself.frame+=1ifself.frame==len(expl_anim[self.size]):self.kill()else:self.image=expl_anim[self.size][self.frame]center=self.rect.centerself.rect=self.image.get_rect()self.rect.center=centerclassPower(pygame.sprite.Sprite):def__init__(self,center):pygame.sprite.Sprite.__init__(self)self.type=random.choice(['shield','gun'])self.image=power_imgs[self.type]self.image.set_colorkey(BLACK)self.rect=self.image.get_rect()self.rect.center=centerself.speedy=3defupdate(self):self.rect.y+=self.speedyifself.rect.top>HEIGHT:self.kill()pygame.mixer.music.play(-1)#游戏回圈show_init=Truerunning=Truewhilerunning:ifshow_init:close=draw_init()ifclose:breakshow_init=Falseall_sprites=pygame.sprite.Group()rocks=pygame.sprite.Group()bullets=pygame.sprite.Group()powers=pygame.sprite.Group()player=Player()all_sprites.add(player)foriinrange(8):new_rock()score=0clock.tick(FPS)#取得输入foreventinpygame.event.get():ifevent.type==pygame.QUIT:running=Falseelifevent.type==pygame.KEYDOWN:ifevent.key==pygame.K_SPACE:player.shoot()#更新游戏all_sprites.update()#判断石头子弹相撞hits=pygame.sprite.groupcollide(rocks,bullets,True,True)forhitinhits:random.choice(expl_sounds).play()score+=hit.radiusexpl=Explosion(hit.rect.center,'lg')all_sprites.add(expl)ifrandom.random()>0.5:pow=Power(hit.rect.center)all_sprites.add(pow)powers.add(pow)new_rock()#判断石头飞船相撞hits=pygame.sprite.spritecollide(player,rocks,True,pygame.sprite.collide_circle)forhitinhits:new_rock()player.health-=hit.radiusexpl=Explosion(hit.rect.center,'sm')all_sprites.add(expl)ifplayer.health<=0:death_expl=Explosion(player.rect.center,'player')all_sprites.add(death_expl)die_sound.play()player.lives-=1player.health=100player.hide()#判断宝物飞船相撞hits=pygame.sprite.spritecollide(player,powers,True)forhitinhits:ifhit.type=='shield':player.health+=20ifplayer.health>100:player.health=100shield_sound.play()elifhit.type=='gun':player.gunup()gun_sound.play()ifplayer.lives==0andnot(death_expl.alive()):show_init=True#画面显示screen.fill(BLACK)screen.blit(background_img,(0,0))all_sprites.draw(screen)draw_text(screen,str(score),18,WIDTH/2,10)draw_health(screen,player.health,5,15)draw_lives(screen,player.lives,player_mini_img,WIDTH-100,15)pygame.display.update()pygame.quit()

成品打包下载地址:

链接:https://pan.baidu.com/s/1v8yegLMmOCYd_9Y2ZtrwSg
提取码:0uyf

评论
建站知识
建站知识
使用技巧
调试安装
运营推广