Я новичок в pygame, и я делаю игру с зомби. Я контролирую человека с помощью клавиш со стрелками, и я хотел бы, чтобы зомби бежал к позиции игрока, когда зомби сначала создается. Я не уверен, как определить позицию игроков, чтобы установить зомби в правильном направлении. Зомби не должен следовать за человеком, просто голова в направлении позиции игроков, когда зомби создается.Как определить положение спрайта в pygame?
class Human(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load("human.jpg")
self.image=self.image.convert()
self.rect=self.image.get_rect()
self.x=100
self.y=430
self.dx=50
self.dy=0
def update(self):
keys=pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.x-=self.dx
if keys[pygame.K_RIGHT]:
self.x+=self.dx
self.rect.center=(self.x,self.y)
class ZombieChase(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load("zombie.jpg")
self.image=self.image.convert()
self.rect=self.image.get_rect()
self.reset()
self.dx=15
self.dy=15
#self.xStart=random.randrange(0,screen.get_width())
def update(self):
self.rect.centery+=self.dy
self.rect.centerx+=self.dx
if self.rect.top>screen.get_height():
self.reset()
if self.rect.right>screen.get_width():
self.reset()
if self.rect.left<0:
self.reset()
def reset(self):
self.rect.top=0
speed=random.randrange(20,36)
xStart=random.randrange(0,screen.get_width())
yStart=0
#humanx, humany !!!!!!this is where i don't know what to put!!!!!
try:
self.dx=(humanx-xStart)/speed
except ZeroDivisionError:
self.dx=0
try:
self.dy=(humany-yStart)/speed
except ZeroDivisionError:
self.dy=1
self.rect.centerx=xStart
Извините, если табуляция неправильная.
Спрайт имеет атрибут rect, который является его положением на экране. –
Сообщите мне, пожалуйста, код. – PygameNerd