I'm pretty new to python and I am trying to make a simple python snake game with the pygame module but every time I load it up it starts the game over sequence and quits the game. There are no errors, its just this issue. I need help with this. Thanks in advance.
import pygame
import sys
import time
import random
# Set up pygame.
pygame.init()
# Set up the window.
windowSurface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption('Grenade Snake')
# Set up the colors.
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
# Set up the snake.
snake = []
snakeLength = 3
for i in range(0, snakeLength):
snake.append((200, 200))
# Set up the apple.
apple = pygame.Surface((10, 10))
apple.fill(GREEN)
appleRect = apple.get_rect()
appleX = random.randint(0, 500)
appleY = random.randint(0, 400)
appleRect.topleft = (appleX, appleY)
# Set up movement variables.
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
MOVESPEED = 10
# Set up the music.
# Set up the font.
font = pygame.font.SysFont(None, 48)
# Set up the score.
score = 0
# Set up game over text.
gameOverText = font.render('Game Over', True, WHITE)
gameOverRect = gameOverText.get_rect()
gameOverRect.center = (500 // 2, 400 // 2)
# Run the game loop.
while True:
# Check for events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
# Change the keyboard variables.
if event.key == pygame.K_LEFT:
moveRight = False
moveLeft = True
if event.key == pygame.K_RIGHT:
moveLeft = False
moveRight = True
if event.key == pygame.K_UP:
moveDown = False
moveUp = True
if event.key == pygame.K_DOWN:
moveUp = False
moveDown = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == pygame.K_LEFT:
moveLeft = False
if event.key == pygame.K_RIGHT:
moveRight = False
if event.key == pygame.K_UP:
moveUp = False
if event.key == pygame.K_DOWN:
moveDown = False
if event.type == pygame.MOUSEBUTTONUP:
# Change the mouse button variables.
if event.button == pygame.LEFT:
moveUp = False
moveDown = False
# Fill the background color.
windowSurface.fill(BLACK)
# Move the snake.
if moveDown and snake[0][1] + 10 < 400:
snake[0] = (snake[0][0], snake[0][1] + 10)
if moveUp and snake[0][1] > 10:
snake[0] = (snake[0][0], snake[0][1] - 10)
if moveLeft and snake[0][0] > 10:
snake[0] = (snake[0][0] - 10, snake[0][1])
if moveRight and snake[0][0] + 10 < 500:
snake[0] = (snake[0][0] + 10, snake[0][1])
# Check if the snake has collied with itself.
for snakeBody in snake[1:]:
if snake[0][0] == snakeBody[0] and snake[0][1] == snakeBody[1]:
pygame.mixer.music.stop()
windowSurface.blit(gameOverText, gameOverRect)
pygame.display.update()
time.sleep(2)
pygame.quit()
sys.exit()
# Check if the snake has eaten an apple.
if snake[0][0] == appleRect.left and snake[0][1] == appleRect.top:
# Play the sound effect.
pickUpSound.play()
# Increment the score.
score = score + 1
# The new head of the snake.
snake.append((0, 0))
# Move the apple.
appleX = random.randint(0, 500)
appleY = random.randint(0, 400)
appleRect.topleft = (appleX, appleY)
# Draw the snake.
for position in snake:
pygame.draw.rect(windowSurface, WHITE, pygame.Rect(position[0], position[1], 10, 10))
# Draw the apple.
windowSurface.blit(apple, appleRect)
# Draw the score.
scoreText = font.render(str(score), True, WHITE)
windowSurface.blit(scoreText, (0, 0))
# Draw the window onto the screen.
pygame.display.update()
time.sleep(0.02)
When you set up the snake:
snake = []
snakeLength = 3
for i in range(0, snakeLength):
snake.append((200, 200))
you place the snake head[0]
and rest of the snake on the same position (200,200)
. Then during the first iteration it checks if the snake's head is on top of any part of the snake's body, and it is.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.