Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

I have 2 problems with snake game in python

Hello everyone. I'm  making the snake game with python but i have 2 major problems:
Firstly, snake length does not change even i eat the apple(actually i have to pass over the apple many times to increase length) and secondly, snake crosses the boundaries without game over.(I've programmed the boundaries). Also how can i make sprite borders? Thank you.

Code
#libraries
import pygame
import random

pygame.init()

white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,155,0)

display_width = 800
display_height = 600

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Snake(alpha edition)")


block_size = 10
FPS = 30
clock = pygame.time.Clock()

font = pygame.font.SysFont(None, 25)

def snake(block_size, snakelist):
   for xny in snakelist:
       pygame.draw.rect(gameDisplay, green, [xny[0], xny[1], block_size, block_size])
   





def gameover_msg(msg,color):
   screen_text = font.render(msg, True, color)
   gameDisplay.blit(screen_text, [display_width/2, display_height/2])


def gameLoop():
   gameExit = False
   gameOver = False
   pos_x = display_width/2
   pos_y = display_height/2
   pos_x_change = 0
   pos_y_change = 0
   randAppleX = round(random.randrange(0, display_width-block_size)/10.0)*10.0
   randAppleY = round(random.randrange(0, display_height-block_size)/10.0)*10.0
   snakelist = []
   snakelength = 1

   while not gameExit:
       
       while gameOver == True:
           gameDisplay.fill(white)
           gameover_msg("Game over, press R to play again or Q to quit", red)
           pygame.display.update()

           for event in pygame.event.get():
               if event.type == pygame.KEYDOWN:
                   if event.key == pygame.K_q:
                       gameExit = True
                       gameOver = False
                   if event.key == pygame.K_r:
                       gameLoop()
                   
           

       
       for event in pygame.event.get():
           if event.type == pygame.QUIT:
               gameExit = True
           if event.type == pygame.KEYDOWN:
               if event.key == pygame.K_LEFT:
                   pos_x_change = -block_size
                   pos_y_change = 0
               elif event.key == pygame.K_RIGHT:
                   pos_x_change = block_size
                   pos_y_change = 0
               elif event.key == pygame.K_UP:
                   pos_y_change = -block_size
                   pos_x_change = 0
               elif event.key == pygame.K_DOWN:
                   pos_y_change = block_size
                   pos_x_change = 0

           if pos_x >= display_width or pos_x < 0 or pos_y >= display_height or pos_y < 0:
               gameOver = True

           
           if pos_x == randAppleX and pos_y == randAppleY:
               randAppleX = round(random.randrange(0, display_width-block_size)/10.0)*10.0
               randAppleY = round(random.randrange(0, display_height-block_size)/10.0)*10.0
               snakelength += 1
             




       pos_x += pos_x_change
       pos_y += pos_y_change

       gameDisplay.fill(white)
       pygame.draw.rect(gameDisplay, red, [randAppleX, randAppleY, block_size, block_size])

       snakehead = []
       snakehead.append(pos_x)
       snakehead.append(pos_y)
       snakelist.append(snakehead)

       if len(snakelist) > snakelength:
           del snakelist[0]

       if snakehead in snakelist[:-1]:
           if not snakehead == snakelist[0]:
               continue
           gameOver = True

       
       snake(block_size, snakelist)
       
       
       pygame.display.update()

       clock.tick(FPS)


   pygame.quit()
   quit()


gameLoop()
ps: I apologise for my english. I am greek. 🤗
when the script checks the collision of the snake against the apple the script looks at the exact coordinates of the snake and the apple, instead of looking to see if your x or y is any where inside of the apple. To do this you will need to do something like this.

if pos_x+block_size+1 >= randAppleX+1 and pos_x+1 <= randAppleX+block_size+1 and pos_y+block_size+1 >= randAppleY+1 and pos_y+1 <= randAppleY+block_size+1:
randAppleX = round(random.randint(0, display_width-block_size)/10.0)*10.0
randAppleY = round(random.randint(0, display_height-block_size)/10.0)*10.0
snakelength += 1

Also you are running this collision within a for loop instead of your games main while loop, that's why it takes a couple of passes of the apple
to actually eat the apple


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Shell] by ɩᴄᴀʀᴜȿ ᴌוᴠᴇѕ™Sep 24, 2026 09:59 AM 5 201 Sep 24, 2026 09:59 AM
Last Post: Anonynuk
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 632 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[Java] by GerymcApr 8, 2019 05:35 PM 2 216 Apr 8, 2019 05:35 PM
Last Post: seanizcool