pygame显示不变



我正试图在pygame中编写我的第一款游戏,并成功制作了一个标题屏幕,但找不到让"播放"按钮带用户进入实际游戏的方法。我有一个专用于标题屏幕的功能,当用户点击播放按钮时,它会停止标题屏幕循环并开始游戏循环,尽管游戏循环代码不起作用。标题屏幕冻结,游戏没有开始。我也从来没有使用过堆栈溢出,所以我会把我的代码粘贴在这里,我想:

import sys
import random
pygame.init()
# title
game_title = 'GAME-TITLE'
# set display
win = pygame.display.set_mode((750, 500))
pygame.display.set_caption(game_title)
# load images
cloud = pygame.image.load('999-cloud-clipart-free-download-transparent-png-cloud-clipart-cloud-clipart-transparent-1044_592.png')
cloud = pygame.transform.scale(cloud, (128, 72))
# clock
clock = pygame.time.Clock()
# font
pygame.font.init() 
font = pygame.font.SysFont('verdanaboldttf', 60)
font_2 = pygame.font.SysFont('timesnewromanttf', 30)
# colors
red = (255, 0, 0)
blue = (0, 0, 255)
green = (0, 255, 0)
white = (255, 255, 255)
light_blue = (173, 216, 230)
blue = (48, 131, 159)
navy = (0, 0, 200)
black = (0, 0, 0)
# clouds
cloud_values = []
i = 0
while i < 10:
cloud_values.append([random.randint(-750, -80), random.randint(-50, 550)])
i += 1
def title_screen():
run_title = True
run = True
show_help = False
play_game = False

while run_title:

clock.tick(10)

pygame.draw.rect(win, light_blue, pygame.Rect(-100, -100, 1000, 1000))
play_button = pygame.draw.rect(win, blue, pygame.Rect(150, 175, 450, 75))
help_button = pygame.draw.rect(win, blue, pygame.Rect(150, 275, 450, 75))
quit_button = pygame.draw.rect(win, blue, pygame.Rect(150, 375, 450, 75))
text = font_2.render('PLAY', True, white)
text_2 = font_2.render('HELP', True, white)
text_3 = font_2.render('QUIT', True, white)
title = font.render(game_title, True, navy)
win.blit(text, (340, 197))
win.blit(text_2, (340, 297))
win.blit(text_3, (340, 397))
win.blit(title, (165, 60))

for i in range(len(cloud_values)):
win.blit(cloud, (cloud_values[i][0], cloud_values[i][1]))
cloud_values[i][0] += 10
if cloud_values[i][0] > 760:
cloud_values[i][0] = random.randint(-750, -80)

keys = pygame.key.get_pressed()

if keys[pygame.K_ESCAPE]:
run = False

pos = pygame.mouse.get_pos()

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
if pos[0] > 150 and pos[0] < 600 and pos[1] > 175 and pos[1] < 250:
play_game = True
elif pos[0] > 150 and pos[0] < 600 and pos[1] > 275 and pos[1] < 375:
show_help = True
elif pos[0] > 150 and pos[0] < 600 and pos[1] > 375 and pos[1] < 450:
run = False
if pos[0] > 150 and pos[0] < 600 and pos[1] > 175 and pos[1] < 250:
pygame.draw.rect(win, blue, pygame.Rect(145, 170, 460, 85))
win.blit(text, (340, 197))
elif pos[0] > 150 and pos[0] < 600 and pos[1] > 275 and pos[1] < 375:
pygame.draw.rect(win, blue, pygame.Rect(145, 270, 460, 85))
win.blit(text_2, (340, 297))
elif pos[0] > 150 and pos[0] < 600 and pos[1] > 375 and pos[1] < 450:
pygame.draw.rect(win, blue, pygame.Rect(145, 370, 460, 85))
win.blit(text_3, (340, 397))

if play_game or show_help or not run:
run_title = False

pygame.display.flip()

return run_title, play_game, run, show_help
def game_play():
run_game = True
run = True
x = 10
while run_game:

# set new background
pygame.draw.rect(win, light_blue, pygame.Rect(-100, -100, 1000, 1000))

# run gameplay here

return run
def show_help_screen():
show_help = True
while show_help:
pygame.draw.rect(win, light_blue, pygame.Rect(-100, -100, 1000, 1000))

# show help_screen
def show_results_screen():
run = False
show_results = True
while show_results:
pygame.draw.rect(win, light_blue, pygame.Rect(-100, -100, 1000, 1000))

# show results

return run
def run_game(run_title, play_game, run, show_help):

run = True
while run:

if play_game:
game_play()
show_results = True

elif show_help:
show_help_screen()
run_title = True

elif show_results:
run = show_results_screen()
pygame.quit()
sys.exit()
run_title, play_game, run, show_help = title_screen()
run_game(run_title, play_game, run, show_help)```

我浏览了您的代码,一次也没有看到更新。

既然这是你的第一次,这里有一个快速的教训:当你的窗口上有任何移动、任何更改、任何正在更改的内容时,除非你更新它,否则它不会显示,使用:

pygame.display.update()

pygame.display.flip()

输出东西时,如果输出一个图像,然后输出第二个图像,请将它们视为层。第一个图像不会显示。(如果它们大小相同,但你知道我的意思。(

最新更新