Click here to Skip to main content
16,017,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to make a snake game for a school project in pygame, but I am having difficulty working out how to make the snake grow. Not looking for the most efficient code possible just a way of doing it. Also not looking for a whole rewritten project or anything, I want it to be my own work just looking for a method on how i can maybe solve this problem. I know it will involve looping a list and maybe another class but this is what I have so far:

Python
import pygame, sys
import os, random
pygame.init()

height = 720
width = 1280
screen = pygame.display.set_mode((width,height))

class Snake(pygame.sprite.Sprite):

     def __init__(self):
          pygame.sprite.Sprite.__init__(self)
          self.image = pygame.Surface((25,25))
          self.image.set_colorkey(black)
          snakeimage = pygame.image.load("block.png")
          self.image.blit(snakeimage,(0,0))
          self.rect = self.image.get_rect()


class Food(pygame.sprite.Sprite):

     def __init__(self,x ,y):
          pygame.sprite.Sprite.__init__(self)
          self.image = pygame.Surface((25,25))
          self.image.set_colorkey(black)
          foodimage = pygame.image.load("apple.png")
          self.image.blit(foodimage, (0,0))
          self.rect = self.image.get_rect()
          self.rect.x = x
          self.rect.y = y


black = (0,0,0)
red = (255,0,0)
white = (255,255,255)
green = (0,100,0)
yellow = (255, 255, 0)
lgreen = (0,255,0)
lred = (255,108,108)
lyellow = (255,255,224)
snakesgroup = pygame.sprite.Group()
foodgroup = pygame.sprite.Group()

largefont = pygame.font.SysFont("comicsansms", 110)
pausedlabel = largefont.render("GAME PAUSED", 1, (black))
pygame.display.set_caption("Snake Game")
clock = pygame.time.Clock()
buttonfont2 = pygame.font.SysFont("Britannic Bold", 35)

