Sean Cantwell
Sean Cantwell

Reputation: 31

Snake and Apple Do Not Align

I am learning about using the PyGame Module for Python 3.6, and in my journey of gaining knowledge, I decided to create a Snake game. My code for the game is below, and everything seems to work fine except the snake and apple do not align, and if they don't align, eating the apple is very difficult. (They're off by just a few pixels if that helps)

# Imports
import pygame
import random

# Colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)

# Setup
pygame_init = pygame.init()
dis_width = 1000
dis_height = 600
window = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption("Slither")
window.fill(white)
fps = 10
clock = pygame.time.Clock()
size = 25
apple = [[0, 0]]
snake = [[0, 0]]
x_step, y_step = 0, 0
apples_ate = 0

# Game Loop
main_menu = True
play_game = False
death_screen = False
while True:
    # Main Menu
    while main_menu:
        # Setup
        snake = [[(dis_width - 200) / 2, dis_height / 2, size]]
        apple = [[random.randrange(0, dis_width - 200 - size), random.randrange(0, dis_height - size), size]]

        # Message
        x_step, y_step = 0, 0
        window.fill(black, rect=[0, 0, dis_width, dis_height])
        font = pygame.font.SysFont(None, 50)
        window.blit(font.render("Press 'Space' To Continue", True, green), [100, 200])
        pygame.display.update()

        # Event Handler
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    main_menu = False
                    play_game = True

    # Play Menu
    while play_game:

        # Event Handler
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    y_step = -size
                    x_step = 0
                elif event.key == pygame.K_DOWN:
                    y_step = size
                    x_step = 0
                elif event.key == pygame.K_LEFT:
                    x_step = -size
                    y_step = 0
                elif event.key == pygame.K_RIGHT:
                    x_step = size
                    y_step = 0

        # Boundries
        if snake[0][0] + x_step < 0 or snake[0][0] + x_step > dis_width - size - 200:
            play_game = False
            death_screen = True
        elif snake[0][1] + y_step < 0 or snake[0][1] + y_step > dis_height - size:
            play_game = False
            death_screen = True
        else:
            snake[0][0] += x_step
            snake[0][1] += y_step

        # Apple Test
        if apple[0][0] == snake[0][0]:
            apple = [[random.randrange(0, dis_width - 200 - size), 
random.randrange(0, dis_height - size), size]]
            apples_ate += 1

        # Draws
        window.fill(white, rect=[dis_width - 200, 0, 200, dis_height])
        window.fill(black, rect=[0, 0, dis_width - 200, dis_height])
        font = pygame.font.SysFont(None, 50)
        window.blit(font.render("SCORE:", True, black), [dis_width - 200, 0])
        window.blit(font.render(str(apples_ate), True, black), [dis_width - 200, 50])
        pygame.draw.rect(window, red, [apple[0][0], apple[0][1], size, size])
        pygame.draw.rect(window, green, [snake[0][0], snake[0][1], size, size])
        pygame.display.update()
        clock.tick(fps)

    # Death Screen
    while death_screen:
        # Message
        window.fill(black, rect=[0, 0, dis_width - 200, dis_height])
        font = pygame.font.SysFont(None, 50)
        window.blit(font.render("You died.", True, red), [100, 100])
        window.blit(font.render("Press 'A' To Play Again.", True, red), [100, 200])
        window.blit(font.render("Press 'Q' To Quit", True, red), [100, 300])
        pygame.display.update()

        # Event Handler
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    main_menu = True
                    death_screen = False
                elif event.key == pygame.K_q:
                    pygame.quit()
                    quit()

Upvotes: 1

Views: 301

Answers (1)

Knight
Knight

Reputation: 23

Your error is at apple = [[random.randrange(0, dis_width - 200 - size), random.randrange(0, dis_height - size), size]]. The apple position can be anything within (0,800). So it can be 12 835 125 and you snake is at (400,300) which is (dis_width /2 -200,dis_height). And your assign x_step with +/-25,size which is snake movement.
So your snake position move by decrement or increment of 25. 0,25,50,.....,375,400,425,.....,750,775. If your apple position is 125 or 275 [125 % 25 = 0] there would be no problem.
But if you apple position is 122 Boom... if apple[0][0] == snake[0][0]: your snake can never eat his favorite fruit. Solution is add step at your randrange function apple = [[random.randrange(0, dis_width - 200 - size,size), random.randrange(0, dis_height - size,size), size]] this would be generate random number within 0,25,50,.....,725,750,775 (I don't know it's generate 800 too if it's generate solve).

And there is other minor error too if apple[0][0] == snake[0][0]: you have to match both x and y of snake position edit that too if apple[0][0] == snake[0][0] and apple[0][1] == snake[0][1]:.

Upvotes: 1

Related Questions