当前位置: 首页 > 后端技术 > Python

用Python开发一款恐龙跑酷游戏,玩一玩

时间:2023-03-26 10:50:33 Python

相信很多人都玩过chrome浏览器提供的恐龙跑酷游戏。当我们断开网络或者直接在浏览器中输入地址“chrome://dino/”,我们就可以进入游戏了。今天我们用Python做一个类似的小游戏素材准备首先我们准备游戏需要的素材,比如恐龙图片,仙人掌图片,天空,地面等,我们把它们放在dino文件夹下。游戏逻辑我们使用Pygame制作游戏,首先初始化游戏页面。importpygame#初始化pygame.init()pygame.mixer.init()#设置窗口大小screen=pygame.display.set_mode((900,200))#设置标题pygame.display.set_caption("DinosaurJump")#使用系统自带的字体my_font=pygame.font.SysFont("arial",20)score=0#背景颜色bg_c??olor=(218,220,225)接下来我们将各种素材加载到内存中#加载普通恐龙dino_list=[]temp=""foriinrange(1,7):temp=pygame.image.load(f"dino/dino_run{i}.png")dino_list.append(temp)dino_rect=temp.get_rect()index=0#x初始值dino_rect.x=100#y初始值dino_rect.y=150#print(dino_rect)#设置y轴初始速度为0y_speed=0#起飞初始速度jumpSpeed=-20#模拟gravitygravity=2loadthegroundground=pygame.image.load("dino/ground.png")#加载仙人掌cactus=pygame.image.load("dino/cactus1.png")cactus_rect=cactus.get_rect()cactus_rect.x,cactus_rect.y=900,140#加载并重启restart=pygame.image.load("dino/restart.png")餐厅rt_rect=restart.get_rect()restart_rect.x,restart_rect.y=(900-restart.get_rect().width)/2,(200-restart.get_rect().height)/2+50#loadgameovergameover=pygame.image.load("dino/gameover.png")gameover_rect=gameover.get_rect()gameover_rect.x,gameover_rect.y=(900-gameover.get_rect().width)/2,(200-gameover.get_rect().height)/2#ground_speed=10ground_move_distance=0#clockclock=pygame.time.Clock()#doitdoitagainis_restart=Falsetext_color=(0,0,0)然后,我们通过while循环死掉tokeepthegameprogresswhileTrue:#30timespersecondclock.tick(30)...在上面的循环中,我们需要两种检测机制,事件检测和碰撞检测Eventdetection#Eventdetectionforeventinpygame.event.get():ifevent.type==pygame.QUIT:ifresult_flag:withopen("result.ini","w+")asf:f.write(str(best))sys.exit()#空格键检测ifevent.type==pygame.KEYDOWN:ifevent.key==pygame.K_SPACEanddino_rect.y==150:y_speed=jumpSpeed主要检测退出事件和空格键事件碰撞检测#碰撞检测ifdino_rect.colliderect(cactus_rect):whilenotis_restart:#eventdetectionforeventinpygame.event.get():ifevent.type==pygame.QUIT:ifresult_flag:withopen("result.ini","w+")asf:f.write(str(best))sys.exit()#空格键检测ifevent.type==pygame.KEYDOWN:ifevent.key==pygame.K_SPACE:is_restart=Truebg_c??olor=(218,220,225)ground_speed=10#重新设置图片screen.blit(restart,restart_rect)screen.blit(gameover,gameover_rect)pygame.display.update()为碰撞,只要恐龙撞到仙人掌,游戏就结束了超过。再次显示图片。由于我们希望游戏记录我们最好的成绩,所以我们对游戏记录采用本地文件存储的方式。当比赛结束时,我们可以根据当前的比赛成绩判断是否将新的成绩写入文件。就是计算跑步距离和最好成绩的代码#Statisticsdistancescore+=ground_speedscore_surface=my_font.render("Distance:"+str(score),True,text_color)#计算最好的分数result_flag=Falseifscore>=best:best=scoreresult_flag=Truebest_result=my_font.render("BestResult:"+str(best),True,text_color)我们还需要为不同的距离增加不同的游戏难度。毕竟距离越远,难度就越大。#改变背景颜色,如果score>4000则得分大于4000:bg_c??olor=(55,55,55)ground_speed=15text_color=(255,255,255)#改变背景颜色,如果score大于8000>8000:bg_c??olor=(220,20,60)ground_speed=20text_color=(255,255,255)#改变背景颜色,分数大于12000ifscore>12000:bg_c??olor=(25,25,112)ground_speed=25text_color=(255,255,255)#设置背景色screen.fill(bg_color)最后我们将所有加载到内存中的元素呈现在屏幕上#settingGroundimage1screen.blit(ground,(0-ground_move_distance,180))#设置地面图像2,右边框外screen.blit(ground,(900-ground_move_distance,180))#设置恐龙图像screen.blit(dino_list[index%6],dino_rect)#设置仙人掌图片screen.blit(cactus,cactus_rect)#设置得分screen.blit(score_surface,(780,20))#设置最佳得分screen.blit(best_result,(20,20))pygame.display.update()为了增加游戏性,我们添加背景音乐和跳跃音效pygame.mixer.music.load("background.mp3")pygame.mixer.music.play(-1,0)sound=pygame.mixer.Sound('preview.mp3')这样,一个简单易上手的恐龙跑酷游戏就完成了,我们来看看效果吧。文末你们的点赞和收藏是对我最大的鼓励!欢迎关注我,分享Python干货,交流Python技术。如果大家对文章有什么见解,或者有什么技术问题,欢迎在评论区留言讨论!