Pages

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

sâmbătă, 7 septembrie 2013

PyGame : First interface - part 6.

I will show you how to make a menu with buttons using pygame module.
The example is simple but you can update with new features.
I will make some buttons: Start game, Options and Exit game.
The pygame come with rect function to draw rectangles.
I start with one python class named my_button.
Like any graphic interface is needed to make visual elements and link with actions.
For each button, I draw one rectangle with text and functions to check mouse.
The functions of my_button are:
label - will add some text;
color - change color when the button is hovering:
draw - will deal with drawing the button, color, and mouse...;
check_hover - return boolean value based on mous eover my_button;
import pygame

pygame.init()

class my_button:
   def __init__(self, text):
      self.text = text
      self.is_hover = False
      self.default_color = (100,100,100)
      self.hover_color = (0,255,25)
      self.font_color = (0,0,255)
      self.obj = None
      
   def label(self):
      font = pygame.font.Font(None, 22)
      return font.render(self.text, 1, self.font_color)
      
   def color(self):
      if self.is_hover:
         return self.hover_color
      else:
         return self.default_color
         
   def draw(self, screen, mouse, rect_coord, label_coord):
      self.obj  = pygame.draw.rect(screen, self.color(), rect_coord)
      screen.blit(self.label(), label_coord)
      self.check_hover(mouse)
      
   def check_hover(self, mouse):
      if self.obj.collidepoint(mouse):
         self.is_hover = True 
      else:
         self.is_hover = False
         
if __name__ == '__main__':

         
   start = my_button('Start game')
   option = my_button('Option')
   exit = my_button('Exit game')
   
   screen = pygame.display.set_mode((400,350))
   clock = pygame.time.Clock()

   run = True
   while run:
      screen.fill((0,0,0))
      mouse = pygame.mouse.get_pos()
      for event in pygame.event.get():
         if event.type == pygame.QUIT:
            run = False
         elif event.type == pygame.MOUSEBUTTONDOWN:
            if my_button_.obj.collidepoint(mouse):
               print('my_button start clicked')
            elif my_button_2.obj.collidepoint(mouse):
               print('my_button option clicked')
            elif my_button_3.obj.collidepoint(mouse):
               print('my_button exit clicked')
      
      start.draw(screen, mouse, (100,100,120,22), (125,103))
      option.draw(screen, mouse, (100,130,120,22), (125,133))
      exit.draw(screen, mouse, (100,160,120,22), (125,163))
      
      pygame.display.update()
      clock.tick(50)
... and this is the result of the pygame script.

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()