1

I'm new to programming and currently taking a fundamental course with zero prior knowledge. My team's current project is to program a game in python. It is a remake of the classic snake game where you eat the object and grow larger while avoiding colliding with yourself.

The current issue im having is that I'm adding in another 'snack' that will minus a tail instead of adding if the wrong block is eaten. How would I go about removing a 'tail' element if the player collides into the wrong element?

Here is also a github link to the full code: https://github.com/GroupProject701/Snek/blob/master/Snek

I assumed that using remove() or kill() would remove a 'tail' from the 'snake' but i'm having difficulties understanding how to execute the functions and executing the proposed idea of the project.

Here is also a github link to the full code: https://github.com/GroupProject701/Snek/blob/master/Snek

this is the code that is used to append an extra 'tail' block to the player and is working properly

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

        if dx == 1 and dy == 0:
            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])))
        elif dx == 0 and dy == 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[-1].x = dx
        self.body[-1].y = dy
# ADD TAIL CODE ABOVE HERE------------------------

This is the code that we tried to execute the plan to remove a 'tail' block from the player if they collide into this object.

    def minusCube(self):
        tail = self.body[-1]
        dx2, dy2 = tail.x, tail.y

        if dx2 == 1 and dy2 == 0:
            self.body.remove(cube((tail.pos[0]-1,tail.pos[1])))
        elif dx2 == -1 and dy2 == 0:
            self.body.remove(cube((tail.pos[0]+1,tail.pos[1])))
        elif dx2 == 0 and dy2 == 1:
            self.body.remove(cube((tail.pos[0],tail.pos[1]-1)))
        elif dx2 == 0 and dy2 == -1:
            self.body.remove(cube((tail.pos[0],tail.pos[1]+1)))

        self.body[+1].x = dx2
        self.body[+1].y = dy2

# MINUS TAIL CODE ABOVE HERE FOR PP3---------------------

We expect the 'snake' to lose a tail block when it collides with the new object, but instead we're getting errors from remove() and this is most likely due to lists not defined.

shirayyyy
  • 11
  • 1
  • It `self.body` is a list, then `del self.body[-1]` removes the last element from the list. See [`del`](https://docs.python.org/3/tutorial/datastructures.html#the-del-statement) statement. – Rabbid76 Jul 28 '19 at 20:59
  • pygame has [pygame.Rect](https://www.pygame.org/docs/ref/rect.html) to keep position and size and it has few methods to check collision - so you don't have to write own method. And it has also [pygame.sprite.Group](https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group) and functions to check collision in groups and remove elemets at once - so you don't have to write own method. [pygame.sprite.groupcollide()](https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.groupcollide) – furas Jul 28 '19 at 21:26

0 Answers0