-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.py
28 lines (24 loc) · 851 Bytes
/
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
import copy
import random
class SnakeSegment:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
class Snake:
def __init__(self):
self.x = 10
self.y = 0
self.size = 10
self.body = [SnakeSegment(320, 240)] # Inicializa a cobra com um segmento em (320, 240)
def move(self):
# Move cada segmento do corpo para a posição de seu predecessor
for i in range(len(self.body)-1, 0, -1):
self.body[i].x = self.body[i-1].x
self.body[i].y = self.body[i-1].y
# Move the head of the snake
self.body[0].x += self.x
self.body[0].y += self.y
def grow(self):
# Adicione um novo segmento ao corpo na mesma posição que a cauda atual
last_segment = copy.copy(self.body[-1])
self.body.append(last_segment)