我有个问题。我有一个屏幕,已经充满了文本和按钮,我想按下按钮,填充我的背景颜色。(我知道关于这个话题有上百个问题,但我无法理解)。这是代码的一部分,我有按钮。我以为当我按下按钮时,很容易做出一个改变颜色的功能,但是当我按下按钮时,它会改变颜色,只要我按住鼠标键。当我不按住时,它只会返回一个带有文本和按钮的屏幕。在
def game_loop():
global display_text
global display_button_1
global display_button_2
gameExit = False
display_text = True
display_button_1 = True
display_button_2 = False
meie_font = pygame.font.SysFont("Arial", 36)
tekst = "This game will go as far as you choose!"
teksti_pilt1 = meie_font.render(tekst, False, (50,50,155))
tekst2 = "You are the smith of your destiny"
teksti_pilt = meie_font.render(tekst2, False, (50,50,155))
########################################################################
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
gameDisplay.fill(white)
########################################################################
if display_text:
gameDisplay.blit(teksti_pilt1, (100, 250))
gameDisplay.blit(teksti_pilt, (100, 400))
########################################################################
if display_button_1:
button("Start playing", 300,500,150,50,green,bright_green, hide_text)
########################################################################
if display_button_2:
#gameDisplay.fill(white)
bg = pygame.image.load("natsalo.jpg")
gameDisplay.blit(bg, (0, 0))
tekst = "You, just have a friendly dinner with your family!"
meie_font = pygame.font.SysFont("Arial", 36)
teksti_pilt = meie_font.render(tekst, False, (25,25,155))
gameDisplay.blit(teksti_pilt, (100, 30))
tekst = "It wasn't so usualy as always, it was always something...!"
meie_font = pygame.font.SysFont("Arial", 36)
teksti_pilt = meie_font.render(tekst, False, (25,25,155))
gameDisplay.blit(teksti_pilt, (50, 100))
tekst = "You are Lee Everett, you are a professor who taught history"
meie_font = pygame.font.SysFont("Arial", 36)
teksti_pilt = meie_font.render(tekst, False, (25,25,155))
gameDisplay.blit(teksti_pilt, (25, 170))
tekst = "For over six years at the University of Georgia"
meie_font = pygame.font.SysFont("Arial", 36)
teksti_pilt = meie_font.render(tekst, False, (25,25,155))
gameDisplay.blit(teksti_pilt, (100, 240))
tekst = "You, just have a friendly dinner with your family!"
meie_font = pygame.font.SysFont("Arial", 36)
teksti_pilt = meie_font.render(tekst, False, (25,25,155))
gameDisplay.blit(teksti_pilt, (100, 310))
button("I am to lazy to wash hands, just sit", 100,500,600,50,green,bright_green, hide_text)
button("Wash your hands", 100,400,600,50,green,bright_green, next_screen)
def next_screen():
gameDisplay.fill( (0,0,0) )
pygame.display.update()
这是完整的代码,如果需要的话。在
^{pr2}$
你必须重新组织代码。在
我用}。在
fill()
、文本、按钮和update()
创建{我使用带数字的变量
display_subscene
来决定显示哪个子序列。在你可以用类来更好地组织它。在
现在主要的问题是使用
mouse.get_pressed()
的按钮,所以它总是在不同的子场景中的同一个位置按下按钮。我不得不把按钮移到不同的地方。在您必须使用
event.type == MOUSEBUTTONDOWN
编辑在每个循环中,我都使用之前设置
event.type == MOUSEBUTTONDOWN
来设置变量mouse_button
,我在button()
中使用这个变量(作为新参数),这样当我按住按钮时,它就不会一直单击。变量mouse_button
必须在每个^{0
。在我为屏幕添加了半透明背景
Pause
-你可以按p
game_loop
来查看它。在相关问题 更多 >
编程相关推荐