Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # Expected environment:
- # C:\Python26
- # C:\Python26\include\pygame
- import pygame
- pygame.init()
- class Button():
- color = "red"
- text_color = pygame.Color("black")
- font = pygame.font.Font(None,30)
- def __init__(self,background,topleft,rel_text,pres_text):
- self.background = background
- self.topleft = topleft
- self.rel_text = rel_text
- self.pres_text = pres_text
- self.__draw(rel_text)
- def pressed(self):
- """Changes color to represent button being pressed"""
- self.color = "blue"
- self.__draw("Pause")
- def released(self):
- """Changes back to the origonal color once relaesed"""
- self.color = "red"
- self.__draw("Bla")
- def __draw(self,text):
- self.shape = pygame.draw.rect(
- self.background, # drawing surface
- pygame.Color(self.color), # line colour
- self.topleft)
- # self.text = self.font.render(text,True,self.text_color)
- # self.background.blit(self.text,(80,100))
- def testClick(self,xy):
- """Tests to see if the button has been clicked
- - excpects x,y coordinates such as mouse position"""
- x,y = xy
- test = pygame.Rect(x,y,1,1)
- if self.shape.contains(test):
- return True
- else:
- return False
- #=========:=========:=========:=========:=========:=========:=========:
- def main():
- """
- capture events and display a message for each
- """
- screen = pygame.display.set_mode((640, 480))
- pygame.display.set_caption("GTunes")
- background = pygame.Surface(screen.get_size())
- background = background.convert()
- background.fill(pygame.color.Color("grey"))
- pause = Button(background,(50, 100, 80, 30),"Play","Pause")
- play = Button(background,(150, 100, 80, 30),"","")
- color = pygame.Color("black")
- font = pygame.font.Font(None,30)
- text = font.render("Bla",True,pygame.Color("black"))
- looping = True
- while looping:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- looping = False
- elif event.type == pygame.KEYDOWN:
- keyName = pygame.key.name(event.key)
- print "key pressed:", keyName
- if event.key == pygame.K_ESCAPE:
- looping = False
- elif event.type == pygame.MOUSEBUTTONDOWN:
- print "mouse down:", pygame.mouse.get_pos()
- if play.testClick(pygame.mouse.get_pos()):
- play.pressed()
- text = font.render("New",True,pygame.Color("black"))
- elif event.type == pygame.MOUSEBUTTONUP:
- print "mouse up:", pygame.mouse.get_pos()
- play.released()
- screen.blit(background, (0, 0))
- screen.blit(text,(80,100))
- pygame.display.flip()
- #=========:=========:=========:=========:=========:=========:=========:
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement