如何在Pygame中单击并转到另一个页面?

2024-09-24 22:32:00 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我为一个学校作业写了这个代码,我应该用pygame来显示一些文本。我把这些文本放在不同的页面中,如果单击屏幕,它将显示下一个屏幕。你知道吗

到目前为止,我掌握的情况如下:

import pygame

pygame.init()
pygame.font.init()
# defining the screen
SIZE = (1000, 700)
screen = pygame.display.set_mode(SIZE)
# define time
clock = pygame.time.Clock()
#define button
button = 0
# define font
fontIntro = pygame.font.SysFont("Times New Roman",30)
# define draw scene
def drawIntro(screen):
    #start
    if button > 0:
        screen.fill((0, 0, 0))
        text = fontIntro.render("Sigle click to start", 1, (255,255,255))
        screen.blit(text, (300, 300, 500, 500))
        pygame.display.flip() 

    #page1
    if button == 1:
        screen.fill((0, 0, 0))
        text = fontIntro.render("page 1", True, (255, 255, 255))
        pygame.display.flip()
    #page2
    if button == 1:
        screen.fill((0, 0, 0))
        text = fontIntro.render("page 2", True, (255, 255, 255))
        screen.blit(text, (300,220,500,200))
        pygame.display.flip()
    #page3
    if button == 1:
        screen.fill((0, 0, 0))
        text = fontIntro.render("page3", True, (255, 255, 255))
        screen.blit(text, (200,190,500,200))
        pygame.display.flip()


running = True
while running:
    for evnt in pygame.event.get():
        if evnt.type == pygame.QUIT:
            running = False
        if evnt.type == pygame.MOUSEMOTION:
            mx,my = evnt.pos
            print(evnt.pos)
            drawIntro(screen)

pygame.quit()

虽然不起作用,有人能帮忙吗?!谢谢!你知道吗


Tags: texttrueifdisplaybuttonrenderfillscreen
2条回答

当用户按下鼠标按钮时,需要增加button计数器。你知道吗

不应在每个pygame.MOUSEMOTION事件的事件循环中调用drawIntro函数,而应在主while循环中调用。另外,将MOUSEMOTION更改为MOUSEBUTTONDOWN,以便每次单击时增加button。你知道吗

drawIntro函数中的条件不正确。你知道吗

import pygame


pygame.init()
SIZE = (1000, 700)
screen = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()
button = 0
fontIntro = pygame.font.SysFont("Times New Roman",30)


def drawIntro(screen):
    #start
    if button == 0:  # == 0
        screen.fill((0, 0, 0))
        text = fontIntro.render("Sigle click to start", 1, (255,255,255))
        screen.blit(text, (300, 300, 500, 500))
    elif button == 1:  #page1
        screen.fill((0, 0, 0))
        text = fontIntro.render("page 1", True, (255, 255, 255))
        screen.blit(text, (300,220,500,200))
    elif button == 2:  #page2
        screen.fill((0, 0, 0))
        text = fontIntro.render("page 2", True, (255, 255, 255))
        screen.blit(text, (300,220,500,200))
    elif button == 3:  #page3
        screen.fill((0, 0, 0))
        text = fontIntro.render("page3", True, (255, 255, 255))
        screen.blit(text, (200,190,500,200))


running = True
while running:
    for evnt in pygame.event.get():
        if evnt.type == pygame.QUIT:
            running = False
        if evnt.type == pygame.MOUSEBUTTONDOWN:  # Once per click.
            button += 1

    drawIntro(screen)
    pygame.display.flip()

pygame.quit()

您在开始时定义button = 0,但在主循环中从不更改其值。在drawIntro函数中,检查if button > 0if button == 1 所以很明显你从来没有执行过那些if语句。你知道吗

您需要通过调用pygame.mouse.get_pressed()来捕捉鼠标按钮,并了解如何正确地切换到下一页。你知道吗

顺便说一下,您还有if button == 1三次,我猜这不是您想要的,因为if语句在编写时会立即执行,因此您的第3页将立即显示。您需要一些计数器来跟踪下一次按下鼠标按钮时需要显示的页面。你知道吗

相关问题 更多 >