-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
78 lines (57 loc) · 2.08 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
import pygame
import random
pygame.init()
square_width = 750
pixel_width = 50
screen = pygame.display.set_mode([square_width] * 2)
clock = pygame.time.Clock()
running = True
def generate_starting_position():
position_range = (pixel_width // 2, square_width - pixel_width // 2, pixel_width)
return [random.randrange(*position_range), random.randrange(*position_range)]
def reset():
target.center = generate_starting_position()
snake_pixel.center = generate_starting_position()
return snake_pixel.copy()
def is_out_of_bounds():
return snake_pixel.bottom > square_width or snake_pixel.top < 0 \
or snake_pixel.left < 0 or snake_pixel.right > square_width
snake_pixel = pygame.rect.Rect([0, 0, pixel_width - 2, pixel_width - 2])
snake_pixel.center = generate_starting_position()
snake = [snake_pixel.copy()]
snake_direction = (0, 0)
snake_length = 1
target = pygame.rect.Rect([0, 0, pixel_width - 2, pixel_width - 2])
target.center = generate_starting_position()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill("black")
if is_out_of_bounds():
snake_length = 1
target.center = generate_starting_position()
snake_pixel.center = generate_starting_position()
snake = [snake_pixel.copy()]
if snake_pixel.center == target.center:
target.center = generate_starting_position()
snake_length += 1
snake.append(snake_pixel.copy())
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
snake_direction = (0, - pixel_width)
if keys[pygame.K_s]:
snake_direction = (0, pixel_width)
if keys[pygame.K_a]:
snake_direction = (- pixel_width, 0)
if keys[pygame.K_d]:
snake_direction = (pixel_width, 0)
for snake_part in snake:
pygame.draw.rect(screen, "green", snake_part)
pygame.draw.rect(screen, "red", target)
snake_pixel.move_ip(snake_direction)
snake.append(snake_pixel.copy())
snake = snake[-snake_length:]
pygame.display.flip()
clock.tick(10)
pygame.quit()