Pygame问题还是我的问题?制作Python Snake游戏时的即时游戏
我是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置蛇时:
您将蛇
头[0]
和其余的蛇放在同一位置(200,200)
上。然后,在第一次迭代中,它检查了蛇的头是否位于蛇身体的任何部分,并且是。When you set up the snake:
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.