Pygame 子类化问题?
所以基本上我只想创建一个以 pygames.sprite.Sprite 作为父级的敌人类。但后来我想移动我创建的这个精灵对象并将其添加到屏幕上。但它说 blit 不是“Enemy”类的属性。很抱歉提出这个新问题,但我该怎么做呢?
阶级敌人:
class Enemy(pygame.sprite.Sprite):
#Class for falling enemys
def __init__(self):
# initialize the pygame sprite
pygame.sprite.Sprite.__init__(self)
# set image and rect
self.image = pygame.image.load("enemysprite.png").convert()
self.rect = self.image.get_rect()
我打电话来尝试将精灵广告到屏幕上
enemy = Enemy()
enemy.blit(100,100)
pygame.display.update()
So basically I just wanted to create an enemy class with pygames.sprite.Sprite as the parent. But then I wanted to move this sprite object I created as well as add it to the screen. But it says blit is not a attribute of class "Enemy". Sorry for the newb question but how do I go about doing this?
Class enemy:
class Enemy(pygame.sprite.Sprite):
#Class for falling enemys
def __init__(self):
# initialize the pygame sprite
pygame.sprite.Sprite.__init__(self)
# set image and rect
self.image = pygame.image.load("enemysprite.png").convert()
self.rect = self.image.get_rect()
What I'm calling to try to ad the sprite to screen
enemy = Enemy()
enemy.blit(100,100)
pygame.display.update()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这种方法:
你必须在 pygame 屏幕中 blit 精灵图像才能显示它。
Try this way :
you have to blit the sprite image in the pygame screen to display it.
Sprite
可以放置在SpriteGroup
中,它具有很好的功能,但也可以批量进行 blit。要移动敌人,请移动
Sprite
的矩形。Sprite
s can be placed inSpriteGroup
s, which have nice features, but also blit all in one batch.To move the enemy, move the
Sprite
's rect.