Hi I am currently writing a snake game code and I am nearly finished however I am having difficulty writing a code which will cause the game to end if the head of the snake collides with its body, I thought I could create a collide function similar to the collide function for the snake and the apple:
pygame.sprite.collide_rect(h, a)
however all the separate parts of the snake act in the same way so the snake will always be constantly colliding with itself. Are there any ways around this.
here is my full snake code:
import pygame
import randomBLACK = (0, 0, 0)
GREEN = (0, 250, 0)
RED = (250, 0, 0)
Width = 15
Space = 3
Xspeed = 18
Yspeed = 0
Factor = 18
clock = pygame.time.Clock()
segments = 2
HitLoop = 0
ScreenWidth = 800
AppleCount = 1#creating initial snake
class HEAD(pygame.sprite.Sprite):def __init__(self, x, y, colour = GREEN):super().__init__()self.image = pygame.Surface([Width, Width])self.image.fill(colour)self.rect = self.image.get_rect()self.rect.x = xself.rect.y = yclass APPLE(pygame.sprite.Sprite):def __init__(self, z, q):super().__init__()self.image = pygame.Surface([Width, Width])self.image.fill(RED)self.rect = self.image.get_rect()self.rect.x = zself.rect.y = qpygame.init()
screen = pygame.display.set_mode([ScreenWidth, ScreenWidth])
pygame.display.set_caption('Snake')
allspriteslist = pygame.sprite.Group()SnakeSegments = []
for i in range(segments):x = 250 - (Width + Space) * iy = 30h = HEAD(x, y)SnakeSegments.append(h)allspriteslist.add(h)AppleList = []
for i in range(0,AppleCount):z = random.randint(10,ScreenWidth-25)q = random.randint(10,ScreenWidth-25)a = APPLE(z, q)AppleList.append(a)allspriteslist.add(a)#main loop
done = False
while not done:for event in pygame.event.get():if event.type == pygame.QUIT:done = Trueif event.type == pygame.KEYDOWN:if event.key == pygame.K_LEFT:if Xspeed == -Factor:Xspeed = 0if Xspeed == Factor:Xspeed = Factorelse:Xspeed = Xspeed - FactorYspeed = 0elif event.key == pygame.K_RIGHT:if Xspeed == Factor:Xspeed = 0if Xspeed == -Factor:Xspeed = -Factorelse:Xspeed = Xspeed + FactorYspeed = 0elif event.key == pygame.K_UP:if Yspeed == -Factor:Yspeed = 0if Yspeed == Factor:Yspeed = Factorelse:Yspeed = Yspeed - FactorXspeed = 0elif event.key == pygame.K_DOWN:if Yspeed == Factor:Yspeed = 0if Yspeed == -Factor:Yspeed = -Factorelse:Yspeed = Yspeed + FactorXspeed = 0clock.tick(10)#snake builderOldSegment = SnakeSegments.pop(-1)allspriteslist.remove(OldSegment)x = SnakeSegments[0].rect.x + Xspeedy = SnakeSegments[0].rect.y + Yspeedh = HEAD(x, y)SnakeSegments.insert(0, h)allspriteslist.add(h,a)allspriteslist.update()# collision had to create apples own list for respawnif pygame.sprite.collide_rect(h, a) == True and HitLoop == 0:SnakeSegments.append(h)AppleList.append(a)HitLoop = HitLoop + 1z = random.randint(10, ScreenWidth - 25)q = random.randint(10, ScreenWidth - 25)OldApple = AppleList.pop()allspriteslist.remove(OldApple)a = APPLE(z, q)allspriteslist.update()# collision had to create a new classif pygame.sprite.collide_rect(h, h) == True:pass# hit timerif HitLoop > 0:HitLoop += 1if HitLoop > 4:HitLoop = 0screen.fill(BLACK)#game wallspygame.draw.rect(screen, GREEN, [0, 0, ScreenWidth, 10])pygame.draw.rect(screen, GREEN, [0, 0, 10, ScreenWidth])pygame.draw.rect(screen, GREEN, [0, ScreenWidth - 10, ScreenWidth, 10])pygame.draw.rect(screen, GREEN, [ScreenWidth - 10, 0, 10, ScreenWidth])if x <= 10:done = Trueif x >= ScreenWidth - Width:done = Trueif y <= 10:done = Trueif y >= ScreenWidth - Width:done = Trueallspriteslist.draw(screen)pygame.display.flip()