Python 设置按钮的动画
我一直在尝试让这段python代码正常工作,但没有效果。所以我要做的是让我的按钮有三种状态:空闲、悬停和点击 然而,由于某些原因,图像没有改变。有人能告诉我我做错了什么吗Python 设置按钮的动画,python,python-2.7,pygame,Python,Python 2.7,Pygame,我一直在尝试让这段python代码正常工作,但没有效果。所以我要做的是让我的按钮有三种状态:空闲、悬停和点击 然而,由于某些原因,图像没有改变。有人能告诉我我做错了什么吗 import pygame,sys,os from pygame.locals import * BG = "bg.jpg" BUTTON1 = "buttonidle.png" BUTTON2 = "buttonhov.png" BUTTON3 = "buttonclick.png" def load_image(nam
import pygame,sys,os
from pygame.locals import *
BG = "bg.jpg"
BUTTON1 = "buttonidle.png"
BUTTON2 = "buttonhov.png"
BUTTON3 = "buttonclick.png"
def load_image(name, colorkey=None):
fullname = os.path.join(name)
try:
image = pygame.image.load(fullname)
except pygame.error, message:
print "Can't load image:", name
raise SystemExit, message
image = image.convert()
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()
class Button(pygame.sprite.Sprite):
global buttonsprite
def __init__(self):
pygame.sprite.Sprite.__init__(self)
if buttonsprite == 1:
self.image,self.rect = load_image(BUTTON1, -1)
screen = pygame.display.get_surface()
self.area = screen.get_rect()
self.rect.topleft = 190, 140
if buttonsprite == 2:
self.image,self.rect = load_image(BUTTON2, -1)
if buttonsprite == 3:
self.image,self.rect = load_image(BUTTON3, -1)
buttonsprite = 1
pygame.init()
screen = pygame.display.set_mode((500,300))
pygame.display.set_caption('Window')
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((255,255,255))
screen.blit(background,(0,0))
pygame.display.flip()
button = Button()
allsprites = pygame.sprite.RenderPlain((button))
allsprites.update()
screen.blit(background,(0,0))
allsprites.draw(screen)
pygame.display.flip()
while True:
x,y = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if x >= 193 and x <= 315 and y >= 143 and y <= 171:
buttonsprite = 2
if event.type == MOUSEBUTTONDOWN:
buttonsprite = 3
else:
buttonsprite = 1
导入pygame、sys、os
从pygame.locals导入*
BG=“BG.jpg”
BUTTON1=“buttonidle.png”
BUTTON2=“buttonhov.png”
BUTTON3=“buttonclick.png”
def load_图像(名称,colorkey=None):
fullname=os.path.join(名称)
尝试:
image=pygame.image.load(全名)
除了pygame.error,消息:
打印“无法加载图像:”,名称
升起系统退出,消息
image=image.convert()
如果colorkey不是None:
如果colorkey为-1:
colorkey=image.get_位于((0,0))
图像。设置颜色键(颜色键,RLEACCEL)
返回image,image.get_rect()
类按钮(pygame.sprite.sprite):
全局按钮式打印
定义初始化(自):
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
如果buttonsprite==1:
self.image,self.rect=load_image(按钮1,-1)
screen=pygame.display.get_surface()
self.area=screen.get_rect()
self.rect.topleft=190140
如果buttonsprite==2:
self.image,self.rect=load_image(按钮2,-1)
如果buttonsprite==3:
self.image,self.rect=加载图像(按钮3,-1)
buttonsprite=1
pygame.init()
screen=pygame.display.set_模式((500300))
pygame.display.set_标题('窗口')
background=pygame.Surface(screen.get_size())
background=background.convert()
背景填充((255255))
屏幕光点(背景,(0,0))
pygame.display.flip()
按钮=按钮()
allsprites=pygame.sprite.RenderPlain((按钮))
allsprites.update()
屏幕光点(背景,(0,0))
所有精灵。绘制(屏幕)
pygame.display.flip()
尽管如此:
x、 y=pygame.mouse.get_pos()
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.quit()
sys.exit()
如果x>=193,x=143,y,我认为这是因为你没有调用某种更新函数来主动改变图像。如果要在看到所需更改后重新创建按钮
基本上,更改值对您想要做的还不够,您需要让按钮知道它有一个新值
不好的相关示例:
def button():
global number
print number
number = 1
button()
>>> 1 <----------------↰
number = 'A STRING NOW' ↑
#now the printed string doesn't above change, so you'd have to call button again for it to change:
button()
>>> A STRING NOW # <-- BAM
def按钮():
全局数
打印号码
数字=1
按钮()
>>>1>>现在是字符串#我认为这是因为您没有调用某种主动更改图像的更新函数。如果要在看到所需更改后重新创建按钮
基本上,更改值对您想要做的还不够,您需要让按钮知道它有一个新值
不好的相关示例:
def button():
global number
print number
number = 1
button()
>>> 1 <----------------↰
number = 'A STRING NOW' ↑
#now the printed string doesn't above change, so you'd have to call button again for it to change:
button()
>>> A STRING NOW # <-- BAM
def按钮():
全局数
打印号码
数字=1
按钮()
>>>1>>现在是字符串#看看这个示例代码。它有一个带有两种状态按钮的菜单。正常并悬停 看看这个示例代码。它有一个带有两种状态按钮的菜单。正常并悬停 谢谢,我想我取得了一些进步。但它仍然不起作用。我马上发布我的新代码。你必须再次渲染图像,类似于allsprites=pygame.sprite.RenderPlain((按钮));allsprites.update()
谢谢,我想我取得了一些进展。但它仍然不起作用。我马上发布我的新代码。你必须再次渲染图像,类似于allsprites=pygame.sprite.RenderPlain((按钮));allsprites.update()