[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] I need some buttons~



thanx I'll try it~ : )

 theGREENzebra <tgz@orotech.net> wrote:

> I found no buttons in pygame, could we do it ourselves ?

Pygame isn't a GUI widget set, it's bindings to SDL. Here's a button class I
wrote and some code below it to call

#
# CLICKABLE BUTTON WITH AN IMAGE INSIDE IT
#
class Button:
def __init__(self,x,y,imagefile):
# load in the icon that will go in the middle of the button
self.insideimg = pygame.image.load(imagefile).convert_alpha()
self.insiderect = self.insideimg.get_rect()
self.ix = self.insiderect.size[0]
self.iy = self.insiderect.size[1]

# set up base values for the size of this button to the image inside it
self.image = pygame.surface.Surface((self.ix+8,self.iy+8))
self.rect = self.image.get_rect()
self.rect.left = x
self.rect.top = y

# set button as unpressed
self.popup()

# draw the button in an "unpressed" state
def popup(self):
self.image.fill((96,96,96))
pygame.draw.line(self.image, (224,224,224), (0,0),(self.ix+8,0) )
pygame.draw.line(self.image, (224,224,224), (0,0),(0,self.ix+8) )
pygame.draw.line(self.image, (224,224,224), (1,1),(self.ix+7,1) )
pygame.draw.line(self.image, (224,224,224), (1,1),(1,self.ix+7) )
pygame.draw.line(self.image, (224,224,224), (2,2),(self.ix+6,2) )
pygame.draw.line(self.image, (224,224,224), (2,2),(2,self.ix+6) )
pygame.draw.rect(self.image, (160,160,160), ((3,3),(self.ix+3,self.iy+3)) )
# put the button icon on the button
self.image.blit(self.insideimg,(4,4))

# draw the button in a "pressed" state
def press(self):
self.image.fill((224,224,224))
pygame.draw.line(self.image, (96,96,96), (0,0),(self.ix+8,0) )
pygame.draw.line(self.image, (96,96,96), (0,0),(0,self.ix+8) )
pygame.draw.line(self.image, (96,96,96), (1,1),(self.ix+7,1) )
pygame.draw.line(self.image, (96,96,96), (1,1),(1,self.ix+7) )
pygame.draw.line(self.image, (96,96,96), (2,2),(self.ix+6,2) )
pygame.draw.line(self.image, (96,96,96), (2,2),(2,self.ix+6) )
pygame.draw.rect(self.image, (160,160,160), ((3,3),(self.ix+3,self.iy+3)) )
# put the button icon on the button
self.image.blit(self.insideimg,(4,4))

# this gets called if the mouse is clicked - check if the button was hit
def clicked(self,pos):
if (self.rect.left < pos[0] < self.rect.right) and (self.rect.top <
pos[1] <
self.rect.bottom):
return 1

#
# BASIC USE
#

# create the button at 100,100 and put an image called go.png inside it
gobut = Button(100,100,"go.png")
# put the button on the screen
screen.blit(gobut.image,gobut.rect)
pygame.display.flip() #update display

while 1: # this would be like your main loop
k = pygame.event.poll() #look for an event
if k.type == MOUSEBUTTONDOWN: #if they click,
if k.button == 1: #if it's the left button,
if gobut.clicked(k.pos): #check if the button's limits are clicked
gobut.press() #depress the button
screen.blit(gobut.image,gobut.rect) #update the screen
print "they clicked the go button"
pygame.display.flip() #update display
do_whatever_else() #call your "go" function here

B
--
* tgz@orotech.net * http://clickass.org * ICQ# 1107012 * AIM/Yahoo:
theGREENzebra * http://mp3.com/thegreenzebra
-----GEEK CODE 3.12-----
GCM/CS/IT/MU d? s: a22 C++++ UL++++ P+ L+++>$ E---- W++ N+ o K- w---$ O- M
V-- PS+ PE Y+ PGP- t++(+++) 5- X R tv(+) b+(-) DI+++ D+ G++ e h++ r+(---) y+
UF*++

____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org



Do you Yahoo!?
HotJobs - Search new jobs daily now