-
Notifications
You must be signed in to change notification settings - Fork 0
/
snek_architecture.py
123 lines (95 loc) · 3.05 KB
/
snek_architecture.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
import pygame as pg
import time as tt
class square (object):
rows = 0
width = 0
def __init__(self, start, dir_x = 1, dir_y = 0, clr = (255,0,0)):
pass
def move(self, dir_x, dir_y):
pass
def draw(self, window, eyes = False):
pass
class snek (object):
body = []
turns = {}
def __init__(self, clr, pos):
self.clr = clr
self.head = square(pos)
self.body.append(self.head)
self.dir_x = 0
self.dir_y = 1
def move (self):
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
keys = pg.key.get_pressed()
for key in keys:
if keys[pg.key.K_LEFT]:
self.dir_x = -1
self.dir_y = 0
self.turns[self.head.pos[:]] = [self.dir_x, self.dir_y]
elif keys[pg.key.K_RIGHT]:
self.dir_x = 1
self.dir_y = 0
self.turns[self.head.pos[:]] = [self.dir_x, self.dir_y]
elif keys[pg.key.K_UP]:
self.dir_x = 0
self.dir_y = -1
self.turns[self.head.pos[:]] = [self.dir_x, self.dir_y]
elif keys[pg.key.K_DOWN]:
self.dir_x = 0
self.dir_y = 1
self.turns[self.head.pos[:]] = [self.dir_x, self.dir_y]
for i, j in enumerate(self.body):
p = j.pos[:]
if p in self.turns:
turn = self.turns[p]
j.move(turn[0], turn[1])
if i == len(self.body) -1:
self.turns.pop(p)
else:
if ((j.dir_x == -1 and j.pos[0] <= 0) or (j.dir_x == 1 and j.pos[0] >= j.rows-1) or
(j.dir_x == 1 and j.pos[1] >= j.rows-1) or (j.dir_x == -1 and j.pos[1] <= 0)):
terminate()
else:
j.move(j.dir_x, j.dir_y)
def reset (self, pos):
pass
def extend (self):
pass
def draw (self, window):
for i, j in enumerate(self.body):
if i == 0:
j.draw(window, True)
else:
j.draw(window)
def draw_grid(width, rows, window):
row_size = width//rows
x = 0
y = 0
for i in range(rows):
x += row_size
y += row_size
pg.draw.line(window, (255,255,255), (x,0), (x, width))
pg.draw.line(window, (255,255,255), (0,y), (width, y))
def redraw (window):
global rows, width, snake
snake.draw(window)
window.fill((0,0,0))
draw_grid(width, rows, window)
pg.display.update()
def terminate (window):
pass
def main():
global width, rows, snake
width = 500
rows = 10
window = pg.display.set_mode((width, width))
snake = snek((255,0,0), (10,10))
goal = True
clock = pg.time.Clock()
while goal:
pg.time.delay(50)
clock.tick(10)
redraw(window)
pass