我正在尝试制作一个蛇游戏,我遇到的唯一问题是,当游戏结束屏幕出现并且您单击重新启动按钮时,它会像我想要的那样显示启动屏幕,但不会重置蛇的位置,这样你就会立即失败。在 gameOver 函数中,我试图将其设置为,当您单击重试按钮时,它将显示起始启动屏幕并将蛇放回到起始位置。
#Project Snake
#Bryce Jones
#Code by Gareth Halfacres, Raspberry Pi Guide Book
#Reference: https://scitechdaily.com/astronomers-reveal-a-breathtaking-new-composite-image-of-the-crab-nebula/
#Reference cont.: https://scitechdaily.com/astronomers-uncover-black-hole-closer-to-earth-than-ever-before/
import pygame, sys, time, random
from pygame.locals import *
import os
os.environ['SDL_VIDEO_WINDOW_POS']='{},{}'.format(600,150)
pygame.init()
pygame.mixer.init()
fpsClock = pygame.time.Clock()
display = pygame.display.set_mode((500, 250), pygame.SRCALPHA, 32)
yellow = pygame.Color(255,255,0)
red = pygame.Color(255,0,0)
green = pygame.Color(0,255,0)
blue = pygame.Color(0,0,255)
black = pygame.Color(0,0,0)
white = pygame.Color(255,255,255)
grey = pygame.Color(150,150,150)
score = 0
score_increment = 10
screen = pygame.display.set_mode((640,480))
snakePosition = [100,100]
snakeSegments = [[100,100],[80,100],[60,100]]
raspberry = pygame.image.load('00_berrysprites.png').convert_alpha(),pygame.image.load('01_berrysprites.png').convert_alpha(),pygame.image.load('02_berrysprites.png').convert_alpha(), pygame.image.load('03_berrysprites.png').convert_alpha(),pygame.image.load('04_berrysprites.png').convert_alpha(),pygame.image.load('05_berrysprites.png').convert_alpha(),pygame.image.load('06_berrysprites.png').convert_alpha(),pygame.image.load('07_berrysprites.png').convert_alpha()
raspberryPosition = [300,300]
raspberrySpawned = 1
direction = 'right'
changeDirection = direction
print('\n\n\t\tProject Snake Game by Bryce Jones\n')
print('\nStarting # Snake Segments = ',snakeSegments)
def splash_screen():
"""creates splash screen"""
done = False
pygame.display.set_caption('Slightly Solid Snake')
splash_background_image = pygame.image.load("capture.jpg").convert()
screen.blit(splash_background_image, [0, 0])
pygame.draw.rect(display, (0,255,0,0),(270,50,100,50))
font = pygame.font.Font(None, 25)
text = font.render ("Start Game", 1, (0,0,255))
display.blit(text, (273,70))
pygame.draw.rect(display, (255,255,255,255),(550,440,150,150))
font = pygame.font.Font(None, 25)
text = font.render ("Quit Game", 1, (0,0,0))
display.blit(text, (550,450))
splashScreenFont = pygame.font.Font('freesansbold.ttf',50)
splashScreenSurf = splashScreenFont.render('...Slightly Solid Snake...',True,green)
splashScreenRect = splashScreenSurf.get_rect()
splashScreenRect.midtop = (330,200)
screen.blit(splashScreenSurf, splashScreenRect)
pygame.display.flip()
while not done:
for event in pygame.event.get():
if event.type == pygame.quit:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
mouseX = event.pos[0]
mouseY = event.pos[1]
print(mouseX,mouseY)
if (mouseX >= 270) and (mouseX < 270 + 100) and (mouseY >= 50) and (mouseY < 50 + 50):
print('Game Start')
done = True
elif (mouseX >= 550) and (mouseX < 550 + 150) and (mouseY >= 440) and (mouseY < 440+150):
exit()
else:
print('Click Start')
splash_screen()
def gameOver():
"""creates splash screen"""
done = False
pygame.display.set_caption('You Lose')
splash_background_image = pygame.image.load("black_hole.jpg").convert()
screen.blit(splash_background_image, [0, 0])
pygame.draw.rect(display, (255,0,0,0),(270,50,100,50))
font = pygame.font.Font(None, 25)
text = font.render ("Retry?", 1, (0,0,255))
display.blit(text, (290,70))
pygame.draw.rect(display, (255,255,255,255),(550,440,150,150))
font = pygame.font.Font(None, 25)
text = font.render ("Quit Game", 1, (0,0,0))
display.blit(text, (550,450))
splashScreenFont = pygame.font.Font('freesansbold.ttf',50)
splashScreenSurf = splashScreenFont.render('...Game Over...',True,red)
splashScreenRect = splashScreenSurf.get_rect()
splashScreenRect.midtop = (330,200)
screen.blit(splashScreenSurf, splashScreenRect)
pygame.display.flip()
print('Game Over')
while not done:
for event in pygame.event.get():
if event.type == pygame.quit:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
mouseX = event.pos[0]
mouseY = event.pos[1]
print(mouseX,mouseY)
if (mouseX >= 270) and (mouseX < 270 + 100) and (mouseY >= 50) and (mouseY < 50 + 50):
print('Game Restart')
done = True
snakePosition = [100,100]
snakeSegments = [[100,100],[80,100],[60,100]]
splash_screen()
elif (mouseX >= 550) and (mouseX < 550 + 150) and (mouseY >= 440) and (mouseY < 440+150):
exit()
else:
print('Click Retry')
done = False
while not done:
pygame.display.set_caption('Slightly Solid Snake')
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_RIGHT or event.key == ord('d'):
changeDirection = 'right'
if event.key == K_LEFT or event.key == ord('a'):
changeDirection = 'left'
if event.key == K_UP or event.key == ord('w'):
changeDirection = 'up'
if event.key == K_DOWN or event.key == ord('s'):
changeDirection = 'down'
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
if changeDirection == 'right' and not direction == 'left':
direction = changeDirection
if changeDirection == 'left' and not direction == 'right':
direction = changeDirection
if changeDirection == 'up' and not direction == 'down':
direction = changeDirection
if changeDirection == 'down' and not direction == 'up':
direction = changeDirection
if direction == 'right':
snakePosition[0] +=20
if direction == 'left':
snakePosition[0] -=20
if direction == 'up':
snakePosition[1] -=20
if direction == 'down':
snakePosition[1] +=20
snakeSegments.insert(0,list(snakePosition))
if snakePosition[0] == raspberryPosition[0] and snakePosition[1] == raspberryPosition[1]:
raspberrySpawned = 0
pygame.mixer.music.load('gworp.mp3')
pygame.mixer.music.play(0)
score += score_increment
print (score)
print('Updated # Segments: ',snakeSegments)
pygame.display.flip()
else:
snakeSegments.pop()
if raspberrySpawned == 0:
x = random.randrange(1,32)
y = random.randrange(1,24)
raspberryPosition = [x*20,y*20]
raspberrySpawned = 1
game_background_image = pygame.image.load('crab_nebula.jpg').convert()
screen.blit(game_background_image, [0,0])
font = pygame.font.Font(None, 25)
text = font.render(f'Score: {score}', True, (255,255,255))
display.blit(text, (273,50))
pygame.display.flip()
for position in snakeSegments:
pygame.draw.rect(screen,yellow,Rect(position[0],position[1],20,20))
#snake = pygame.image.load('snake.png').convert_alpha()
#screen.blit(snake, (snakePosition[0], snakePosition[1],20,20))
#pygame.display.flip
for rasp in raspberry:
screen.blit(rasp,(raspberryPosition[0], raspberryPosition[1],20,20))
pygame.display.flip()
time.sleep(0.1)
if snakePosition[0] > 620 or snakePosition[0] < 0:
pygame.mixer.music.load('dazed_sound.mp3')
pygame.mixer.music.play(0)
gameOver()
if snakePosition[1] > 460 or snakePosition[1] < 0:
pygame.mixer.music.load('dazed_sound.mp3')
pygame.mixer.music.play(0)
gameOver()
for snakeBody in snakeSegments[1:]:
if snakePosition[0] == snakeBody[0] and snakePosition[1] == snakeBody[1]:
pygame.mixer.music.load('dazed_sound.mp3')
pygame.mixer.music.play(0)
gameOver()
fpsClock.tick(3)
pygame.quit()
您为局部变量分配新值。在函数中,您必须使用
global snakePosition
来通知函数您要为全局/外部变量赋值。或者将所有值保留在外部列表或字典中。
def gameOver():
global snakePosition
global snakeSegments
# ... rest ...
或者也许在主循环中设置这些值 - 运行后
gameOver()