-
Notifications
You must be signed in to change notification settings - Fork 0
/
test4.py
109 lines (90 loc) · 2.67 KB
/
test4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
这是一个贪吃蛇程序,用于测试level4.py能否正常工作
由林奕和autogpt合作完成,
"""
import pygame
import random
# 初始化游戏
pygame.init()
# 设置游戏窗口大小和标题
window_width = 800
window_height = 600
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("贪吃蛇")
# 定义颜色
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
# 蛇的位置和移动速度
snake_size = 20
snake_speed = 10
x_change = 0
y_change = 0
# 初始蛇的长度和位置
snake_length = 1
snake_body = []
snake_head = [window_width / 2, window_height / 2]
# 食物的位置
food_pos = [
random.randrange(1, (window_width // 20)) * 20,
random.randrange(1, (window_height // 20)) * 20,
]
# 游戏结束标志
game_over = False
# 不断更新游戏画面
clock = pygame.time.Clock()
# 游戏循环
while not game_over:
# 监听事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -snake_size
y_change = 0
elif event.key == pygame.K_RIGHT:
x_change = snake_size
y_change = 0
elif event.key == pygame.K_UP:
x_change = 0
y_change = -snake_size
elif event.key == pygame.K_DOWN:
x_change = 0
y_change = snake_size
# 更新蛇头位置
snake_head[0] += x_change
snake_head[1] += y_change
# 判断是否吃到食物
if snake_head[0] == food_pos[0] and snake_head[1] == food_pos[1]:
food_pos = [
random.randrange(1, (window_width // 20)) * 20,
random.randrange(1, (window_height // 20)) * 20,
]
snake_length += 1
# 更新蛇身体
snake_body.append(list(snake_head))
if len(snake_body) > snake_length:
del snake_body[0]
# 检测蛇是否撞墙或自咬
for segment in snake_body[:-1]:
if segment == snake_head:
game_over = True
if snake_head[0] not in range(0, window_width) or snake_head[1] not in range(
0, window_height
):
game_over = True
# 渲染游戏画面
window.fill(black)
for segment in snake_body:
pygame.draw.rect(
window, green, (segment[0], segment[1], snake_size, snake_size)
)
pygame.draw.rect(window, red, (food_pos[0], food_pos[1], snake_size, snake_size))
# 刷新屏幕
pygame.display.update()
# 控制游戏帧率
clock.tick(snake_speed)
# 结束游戏并退出
pygame.quit()