1.import os
2.import cfg
3.importc++课程设计报告 sys
4.import pygame
5.import random
6.from modules import *
7.
8.
9.'''游戏初始化'''
10.def initGame():
11. # 初始化pygame, 设置展示窗口
12. pygame.init()
13. screen = pygame.display.set_mode(cfg.SCREENSIZE)
14. pygame.display.set_caption('catch coins —— 九歌')
15. # 加载必要的游戏素材
16. game_images = {}
17. for key, value in cfg.IMAGE_PATHS.items():
18. if isinstance(value, list):
19. images = []
20. for item in value: images.append(pygame.image.load(item))
21. game_images[key] = images
22. else:
23. game_images[key] = pygame.image.load(value)
24. game_sounds = {}
25. for key, value in cfg.AUDIO_PATHS.items():
26. if key == 'bgm': continue
27. game_sounds[key] = pygame.mixer.Sound(value)
28. # 返回初始化数据
29. return screen, game_images, game_sounds
30.
31.
32.'''主函数'''
33.def main():
34. # 初始化
35. screen, game_images, game_sounds = initGame()
36. # 播放背景音乐
37. pygame.mixer.music.load(cfg.AUDIO_PATHS['bgm'])
38. pygame.mixer.music.play(-1, 0.0)
39. # 字体加载
40. font = pygame.font.Font(cfg.FONT_PATH, 40)
41. # 定义hero
42. hero = Hero(game_images['hero'], position=(375, 520))
43. # 定义食物组
44. food_sprites_group = pygame.sprite.Group()
45. generate_food_freq = random.randint(10, 20)
46. generate_food_count = 0
47. # 当前分数/历史最高分
48. score = 0
49. highest_score = 0 if not os.path.exists(cfg.HIGHEST_SCORE_RECORD_FILEPATH) else int(open(cfg.HIGHEST_SCORE_RECORD_FILEPATH).read())
50. # 游戏主循环
51. clock = pygame.time.Clock()
52. while True:
53. # --填充背景
54. screen.fill(0)
55. screen.blit(game_images['background'], (0, 0))
56. # --倒计时信息
57. countdown_text = 'Count down: ' + str((90000 - pygame.time.get_ticks()) // 60000) + ":" + str((90000 - _ticks()) // 1000 % 60).zfill(2)
58. countdown_text = font.render(countdown_text, True, (0, 0, 0))
59. countdown_rect = countdown_text.get_rect()
60. countdown_rect.topright = [cfg.SCREENSIZE[0]-30, 5]
61. screen.blit(countdown_text, countdown_rect)
62. # --按键检测
63. for event in pygame.event.get():
64. if event.type == pygame.QUIT:
65. pygame.quit()
66. sys.exit()
67. key_pressed = pygame.key.get_pressed()
68. if key_pressed[pygame.K_a] or key_pressed[pygame.K_LEFT]:
69. hero.move(cfg.SCREENSIZE, 'left')
70. if key_pressed[pygame.K_d] or key_pressed[pygame.K_RIGHT]:
71. hero.move(cfg.SCREENSIZE, 'right')
72. # --随机生成食物
73. generate_food_count += 1
74. if generate_food_count > generate_food_freq:
75. generate_food_freq = random.randint(10, 20)
76. generate_food_count = 0
77. food = Food(game_images, random.choice(['gold',] * 10 + ['apple']), cfg.SCREENSIZE)
78. food_sprites_group.add(food)
79. # --更新食物
80. for food in food_sprites_group:
81. if food.update(): food_sprites_group.remove(food)
82. # --碰撞检测
83. for food in food_sprites_group:
84. if pygame.sprite.collide_mask(food, hero):
85. game_sounds['get'].play()
86. food_sprites_group.remove(food)
87. score += food.score
88. if score > highest_score: highest_score = score
89. # --画hero
90. hero.draw(screen)
91. # --画食物
92. food_sprites_group.draw(screen)
93. # --显示得分
94. score_text = f'Score: {score}, Highest: {highest_score}'
95. score_text = font.render(score_text, True, (0, 0, 0))
96. score_rect = score_text.get_rect()
97. score_rect.topleft = [5, 5]
98. screen.blit(score_text, score_rect)
99. # --判断游戏是否结束
100. if pygame.time.get_ticks() >= 90000:
101. break
102. # --更新屏幕
103. pygame.display.flip()
104. clock.tick(cfg.FPS)
105. # 游戏结束, 记录最高分并显示游戏结束画面
106. fp = open(cfg.HIGHEST_SCORE_RECORD_FILEPATH, 'w')
107. fp.write(str(highest_score))
108. fp.close()
109. return showEndGameInterface(screen, cfg, score, highest_score)
110.
111.
112.'''run'''
113.if __name__ == '__main__':
114. while main():
115. pass
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论