Python小游戏(404小恐龙)

举报
红目香薰 发表于 2022/01/22 23:49:34 2022/01/22
【摘要】 玩法:上下控制起跳躲避 源码分享: import cfgimport sysimport randomimport pygamefrom modules import * '''main'''def main(highest_score): # 游戏初始化 pygame.init() screen = py...

玩法:上下控制起跳躲避

源码分享:


  
  1. import cfg
  2. import sys
  3. import random
  4. import pygame
  5. from modules import *
  6. '''main'''
  7. def main(highest_score):
  8. # 游戏初始化
  9. pygame.init()
  10. screen = pygame.display.set_mode(cfg.SCREENSIZE)
  11. pygame.display.set_caption('九歌')
  12. # 导入所有声音文件
  13. sounds = {}
  14. for key, value in cfg.AUDIO_PATHS.items():
  15. sounds[key] = pygame.mixer.Sound(value)
  16. # 游戏开始界面
  17. GameStartInterface(screen, sounds, cfg)
  18. # 定义一些游戏中必要的元素和变量
  19. score = 0
  20. score_board = Scoreboard(cfg.IMAGE_PATHS['numbers'], position=(534, 15), bg_color=cfg.BACKGROUND_COLOR)
  21. highest_score = highest_score
  22. highest_score_board = Scoreboard(cfg.IMAGE_PATHS['numbers'], position=(435, 15), bg_color=cfg.BACKGROUND_COLOR, is_highest=True)
  23. dino = Dinosaur(cfg.IMAGE_PATHS['dino'])
  24. ground = Ground(cfg.IMAGE_PATHS['ground'], position=(0, cfg.SCREENSIZE[1]))
  25. cloud_sprites_group = pygame.sprite.Group()
  26. cactus_sprites_group = pygame.sprite.Group()
  27. ptera_sprites_group = pygame.sprite.Group()
  28. add_obstacle_timer = 0
  29. score_timer = 0
  30. # 游戏主循环
  31. clock = pygame.time.Clock()
  32. while True:
  33. for event in pygame.event.get():
  34. if event.type == pygame.QUIT:
  35. pygame.quit()
  36. sys.exit()
  37. elif event.type == pygame.KEYDOWN:
  38. if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
  39. dino.jump(sounds)
  40. elif event.key == pygame.K_DOWN:
  41. dino.duck()
  42. elif event.type == pygame.KEYUP and event.key == pygame.K_DOWN:
  43. dino.unduck()
  44. screen.fill(cfg.BACKGROUND_COLOR)
  45. # --随机添加云
  46. if len(cloud_sprites_group) < 5 and random.randrange(0, 300) == 10:
  47. cloud_sprites_group.add(Cloud(cfg.IMAGE_PATHS['cloud'], position=(cfg.SCREENSIZE[0], random.randrange(30, 75))))
  48. # --随机添加仙人掌/飞龙
  49. add_obstacle_timer += 1
  50. if add_obstacle_timer > random.randrange(50, 150):
  51. add_obstacle_timer = 0
  52. random_value = random.randrange(0, 10)
  53. if random_value >= 5 and random_value <= 7:
  54. cactus_sprites_group.add(Cactus(cfg.IMAGE_PATHS['cacti']))
  55. else:
  56. position_ys = [cfg.SCREENSIZE[1]*0.82, cfg.SCREENSIZE[1]*0.75, cfg.SCREENSIZE[1]*0.60, cfg.SCREENSIZE[1]*0.20]
  57. ptera_sprites_group.add(Ptera(cfg.IMAGE_PATHS['ptera'], position=(600, random.choice(position_ys))))
  58. # --更新游戏元素
  59. dino.update()
  60. ground.update()
  61. cloud_sprites_group.update()
  62. cactus_sprites_group.update()
  63. ptera_sprites_group.update()
  64. score_timer += 1
  65. if score_timer > (cfg.FPS//12):
  66. score_timer = 0
  67. score += 1
  68. score = min(score, 99999)
  69. if score > highest_score:
  70. highest_score = score
  71. if score % 100 == 0:
  72. sounds['point'].play()
  73. if score % 1000 == 0:
  74. ground.speed -= 1
  75. for item in cloud_sprites_group:
  76. item.speed -= 1
  77. for item in cactus_sprites_group:
  78. item.speed -= 1
  79. for item in ptera_sprites_group:
  80. item.speed -= 1
  81. # --碰撞检测
  82. for item in cactus_sprites_group:
  83. if pygame.sprite.collide_mask(dino, item):
  84. dino.die(sounds)
  85. for item in ptera_sprites_group:
  86. if pygame.sprite.collide_mask(dino, item):
  87. dino.die(sounds)
  88. # --将游戏元素画到屏幕上
  89. dino.draw(screen)
  90. ground.draw(screen)
  91. cloud_sprites_group.draw(screen)
  92. cactus_sprites_group.draw(screen)
  93. ptera_sprites_group.draw(screen)
  94. score_board.set(score)
  95. highest_score_board.set(highest_score)
  96. score_board.draw(screen)
  97. highest_score_board.draw(screen)
  98. # --更新屏幕
  99. pygame.display.update()
  100. clock.tick(cfg.FPS)
  101. # --游戏是否结束
  102. if dino.is_dead:
  103. break
  104. # 游戏结束界面
  105. return GameEndInterface(screen, cfg), highest_score
  106. '''run'''
  107. if __name__ == '__main__':
  108. highest_score = 0
  109. while True:
  110. flag, highest_score = main(highest_score)
  111. if not flag: break

祝大家玩得开心,欢迎点赞、关注、评论,欢迎一健三联。

文章来源: laoshifu.blog.csdn.net,作者:红目香薰,版权归原作者所有,如需转载,请联系作者。

原文链接:laoshifu.blog.csdn.net/article/details/120425258

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。