Advertisement
Guest User

Pygame Problem

a guest
Jul 22nd, 2011
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.15 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Expected environment:
  4. # C:\Python26
  5. # C:\Python26\include\pygame
  6.  
  7. import pygame
  8. pygame.init()
  9.  
  10.  
  11. class Button():
  12.  
  13.     color = "red"
  14.     text_color = pygame.Color("black")
  15.     font = pygame.font.Font(None,30)
  16.    
  17.     def __init__(self,background,topleft,rel_text,pres_text):
  18.         self.background = background
  19.         self.topleft = topleft
  20.         self.rel_text = rel_text
  21.         self.pres_text = pres_text
  22.         self.__draw(rel_text)
  23.  
  24.     def pressed(self):
  25.         """Changes color to represent button being pressed"""
  26.         self.color = "blue"
  27.         self.__draw("Pause")
  28.  
  29.     def released(self):
  30.         """Changes back to the origonal color once relaesed"""
  31.         self.color = "red"
  32.         self.__draw("Bla")
  33.    
  34.  
  35.     def __draw(self,text):
  36.          self.shape = pygame.draw.rect(
  37.              self.background,               # drawing surface
  38.              pygame.Color(self.color),    # line colour
  39.              self.topleft)
  40.          
  41. #        self.text = self.font.render(text,True,self.text_color)
  42.          
  43. #        self.background.blit(self.text,(80,100))
  44.  
  45.     def testClick(self,xy):
  46.         """Tests to see if the button has been clicked
  47.        - excpects x,y coordinates such as mouse position"""
  48.         x,y = xy
  49.         test = pygame.Rect(x,y,1,1)
  50.         if self.shape.contains(test):
  51.             return True
  52.         else:
  53.             return False
  54.  
  55.        
  56.        
  57.  
  58. #=========:=========:=========:=========:=========:=========:=========:
  59. def main():
  60.     """
  61.    capture events and display a message for each
  62.    """
  63.     screen = pygame.display.set_mode((640, 480))
  64.     pygame.display.set_caption("GTunes")
  65.    
  66.     background = pygame.Surface(screen.get_size())
  67.     background = background.convert()
  68.     background.fill(pygame.color.Color("grey"))
  69.  
  70.     pause = Button(background,(50, 100, 80, 30),"Play","Pause")
  71.     play = Button(background,(150, 100, 80, 30),"","")
  72.  
  73.    
  74.  
  75.     color = pygame.Color("black")
  76.  
  77.     font = pygame.font.Font(None,30)
  78.     text = font.render("Bla",True,pygame.Color("black"))
  79.    
  80.    
  81.    
  82.     looping = True
  83.     while looping:
  84.         for event in pygame.event.get():
  85.             if event.type == pygame.QUIT:
  86.                 looping = False
  87.             elif event.type == pygame.KEYDOWN:
  88.                 keyName = pygame.key.name(event.key)
  89.                 print "key pressed:", keyName
  90.                 if event.key == pygame.K_ESCAPE:
  91.                     looping = False
  92.             elif event.type == pygame.MOUSEBUTTONDOWN:
  93.                 print "mouse down:", pygame.mouse.get_pos()
  94.                 if play.testClick(pygame.mouse.get_pos()):
  95.                     play.pressed()
  96.                     text = font.render("New",True,pygame.Color("black"))
  97.             elif event.type == pygame.MOUSEBUTTONUP:
  98.                 print "mouse up:", pygame.mouse.get_pos()
  99.                 play.released()
  100.  
  101.        
  102.        
  103.         screen.blit(background, (0, 0))
  104.         screen.blit(text,(80,100))
  105.         pygame.display.flip()
  106.    
  107. #=========:=========:=========:=========:=========:=========:=========:
  108. if __name__ == "__main__":
  109.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement