Advertisement
Guest User

Untitled

a guest
Aug 14th, 2017
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | None | 0 0
  1. import pygame, sys #import libraries
  2. mainClock = pygame.time.Clock() #setup clock for fps control
  3. from pygame.locals import * #more pygame setup
  4. pygame.init() #even more pygame setup
  5. screen = pygame.display.set_mode((100,100),0,32) #define your display
  6. Button_Img = pygame.image.load('button_img.png') #load button image
  7. Click = False #set the click state to false
  8. while True: #game loop
  9.     mx,my = pygame.mouse.get_pos() #get mouse position
  10.     MouseRect = pygame.Rect(mx,my,2,2) #set up mouse rect for collision
  11.     screen.fill((0,0,0)) #reset display by filling with black
  12.     ButtonRect = pygame.Rect(5,5,20,20) #set up the button rect for collision
  13.     if MouseRect.colliderect(ButtonRect): #test for mouse collision with button
  14.         if Click == True: #check if the user just click
  15.             print('Somebody clicked me! :D') #some sort of response to the click
  16.     screen.blit(Button_Img,(5,5)) #display the button image
  17.     Click = False #reset click value so the button response isn't spammed
  18.     for event in pygame.event.get(): #loop through current input events
  19.         if event.type == QUIT: #check if the user closed the window
  20.             pygame.quit() #close pygame
  21.             sys.exit() #close python process
  22.         if event.type == MOUSEBUTTONDOWN: #check for mouse activity
  23.             if event.button == 1: #check for left click
  24.                 Click = True #set click to true
  25.     pygame.display.update() #update display
  26.     mainClock.tick(40) #set the framerate to 40
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement