From cab046290bdd912f0b57d8012e3ef4f4319f496a Mon Sep 17 00:00:00 2001 From: Naksh-Rathore Date: Sun, 8 Jun 2025 16:51:42 -0500 Subject: [PATCH] Improve colors, reset snake with two segments, and make score a snake property - Adjusted color combinations for better aesthetics - Modified reset to initialize snake with a head and tail - Kept score as a snake property for better modularity and encapsulation --- snake.py | 53 ++++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/snake.py b/snake.py index 14fd8c97..5aff7fe0 100644 --- a/snake.py +++ b/snake.py @@ -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" @@ -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(): @@ -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 @@ -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): @@ -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)) @@ -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() @@ -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() - - - + \ No newline at end of file