【问题标题】:animating a button动画按钮
【发布时间】:2012-07-10 23:20:25
【问题描述】:

我一直试图让这个 python 代码的 sn-p 工作,但无济于事。所以我想要做的是让我的按钮有 3 种状态:空闲、悬停和单击。

但是,由于某种原因,图像没有改变。有人可以告诉我我做错了什么吗?

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

谢谢!

编辑:

好的,我得到了一些帮助,并添加了更新功能。但它似乎仍然不起作用。

import pygame,sys,os
from pygame import *

BG = "bg.jpg"
BUTTON1 = "buttonidle.png"
BUTTON2 = "buttonhov.png"
BUTTON3 = "buttonclick.png"

buttonsprite = 1

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)
        self.image,self.rect = load_image(BUTTON3, -1)
        screen = pygame.display.get_surface()
        self.area = screen.get_rect()
        self.rect.topleft = 190, 140

    def update(self):
        if buttonsprite == 1:
            self.image,self.rect = load_image(BUTTON1, -1)
        if buttonsprite == 2:
            self.image,self.rect = load_image(BUTTON2, -1)
        if buttonsprite == 3:
            self.image,self.rect = load_image(BUTTON3, -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 event.type == MOUSEBUTTONUP:
            if x>=193 and x<=315 and y>=143 and y<=171:
                buttonsprite = 2
                button.update()
            else:
                buttonsprite = 1
                button.update()
        if event.type == MOUSEBUTTONDOWN:
            if x>=193 and x<=315 and y>=143 and y<=171:
                buttonsprite = 3
                button.update()
            else:
                pass

【问题讨论】:

    标签: python python-2.7 pygame


    【解决方案1】:

    我认为这是因为您没有调用某种主动更改图像的更新函数。如果您在看到所需更改后重新创建按钮。

    基本上,更改值并不足以实现您想要做的事情,您需要让 Button 知道它有一个新值。

    不好的相关例子:

    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
    

    【讨论】:

    • 谢谢,我想我取得了一些进展。但它仍然不起作用。稍后我会发布我的新代码。
    • 你将不得不再次渲染图像,类似于allsprites = pygame.sprite.RenderPlain((button)) ; allsprites.update()
    【解决方案2】:

    看看这个示例代码。它有一个带有两种状态按钮的菜单。正常并悬停。 https://gist.github.com/2802185

    【讨论】:

      猜你喜欢
      • 2021-04-09
      • 2014-03-05
      • 2015-05-22
      • 2015-12-14
      • 2013-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多