Pages

Se afișează postările cu eticheta class. Afișați toate postările
Se afișează postările cu eticheta class. Afișați toate postările

sâmbătă, 1 octombrie 2011

PyGame : Test mouse class.

Today I will show how to test mouse with pygame.
This is very simple, just you try to use MOUSEBUTTONDOWN.
Also, the click mouse only registers once so you can use it with another event.
If you don't want to use an event handler, you can check for input with:
pygame.mouse.get_pos() 
pygame.mouse.get_pressed()
I created a class called Game, which contains four functions.
A function that needs to pay attention is mainLoop.
This function deals with events in the following order.
While there is no event QUIT then read events.
If there is event QUIT or MOUSEBUTTONDOWN then is running this:
Event QUIT the program is quit.
Event MOUSEBUTTONDOWN print mouse events next use update function with print "update function".
Press mouse buttons: left,right,mouse wheel ,spinning mouse wheel.
(1, 0, 0)
update function
(0, 0, 1)
update function
(0, 1, 0)
update function
(0, 0, 0)
update function
The code is shown below.
import pygame
from pygame.locals import *

screen_mode = (640, 480)
color_black = 0, 0, 0

class Game:
 def __init__(self):
  pygame.init()
  self.screen = pygame.display.set_mode(screen_mode)
  pygame.display.set_caption("Pygame first window")
  self.quit = False
  
 def update(self):
  print "update function"
  return
  
 def draw(self):
  self.screen.fill(color_black)
  pygame.display.flip()
  
 def mainLoop(self):
  while not self.quit:
   for event in pygame.event.get():
    if event.type == pygame.QUIT:
     self.quit = True
    elif  event.type  == pygame.MOUSEBUTTONDOWN:
     print pygame.mouse.get_pressed()
     self.update()
   self.draw()
 
if __name__ == '__main__':
 game = Game()
 game.mainLoop()