admin 管理员组文章数量: 887019
2024年2月27日发(作者:删除的大文件怎么恢复)
python小游戏源代码
吃金币小游戏源码分享:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import os
import cfg
import sys
import pygame
import random
from modules import *
'''游戏初始化'''
def initGame():
# 初始化pygame, 设置展示窗口
()
screen = _mode(SIZE)
_caption('catch coins —— 九歌')
# 加载必要的游戏素材
game_images = {}
for key, value in _():
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
if isinstance(value, list):
images = []
for item in value: ((item))
game_images[key] = images
else:
game_images[key] = (value)
game_sounds = {}
for key, value in _():
if key == 'bgm': continue
game_sounds[key] = (value)
# 返回初始化数据
return screen, game_images, game_sounds
'''主函数'''
def main():
# 初始化
screen, game_images, game_sounds = initGame()
# 播放背景音乐
(_PATHS['bgm'])
(-1, 0.0)
# 字体加载
font = (_PATH, 40)
# 定义hero
hero = Hero(game_images['hero'], position=(375, 520))
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 定义食物组
food_sprites_group = ()
generate_food_freq = t(10, 20)
generate_food_count = 0
# 当前分数/历史最高分
score = 0
highest_score = 0 if not (T_SCORE_RECORD_FILEPATH) else
int(open(T_SCORE_RECORD_FILEPATH).read())
# 游戏主循环
clock = ()
while True:
# --填充背景
(0)
(game_images['background'], (0, 0))
# --倒计时信息
countdown_text = 'Count down: ' + str((90000 - _ticks()) // 60000) + ":" + str((90000 - _ticks()) //
1000 % 60).zfill(2)
countdown_text = (countdown_text, True, (0, 0, 0))
countdown_rect = countdown__rect()
countdown_ht = [SIZE[0]-30, 5]
(countdown_text, countdown_rect)
# --按键检测
for event in ():
if == :
()
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
()
key_pressed = _pressed()
if key_pressed[pygame.K_a] or key_pressed[pygame.K_LEFT]:
(SIZE, 'left')
if key_pressed[pygame.K_d] or key_pressed[pygame.K_RIGHT]:
(SIZE, 'right')
# --随机生成食物
generate_food_count += 1
if generate_food_count > generate_food_freq:
generate_food_freq = t(10, 20)
generate_food_count = 0
food = Food(game_images, (['gold',] * 10 + ['apple']), SIZE)
food_sprites_(food)
# --更新食物
for food in food_sprites_group:
if (): food_sprites_(food)
# --碰撞检测
for food in food_sprites_group:
if e_mask(food, hero):
game_sounds['get'].play()
food_sprites_(food)
score +=
if score > highest_score: highest_score = score
# --画hero
(screen)
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# --画食物
food_sprites_(screen)
# --显示得分
score_text = f'Score: {score}, Highest: {highest_score}'
score_text = (score_text, True, (0, 0, 0))
score_rect = score__rect()
score_t = [5, 5]
(score_text, score_rect)
# --判断游戏是否结束
if _ticks() >= 90000:
break
# --更新屏幕
()
()
# 游戏结束, 记录最高分并显示游戏结束画面
fp = open(T_SCORE_RECORD_FILEPATH, 'w')
(str(highest_score))
()
return showEndGameInterface(screen, cfg, score, highest_score)
'''run'''
if __name__ == '__main__':
while main():
pass
版权声明:本文标题:python小游戏源代码 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1709034807h536639.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论