Python Pygame如何实现水果忍者游戏(pygame,python,开发技术)

时间:2024-04-29 15:58:02 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

一、准备中

1.0 游戏规则

Python版本的水果忍者小编初始化设置是玩家3条生命值,切到相应的水果相应加分,切到易爆物

比如炸弹这些就会相应的减少生命值,在生命值内可以一直切切切,切的越多分数越高,相应的生命值耗尽即结束游戏哦!快试试你能得几分?

哈哈哈,今天也录制了游戏视频的,看着视频更有玩游戏的感觉嘛~

1.1 游戏图片素材(可修改)

Python Pygame如何实现水果忍者游戏

Python Pygame如何实现水果忍者游戏

1.2 游戏字体素材(可修改)

Python Pygame如何实现水果忍者游戏

二、环境安装

本文用的Python3、Pycharm写的。模块Pygame、random随机出现水果以及一些自带的。

这里模块安装命令统一镜像源豆瓣:

pip install -i https://pypi.douban.com/simple/ +模块名

三、开始敲代码

3.0 设置界面玩家生命值等

player_lives=3#生命score=0#得分fruits=['melon','orange','pomegranate','guava','bomb']#水果和炸弹

3.1 导入模块

importpygame,sysimportosimportrandom

3.2 界面背景、字体设置

background=pygame.image.load('背景图/02.png')#背景font=pygame.font.Font(os.path.join(os.getcwd(),'字体/comic.ttf'),42)#字体score_text=font.render('Score:'+str(score),True,(255,255,255))#得分的字体样式

3.3 游戏窗口设置

WIDTH=800HEIGHT=500FPS=12#gameDisplay的帧率,1/12秒刷新一次pygame.init()pygame.display.set_caption('水果忍者_csdn账号:顾木子吖')#标题gameDisplay=pygame.display.set_mode((WIDTH,HEIGHT))#游戏窗口clock=pygame.time.Clock()

3.4 随机生成水果的位置与数据存放

defgenerate_random_fruits(fruit):fruit_path="images/"+fruit+".png"data[fruit]={'img':pygame.image.load(fruit_path),'x':random.randint(100,500),#水果在x坐标轴上的位置'y':800,'speed_x':random.randint(-10,10),#水果在x方向时的速度和对角线移动'speed_y':random.randint(-80,-60),#y方向时的速度'throw':False,#如果生成水果的位置在gameDisplay之外,将被丢弃't':0,'hit':False,}ifrandom.random()>=0.75:#返回在[0.0,1.0]范围内的下一个随机浮点数,以保持水果在游戏中的显示。data[fruit]['throw']=Trueelse:data[fruit]['throw']=False

3.5 用一个字典来存放水果的数据

data={}forfruitinfruits:generate_random_fruits(fruit)defhide_cross_lives(x,y):gameDisplay.blit(pygame.image.load("images/red_lives.png"),(x,y))

3.6 在屏幕中绘制字体

font_name=pygame.font.match_font('comic.ttf')defdraw_text(display,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.midtop=(x,y)gameDisplay.blit(text_surface,text_rect)

3.7 绘制玩家的生命

defdraw_lives(display,x,y,lives,image):foriinrange(lives):img=pygame.image.load(image)img_rect=img.get_rect()img_rect.x=int(x+35*i)img_rect.y=ydisplay.blit(img,img_rect)

3.8 游戏开始与结束画面

defshow_gameover_screen():gameDisplay.blit(background,(0,0))draw_text(gameDisplay,"FRUITNINJA!",90,WIDTH/2,HEIGHT/4)ifnotgame_over:draw_text(gameDisplay,"Score:"+str(score),50,WIDTH/2,HEIGHT/2)draw_text(gameDisplay,"Pressanykeytostartthegame",64,WIDTH/2,HEIGHT*3/4)pygame.display.flip()waiting=Truewhilewaiting:clock.tick(FPS)foreventinpygame.event.get():ifevent.type==pygame.QUIT:pygame.quit()ifevent.type==pygame.KEYUP:waiting=False

3.9 游戏主循环

first_round=Truegame_over=True#超过3个炸弹,终止游戏循环game_running=True#管理游戏循环whilegame_running:ifgame_over:iffirst_round:show_gameover_screen()first_round=Falsegame_over=Falseplayer_lives=3draw_lives(gameDisplay,690,5,player_lives,'images/red_lives.png')score=0foreventinpygame.event.get():#检查是否关闭窗口ifevent.type==pygame.QUIT:game_running=FalsegameDisplay.blit(background,(0,0))gameDisplay.blit(score_text,(0,0))draw_lives(gameDisplay,690,5,player_lives,'images/red_lives.png')forkey,valueindata.items():ifvalue['throw']:value['x']+=value['speed_x']#x方向上移动水果value['y']+=value['speed_y']#y方向上移动value['speed_y']+=(1*value['t'])#递增value['t']+=1ifvalue['y']<=800:gameDisplay.blit(value['img'],(value['x'],value['y']))#动态显示水果else:generate_random_fruits(key)current_position=pygame.mouse.get_pos()#获取鼠标的位置,单位为像素ifnotvalue['hit']andcurrent_position[0]>value['x']andcurrent_position[0]<value['x']+60\andcurrent_position[1]>value['y']andcurrent_position[1]<value['y']+60:ifkey=='bomb':player_lives-=1ifplayer_lives==0:hide_cross_lives(690,15)elifplayer_lives==1:hide_cross_lives(725,15)elifplayer_lives==2:hide_cross_lives(760,15)#超过3次炸弹,提示游戏结束,重置窗口ifplayer_lives<0:show_gameover_screen()game_over=Truehalf_fruit_path="images/explosion.png"else:half_fruit_path="images/"+"half_"+key+".png"value['img']=pygame.image.load(half_fruit_path)value['speed_x']+=10ifkey!='bomb':score+=1score_text=font.render('Score:'+str(score),True,(255,255,255))value['hit']=Trueelse:generate_random_fruits(key)pygame.display.update()clock.tick(FPS)pygame.quit()

四、游戏展示效果

4.1 Part 1 动态视频展示效果如下

视频链接

Python版水果忍者,有趣有趣~

4.2 Part 2 静态截图展示效果如下

(1)游戏进入界面&mdash;&mdash;

Python Pygame如何实现水果忍者游戏

(2)修改下背景图进入的界面&mdash;&mdash;这个感觉貌似好看点儿~

Python Pygame如何实现水果忍者游戏

4.3 Part 3 静态进入游戏界面截图如下

Python Pygame如何实现水果忍者游戏

Python Pygame如何实现水果忍者游戏

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Python Pygame如何实现水果忍者游戏的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Unity如何制作动画编辑器下一篇:

17 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18