0

I can't seem to the error in this code, the game simply quits right after launching it. If anybody could read through my code and answer me what I'm doing wrong, and I'd be very grateful. I've already read through all of it and couldn't figure it out. I don't know why the game quits right after launching it. (Meaning I can't even move my block).

import pygame
import time

pygame.init()

width = 800
height = 600
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)

gameDisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption("First snake game.")

clock = pygame.time.Clock()

block_size = 1
FPS = 100





def gameloop():

    gameExit = False

    lead_x = width/2
    lead_y = height/2
    lead_x_change = 0
    lead_y_change = 0

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_w:
                    lead_y_change = -block_size
                    lead_x_change = 0
                if event.key == pygame.K_s:
                    lead_y_change = block_size
                    lead_x_change = 0
                if event.key == pygame.K_d:
                    lead_x_change = block_size
                    lead_y_change = 0
                if event.key == pygame.K_a:
                    lead_x_change =  -block_size
                    lead_y_change = 0
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_a or event.key == pygame.K_d:
                    lead_x_change = 0
                if event.key == pygame.K_w or event.key == pygame.K_s:
                    lead_y_change = 0


        lead_x += lead_x_change
        lead_y += lead_y_change

        gameDisplay.fill(white)
        pygame.draw.rect(gameDisplay, black, [lead_x, lead_y, block_size, block_size])
        pygame.display.update()
        time.sleep(2)
        pygame.quit()
        quit()


    clock.tick(FPS)
gameloop()

`

1
  • You have pygame.quit() inside of your while loop.... Commented Aug 28, 2018 at 20:47

1 Answer 1

1

Not sure if I'm understanding the code correctly or not, but at the end of the gameloop() function you have pygame.quit() and quit() so comment that out and see what happens.

Sign up to request clarification or add additional context in comments.

1 Comment

Oops, I copied @rahlf23's comment without seeing it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.