-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuman_snake.py
171 lines (148 loc) · 5.92 KB
/
human_snake.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import pygame
import random
pygame.init()
# Set up the screen
screen_width = 800
screen_height = 800
square_size = 32
side_length = screen_width // square_size
start_pos = (side_length // 2) * square_size
head_x = start_pos
head_y = start_pos
snake_squares = [
[start_pos, start_pos + square_size],
[start_pos, start_pos]
]
snake_len = 2
started = False
first_press = False
directions = ['right', 'left', 'up', 'down']
direction = [0, 0, 0, 0]
curr_direction = ""
apple_x = random.randint(0, (screen_width // square_size) - 1) * square_size
apple_y = random.randint(0, (screen_height // square_size) - 1) * square_size
# Set up the Pygame window
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Snake Game")
screen.fill((240, 240, 240))
def restart():
global started, head_x, head_y, snake_squares, snake_len, direction, apple_x, apple_y
started = False
first_press = False
head_x = start_pos
head_y = start_pos
snake_squares = [
[start_pos, start_pos + square_size],
[start_pos, start_pos]
]
snake_len = 2
direction = [0, 0, 0, 0]
apple_x = random.randint(0, (screen_width // square_size) - 1) * square_size
apple_y = random.randint(0, (screen_height // square_size) - 1) * square_size
draw() # Start a new frame
def game_over():
screen.fill((240, 240, 240))
draw_snake()
font = pygame.font.SysFont("Courier New", 35)
text = font.render("GAME OVER", True, (184, 0, 0))
text_rect = text.get_rect(center=(screen_width // 2, screen_height // 2))
screen.blit(text, text_rect)
text = font.render("Press 'restart' or 'R' to try again", True, (184, 0, 0))
text_rect = text.get_rect(center=(screen_width // 2, screen_height // 2 + 50))
screen.blit(text, text_rect)
pygame.display.flip()
running = False
def apple_handler():
global snake_len, apple_x, apple_y
if head_x == apple_x and head_y == apple_y:
apple_x = random.randint(0, (screen_width // square_size) - 1) * square_size
apple_y = random.randint(0, (screen_height // square_size) - 1) * square_size
snake_len += 1
pygame.draw.rect(screen, (184, 0, 0), (apple_x, apple_y, square_size - 1, square_size - 1))
def collision_detection():
if head_x < 0 or head_x >= screen_width or head_y < 0 or head_y >= screen_height:
game_over()
return True
for i in range(len(snake_squares) - 1):
if snake_squares[i][0] == head_x and snake_squares[i][1] == head_y:
game_over()
return True
#i think that it keeps going instead of failing during self collision because the key press makes running/started false
return False
def draw_snake():
for square in snake_squares:
pygame.draw.rect(screen, (0, 191, 0), (square[0], square[1], square_size - 2, square_size - 2))
def draw_score():
font = pygame.font.SysFont("Courier New", 35)
text = font.render("Score: " + str(snake_len - 2), True, (0, 0, 0))
text_rect = text.get_rect(topright=(screen_width - 10, 10))
screen.blit(text, text_rect)
def draw():
global count, head_x, head_y, snake_squares, direction, started, curr_direction
if started:
head_x = head_x + direction[0] * square_size - direction[1] * square_size
head_y = head_y + direction[3] * square_size - direction[2] * square_size
curr_direction = directions[direction.index(1)]
screen.fill((240, 240, 240))
if collision_detection():
return
snake_squares.append([head_x, head_y])
if len(snake_squares) > snake_len:
snake_squares.pop(0)
else:
screen.fill((240, 240, 240))
font = pygame.font.SysFont("Courier New", 35)
text = font.render("Welcome to Snake", True, (184, 0, 0))
text_rect = text.get_rect(center=(screen_width // 2, screen_height // 2 - 100))
screen.blit(text, text_rect)
text = font.render("Use the arrow keys to start", True, (184, 0, 0))
text_rect = text.get_rect(center=(screen_width // 2, screen_height // 2 - 50))
screen.blit(text, text_rect)
if direction[3]: # will flip the snake if you are going down initially
snake_squares = [
[start_pos, start_pos],
[start_pos, start_pos + square_size]
]
draw_snake()
apple_handler()
draw_score()
pygame.display.flip()
# Game loop
running = True
clock = pygame.time.Clock()
while running:
clock.tick(15) # Limit frame rate to 15 FPS
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT: # If the right arrow key is pressed
if curr_direction != "left":
direction = [1, 0, 0, 0]
if not first_press:
started = True
first_press = True
elif event.key == pygame.K_LEFT: # If the left arrow key is pressed
if curr_direction != "right":
direction = [0, 1, 0, 0]
if not first_press:
started = True
first_press = True
elif event.key == pygame.K_UP: # If the up arrow key is pressed
if curr_direction != "down":
direction = [0, 0, 1, 0]
if not first_press:
started = True
first_press = True
elif event.key == pygame.K_DOWN: # If the down arrow key is pressed
if curr_direction != "up":
direction = [0, 0, 0, 1]
if not first_press:
started = True
first_press = True
if event.key == pygame.K_r:
restart()
if event.key == pygame.K_a:
snake_len += 1
draw()
pygame.quit()