Я новичок в 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)
Я не вижу, что я сделал не так / по-другому !!! Буду очень признателен за любую помощь!
Спасибо