I want to create simple game in python with pygame. There are some buttons, but it doesnt respond sometimes. I want to have low frame rate because its simple game but also i want to have buttons waiting on click like always. is it posible?
Its like if i dont click in that 60/1000 miliseconds its not working, am I right? If I am then is there other posibility to run game at 60 fps and waiting for input like always?
using this to catch click:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if pos[0] > 500 and pos[0] < 600 and pos[1] > 620 and pos[1] < 680:
button_hod_clicked = True
using this in main loop:
clock.tick(FPS)
structure:
def check_buttons(pos):
button_hod_clicked = False
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if pos[0] > 500 and pos[0] < 600 and pos[1] > 620 and pos[1] < 680:
button_hod_clicked = True
return button_hod_clicked
def main():
run = True
striedanie = True
while run:
clock.tick(FPS)
pos = pygame.mouse.get_pos()
button_hod_clicked = check_buttons(pos)
if button_hod_clicked:
if striedanie == True:
player1_position, kolko_hodil = player_movement(player1_position)
striedanie = False
else:
player2_position, kolko_hodil = player_movement(player2_position)
striedanie = True
time.sleep(1)
Instead of getting the mouse position every frame, just get the mouse position if the event handler detects MOUSEBUTTONUP
and call the check_buttons
function.
def check_buttons(pos):
return pos[0] > 500 and pos[0] < 600 and pos[1] > 620 and pos[1] < 680
button_hod_clicked = False
def main():
global button_hod_clicked
...
...
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
button_hod_clicked = check_buttons(pos)
if button_hod_clicked:
...
Let me know if this works.
Note: time.sleep
pauses the whole program, so it freezes the window.