-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
157 lines (127 loc) · 4.71 KB
/
main.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import random
import pygame
# Colors
dark_green = (0, 51, 0)
green = (0, 204, 0)
dark_yellow = (204, 204, 0)
yellow = (255, 255, 0)
magenta = (255, 0, 127)
red = (153, 0, 0)
# Global Game Setup
pygame.init()
screen = pygame.display.set_mode((1280, 720))
pygame.display.set_caption('Codetrotters Snake Game Expedition')
clock = pygame.time.Clock()
font_style = pygame.font.SysFont(None, 30)
running = True
game_over = False
dt = 0
# Display a message (function)
def display_message(msg, color):
message = font_style.render(msg, True, color)
screen.blit(message, [screen.get_width() / 3, screen.get_height() / 3])
def display_score(current_score):
text = font_style.render("Score: %s Speed: %s" % (current_score, speed), True, green)
screen.blit(text, [10, 10])
def spawn_food():
return pygame.Vector2(random.randint(0, screen.get_width() - 10), random.randint(0, screen.get_height() - 10))
# Per-Game Setup
speed = 15
snake_head_position = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)
food_position = None
xd = 0
yd = 0
score = 0
tail_length = 0
tail = []
while running:
# poll for events
# pygame.QUIT event means the user clicked X to close your window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
while game_over:
screen.fill(red)
display_message("Game Over! Press (Q)uit or (R)etry!", "white")
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = False
running = False
if event.key == pygame.K_r:
game_over = False
# Reset the game
speed = 10
snake_head_position = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)
xd = 0
yd = 0
score = 0
tail_length = 0
tail = []
while running and not game_over:
# Listen for arrow keys getting pressed by the player and set how
# the X and Y coordinates should change (delta) as the snake is moving
# in that direction.
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
yd = -10
xd = 0
if event.key == pygame.K_DOWN:
yd = +10
xd = 0
if event.key == pygame.K_LEFT:
yd = 0
xd = -10
if event.key == pygame.K_RIGHT:
yd = 0
xd = +10
if event.key == pygame.K_q:
game_over = True
running = False
# Update Tail
tail.append(pygame.Vector2(snake_head_position.x + xd/speed, snake_head_position.y + yd/speed))
if len(tail) > tail_length:
tail.pop(0)
# Change the snake head's position by applying the X and Y delta.
snake_head_position.x += xd
snake_head_position.y += yd
if (snake_head_position.x < 0 or snake_head_position.x > screen.get_width() - 10 or
snake_head_position.y < 0 or snake_head_position.y > screen.get_height() - 10):
game_over = True
# fill the screen with a color to wipe away anything from last frame
screen.fill(dark_green)
display_score(score)
# Welcome message
if snake_head_position == pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2):
display_message("START the game by using your arrow keys!", "white")
# Spawn food
if food_position is None:
food_position = spawn_food()
# Draw food
food = pygame.draw.rect(screen, magenta, [food_position.x, food_position.y, 10, 10])
# Draw head
snake_head = pygame.draw.rect(screen, yellow, [snake_head_position.x, snake_head_position.y, 10, 10])
# Draw tail
for i, t in enumerate(tail):
tail_color = dark_yellow
if i % 2 == 0:
tail_color = yellow
part = pygame.draw.rect(screen, tail_color, [t.x, t.y, 10, 10])
# Check if we ate our own tail!
if tail_length > 1 and i < 1 and part.colliderect(snake_head):
game_over = True
# Eat food
if food.colliderect(snake_head):
tail_length += 1
score += 1
speed += 2
food_position = spawn_food()
# flip() the display to put your work on screen
pygame.display.flip()
clock.tick(speed)
pygame.quit()