当前位置:   article > 正文

如何用Python写一个Google:Dino Game小恐龙快跑_谷歌小恐龙python代码

谷歌小恐龙python代码
  1. import pygame
  2. import random
  3. # 初始化 Pygame
  4. pygame.init()
  5. # 设置游戏窗口大小
  6. SCREEN_WIDTH = 600
  7. SCREEN_HEIGHT = 200
  8. # 设置颜色
  9. BLACK = (0, 0, 0)
  10. WHITE = (255, 255, 255)
  11. RED = (255, 0, 0)
  12. # 创建游戏窗口
  13. screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
  14. # 设置游戏标题
  15. pygame.display.set_caption('Google Dino')
  16. # 加载跳跃音效
  17. jump_sound = pygame.mixer.Sound('jump.wav')
  18. # 加载障碍物图像
  19. cactus_img = pygame.image.load('cactus.png')
  20. bird_img = pygame.image.load('bird.png')
  21. # 定义 Dino 类
  22. class Dino(pygame.sprite.Sprite):
  23. def __init__(self):
  24. super().__init__()
  25. self.image = pygame.image.load('dino.png')
  26. self.rect = self.image.get_rect()
  27. self.rect.x = 50
  28. self.rect.y = SCREEN_HEIGHT - 70
  29. self.jump_speed = 10
  30. self.gravity = 1
  31. def jump(self):
  32. self.rect.y -= self.jump_speed
  33. def update(self):
  34. self.rect.y += self.gravity
  35. if self.rect.y >= SCREEN_HEIGHT - 70:
  36. self.rect.y = SCREEN_HEIGHT - 70
  37. # 定义障碍物类
  38. class Obstacle(pygame.sprite.Sprite):
  39. def __init__(self, img):
  40. super().__init__()
  41. self.image = img
  42. self.rect = self.image.get_rect()
  43. self.rect.x = SCREEN_WIDTH
  44. self.rect.y = SCREEN_HEIGHT - 90
  45. def update(self):
  46. self.rect.x -= 5
  47. # 创建 Dino 对象
  48. dino = Dino()
  49. # 创建障碍物组
  50. obstacle_group = pygame.sprite.Group()
  51. # 设置游戏帧率
  52. clock = pygame.time.Clock()
  53. # 初始化分数和难度
  54. score = 0
  55. difficulty = 1
  56. # 游戏主循环
  57. running = True
  58. while running:
  59. # 处理游戏事件
  60. for event in pygame.event.get():
  61. if event.type == pygame.QUIT:
  62. running = False
  63. elif event.type == pygame.KEYDOWN:
  64. if event.key == pygame.K_SPACE and dino.rect.y == SCREEN_HEIGHT - 70:
  65. dino.jump()
  66. jump_sound.play()
  67. # 更新 Dino 对象
  68. dino.update()
  69. # 创建障碍物
  70. if random.randint(1, 100) <= difficulty:
  71. obstacle_type = random.randint(1, 2)
  72. if obstacle_type == 1:
  73. obstacle = Obstacle(cactus_img)
  74. else:
  75. obstacle = Obstacle(bird_img)
  76. obstacle_group.add(obstacle)
  77. # 更新障碍物
  78. for obstacle in obstacle_group:
  79. obstacle.update()
  80. if obstacle.rect.x <= -50:
  81. obstacle_group.remove(obstacle)
  82. score += 1
  83. if score % 10 == 0:
  84. difficulty += 1
  85. # 检测碰撞
  86. if pygame.sprite.spritecollide(dino, obstacle_group, False):
  87. running = False
  88. # 绘制游戏画面
  89. screen.fill(WHITE)
  90. pygame.draw.rect(screen, BLACK, [0, SCREEN_HEIGHT - 70, SCREEN_WIDTH, 2])
  91. screen.blit(dino.image, dino.rect)
  92. obstacle_group.draw(screen)
  93. # 绘制分数
  94. font = pygame.font.Font(None, 36)
  95. text = font.render('Score: ' + str(score), True, RED)
  96. screen.blit(text, [SCREEN_WIDTH - 120, 10])
  97. # 更新游戏画面
  98. pygame.display.update()
  99. # 控制游戏帧率
  100. clock.tick(30)
  101. # 结束 Pygame
  102. pygame.quit()

请注意,在运行此游戏之前,您需要准备一些图像和音频文件,并将其命名为 dino.pngcactus.pngbird.png 和 jump.wav

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号