-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathClass_Snake.py
49 lines (40 loc) · 1.35 KB
/
Class_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
import pygame
import random
from Constants_Snake import *
class Snake:
def __init__ (self):
initPos = screenSize/2
self.body = [ (initPos, initPos) ]
self.direction = "Left"
def move(self):
(x, y) = self.body[0]
(dx, dy) = movMap[self.direction]
newHead = (x+dx, y+dy)
self.body.insert(0, newHead)
def valid(self):
(x, y) = self.body[0]
if x < 0 or x > screenSize-pixelSize or y < 0 or y > screenSize-pixelSize:
return False
head = self.body[0]
if self.body.count(head) > 1:
return False
return True
def setDirection(self, direction):
if len(self.body) is 1:
self.direction = direction
return
indexDir = directions.index(self.direction)
indexNewDir = directions.index(direction)
if (indexDir+2)%4 is not indexNewDir:
self.direction = direction
def pop(self):
self.body.pop()
class Food:
def generateCoords(self):
return (random.randint(1, lim)*pixelSize, random.randint(1, lim)*pixelSize)
def appearFood(self, snake):
#while the new food is in the body of the snake appear a new food
while self.pos in snake.body:
self.pos = self.generateCoords()
def __init__ (self):
self.pos = self.generateCoords()