-
Notifications
You must be signed in to change notification settings - Fork 0
/
theSnake.py
81 lines (61 loc) · 2.2 KB
/
theSnake.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
import math
import random
import pygame as pg
scale = 20
class SnakeObj(object):
def __init__(self):
self.pos = [0,0]
self.direction = [1,0]
self.sizeIndex = 0
self.tailIncrease = 2
self.tail= []
self.tailPosition = [0,0]
def setPos(self, x, y):
self.pos = [x,y]
def show(self):
from snake import win, width, height
for t in self.tail:
pg.draw.rect(win, (0, 255, 0), (t[0],t[1], scale, scale))
pg.draw.rect(win, (0, 0, 255),(self.pos[0]-1, self.pos[1]-1, scale+2, scale+2)) #add this for border around head
pg.draw.rect(win, (0, 255, 0),(self.pos[0], self.pos[1], scale, scale))
def update(self):
if (self.sizeIndex > 0):
if (self.sizeIndex == len(self.tail)):
self.tail.pop(0)
self.tail.append([self.pos[0], self.pos[1]])
self.pos[0] += self.direction[0]*scale
self.pos[1] += self.direction[1]*scale
if (len(self.tail) > 0):
self.tailPosition = self.tail[0]
else:
self.tailPosition = self.pos
def isDead(self):
from snake import win, width, height
if (self.pos[0] < 0 or self.pos[1] < 0 or self.pos[0] > width - scale or self.pos[1] > height - scale):
self.sizeIndex = 0
self.tail.clear()
return True
for i in range(len(self.tail)):
if (self.pos[0] == self.tail[i][0] and self.pos[1] == self.tail[i][1]):
self.sizeIndex = 0
self.tail.clear()
return True
return False
def hitApple(self, x, y):
if(self.pos == [x,y]):
self.sizeIndex += self.tailIncrease
return True
return False
def getPosition(self):
return self.pos
def getTailPosition(self):
return self.tailPosition
def goToPosition(self,goalPos):
if (self.pos[1] < goalPos[1]):
self.direction = [0, 1]
elif (self.pos[1] > goalPos[1]):
self.direction = [0, -1]
elif(self.pos[0] > goalPos[0]):
self.direction = [-1,0]
elif(self.pos[0] < goalPos[0]):
self.direction = [1, 0]