所以我为一个学校作业写了这个代码,我应该用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()
虽然不起作用,有人能帮忙吗?!谢谢!你知道吗
当用户按下鼠标按钮时,需要增加
button
计数器。你知道吗不应在每个
pygame.MOUSEMOTION
事件的事件循环中调用drawIntro
函数,而应在主while
循环中调用。另外,将MOUSEMOTION
更改为MOUSEBUTTONDOWN
,以便每次单击时增加button
。你知道吗drawIntro
函数中的条件不正确。你知道吗您在开始时定义
button = 0
,但在主循环中从不更改其值。在drawIntro
函数中,检查if button > 0
或if button == 1
所以很明显你从来没有执行过那些if
语句。你知道吗您需要通过调用
pygame.mouse.get_pressed()
来捕捉鼠标按钮,并了解如何正确地切换到下一页。你知道吗顺便说一下,您还有
if button == 1
三次,我猜这不是您想要的,因为if
语句在编写时会立即执行,因此您的第3页将立即显示。您需要一些计数器来跟踪下一次按下鼠标按钮时需要显示的页面。你知道吗相关问题 更多 >
编程相关推荐