-2
\$\begingroup\$

I have made a snake game with pygame but there are some problems. To begin with the code works but there are some minor problems. Firstly, snake isn't eating the apple(actually you have to pass over the apple many time to incease length) and secondly snake crosses the boundaries without game over. P.S please be lenient and don't delete my post because i'm making this game up to 2 months and i haven't found the mistakes yet. Thank you. :)

import pygame
import random
import sys

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)")

movement = 10
block_size = 10
FPS = 22

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
    clock = pygame.time.Clock()
    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 = -movement
                    pos_y_change = 0
                elif event.key == pygame.K_RIGHT:
                    pos_x_change = movement
                    pos_y_change = 0
                elif event.key == pygame.K_UP:
                    pos_y_change = -movement
                    pos_x_change = 0
                elif event.key == pygame.K_DOWN:
                    pos_y_change = movement
                    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()
    sys.exit()
    quit()


gameLoop()
\$\endgroup\$

closed as off-topic by Martin R, Josay, 200_success, Ludisposed, Graipher Feb 10 at 13:41

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Martin R, Josay, 200_success, Ludisposed, Graipher
If this question can be reworded to fit the rules in the help center, please edit the question.

Browse other questions tagged or ask your own question.