-1

I'm trying to make my first game, so I thought the snake game would be a nice doable challenge (guess I was wrong) to get started.

The problem seems to be that when I append a snake part to the body list, containing all the parts, it seems to append it multiple times. This makes all the parts to be in the same position and it loses the whole rest of the "snake" body. How would I fix this or is there some other issue with the code.

import pygame
pygame.init()


class Cube():
    pos = [0, 0]

    def __init__(self, x, y, color=(255, 0, 0)):
        self.color = color
        self.pos[0] = x
        self.pos[1] = y

    def draw(self):
        x = (self.pos[0] * xSizeBtwn) + 2
        y = (self.pos[1] * ySizeBtwn) + 2
        pygame.draw.rect(win, self.color, (x, y, xSizeBtwn - 2, ySizeBtwn - 2))
        pygame.display.update()


class Snake():
    body = []
    pos = []

    def __init__(self, color=(255, 0, 0)):
        self.color = color
        self.dirx = 1
        self.diry = 0
        self.length = 10
        self.pos = [3, 3]

    def move(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_a]:
            self.dirx = -1
            self.diry = 0
            print("moved a")

        elif keys[pygame.K_d]:
            self.dirx = 1
            self.diry = 0
            print("moved d")

        elif keys[pygame.K_w]:
            self.diry = -1
            self.dirx = 0
            print("moved s")

        elif keys[pygame.K_s]:
            self.diry = 1
            self.dirx = 0
            print("moved s")

        self.newPosX = self.pos[0] + self.dirx
        self.newPosY = self.pos[1] + self.diry
        self.pos[0] = self.newPosX
        self.pos[1] = self.newPosY

        present = True
        for b in self.body:
            print(b.pos, self.newPosX, self.newPosY)
            if b.pos[0] == self.newPosX and b.pos[1] == self.newPosY:
                print("true")
                present = False
        # the problem is right here (i think)
        if present:
            self.body.append(Cube(self.newPosX, self.newPosY, self.color))

        if len(self.body) > self.length:
            self.body.pop(0)

    def draw(self):
        for b in self.body:
            b.draw()
            print(b.pos)


def drawAll():
    win.fill((30, 30, 30))
    drawGrid()
    pygame.display.update()


def drawGrid():

    x = 0
    y = 0
    for n in range(rows - 1):
        y += ySizeBtwn
        pygame.draw.line(win, (255, 255, 255), (0, (int)(y)), (gameHeight, (int)(y)))
    for n in range(columns - 1):
        x += xSizeBtwn
        pygame.draw.line(win, (255, 255, 255), ((int)(x), 0), ((int)(x), gameWidth))


def main():

    global gameWidth, gameHeight, columns, rows, xSizeBtwn, ySizeBtwn, win
    gameWidth = 500
    gameHeight = 500
    columns = 15
    rows = 15
    xSizeBtwn = gameWidth / columns
    ySizeBtwn = gameHeight / rows

    win = pygame.display.set_mode((gameWidth, gameHeight))
    pygame.display.set_caption("Snake")

    clock = pygame.time.Clock()
    run = True
    snake = Snake()
    while run:
        pygame.time.delay(100)
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        drawAll()
        snake.move()
        snake.draw()


main()
pygame.quit()

Sorry for the messy code, I just started programming.

Also, if you have any tips, it would be really helpful.

William Miller
  • 9,839
  • 3
  • 25
  • 46

1 Answers1

0

The following initializations are probably not doing what you think they are doing: these variables will be shared across classes.

class Cube():
    pos = [0, 0]
    ...

class Snake():
    body = []
    pos = []
    ...

Instead, you should initialize those variables in the __init__ function as follows:

class Cube:
    def __init__(self, x, y, color=(255, 0, 0)):
        self.pos = [0, 0]
        self.color = color
        self.pos[0] = x
        self.pos[1] = y
...


class Snake:
    def __init__(self, color=(255, 0, 0)):
        self.color = color
        self.dirx = 1
        self.diry = 0
        self.length = 10
        self.pos = [3, 3]
        self.body = []

Result

snake

Kent Shikama
  • 3,910
  • 3
  • 22
  • 55