找回密码
 会员注册
查看: 29|回复: 0

Python实战GoogleChrome的离线小恐龙游戏

[复制链接]

4

主题

0

回帖

13

积分

新手上路

积分
13
发表于 2024-9-7 15:01:10 | 显示全部楼层 |阅读模式
文章目录GoogleChrome的离线小恐龙游戏项目结构大纲📊👣逐步编码过程🧩💡第一步:项目初始化与主程序框架第二步:实现T-Rex的跳跃功能第三步:添加障碍物和碰撞检测第四步:添加得分机制和显示得分第五步:游戏结束处理和重新开始选项第六步:添加背景和地面效果图GoogleChrome的离线小恐龙游戏本文章通过详细的列举项目结构大纲和列举逐步编码过程和思路,便于学习者能够更加快速方便地掌握该游戏的开发。项目结构大纲📊👣t_rex_game/│├──main.py#主程序文件├──trex.py#T-Rex角色类├──assets/│└──trex.png#T-Rex图片文件├──obstacles.py#障碍物类12345678逐步编码过程🧩💡第一步:项目初始化与主程序框架main.pyimportpygameimportsysfromtreximportTRex#初始化pygamepygame.init()#设置屏幕尺寸screen_width=800screen_height=400screen=pygame.display.set_mode((screen_width,screen_height))#定义颜色white=(255,255,255)#设置帧率clock=pygame.time.Clock()fps=30#游戏主循环defgame_loop():t_rex=TRex(screen_width,screen_height)whileTrue:foreventinpygame.event.get():ifevent.type==pygame.QUIT:pygame.quit()sys.exit()screen.fill(white)t_rex.draw(screen)pygame.display.update()clock.tick(fps)if__name__=="__main__":game_loop()1234567891011121314151617181920212223242526272829303132333435363738trex.pyimportpygameimportosclassTRex:def__init__(self,screen_width,screen_height):#获取当前脚本文件所在的目录current_path=os.path.dirname(__file__)#拼接图片文件的完整路径image_path=os.path.join(current_path,'assets','trex.png')self.image=pygame.image.load(image_path)self.image=pygame.transform.scale(self.image,(50,50))#缩放图片self.x=50self.y=screen_height-70self.screen_width=screen_widthself.screen_height=screen_heightdefdraw(self,screen):screen.blit(self.image,(self.x,self.y))123456789101112131415161718注意点首先,trex.png指的是小恐龙的照片,要先保证照片能够正常显示;然后要使用pygame.transform.scale函数来缩放图片大小。调整后的图片大小为50x50像素,你可以根据需要调整这个尺寸。第二步:实现T-Rex的跳跃功能main.pyimportpygameimportsysfromtreximportTRex#初始化pygamepygame.init()#设置屏幕尺寸screen_width=800screen_height=400screen=pygame.display.set_mode((screen_width,screen_height))#定义颜色white=(255,255,255)#设置帧率clock=pygame.time.Clock()fps=30#游戏主循环defgame_loop():t_rex=TRex(screen_width,screen_height)whileTrue:foreventinpygame.event.get():ifevent.type==pygame.QUIT:pygame.quit()sys.exit()ifevent.type==pygame.KEYDOWN:ifevent.key==pygame.K_SPACE:t_rex.jump()t_rex.update()#更新T-Rex的状态screen.fill(white)t_rex.draw(screen)pygame.display.update()clock.tick(fps)if__name__=="__main__":game_loop()123456789101112131415161718192021222324252627282930313233343536373839404142trex.pyimportpygameimportosclassTRex:def__init__(self,screen_width,screen_height):#获取当前脚本文件所在的目录current_path=os.path.dirname(__file__)#拼接图片文件的完整路径image_path=os.path.join(current_path,'assets','trex.png')self.image=pygame.image.load(image_path)self.image=pygame.transform.scale(self.image,(50,50))#缩放图片self.x=50self.y=screen_height-70self.jump_speed=20#跳跃速度(增加)self.gravity=2#重力(增加)self.velocity=0#初始速度self.is_jumping=False#跳跃状态self.screen_width=screen_widthself.screen_height=screen_heightdefjump(self):ifnotself.is_jumping:self.is_jumping=Trueself.velocity=-self.jump_speeddefupdate(self):ifself.is_jumping:self.y+=self.velocityself.velocity+=self.gravity#检查是否着地ifself.y>=self.screen_height-70:self.y=self.screen_height-70self.is_jumping=Falseself.velocity=0defdraw(self,screen):screen.blit(self.image,(self.x,self.y))1234567891011121314151617181920212223242526272829303132333435363738注意点在完成跳跃功能时,我们会使小恐龙跳跃回弹到地面的时间尽可能短。我们可以通过设置:跳跃速度:将self.jump_speed从15增加到20。重力加速度:将self.gravity从1增加到2。第三步:添加障碍物和碰撞检测我们需要创建一个Obstacle类,并在主程序中生成障碍物。同时,实现T-Rex与障碍物的碰撞检测。项目结构大纲📊👣t_rex_game/│├──main.py#主程序文件├──trex.py#T-Rex角色类├──obstacles.py#障碍物类├──assets/│└──trex.png#T-Rex图片文件│└──cactus.png#障碍物图片文件└──utils.py#工具函数123456789main.pyimportpygameimportsysfromtreximportTRexfromobstaclesimportObstacleimportrandom#初始化pygamepygame.init()#设置屏幕尺寸screen_width=800screen_height=400screen=pygame.display.set_mode((screen_width,screen_height))#定义颜色white=(255,255,255)#设置帧率clock=pygame.time.Clock()fps=30#游戏主循环defgame_loop():t_rex=TRex(screen_width,screen_height)obstacles=[]defcreate_obstacle()bstacle=Obstacle(screen_width,screen_height)obstacles.append(obstacle)#每隔一段时间创建一个新障碍物obstacle_timer=0whileTrue:foreventinpygame.event.get():ifevent.type==pygame.QUIT:pygame.quit()sys.exit()ifevent.type==pygame.KEYDOWN:ifevent.key==pygame.K_SPACE:t_rex.jump()t_rex.update()#更新T-Rex的状态#创建新障碍物obstacle_timer+=1ifobstacle_timer>50:create_obstacle()obstacle_timer=0#更新障碍物状态forobstacleinobstaclesbstacle.update()#检测碰撞ift_rex.xobstacle.xand\t_rex.yobstacle.y:#碰撞检测到pygame.quit()sys.exit()#清除离开屏幕的障碍物obstacles=[obstacleforobstacleinobstaclesifobstacle.x+obstacle.width>0]screen.fill(white)t_rex.draw(screen)forobstacleinobstaclesbstacle.draw(screen)pygame.display.update()clock.tick(fps)if__name__=="__main__":game_loop()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576trex.pyimportpygameimportosclassTRex:def__init__(self,screen_width,screen_height):#获取当前脚本文件所在的目录current_path=os.path.dirname(__file__)#拼接图片文件的完整路径image_path=os.path.join(current_path,'assets','trex.png')self.image=pygame.image.load(image_path)self.image=pygame.transform.scale(self.image,(50,50))#缩放图片self.x=50self.y=screen_height-70self.width=50self.height=50self.jump_speed=20#跳跃速度self.gravity=2#重力self.velocity=0#初始速度self.is_jumping=False#跳跃状态self.screen_width=screen_widthself.screen_height=screen_heightdefjump(self):ifnotself.is_jumping:self.is_jumping=Trueself.velocity=-self.jump_speeddefupdate(self):ifself.is_jumping:self.y+=self.velocityself.velocity+=self.gravity#检查是否着地ifself.y>=self.screen_height-70:self.y=self.screen_height-70self.is_jumping=Falseself.velocity=0defdraw(self,screen):screen.blit(self.image,(self.x,self.y))1234567891011121314151617181920212223242526272829303132333435363738394041obstacles.pyimportpygameimportosclassObstacle:def__init__(self,screen_width,screen_height):#获取当前脚本文件所在的目录current_path=os.path.dirname(__file__)#拼接图片文件的完整路径image_path=os.path.join(current_path,'assets','cactus.png')self.image=pygame.image.load(image_path)self.image=pygame.transform.scale(self.image,(50,50))#缩放图片self.x=screen_widthself.y=screen_height-70self.width=50self.height=50self.speed=10defupdate(self):self.x-=self.speeddefdraw(self,screen):screen.blit(self.image,(self.x,self.y))1234567891011121314151617181920212223解释障碍物类:Obstacle类用于表示游戏中的障碍物。每个障碍物都有位置、大小和速度属性。创建和更新障碍物:在主程序中,我们定期创建新障碍物,并在每一帧更新它们的位置。碰撞检测:在主程序的每一帧,我们检查T-Rex与每个障碍物是否发生碰撞。如果发生碰撞,游戏结束。第四步:添加得分机制和显示得分main.pyimportpygameimportsysfromtreximportTRexfromobstaclesimportObstacleimportrandom#初始化pygamepygame.init()#设置屏幕尺寸screen_width=800screen_height=400screen=pygame.display.set_mode((screen_width,screen_height))#定义颜色white=(255,255,255)black=(0,0,0)#设置帧率clock=pygame.time.Clock()fps=30#游戏主循环defgame_loop():t_rex=TRex(screen_width,screen_height)obstacles=[]score=0defcreate_obstacle()bstacle=Obstacle(screen_width,screen_height)obstacles.append(obstacle)#每隔一段时间创建一个新障碍物obstacle_timer=0#创建字体对象font=pygame.font.Font(None,36)whileTrue:foreventinpygame.event.get():ifevent.type==pygame.QUIT:pygame.quit()sys.exit()ifevent.type==pygame.KEYDOWN:ifevent.key==pygame.K_SPACE:t_rex.jump()t_rex.update()#更新T-Rex的状态#创建新障碍物obstacle_timer+=1ifobstacle_timer>50:create_obstacle()obstacle_timer=0#更新障碍物状态forobstacleinobstaclesbstacle.update()#检测碰撞ift_rex.xobstacle.xand\t_rex.yobstacle.y:#碰撞检测到pygame.quit()sys.exit()#更新得分forobstacleinobstacles:ifobstacle.x+obstacle.width=self.screen_height-70:self.y=self.screen_height-70self.is_jumping=Falseself.velocity=0defdraw(self,screen):screen.blit(self.image,(self.x,self.y))1234567891011121314151617181920212223242526272829303132333435363738394041obstacles.pyimportpygameimportosclassObstacle:def__init__(self,screen_width,screen_height):#获取当前脚本文件所在的目录current_path=os.path.dirname(__file__)#拼接图片文件的完整路径image_path=os.path.join(current_path,'assets','cactus.png')self.image=pygame.image.load(image_path)self.image=pygame.transform.scale(self.image,(50,50))#缩放图片self.x=screen_widthself.y=screen_height-70self.width=50self.height=50self.speed=10defupdate(self):self.x-=self.speeddefdraw(self,screen):screen.blit(self.image,(self.x,self.y))1234567891011121314151617181920212223解释更新得分机制:在每一帧中,检查每个障碍物是否已经通过了T-Rex的位置(obstacle.x+obstacle.width50:create_obstacle()obstacle_timer=0#更新障碍物状态forobstacleinobstaclesbstacle.update()#检测碰撞ift_rex.xobstacle.xand\t_rex.yobstacle.y:#碰撞检测到,显示游戏结束画面show_game_over_screen(score)return#更新得分forobstacleinobstacles:ifobstacle.x+obstacle.width50:create_obstacle()obstacle_timer=0#更新障碍物状态forobstacleinobstaclesbstacle.update()#检测碰撞ift_rex.xobstacle.xand\t_rex.yobstacle.y:#碰撞检测到,显示游戏结束画面show_game_over_screen(score)return#更新得分forobstacleinobstacles:ifobstacle.x+obstacle.widthbg_color_change_interval:bg_color=blackifbg_color==whiteelsewhitebg_color_change_timer=0screen.fill(bg_color)screen.blit(ground,(0,screen_height-ground_height))#显示地面t_rex.draw(screen)forobstacleinobstaclesbstacle.draw(screen)#显示得分score_text=font.render(f'Score:{score}',True,blackifbg_color==whiteelsewhite)screen.blit(score_text,(10,10))pygame.display.update()clock.tick(fps)if__name__=="__main__":whileTrue:game_loop()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132trex.pyimportpygameimportosclassTRex:def__init__(self,screen_width,screen_height):#获取当前脚本文件所在的目录current_path=os.path.dirname(__file__)#拼接图片文件的完整路径image_path=os.path.join(current_path,'assets','trex.png')self.image=pygame.image.load(image_path)self.image=pygame.transform.scale(self.image,(50,50))#缩放图片self.x=50self.y=screen_height-70-20#调整位置以适应地面高度self.width=50self.height=50self.jump_speed=20#跳跃速度self.gravity=2#重力self.velocity=0#初始速度self.is_jumping=False#跳跃状态self.screen_width=screen_widthself.screen_height=screen_heightdefjump(self):ifnotself.is_jumping:self.is_jumping=Trueself.velocity=-self.jump_speeddefupdate(self):ifself.is_jumping:self.y+=self.velocityself.velocity+=self.gravity#检查是否着地ifself.y>=self.screen_height-70-20:self.y=self.screen_height-70-20self.is_jumping=Falseself.velocity=0defdraw(self,screen):screen.blit(self.image,(self.x,self.y))1234567891011121314151617181920212223242526272829303132333435363738394041obstacles.pyimportpygameimportosclassObstacle:def__init__(self,screen_width,screen_height):#获取当前脚本文件所在的目录current_path=os.path.dirname(__file__)#拼接图片文件的完整路径image_path=os.path.join(current_path,'assets','cactus.png')self.image=pygame.image.load(image_path)self.image=pygame.transform.scale(self.image,(50,50))#缩放图片self.x=screen_widthself.y=screen_height-70-20#调整位置以适应地面高度self.width=50self.height=50self.speed=10defupdate(self):self.x-=self.speeddefdraw(self,screen):screen.blit(self.image,(self.x,self.y))1234567891011121314151617181920212223解释背景颜色切换:在主程序中,我们使用一个计时器bg_color_change_timer来控制背景颜色的变化。每当计时器达到设定的间隔时,背景颜色在白色和黑色之间切换。地面调整:调整了T-Rex和障碍物的垂直位置以适应地面高度。效果图
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2025-1-11 05:52 , Processed in 0.438681 second(s), 26 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表