pygame实现非图片按钮效果
本文实例为大家分享了pygame实现非图片按钮效果的具体代码,供大家参考,具体内容如下
按钮类程序
#-*-coding=utf-8-*- importthreading importpygame frompygame.localsimportMOUSEBUTTONDOWN classBFControlId(object): _instance_lock=threading.Lock() def__init__(self): self.id=1 @classmethod definstance(cls,*args,**kwargs): ifnothasattr(BFControlId,"_instance"): BFControlId._instance=BFControlId(*args,**kwargs) returnBFControlId._instance defget_new_id(self): self.id+=1 returnself.id CLICK_EFFECT_TIME=100 classBFButton(object): def__init__(self,parent,rect,text='Button',click=None): self.x,self.y,self.width,self.height=rect self.bg_color=(225,225,225) self.parent=parent self.surface=parent.subsurface(rect) self.is_hover=False self.in_click=False self.click_loss_time=0 self.click_event_id=-1 self.ctl_id=BFControlId().instance().get_new_id() self._text=text self._click=click self._visible=True self.init_font() definit_font(self): font=pygame.font.Font(None,28) white=100,100,100 self.textImage=font.render(self._text,True,white) w,h=self.textImage.get_size() self._tx=(self.width-w)/2 self._ty=(self.height-h)/2 @property deftext(self): returnself._text @text.setter deftext(self,value): self._text=value self.init_font() @property defclick(self): returnself._click @click.setter defclick(self,value): self._click=value @property defvisible(self): returnself._visible @visible.setter defvisible(self,value): self._visible=value defupdate(self,event): ifself.in_clickandevent.type==self.click_event_id: ifself._click:self._click(self) self.click_event_id=-1 return x,y=pygame.mouse.get_pos() ifx>self.xandxself.yandy 主要给按钮实现了:
1.鼠标悬停效果
2.按钮点击效果
3.文本绘制效果
4.点击后事件触发效果
5.按钮的隐藏和显示控制使用方法:
btn=BFButton(my_surface,my_rect,text=my_label,click=my_method)
在事件响应处
btn.update(event)
在绘图处
btn.draw()
下面附一个例子
#-*-coding=utf-8-*- importpygame frombf_buttonimportBFButton pygame.init() screencaption=pygame.display.set_caption('bfcontrol') screen=pygame.display.set_mode((400,400)) defdo_click1(btn): pygame.display.set_caption('iclick%s,ctlidis%s'%(btn._text,btn.ctl_id)) btn.text='beclick' defdo_click2(btn): btn.visible=False defdo_click3(btn): pygame.quit() exit() button1=BFButton(screen,(120,100,160,40)) button1.text='Play' button1.click=do_click1 button2=BFButton(screen,(120,180,160,40),text='Hide',click=do_click2) button3=BFButton(screen,(120,260,160,40),text='Quit',click=do_click3) whileTrue: foreventinpygame.event.get(): ifevent.type==pygame.QUIT: pygame.quit() exit() button1.update(event) button2.update(event) button3.update(event) screen.fill((255,255,255)) button1.draw() button2.draw() button3.draw() pygame.display.update()例子里有两个按钮
第一个按钮事件是修改界面标题和按钮上的文字
第二个按钮事件是隐藏自己
第三个按钮事件是退出为方便按钮管理,其实可以定一个ButtonGroup类
classBFButtonGroup(object): def__init__(self): self.btn_list=[] defadd_button(self,button): self.btn_list.append(button) defmake_button(self,screen,rect,text='Button',click=None): button=BFButton(screen,rect,text=text,click=click) self.add_button(button) defupdate(self,event): forbuttoninself.btn_list:button.update(event) defdraw(self): forbuttoninself.btn_list:button.draw()这样使用的时候只需要对ButtonGroup进行update和draw
#-*-coding=utf-8-*- importpygame frombf_buttonimportBFButton,BFButtonGroup pygame.init() screencaption=pygame.display.set_caption('bfcontrol') screen=pygame.display.set_mode((400,400)) defdo_click1(btn): pygame.display.set_caption('iclick%s,ctlidis%s'%(btn._text,btn.ctl_id)) btn.text='beclick' defdo_click2(btn): btn.visible=False defdo_click3(btn): pygame.quit() exit() btn_group=BFButtonGroup() btn_group.make_button(screen,(120,100,160,40),text='Play',click=do_click1) btn_group.make_button(screen,(120,180,160,40),text='Hide',click=do_click2) btn_group.make_button(screen,(120,260,160,40),text='Quit',click=do_click3) whileTrue: foreventinpygame.event.get(): ifevent.type==pygame.QUIT: pygame.quit() exit() btn_group.update(event) screen.fill((255,255,255)) btn_group.draw() pygame.display.update()以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。