蛇游戏:蛇和苹果不会出现在屏幕上

2024-04-26 21:35:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用Python做蛇游戏,可惜我的蛇和苹果没有出现在屏幕上,我看到的只是一个灰色的屏幕,上面什么都没有,你能帮我吗?谢谢! 另外,我想解释一下为什么我现在的代码不起作用,这样我以后就可以避免了,再次感谢。你知道吗

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)

Tags: oreventreturniftimedeftypedisplay
1条回答
网友
1楼 · 发布于 2024-04-26 21:35:43

如注释中所述,对象不会出现,因为您从不通过调用displaySnakedisplay_apple函数来绘制它们。在while循环中反复定义这些函数也是没有意义的。你知道吗

下面是代码的固定版本。我已经修改了移动代码,所以蛇在每一帧都会不停地移动。(它仍然可以改进,但我尽量保持简单。)

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)

相关问题 更多 >