def gamewindow1 ():

     stopped = True

     while stopped:
        playbut = pygame.Rect(100, 550, 100, 50)
        quitbut = pygame.Rect(500,550,100,50)
        leadbut = pygame.Rect(900,550,220,50)
        mouse = pygame.mouse.get_pos()

        screen.fill(black)
        myfont=pygame.font.SysFont("Britannic Bold", 80)
        buttonfont = pygame.font.SysFont("Britannic Bold", 40)
        leadfont = pygame.font.SysFont("Britannic Bold", 30)
        nlabel=myfont.render("WELCOME TO SNAKE", 1, (255, 0, 0))
        leadlabel = myfont.render("LEADERBOARDS ARE NOT YET AVAILIBLE", 1, (black))
        scorefont = pygame.font.SysFont("Brittanic Bold", 30)

        pygame.display.flip()
        clock.tick(60)
        playbutfont = buttonfont.render("Play!", 1,(black))
        quitbutfont = buttonfont.render("Quit!", 1 ,(black))
        leadbutfont = buttonfont.render("Leaderboards!", 1,(black))

        screen.blit(nlabel,(380,110 ))

        if 500+100 > mouse[0] > 500 and 550 + 50  > mouse[1] > 550:
             pygame.draw.rect(screen, (lred), quitbut)
        else:
             pygame.draw.rect(screen, [255, 0, 0], quitbut)
        if 100+100 > mouse[0] > 100 and 550 + 50 > mouse[1] > 550:
             pygame.draw.rect(screen, (lgreen),  playbut)
        else:
            pygame.draw.rect(screen, (green),  playbut)
        if 900+220 > mouse[0] > 900 and 550 + 50 > mouse[1] > 550:
             pygame.draw.rect(screen, (lyellow),  leadbut)
        else:
             pygame.draw.rect(screen, (yellow),  leadbut)
        screen.blit(playbutfont,(115,560))
        screen.blit(quitbutfont, (515, 560))
        screen.blit(leadbutfont,(915, 560))


        pygame.display.update()
        clock.tick(20)




        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                stopped = False
                sys.exit
                pygame.quit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                 if quitbut.collidepoint(mouse):
                      pygame.quit()
                      stopped = False
                      sys.exit
                 if leadbut.collidepoint(mouse):
                      stopped = False
                      screen.fill(white)
                      inlead = True
                      screen.blit(leadlabel, (100,360))
                      pygame.display.set_caption("Leaderboards")
                      pygame.display.flip()
                      while inlead:
                           for event in pygame.event.get():
                                if event.type == pygame.QUIT:
                                     pygame.quit()
                                     stopped = False
                                     sys.exit
                                if event.type == pygame.KEYDOWN:
                                     if event.key == pygame.K_ESCAPE:
                                          pygame.display.set_caption("Snake Game")
                                          pygame.mouse.set_visible(True)

                                          inlead = False
                                          stopped = True







                 if playbut.collidepoint(mouse):
                      pygame.display.set_caption("Game")
                      screen.fill(black)
                      pygame.mouse.set_visible(False)  
                      pygame.display.flip()
                      stopped = False
                      ingame = True
                      score = 0
                      snake = Snake()
                      snake.rect.x = random.randint(10,1180)
                      snake.rect.y = random.randint(10,650)
                      xcoord = random.randint(10,1180)
                      ycoord =  random.randint(10,650)
                      food = Food(xcoord,ycoord)


                      foodgroup.add(food)
                      snakesgroup.add(snake)
                      speed = (0,0)

                      while ingame:

                           for event in pygame.event.get():
                                if event.type == pygame.QUIT:
                                     ingame = False
                                     sys.exit
                                     pygame.quit()
                                if event.type == pygame.KEYDOWN:
                                     if event.key == pygame.K_ESCAPE:
                                          pygame.display.set_caption("Snake Game")
                                          pygame.mouse.set_visible(True)
                                          foodgroup.remove(food)
                                          snakesgroup.remove(snake)
                                          ingame = False
                                          stopped = True
                                     if event.key == pygame.K_p:
                                          paused = True
                                          ingame = False
                                          resumebut = pygame.Rect(100, 550, 100, 50)
                                          quitbut = pygame.Rect(900,550,100,50)
                                          resumebutfont = buttonfont2.render("Resume", 1 , (black))

                                          while paused:
                                               mouse = pygame.mouse.get_pos()
                                               for event in pygame.event.get():
                                                    if event.type == pygame.MOUSEBUTTONDOWN:
                                                         if quitbut.collidepoint(mouse):
                                                              pygame.quit()
                                                              paused = False
                                                              sys.exit
                                                         if resumebut.collidepoint(mouse):
                                                              pygame.mouse.set_visible(False)
                                                              paused = False
                                                              ingame = True

                                                    if event.type == pygame.QUIT:
                                                         paused = False
                                                         pygame.quit()
                                                         sys.exit()
                                                    if event.type == pygame.KEYDOWN:
                                                         if event.key == pygame.K_ESCAPE:

                                                              foodgroup.remove(food)
                                                              snakesgroup.remove(snake)
                                                              paused = False
                                                              stopped = True
                                               screen.fill(white)          
                                               if 900+100 > mouse[0] > 900 and 550 + 50  > mouse[1] > 550:
                                                    pygame.draw.rect(screen, (lred), quitbut)
                                               else:
                                                    pygame.draw.rect(screen, [255, 0, 0], quitbut)
                                               if 100+100 > mouse[0] >100 and 550 + 50 > mouse[1] >550:
                                                   pygame.draw.rect(screen, (green), resumebut)
                                               else:
                                                   pygame.draw.rect(screen,(lgreen),resumebut)
                                               pygame.display.set_caption("Paused")
                                               pygame.mouse.set_visible(True)
                                               screen.blit(pausedlabel,(200,100))
                                               screen.blit(resumebutfont, (100,560))
                                               screen.blit(quitbutfont, (915, 560))

                                               pygame.display.update()

                                     keys = pygame.key.get_pressed()
                                     if keys[pygame.K_LEFT]:
                                          speed = (-5,0)
                                     if keys[pygame.K_RIGHT]:
                                          speed = (5,0)
                                     if keys[pygame.K_UP]:
                                          speed = (0,-5)
                                     if keys[pygame.K_DOWN]:
                                          speed = (0,5)

                           if snake.rect.colliderect(food) :
                                score = score + 1
                                foodgroup.remove(food)
                                xcoord = random.randint(2,1278)
                                ycoord = random.randint(2,718)
                                food = Food(xcoord, ycoord)
                                foodgroup.add(food)

                           if snake.rect.x < 2  or snake.rect.x > 1278:
                                ingame = False
                                stopped = True
                                foodgroup.remove(food)
                                snakesgroup.remove(snake)
                                pygame.mouse.set_visible(True)
                           if snake.rect.y < 2 or snake.rect.y > 718 :
                                ingame = False
                                stopped = True
                                foodgroup.remove(food)
                                snakesgroup.remove(snake)

                                pygame.mouse.set_visible(True)

                           scorelabel = scorefont.render("Score: " + str(score), 1,white)
                           screen.fill(black)
                           snake.rect.x += speed[0]
                           snake.rect.y += speed[1]
                           foodgroup.draw(screen)
                           snakesgroup.draw(screen)
                           screen.blit(scorelabel, (0,0))
                           clock.tick(60)
                           pygame.display.flip()







gamewindow1()


What I have tried:

I have tried adding in lists and using for loops but I am having no luck.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900