简体   繁体   中英

Snake game: snake and apples do not appear on the screen

I'm trying to make the snake game in python, but sadly my snake and my apples aren't appearing on the screen, all i see is a gray screen with nothing on it, can you help me out? thanks! PS - i would like an explanation as well about why my current code isn't working so i would avoid it in the future, thanks again.

import pygame, sys, random, time
from pygame.locals import *

pygame.init()
mainClock = pygame.time.Clock()
windowWidth = 500
windowHeight = 500
windowSurface = pygame.display.set_mode((windowWidth, windowHeight), 0, 32)
red = (255, 0, 0)
green = (0, 255, 0)
color = (100, 100, 100)
snakeHead = [250, 250]
snakePosition = [[250, 250],[240, 250],[230, 250]]
applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]

def collisionBoundarias(snakeHead):
    if snakeHead[0] >= 500 or snakeHead[0] < 0 or snakeHead[1] >= 500 or snakeHead[1] < 0:
        return 1
    else:
        return 0

def collisionSelf(SnakePosition):
    snakeHead = snakePosition[0]
    if snakeHead in snakePosition[1:]:
        return 1
    else:
        return 0

while True:
    windowSurface.fill(color)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.type == pygame.K_RIGHT or event.key == pygame.K_d:
                snakeHead[0] += 10
            if event.type == pygame.K_LEFT or event.key == pygame.K_a:
                snakeHead[0] -= 10
            if event.type == pygame.K_UP or event.type == pygame.K_w:
                snakeHead[1] += 10
            if event.type == pygame.K_DOWN or event.type == pygame.K_s:
                snakeHead[1] -= 10

    def displaySnake(snakePosition):
        for position in snakePosition:
            pygame.draw.rect(windowSurface   ,red,pygame.Rect(position[0],position[1],10,10))

    def display_apple(windowSurface, applePosition, apple):
        windowSurface.blit(apple ,(applePosition[0], applePosition[1]))

    snakePosition.insert(0,list(snakeHead))
    snakePosition.pop()


    def displayFinalScore(displayText, finalScore):
        largeText = pygame.font.Font('freesansbold.ttf',35)
        TextSurf = largeText.render(displayText, True, (255, 255, 255))
        TextRect = TextSurf.get_rect()
        TextRect.center = ((windowWidth/2),(windowHeight/2))
        windowSurface.blit(TextSurf, TextRect)
        pygame.display.update()
        time.sleep(2)

    def collisionApple(applePosition, score):
        applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
        score += 1
        return applePosition, score

    if snakeHead == applePosition:
        applePosition, score = collisionApple(applePosition, score)
        snakePosition.insert(0,list(snakeHead))


    pygame.display.update()
    clock = pygame.time.Clock()
    clock.tick(20)

As mentioned in the comments, the objects don't appear because you never draw them by calling the displaySnake and display_apple functions. It also makes no sense to define these functions in the while loop again and again.

Here's a fixed version of the code. I've changed the movement code, so that the snake moves continually each frame. (It could still be improved, but I tried to keep it simple.)

import pygame, sys, random, time
from pygame.locals import *


pygame.init()
mainClock = pygame.time.Clock()
windowWidth = 500
windowHeight = 500
windowSurface = pygame.display.set_mode((windowWidth, windowHeight), 0, 32)
clock = pygame.time.Clock()
red = (255, 0, 0)
green = (0, 255, 0)
color = (100, 100, 100)
snakeHead = [250, 250]
snakePosition = [[250, 250],[240, 250],[230, 250]]
applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
apple = pygame.Surface((10, 10))
apple.fill(green)
score = 0
speed = 10
# Define a velocity variable (a list or better a pygame.Vector2).
velocity = pygame.Vector2(speed, 0)


def collisionBoundarias(snakeHead):
    return snakeHead[0] >= 500 or snakeHead[0] < 0 or snakeHead[1] >= 500 or snakeHead[1] < 0


def collisionApple(applePosition, score):
    applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
    score += 1
    return applePosition, score


def displaySnake(snakePosition):
    for position in snakePosition:
        pygame.draw.rect(windowSurface, red, pygame.Rect(position[0], position[1], 10, 10))


def display_apple(windowSurface, applePosition, apple):
    windowSurface.blit(apple, (applePosition[0], applePosition[1]))


while True:
    # Event handling.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            # Replace `type` with `key`.
            if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
                velocity.x = speed
                velocity.y = 0
            if event.key == pygame.K_LEFT or event.key == pygame.K_a:
                velocity.x = -speed
                velocity.y = 0
            if event.key == pygame.K_UP or event.key == pygame.K_w:
                velocity.x = 0
                velocity.y = -speed
            if event.key == pygame.K_DOWN or event.key == pygame.K_s:
                velocity.x = 0
                velocity.y = speed

    # Game logic.
    snakeHead[0] += velocity.x  # Update the x-position every frame.
    snakeHead[1] += velocity.y  # Update the y-position every frame.
    snakePosition.insert(0, snakeHead)
    snakePosition.pop()

    if snakeHead == applePosition:
        applePosition, score = collisionApple(applePosition, score)
        snakePosition.insert(0, snakeHead)

    # Drawing.
    windowSurface.fill(color)
    displaySnake(snakePosition)
    display_apple(windowSurface, applePosition, apple)
    pygame.display.update()
    clock.tick(20)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM