Pygame问题还是我的问题?制作Python Snake游戏时的即时游戏

发布于 2025-02-11 06:19:52 字数 5446 浏览 1 评论 0原文

我是Python的新手,我正在尝试使用Pygame模块制作一个简单的Python蛇游戏,但是每次我加载它时,它都会通过序列开始游戏并退出游戏。没有错误,只是这个问题。我需要帮助。提前致谢。

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)

‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎

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)

‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぇ气 2025-02-18 06:19:52

设置蛇时:

snake = []
snakeLength = 3
for i in range(0, snakeLength):
    snake.append((200, 200))

您将蛇头[0]和其余的蛇放在同一位置(200,200)上。然后,在第一次迭代中,它检查了蛇的头是否位于蛇身体的任何部分,并且是。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文