我正在尝试用python编写一个游戏(它是一个学校作业),我希望游戏有多个屏幕,例如标题屏幕,关于屏幕,以及你可以玩游戏的屏幕。但是,为了简单起见,我希望所有这些屏幕都显示在同一个窗口中。
目前,我一直在定义运行特定屏幕的命令,并告诉python在变量正确时运行该命令(即当用户按下按钮将它们带到下一个屏幕时)。但是,我得到的只是一个黑屏,python立即关闭。有趣的是,日志显示没有任何问题的迹象,这很奇怪。
如果您可以帮我修复此代码,我们将不胜感激。
import pygame
import random
name = 'Squash Ninja'
size = (700, 500)
rect_x = 50
rect_y = 50
rect_x1 = 345
rect_y1 = 397
rect_x_change = 10
rect_y_change = 10
rect_x_change1 = -10
rect_y_change1 = -10
vari = 0
#Colours#
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREEN = (18, 128, 13)
YELLOW = (234, 255, 0)
ORANGE = (255, 132, 0)
LGREEN = (0,255,0)
PURPLE = (204, 14, 166)
BLACK = (0, 0, 0)
#Starts Pygame#
pygame.init()
#Opens window#
size = (700, 500)
screen = pygame.display.set_mode(size)
#Running Variables#
done = False
title = True
about = False
#Screens#
def title():
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
print('User pressed mouse.')
elif event.type == pygame.KEYDOWN:
print("User pressed a key")
pygame.draw.line(screen, BLUE, [0,0], [100,100], 5)
elif event.type == pygame.KEYUP:
print("User let go of a key.")
elif event.type == pygame.QUIT:
done = True
screen.fill(BLUE)
pygame.draw.rect(screen,BLACK,[200,20,350,100],0)
pygame.draw.rect(screen,GREEN,[280,180,200,50],0)
pygame.draw.rect(screen,GREEN,[280,240,200,50],0)
pygame.draw.rect(screen,GREEN,[280,300,200,50],0)
font = pygame.font.SysFont('Calibri', 45, True, False)
text = font.render("Squash Ninja", True, LGREEN)
screen.blit(text, [255, 75])
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Play", True, YELLOW)
screen.blit(text, [350, 200])
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("About", True, YELLOW)
screen.blit(text, [340, 260])
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Quit", True, YELLOW)
screen.blit(text, [355, 320])
font = pygame.font.SysFont('Calibri', 10, True, False)
text = font.render("Version 1.2", True,WHITE)
screen.blit(text, [650, 470])
font = pygame.font.SysFont('Calibri', 10, True, False)
text = font.render("Created by Adrian Ngai", True,WHITE)
screen.blit(text, [600, 480])
font = pygame.font.SysFont('Calibri', 10, True, False)
text = font.render("Copyright 2014, all rights reserved.", True,WHITE)
screen.blit(text, [555, 490])
mouse = pygame.mouse.get_pos ()
if 280 + 200 > mouse [0] > 280 and 180+50 > mouse[1] > 180:
pygame.draw.rect(screen,LGREEN,[280,180,200,50],0)
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Play", True, RED)
screen.blit(text, [350, 200])
if 280 + 200 > mouse [0] > 280 and 240+50 > mouse[1] > 240:
pygame.draw.rect(screen,LGREEN,[280,240,200,50],0)
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("About", True, RED)
screen.blit(text, [340, 260])
if event.type == pygame.MOUSEBUTTONDOWN:
title = False
about = True
if 280 + 200 > mouse [0] > 280 and 300+50 > mouse[1] > 300:
pygame.draw.rect(screen,LGREEN,[280,300,200,50],0)
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Quit", True, RED)
screen.blit(text, [355, 320])
if event.type == pygame.MOUSEBUTTONDOWN:
done = True
#bouncing rectangle#
pygame.draw.rect(screen, LGREEN, [rect_x, rect_y, 50, 50])
rect_x += rect_x_change
rect_y += rect_y_change
if rect_y > 450 or rect_y < 0:
rect_y_change = rect_y_change * -1
if rect_x > 650 or rect_x < 0:
rect_x_change = rect_x_change * -1
pygame.draw.rect(screen, PURPLE, [rect_x1, rect_y1, 50, 50])
rect_x1 += rect_x_change1
rect_y1 += rect_y_change1
if rect_y1 > 450 or rect_y1 < 0:
rect_y_change1 = rect_y_change1 * -1
if rect_x1 > 650 or rect_x1 < 0:
rect_x_change1 = rect_x_change1 * -1
for i in range(50):
x = random.randrange(0, 800)
y = random.randrange(0, 800)
pygame.draw.circle(screen,WHITE,[x,y],3)
pygame.display.flip()
clock.tick(20)
def about() :
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
print('User pressed mouse.')
elif event.type == pygame.KEYDOWN:
print("User pressed a key")
pygame.draw.line(screen, BLUE, [0,0], [100,100], 5)
if event.type == pygame.QUIT:
done = True
screen.fill(BLUE)
pygame.draw.rect(screen,BLACK,[200,20,350,100],0)
font = pygame.font.SysFont('Calibri', 45, True, False)
text = font.render("About", True, LGREEN)
screen.blit(text, [255, 75])
pygame.draw.rect(screen,GREEN,[280,300,200,50],0)
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Back", True, YELLOW)
screen.blit(text, [355, 320])
mouse = pygame.mouse.get_pos ()
if 280 + 200 > mouse [0] > 280 and 300+50 > mouse[1] > 300:
pygame.draw.rect(screen,LGREEN,[280,300,200,50],0)
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Back", True, RED)
screen.blit(text, [355, 320])
if event.type == pygame.MOUSEBUTTONDOWN:
title = True
about = False
pygame.display.flip()
clock.tick(20)
def done():
while not done:
print()
while done:
pygame.quit()
#Game Loop#
while vari == 0:
done()
while title:
title()
while about:
about()
#Closing Sequence#
print('Test fail :(')
pygame.quit()
答案 0 :(得分:0)
我认为您的问题可能就在这里
done = False
...
def done():
while not done:
print()
while done:
pygame.quit()
即使你有变量&#34;已完成&#34;在之前分配,您将其重新分配给某个功能。因为已完成不再为假,所以该函数执行第二个循环(&#34;完成时:&#34;)并立即退出。
要获得您期望的行为,您应该将函数done()重命名为is_done()。即。
def is_done():
while not done:
print()
while done:
pygame.quit()