Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions snake.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
cols = 25
rows = 20

snakeColor = (2, 107, 245)

class cube():
class Cube():
rows = 20
w = 500
def __init__(self, start, dirnx=1, dirny=0, color=(255,0,0)):
def __init__(self, start, dirnx=1, dirny=0, color=snakeColor):
self.pos = start
self.dirnx = dirnx
self.dirny = dirny # "L", "R", "U", "D"
Expand Down Expand Up @@ -43,17 +44,18 @@ def draw(self, surface, eyes=False):



class snake():
class Snake():
body = []
turns = {}

def __init__(self, color, pos):
#pos is given as coordinates on the grid ex (1,5)
self.color = color
self.head = cube(pos)
self.body.append(self.head)
self.head = Cube(pos, color=self.color)
self.body = [self.head]
self.dirnx = 0
self.dirny = 1
self.score = 0

def move(self):
for event in pygame.event.get():
Expand Down Expand Up @@ -91,25 +93,30 @@ def move(self):


def reset(self,pos):
self.head = cube(pos)
self.body = []
self.body.append(self.head)
self.head = Cube(pos)
self.body = [self.head]
self.turns = {}
self.dirnx = 0
self.dirny = 1
self.score = 0
self.addCube()

print("Game over!\n")

def addCube(self):
tail = self.body[-1]
dx, dy = tail.dirnx, tail.dirny

self.score += 1

if dx == 1 and dy == 0:
self.body.append(cube((tail.pos[0]-1,tail.pos[1])))
self.body.append(Cube((tail.pos[0]-1,tail.pos[1])))
elif dx == -1 and dy == 0:
self.body.append(cube((tail.pos[0]+1,tail.pos[1])))
self.body.append(Cube((tail.pos[0]+1,tail.pos[1])))
elif dx == 0 and dy == 1:
self.body.append(cube((tail.pos[0],tail.pos[1]-1)))
self.body.append(Cube((tail.pos[0],tail.pos[1]-1)))
elif dx == 0 and dy == -1:
self.body.append(cube((tail.pos[0],tail.pos[1]+1)))
self.body.append(Cube((tail.pos[0],tail.pos[1]+1)))

self.body[-1].dirnx = dx
self.body[-1].dirny = dy
Expand All @@ -125,13 +132,11 @@ def draw(self, surface):

def redrawWindow():
global win
win.fill((0,0,0))
win.fill((28, 138, 6))
drawGrid(width, rows, win)
s.draw(win)
snack.draw(win)
pygame.display.update()
pass



def drawGrid(w, rows, surface):
Expand All @@ -143,8 +148,8 @@ def drawGrid(w, rows, surface):
x = x + sizeBtwn
y = y +sizeBtwn

pygame.draw.line(surface, (255,255,255), (x, 0),(x,w))
pygame.draw.line(surface, (255,255,255), (0, y),(w,y))
pygame.draw.line(surface, (102, 102, 102), (x, 0),(x,w))
pygame.draw.line(surface, (102, 102, 102), (0, y),(w,y))



Expand All @@ -165,9 +170,9 @@ def randomSnack(rows, item):
def main():
global s, snack, win
win = pygame.display.set_mode((width,height))
s = snake((255,0,0), (10,10))
s = Snake(snakeColor, (10,10))
s.addCube()
snack = cube(randomSnack(rows,s), color=(0,255,0))
snack = Cube(randomSnack(rows,s), color=(0,255,0))
flag = True
clock = pygame.time.Clock()

Expand All @@ -177,22 +182,20 @@ def main():
s.move()
headPos = s.head.pos
if headPos[0] >= 20 or headPos[0] < 0 or headPos[1] >= 20 or headPos[1] < 0:
print("Score:", len(s.body))
print("Score:", s.score)
s.reset((10, 10))

if s.body[0].pos == snack.pos:
s.addCube()
snack = cube(randomSnack(rows,s), color=(0,255,0))
snack = Cube(randomSnack(rows,s), color=(0,255,0))

for x in range(len(s.body)):
if s.body[x].pos in list(map(lambda z:z.pos,s.body[x+1:])):
print("Score:", len(s.body))
print("Score:", s.score)
s.reset((10,10))
break

redrawWindow()

main()