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!