基于Python实现天天酷跑功能

python教程

共 1239字,需浏览 3分钟

 ·

2021-01-13 01:40


这篇文章主要介绍了基于Python实现天天酷跑功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下


感觉上次写的植物大战僵尸与俄罗斯方块的反应还不错,这次这个文章就更有动力了
这次就写一个天天酷跑吧

写出来的效果图就是这样了
下面就更新一下全部的代码吧
还是老样子先定义

  1. import pygame,sys

  2. import random

写一下游戏配置

  1. width = 1200 #窗口宽度

  2. height = 508 #窗口高度

  3. size = width, height

  4. score=None #分数

  5. myFont=myFont1=None #字体

  6. surObject=None #障碍物图片

  7. surGameOver=None #游戏结束图片

  8. bg=None #背景对象

  9. role=None #人物对象

  10. object=None #障碍物对象

  11. objectList=[] #障碍物对象数组

  12. clock=None #时钟

  13. gameState=None #游戏状态(0,1)表示(游戏中,游戏结束)

写人物

  1. class Role: #人物

  2. def __init__(self,surface=None,y=None):

  3. self.surface=surface

  4. self.y=y

  5. self.w=(surface.get_width())/12

  6. self.h=surface.get_height()/2

  7. self.currentFrame=-1

  8. self.state=0 #0代表跑步状态,1代表跳跃状态,2代表连续跳跃

  9. self.g=1 #重力加速度

  10. self.vy=0 #y轴速度

  11. self.vy_start=-20 #起跳开始速度

  12. def getRect(self):

  13. return (0,self.y+12,self.w,self.h)

写障碍物

  1. class Object: #障碍物

  2. def __init__(self,surface,x=0,y=0):

  3. self.surface=surface

  4. self.x=x

  5. self.y=y

  6. self.w=surface.get_width()

  7. self.h=surface.get_height()

  8. self.currentFrame=random.randint(0,6)

  9. self.= 100

  10. self.= 100

  11. def getRect(self):

  12. return (self.x,self.y,self.w,self.h)

  13. def collision(self,rect1,rect2):

  14. #碰撞检测

  15. if (rect2[0]>=rect1[2]-20) or (rect1[0]+40>=rect2[2])or (rect1[1]+rect1[3]<rect2[1]+20) or (rect2[1]+rect2[3]<rect1[1]+20):

  16. return False

  17. return True

写背景

  1. class Bg: #背景

  2. def __init__(self,surface):

  3. self.surface=surface

  4. self.dx=-10

  5. self.w=surface.get_width()

  6. self.rect=surface.get_rect()

  1. def initGame():

  2. global bg,role,clock,gameState,surObject,surGameOver,score,myFont,myFont1,objectList

  3. #分数初始化

  4. score=0

  5. #初始化

  6. objectList=[]

  7. #加载字体

  8. myFont=pygame.font.Font("./freesansbold.ttf",32)

  9. myFont1=pygame.font.Font("./freesansbold.ttf",64)

  10. # 创建时钟对象 (可以控制游戏循环频率)

  11. clock = pygame.time.Clock()

  12. #初始化游戏状态

  13. gameState=0

  14. #游戏背景

  15. surBg=pygame.image.load("image/bg.bmp").convert_alpha()

  16. bg=Bg(surBg)

  17. #结束画面

  18. surGameOver=pygame.image.load("image/gameover.bmp").convert_alpha()

  19. #人物图片

  20. surRole=pygame.image.load("image/role.png").convert_alpha()

  21. role=Role(surRole,508-85)

  22. #障碍物图片

  23. surObject=pygame.image.load("image/object.png").convert_alpha()

  24.  

  25.  

  26. def addObject():

  27. global surObject,object,objectList,object

  28. rate=4

  29. #是否生成障碍物

  30. if not random.randint(0,300)<rate:

  31. return

  32. y=random.choice([height-100,height-200,height-300,height-400])

  33. object=Object(surObject,width+40,y)

  34. objectList.append(object)

  35.  

  36.  

  37. def updateLogic():

  38. global gameState,score

  39. #键盘事件处理

  40. for event in pygame.event.get():

  41. if event.type == pygame.QUIT:

  42. sys.exit()

  43. elif event.type==pygame.KEYDOWN:

  44. #空格键跳跃

  45. if gameState==0:

  46. if event.key==pygame.K_SPACE:

  47. if role.state==0:

  48. role.state=1

  49. role.vy=role.vy_start

  50. elif role.state==1:

  51. role.state=2

  52. role.vy=role.vy_start

  53. elif gameState==1:

  54. if event.key==pygame.K_SPACE:

  55. #重新开始游戏

  56. initGame()

  57. if gameState==0:

  58. #背景的移动

  59. bg.dx+=10

  60. if bg.dx==1200:

  61. bg.dx=0

  62. #人物的移动

  63. if role.state==0:

  64. role.currentFrame+=1

  65. if role.currentFrame==12:

  66. role.currentFrame=0

  67. else:

  68. role.y+=role.vy

  69. role.vy+=role.g

  70. if role.y>=508-85:

  71. role.y=508-85

  72. role.state=0

  73. #障碍物的移动

  74. addObject()

  75. for object in objectList:

  76. object.x-=10 #障碍物移动

  77. # 障碍物超出屏幕,移除障碍物

  78. if object.x+object.w<=0:

  79. objectList.remove(object)

  80. score+=10 #避开障碍物,加10分

  81. print("移除了一个目标")

  82. #碰撞检测

  83. if object.collision(role.getRect(),object.getRect()):

  84. if(object.currentFrame==6):

  85. objectList.remove(object)

  86. score+=100 #吃金币加100分

  87. print(score)

  88. print("吃了一个金币")

  89. else:

  90. gameState=1 #游戏失败

  91. print("发生了碰撞!")

ok啦,这就是这个天天酷跑的全部代码啦,有问题可以留言,我看到都会回的。

到此这篇关于基于Python实现天天酷跑功能的文章就介绍到这了

  1. 欢迎大家点赞,留言,转发,转载,感谢大家的相伴与支持

    万水千山总是情,点个【在看】行不行

  2. *声明:本文于网络整理,版权归原作者所有,如来源信息有误或侵犯权益,请联系我们删除或授权事

浏览 36
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报