python - pygame 显示没有改变

标签 python 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)```

最佳答案

我浏览了你的代码,但一次都没有看到更新。

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

pygame.display.update()

pygame.display.flip()

输出内容时,如果输出一个图像,然后输出第二个图像,请将它们视为图层。第一张图片不会显示。 (前提是它们大小相同,但你知道我的意思。)

关于python - pygame 显示没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64994899/

相关文章:

python - Sklearn StandardScaler + ElasticNet 具有可解释系数

python - 我无法减少从天花板上落下的雨滴数量

python - 我的 python 角色一次只移动 1 个像素

python - Python如何处理one-hot编码数据?

python - 如何使用 pyPortMidi 或 pygame 发送 midi 控制更改消息 (CC)?

python - 从 pygame 中的二维列表渲染背景框类时出现问题

python - 如何计算 pygame.Surface 的屏幕中间?

Python 调用记录。获取基类名称

python - 解决 PyCharm python 错误 - dyld : Library not loaded

python - 如何在 Python 中使用 cv2 显示图像