Reputation: 75
I'm programming a small game with Pygame and I'm not sure how to get mouse clicks. I've gotten a bit of it figured out, but honestly I'm not having the best of times.
Here's what I have:
import pygame
pygame.init()
def menu():
title_font = pygame.font.SysFont("monospace",64)
button_font = pygame.font.SysFont("monospace", 30)
screen_size = (1280,950)
screen = pygame.display.set_mode(screen_size)
background = (255,255,255)
screen.fill(background)
play_button = pygame.Rect(250,600,200,100)
quit_button = pygame.Rect(850,600,200,100)
controls_button = pygame.Rect(550,600,200,100)
pygame.draw.rect(screen, (0,255,0), play_button)
pygame.draw.rect(screen, (255,0,0), quit_button)
pygame.draw.rect(screen, (255,229,0), controls_button)
title = title_font.render("Fightastic!", 1, (0,0,0))
screen.blit(title, (450,300))
play_text = button_font.render("START",1,(0,0,0))
screen.blit(play_text, (310,635))
quit_text = button_font.render("QUIT",1,(0,0,0))
screen.blit(quit_text, (910,635))
controls_text = button_font.render("CONTROLS",1,(0,0,0))
screen.blit(controls_text, (580,635))
buttons = [play_button, quit_button, controls_button]
while True:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
exit()
mouse_cursor = pygame.mouse.get_pos()
mouse_pressed = pygame.mouse.get_pressed()
option = 0
for i in range(len(buttons)):
if buttons[i].collidepoint( mouse_cursor):
option = i+1
if option == 1:
print ("YO I GOT CLICKED")
elif option == 2:
print ("CLICKED MY DUDE")
elif option == 3:
quit()
pygame.display.update()
menu()
The game's menu is the only part that will require clicks so that's all I've shown.
Thanks!
Upvotes: 1
Views: 564
Reputation: 142641
You already have pygame.mouse.get_pressed()
so use it to check which button was clicked.
option = 0
if mouse_pressed[0]: # check if left button was clicked
for i, but in enumerate(buttons, 1): # get button and its number
if but.collidepoint(mouse_cursor): # check collision
option = i # remember button number
break # no need to check other buttons
But using pygame.mouse.get_pressed()
makes one problem - it returns True
all the time you hold mouse button pressed (so it looks like many clicks in every second) so if you change button on screen and new button will be in the same place then it will automatically click in new button. Better use event.type == pygame.MOUSEBUTTONDOWN
which create only one click.
while True:
option = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
for i, but in enumerate(buttons, 1):
if but.collidepoint(event.pos):
option = i
break # no need to check other buttons
if option == 1:
print ("YO I GOT CLICKED")
elif option == 2:
print ("CLICKED MY DUDE")
elif option == 3:
quit()
pygame.display.update()
BTW:
You could keep buttons with callbacks (functions names without ()
)
buttons = [
(play_button, play_function),
(controls_button, control_function),
(quit_button, quit_function),
]
and then you would call function/callback directly (using ()
) without option
if event.type == pygame.MOUSEBUTTONDOWN:
for rect, callback in buttons:
if rect.collidepoint(event.pos):
callback() # execute function
break # no need to check other buttons
Full working code
import pygame
# --- constants --- (UPPER_CASE_NAMES)
SCREEN_WIDTH = 1280
SCREEN_HEIGHT = 950
WHITE = (255, 255, 255)
# --- functions --- (lower_case_names)
def play_function():
print("YO I GOT CLICKED")
def controls_function():
print("CLICKED MY DUDE")
def quit_function():
pygame.quit()
quit()
def menu(screen):
# - init -
title_font = pygame.font.SysFont("monospace", 64)
button_font = pygame.font.SysFont("monospace", 30)
# - objects -
play_button = pygame.Rect(250,600,200,100)
quit_button = pygame.Rect(850,600,200,100)
controls_button = pygame.Rect(550,600,200,100)
buttons = [
(play_button, play_function),
(controls_button, controls_function),
(quit_button, quit_function),
]
# - draws -
screen.fill(WHITE)
title = title_font.render("Fightastic!", 1, (0,0,0))
screen.blit(title, (450,300))
pygame.draw.rect(screen, (0,255,0), play_button)
play_text = button_font.render("START",1,(0,0,0))
screen.blit(play_text, (310,635))
pygame.draw.rect(screen, (255,0,0), quit_button)
quit_text = button_font.render("QUIT",1,(0,0,0))
screen.blit(quit_text, (910,635))
pygame.draw.rect(screen, (255,229,0), controls_button)
controls_text = button_font.render("CONTROLS",1,(0,0,0))
screen.blit(controls_text, (580,635))
pygame.display.update()
# - mainloop -
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
for rect, callback in buttons:
if rect.collidepoint(event.pos):
callback() # execute function
break # no need to check other buttons
clock.tick(5) # 5 FPS - to slow down game and use less CPU
# --- main ---
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
menu(screen)
Upvotes: 2
Reputation: 1261
Using mouse input is actually relatively easy to use and you can program powerful interactivity with little code.
I took the code that you gave and made some adjustments and fixes.
Indented the line pygame.display.update()
.
Implemented full mouse click detection.
Modified the way option
was used and the order of the list buttons
so that the buttons are in order from left to right.
The main addition is the mouse_pressed[0] == 1
part in the if statement. This takes the list of mouse buttons(left, right, middle) and an int 0 or 1 indicating if they are pressed or not. Hence the code checks if the left button is being pressed.
Here is the code:
import pygame
pygame.init()
def menu():
title_font = pygame.font.SysFont("monospace",64)
button_font = pygame.font.SysFont("monospace", 30)
screen_size = (1280,950)
screen = pygame.display.set_mode(screen_size)
background = (255,255,255)
screen.fill(background)
play_button = pygame.Rect(250,600,200,100)
quit_button = pygame.Rect(850,600,200,100)
controls_button = pygame.Rect(550,600,200,100)
pygame.draw.rect(screen, (0,255,0), play_button)
pygame.draw.rect(screen, (255,0,0), quit_button)
pygame.draw.rect(screen, (255,229,0), controls_button)
title = title_font.render("Fightastic!", 1, (0,0,0))
screen.blit(title, (450,300))
play_text = button_font.render("START",1,(0,0,0))
screen.blit(play_text, (310,635))
quit_text = button_font.render("QUIT",1,(0,0,0))
screen.blit(quit_text, (910,635))
controls_text = button_font.render("CONTROLS",1,(0,0,0))
screen.blit(controls_text, (580,635))
buttons = [play_button, controls_button, quit_button]
while True:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
exit()
mouse_cursor = pygame.mouse.get_pos()
mouse_pressed = pygame.mouse.get_pressed()
option = -1
for i in range(len(buttons)):
if buttons[i].collidepoint( mouse_cursor ) and mouse_pressed[0] == 1:
option = i
if option == 0:
print ("YO I GOT CLICKED")
elif option == 1:
print ("CLICKED MY DUDE")
elif option == 2:
quit()
pygame.display.update()
menu()
I hope this was helpful and if you have any further questions please feel free to post a comment below!
Upvotes: 1