pygame實(shí)現(xiàn)非圖片按鈕效果
本文實(shí)例為大家分享了pygame實(shí)現(xiàn)非圖片按鈕效果的具體代碼,供大家參考,具體內(nèi)容如下
按鈕類(lèi)程序
# -*- coding=utf-8 -*-
import threading
import pygame
from pygame.locals import MOUSEBUTTONDOWN
class 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.id
CLICK_EFFECT_TIME = 100
class 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))
主要給按鈕實(shí)現(xiàn)了:
1.鼠標(biāo)懸停效果
2.按鈕點(diǎn)擊效果
3.文本繪制效果
4.點(diǎn)擊后事件觸發(fā)效果
5.按鈕的隱藏和顯示控制
使用方法:
btn = BFButton(my_surface,my_rect,text=my_label,click=my_method)
在事件響應(yīng)處
btn.update(event)
在繪圖處
btn.draw()
下面附一個(gè)例子
# -*- coding=utf-8 -*-
import pygame
from bf_button import BFButton
pygame.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 = False
def do_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)
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()
例子里有兩個(gè)按鈕
第一個(gè)按鈕事件是修改界面標(biāo)題和按鈕上的文字
第二個(gè)按鈕事件是隱藏自己
第三個(gè)按鈕事件是退出

為方便按鈕管理,其實(shí)可以定一個(gè)ButtonGroup類(lèi)
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()
這樣使用的時(shí)候只需要對(duì)ButtonGroup進(jìn)行update和draw
# -*- coding=utf-8 -*-
import pygame
from bf_button import BFButton,BFButtonGroup
pygame.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 = False
def 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()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
PyCharm實(shí)現(xiàn)遠(yuǎn)程調(diào)試的全過(guò)程(附圖文講解)
這篇文章主要介紹了PyCharm實(shí)現(xiàn)遠(yuǎn)程調(diào)試的全過(guò)程,文中通過(guò)圖文結(jié)合的方式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-05-05
Python實(shí)現(xiàn)將羅馬數(shù)字轉(zhuǎn)換成普通阿拉伯?dāng)?shù)字的方法
這篇文章主要介紹了Python實(shí)現(xiàn)將羅馬數(shù)字轉(zhuǎn)換成普通阿拉伯?dāng)?shù)字的方法,簡(jiǎn)單分析了羅馬數(shù)字的構(gòu)成并結(jié)合實(shí)例形式給出了Python轉(zhuǎn)換羅馬數(shù)字為阿拉伯?dāng)?shù)字的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-04-04
Django基于ORM操作數(shù)據(jù)庫(kù)的方法詳解
這篇文章主要介紹了Django基于ORM操作數(shù)據(jù)庫(kù)的方法,結(jié)合實(shí)例形式總結(jié)分析了Django使用ORM操作數(shù)據(jù)庫(kù)的相關(guān)配置、增刪改查等相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
詳細(xì)分析Python collections工具庫(kù)
這篇文章主要介紹了詳解Python collections工具庫(kù)的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
Django中使用 Closure Table 儲(chǔ)存無(wú)限分級(jí)數(shù)據(jù)
對(duì)于數(shù)據(jù)量大的情況(比如用戶(hù)之間有邀請(qǐng)鏈,有點(diǎn)三級(jí)分銷(xiāo)的意思),就要用到 closure table 的結(jié)構(gòu)來(lái)進(jìn)行存儲(chǔ)。這篇文章主要介紹了Django中使用 Closure Table 儲(chǔ)存無(wú)限分級(jí)數(shù)據(jù),需要的朋友可以參考下2019-06-06
Python使用裝飾器進(jìn)行django開(kāi)發(fā)實(shí)例代碼
這篇文章主要介紹了Python使用裝飾器進(jìn)行django開(kāi)發(fā)實(shí)例代碼,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
Jupyter Notebook運(yùn)行Python代碼實(shí)現(xiàn)傳參方式
這篇文章主要介紹了Jupyter Notebook運(yùn)行Python代碼實(shí)現(xiàn)傳參方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
python 判斷txt每行內(nèi)容中是否包含子串并重新寫(xiě)入保存的實(shí)例
這篇文章主要介紹了python 判斷txt每行內(nèi)容中是否包含子串并重新寫(xiě)入保存的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03

