pygame - Pygame.MOUSEBUTTONDOWN 的问题
问题描述
我是 python 和 Pygame(以及所有编码!)的初学者。我定义了一个交互式按钮
def button(msg, x, y, w, h, ic, ac, action=None):
mouse = pygame.mouse.get_pos()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac, (x, y, w, h))
smallText = pygame.font.SysFont("comicsans", 25)
textSurf = smallText.render(msg, True, black)
textRect = textSurf.get_rect()
textRect.center = ( (x + (w/2)), (y + ( h/2)))
gameDisplay.blit(textSurf, textRect)
if event.type == pygame.MOUSEBUTTONDOWN and action != None:
action()
else:
pygame.draw.rect(gameDisplay, ic, (x, y, w, h))
smallText = pygame.font.SysFont("comicsans", 25)
textSurf = smallText.render(msg, True, black)
textRect = textSurf.get_rect()
textRect.center = ( (x + (w/2)), (y + ( h/2)))
gameDisplay.blit(textSurf, textRect)
当我从介绍屏幕调用按钮时,一切正常
global gameDisplay
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay = pygame.display.set_mode((1060,800))
pygame.display.set_caption ("Maze")
gameDisplay.fill(white)
largeText = pygame.font.SysFont("comicsans", 115)
smallText = pygame.font.SysFont("comicsans", 50)
TextSurf = largeText.render("Monster Mazes", True, black)
TextRect = TextSurf.get_rect()
TextRect.center = ((1060/2), (800/3))
TextSurf1 = smallText.render("Collect the food & drink before boarding the spaceship", True, black)
TextRect1 = TextSurf1.get_rect()
TextRect1.center = ((1060/2), (800/3*2))
TextSurf2 = smallText.render("Don't hit the walls!", True, black)
TextRect2 = TextSurf2.get_rect()
TextRect2.center = ((1060/2), ((800/3*2)+100))
gameDisplay.blit(TextSurf, TextRect)
gameDisplay.blit(TextSurf1, TextRect1)
gameDisplay.blit(TextSurf2, TextRect2)
button("Go!", 200, 400, 100, 50, green, bright_green, set_maze)
button("Quit", 760, 400, 100, 50, red, bright_red, quitgame)
pygame.display.update()
clock.tick(15)
但是,当我从屏幕上的游戏中调用它时,它的行为就像总是按下鼠标按钮一样,所以只要光标悬停在按钮上,即使未单击鼠标按钮,也会调用该操作
def game_over():
global score
global lives
global width
global height
global maze
global gameDisplay
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(white)
bonus_score = lives*50
total_score = score + bonus_score
gameDisplay.fill(white)
largeText = pygame.font.SysFont("comicsans", 115)
smallText = pygame.font.SysFont("comicsans", 50)
TextSurf = largeText.render("Game Over!", True, black)
TextRect = TextSurf.get_rect()
TextRect.center = ((width/2), (height/3))
TextSurfScore = smallText.render("Score : " + str(score), True, black)
TextRectScore = TextSurfScore.get_rect()
TextRectScore.center = ((width/2), (height/3*2))
TextSurfScoreBonus = smallText.render("Bonus : " + str(bonus_score), True, black)
TextRectScoreBonus = TextSurfScoreBonus.get_rect()
TextRectScoreBonus.center = ((width/2), (height/3*2)+50)
TextSurfScoreTotal = smallText.render("Total : " + str(total_score), True, black)
TextRectScoreTotal = TextSurfScoreTotal.get_rect()
TextRectScoreTotal.center = ((width/2), (height/3*2)+100)
gameDisplay.blit(TextSurf, TextRect)
gameDisplay.blit(TextSurfScore, TextRectScore)
gameDisplay.blit(TextSurfScoreBonus, TextRectScoreBonus)
gameDisplay.blit(TextSurfScoreTotal, TextRectScoreTotal)
button("Play again", 200, 400, 100, 50, green, bright_green, play_again)
button("Quit", 760, 400, 100, 50, red, bright_red, quitgame)
pygame.display.update()
clock.tick(30)
我看不出我做错了什么/不同!我真的很感激任何帮助!
谢谢
解决方案
我找到了解决方案!游戏结束是一个功能,但介绍不是。我只需要参加全球活动就可以了。
推荐阅读
- html - 只滚动页面的一部分
- assembly - ARM 程序集打印大且不正确的值
- php - 如何在 laravel 中使用 postgresql 的 split_part?
- powershell - 将 AD 用户帐户密码和电子邮件凭据重置为用户
- algorithm - 依赖嵌套的运行时分析
- python - 如何在python中使用广播计算矩阵的成对余弦距离
- python - Django - 处理多个不同级别的嵌套模型
- batch-file - 通过具有任何特定模式的文件夹
- assembly - 我的 mips 代码中的错误是返回 X 的倍数,其中 X 是 1、2、4、8、16 等
- ldap - 连接尝试失败,因为连接方没有正确响应 python