sâmbătă, 1 octombrie 2011

pygame - test mouse class - update

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 is 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 show bellow.
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()

Niciun comentariu:

Trimiteți un comentariu