-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
138 lines (108 loc) · 3.84 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
import random
import pygame
def snap(snap_pos):
return 50 * round(snap_pos / 50)
pygame.init()
screen = pygame.display.set_mode((500, 450))
clock = pygame.time.Clock()
deltaTime = 0
player_x = 100
player_y = 200
x_velocity = 0
y_velocity = 0
speed = 2
tick = 0
x_positions = [25, 75, 125, 175, 225, 275, 325, 375, 425, 475]
y_positions = [25, 75, 125, 175, 225, 275, 325, 375, 425]
fruits = []
allowed = False
tails = [(player_x, player_y), (player_x - 50, player_y), (
player_x - 100, player_y)] #, (player_x - 150, player_y), (player_x - 200, player_y), (player_x - 250, player_y)]
# 0 - 50 = -50 = LEFT
# (len(tails) - 1) - (len(tails) - 2) < 0
class Res(Exception): pass
running = True
while running:
# EVENTS
tick += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# DRAW BACKGROUND
for x in range(6):
for y in range(10):
pygame.draw.rect(screen, "#deeced", pygame.Rect(x * 50 * 2 + 50, y * 50 * 2, 50, 50))
pygame.draw.rect(screen, "#deeced", pygame.Rect(x * 50 * 2, y * 50 * 2 + 50, 50, 50))
for x in range(6):
for y in range(10):
pygame.draw.rect(screen, "#d1e4e6", pygame.Rect(x * 50 * 2 + 50, y * 50 * 2 + 50, 50, 50))
pygame.draw.rect(screen, "#d1e4e6", pygame.Rect(x * 50 * 2, y * 50 * 2, 50, 50))
# CREATE FOOD
if len(fruits) < 5:
random_pos = (random.choice(x_positions), random.choice(y_positions))
while True:
try:
for i in tails:
if random_pos == i:
raise Res
break
except Res:
random_pos = (random.choice(x_positions), random.choice(y_positions))
continue
fruits.append(random_pos)
for pos in fruits:
pygame.draw.circle(screen, "red", pos, 25)
# CONTROLS
keys = pygame.key.get_pressed()
if keys[pygame.K_UP] and y_velocity == 0 and allowed:
allowed = False
x_velocity = 0
y_velocity = -speed
if keys[pygame.K_DOWN] and y_velocity == 0 and allowed:
allowed = False
x_velocity = 0
y_velocity = speed
if keys[pygame.K_LEFT] and x_velocity == 0 and allowed:
allowed = False
x_velocity = -speed
y_velocity = 0
if keys[pygame.K_RIGHT] and x_velocity == 0 and allowed:
allowed = False
x_velocity = speed
y_velocity = 0
# MOVEMENT
if x_velocity == 0:
player_x = snap(player_x)
elif y_velocity == 0:
player_y = snap(player_y)
# CREATE TAIL
if x_velocity != 0 or y_velocity != 0:
for i in range(len(tails) - 1, 0, -1):
if i != 0:
if tick == 10:
pygame.draw.rect(screen, "#ecd613", pygame.Rect(tails[i - 1][0], tails[i - 1][1], 50, 50))
tails[i] = (tails[i - 1][0], tails[i - 1][1])
else:
pygame.draw.rect(screen, "#ecd613", pygame.Rect(tails[i][0], tails[i][1], 50, 50))
tails[0] = (player_x, player_y)
pygame.draw.rect(screen, "#ecd613", pygame.Rect(tails[0][0], tails[0][1], 50, 50))
# CHECKS
for i in fruits:
if snap(player_x) + 25 == i[0] and snap(player_y) + 25 == i[1]:
fruits.remove(i)
tails.append((tails[len(tails) - 1][0], tails[len(tails) - 1][1]))
if player_x > 475 or player_y > 425 or player_x < 0 or player_y < 0:
break
if tick == 10:
allowed = True
player_x += x_velocity * 25
player_y += y_velocity * 25
tick = 0
if x_velocity != 0 or y_velocity != 0:
for i in tails:
if player_x == i[0] and player_y == i[1]:
exit(0)
# FRAME
pygame.display.flip()
deltaTime = clock.tick(60) / 1000
pygame.quit()