-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.py
55 lines (45 loc) · 1.76 KB
/
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
import pygame
from pygame import Rect
from Dir import Dir
from Config import Config as C
from GameObject import GameObject
class Snake(GameObject):
# Class Snake models a snake
def __init__(self, x, y):
self.num_squares = (C.WIDTH / C.SQUARE_SIZE, C.HEIGHT / C.SQUARE_SIZE)
self.dir: Dir = Dir.UP
self.head = (x, y)
self.body = [
self.head,
(self.head[0], self.head[1] + 1),
(self.head[0], self.head[1] + 2)
]
def move(self):
self.body.pop(len(self.body) - 1)
self.head = ((self.head[0] + self.dir[0]), (self.head[1] + self.dir[1]))
self.wrap()
if self.head in self.body:
return True
self.body.insert(0, self.head)
return False
def jump_to(self, pos):
self.head = pos
self.wrap()
def wrap(self):
if self.head[0] >= C.WIDTH / C.SQUARE_SIZE:
self.head = (0, self.head[1])
elif self.head[0] < 0: # Don't check for =0 since we draw from top-left
self.head = (C.WIDTH / C.SQUARE_SIZE, self.head[1])
elif self.head[1] >= C.HEIGHT / C.SQUARE_SIZE:
self.head = (self.head[0], 0)
elif self.head[1] < 0:
self.head = (self.head[0], C.HEIGHT / C.SQUARE_SIZE)
def eat(self): # "Move" backwards without erasing a square
new_pos = (self.body[-1][0] + self.dir[0], self.body[-1][1] + self.dir[1])
self.body.append(new_pos)
def update(self, snake):
return self.move()
def draw(self, screen):
for sq in self.body:
rect = Rect(sq[0]*C.SQUARE_SIZE, sq[1]*C.SQUARE_SIZE, C.SQUARE_SIZE, C.SQUARE_SIZE)
pygame.draw.rect(screen, C.GREEN, rect)