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 need 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 button is hovering:
draw - will deal with drawing the button ,color and mouse...;
check_hover - return boolean value based on mouse over 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 result of the pygame script.

luni, 2 septembrie 2013

PyGame - first interface - part 5

Today I worked with pygame and OpenGL python modules.

I want to make a simple script to test and load pygame , OpenGL modules.

This is the script and give me also in vars some opengl features.

If modules and features can be loaded then return True or False in some vars.

import sys

try:
    import OpenGL
    from OpenGL.GL import *
    bOpenGL=True
except:
    bOpenGL=False
   
try:
    from OpenGL.GLU import *
    bGLU=True
except:
    bGLU=False

try:
    from OpenGL.GLUT import *
    bGLUT=True
except:
    bGLUT=False

try:
    from OpenGL.GL.EXT import *
    bEXT=True  
except:
    bEXT=False

try:
    from OpenGL.GL.ARB.shader_objects import *
    bshader_objects=True
except:
    bshader_objects=False
try:
    from OpenGL.GL.ARB.vertex_shader import *
    bvertex_shader=True
except:
    bvertex_shader=False
try:
    from OpenGL.GL.ARB.fragment_shader import *
    bfragment_shader=True
except:
    bfragment_shader=False
try:    
    from OpenGL.GL.ARB.multitexture import *
    bmultitexture=True
except:
    bmultitexture=False

if sys.version_info.major < 3:
    print "Python vers=",sys.version_info
    print "bOpenGL=", bOpenGL
    print "OpenGL vers=",OpenGL.__version__
    print "bGLU=", bGLU
    print "bGLUT=", bGLUT
    print "bEXT=", bEXT
    print "bshader_objects=", bshader_objects
    print "bvertex_shader=", bvertex_shader
    print "bfragment_shader=", bfragment_shader
    print "bmultitexture=", bmultitexture
else:
    print ("Python vers=",sys.version_info)
    print ("bOpenGL=", bOpenGL)
    print ("OpenGL vers=",OpenGL.__version__)
    print ("bGLU=", bGLU)
    print ("bGLUT=", bGLUT)
    print ("bEXT=", bEXT)
    print ("bshader_objects=", bshader_objects)
    print ("bvertex_shader=", bvertex_shader)
    print ("bfragment_shader=", bfragment_shader)
    print ("bmultitexture=", bmultitexture)

The output result is this :

$ python test.py 
Python vers= sys.version_info(major=2, minor=7, micro=3, releaselevel='final', serial=0)
bOpenGL= True
OpenGL vers= 3.0.1
bGLU= True
bGLUT= True
bEXT= True
bshader_objects= True
bvertex_shader= True
bfragment_shader= True
bmultitexture= True