Ваш код не работает, потому что вы звоните pygame.event.get()
несколько раз за кадр;один раз в START
и один раз в button
.
Подумайте о том, что происходит, когда в очереди происходит событие MOUSEBUTTONDOWN
, затем START
вызывается pygame.event.get()
: событие удаляется из очереди, а когда pygame.event.get()
вызывается в button
, событие MOUSEBUTTONDOWN
уже прошло и if (event.type == pygame.MOUSEBUTTONDOWN )
никогда не будет True
.
Таким образом, вместо вызова for event in pygame.event.get():
в START
, вы можете сохранить результат pygame.event.get()
впеременная и итерации по этому в START
и button
.Таким образом, вы не потеряете события.
Вот пример:
...
def button(events, normal, activated, x_pos, y_pos, length, height, func=None):
button = pygame.image.load(normal)
mousex, mousey = pygame.mouse.get_pos()
if (mousex>x_pos)and(mousex<(length+x_pos))and(mousey>y_pos)and(mousey<(y_pos+height)):
button = pygame.image.load(activated)
# iterate over the list if events instead of calling pygame.event.get
for event in events:
if (event.type == pygame.MOUSEBUTTONDOWN )and (event.button == 1):
if (mousex>x_pos)and(mousex<(mousex+x_pos))and(mousey>y_pos)and(mousey<(y_pos+height)):
func()
window.blit(button,(x_pos, y_pos))
...
def START():
global screen
while screen != "QUIT":
# store the list of events in a variable
events = pygame.event.get()
# iterate over the new list
for event in events:
if event.type == QUIT:
screen = "QUIT"
bg = pygame.image.load("START.png")
window.blit(bg,(0,0))
# pass the list of events to the button function
button(events, "play_u.png", "play_a.png", 350, 200, 325, 75, game)
pygame.display.update()