【问题标题】:Change a sprite image overtime超时更改精灵图像
【发布时间】:2015-07-21 12:10:11
【问题描述】:

您好,我有一个物体,它是一个航天器,我希望它在我按下按钮时改变颜色(改变图像)这是我的物体:

class soucoupe(pygame.sprite.Sprite):


def __init__(self, image, height, speed):
    pygame.sprite.Sprite.__init__(self)

    self.speed = -2
    self.image = pygame.Surface([64, 34])
    self.image = blus # this is the image

    self.pos = image.get_rect()
    self.pos.x=520
    self.pos.y=680
    self.rect = self.image.get_rect()

def move(self):
    global dpush
    global gpush

    if dpush and gpush ==1 or dpush and gpush ==0:  
        self.pos = self.pos.move(0*self.speed, 0)
    if dpush==1:
        self.pos = self.pos.move(-self.speed, 0)
    if gpush==1:
        self.pos = self.pos.move(self.speed, 0)

按下按钮时我尝试过: soucoupe.image=image2 但它不起作用图像仍然相同,看起来很容易解决,但我不知道。

【问题讨论】:

  • 你还没有发布所有的代码。您还应该查看编写代码的既定指南,请参阅 PEP-8

标签: python image pygame sprite


【解决方案1】:

你没有所有相关的代码,但你说你有:

soucoupe.image= image2

with 会设置soucoupeimage 属性class

这和设置一个类的instance的属性是不一样的。

您可能会在某处创建soucoupe 的实例,例如

something = soucoupe(image, height, speed)

那你要设置somethingimage属性

something.image = image2

您的代码还有一些其他问题,但这些超出了此答案的范围。

【讨论】:

  • 好的,我明白了,现在我解决了这个问题,很抱歉我开始你说代码中的其他问题。如果你有时间,你能把它们指给我吗
  • 例子:为什么要设置self.image两次?为什么将位置存储在self.posself.rect中?
  • 是的,那是我做的一些测试,我应该删除你是对的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多