【发布时间】:2021-08-24 20:13:26
【问题描述】:
我有一个类可以在屏幕上绘制对象
我的班级:
class Sword():
def __init__(self, image, rect, speed, center):
self.image = image
self.rect = rect
self.rect.x,self.rect.y = center #set x and y position
self.speed = speed
self.alive = True
def live(self, enemy): #check if the image touches any object
if self.rect.colliderect(enemy):
self.alive = False
mixer.music.play()
def update(self): #move object
self.rect.x += self.speed
def draw(self, surface): #display object
if self.alive:
surface.blit(self.image, (self.rect))
我尝试使用循环添加一些对象:
extend = []#object array
liste = [100,200,300]#object points x and y
image= pygame.image.load('sword.png')
rect = image.get_rect()
for i in range(3):
extend.append(Sword(image,rect,1,(liste[i],liste[i])))
while True:
for tile in extend:
tile.draw(display)
在我的主循环中,我调用了这个列表,但它根据最后一个位置绘制了所有图像 Exp:rect<300,300,120,40>
为什么它会将所有对象绘制在同一个位置?
【问题讨论】:
-
什么是
sword.get_rect()? -
得到图片的rect exp: rect
-
但是
sword是什么? -
我正在使用 pygame 模块制作游戏,Sword 类在屏幕上绘制了一些东西
-
是的,我看到
Sword是一个类。sword是什么?