侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

pygame实现非图片按钮效果

Python  /  管理员 发布于 5年前   278

本文实例为大家分享了pygame实现非图片按钮效果的具体代码,供大家参考,具体内容如下

按钮类程序

# -*- coding=utf-8 -*-import threadingimport pygamefrom pygame.locals import MOUSEBUTTONDOWNclass BFControlId(object): _instance_lock = threading.Lock() def __init__(self):  self.id = 1 @classmethod def instance(cls, *args, **kwargs):  if not hasattr(BFControlId, "_instance"):   BFControlId._instance = BFControlId(*args, **kwargs)  return BFControlId._instance def get_new_id(self):  self.id += 1  return self.idCLICK_EFFECT_TIME = 100class BFButton(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() def init_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 def text(self):  return self._text @text.setter def text(self, value):  self._text = value  self.init_font() @property def click(self):  return self._click @click.setter def click(self, value):  self._click = value @property def visible(self):  return self._visible @visible.setter def visible(self, value):  self._visible = value def update(self, event):  if self.in_click and event.type == self.click_event_id:   if self._click: self._click(self)   self.click_event_id = -1   return  x, y = pygame.mouse.get_pos()  if x > self.x and x < self.x + self.width and y > self.y and y < self.y + self.height:   self.is_hover = True   if event.type == MOUSEBUTTONDOWN:    pressed_array = pygame.mouse.get_pressed()    if pressed_array[0]:     self.in_click = True     self.click_loss_time = pygame.time.get_ticks() + CLICK_EFFECT_TIME     self.click_event_id = pygame.USEREVENT+self.ctl_id     pygame.time.set_timer(self.click_event_id,CLICK_EFFECT_TIME-10)  else:   self.is_hover = False def draw(self):  if self.in_click:   if self.click_loss_time < pygame.time.get_ticks():    self.in_click = False  if not self._visible:   return  if self.in_click:   r,g,b = self.bg_color   k = 0.95   self.surface.fill((r*k, g*k, b*k))  else:   self.surface.fill(self.bg_color)  if self.is_hover:   pygame.draw.rect(self.surface, (0,0,0), (0,0,self.width,self.height), 1)   pygame.draw.rect(self.surface, (100,100,100), (0,0,self.width-1,self.height-1), 1)   layers = 5   r_step = (210-170)/layers   g_step = (225-205)/layers   for i in range(layers):    pygame.draw.rect(self.surface, (170+r_step*i, 205+g_step*i, 255), (i, i, self.width - 2 - i*2, self.height - 2 - i*2), 1)  else:   self.surface.fill(self.bg_color)   pygame.draw.rect(self.surface, (0,0,0), (0,0,self.width,self.height), 1)   pygame.draw.rect(self.surface, (100,100,100), (0,0,self.width-1,self.height-1), 1)   pygame.draw.rect(self.surface, self.bg_color, (0,0,self.width-2,self.height-2), 1)  self.surface.blit(self.textImage, (self._tx, self._ty))

主要给按钮实现了:

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 -*-import pygamefrom bf_button import BFButtonpygame.init()screencaption = pygame.display.set_caption('bf control')screen = pygame.display.set_mode((400,400))def do_click1(btn): pygame.display.set_caption('i click %s,ctl id is %s' % (btn._text,btn.ctl_id)) btn.text = 'be click'def do_click2(btn): btn.visible = Falsedef do_click3(btn): pygame.quit() exit()button1 = BFButton(screen, (120,100,160,40))button1.text = 'Play'button1.click = do_click1button2 = BFButton(screen, (120,180,160,40),text='Hide',click=do_click2)button3 = BFButton(screen, (120,260,160,40),text='Quit',click=do_click3)while True: for event in pygame.event.get():  if event.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类

class BFButtonGroup(object): def __init__(self):  self.btn_list = [] def add_button(self, button):  self.btn_list.append(button) def make_button(self, screen, rect, text='Button', click=None):  button = BFButton(screen, rect,text=text,click=click)  self.add_button(button) def update(self, event):  for button in self.btn_list: button.update(event) def draw(self):  for button in self.btn_list: button.draw()

这样使用的时候只需要对ButtonGroup进行update和draw

# -*- coding=utf-8 -*-import pygamefrom bf_button import BFButton,BFButtonGrouppygame.init()screencaption = pygame.display.set_caption('bf control')screen = pygame.display.set_mode((400,400))def do_click1(btn): pygame.display.set_caption('i click %s,ctl id is %s' % (btn._text,btn.ctl_id)) btn.text = 'be click'def do_click2(btn): btn.visible = Falsedef do_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)while True: for event in pygame.event.get():  if event.type == pygame.QUIT:    pygame.quit()    exit()  btn_group.update(event) screen.fill((255,255,255)) btn_group.draw()  pygame.display.update() 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    pygame实现成语填空游戏
    下一条:
    pygame实现贪吃蛇游戏(下)
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客