所以这就是我的问题,我在(非常)开始让我的Rogue像游戏一样,并且我坚持让敌人追逐玩家,它必须是一件容易而且显而易见的东西,但我的技能很低......所以这是我的怪物类
class monster():
def __init__(self , mright, mleft, mup, mdown):
self.mright = pygame.image.load(mright).convert_alpha()
self.mleft = pygame.image.load(mleft).convert_alpha()
self.mup = pygame.image.load(mup).convert_alpha()
self.mdown = pygame.image.load(mdown).convert_alpha()
self.sqrx = 7
self.sqry = 2
self.x = 210
self.y = 60
self.direction = self.mdown
char = character(right , left , up , down , Level)
def chase(self,direction):
if self.sqrx > char.sqrx :
self.sqrx -= 1
self.x = self.sqrx * size_sprite
self.direction = self.mleft
if self.sqrx < char.sqrx :
self.sqrx += 1
self.x = self.sqrx * size_sprite
self.direction = self.mright
if self.sqry > char.sqry :
self.sqry -= 1
self.y = self.sqry * size_sprite
self.direction = self.mup
if self.sqry < char.sqry :
self.sqry += 1
self.y = self.sqry * size_sprite
self.direction = self.mdown
游戏循环
while gameon:
pygame.time.Clock().tick(30)
for event in pygame.event.get():
if event.type == QUIT:
gameon = 0
on = 0
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
gameon = 0
elif event.key == K_RIGHT:
char.move('right')
elif event.key == K_LEFT:
char.move('left')
elif event.key == K_UP:
char.move('up')
elif event.key == K_DOWN:
char.move('down')
level.display(window)
window.blit(monst.direction , (monst.x , monst.y))
window.blit(char.direction , (char.x , char.y))
pygame.display.flip()
答案 0 :(得分:1)
您应该在mainloop中使用chase()
- 请参阅代码中的注释
while gameon:
pygame.time.Clock().tick(30)
# --- events ---
for event in pygame.event.get():
if event.type == QUIT:
gameon = 0
on = 0
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
gameon = 0
elif event.key == K_RIGHT:
char.move('right')
elif event.key == K_LEFT:
char.move('left')
elif event.key == K_UP:
char.move('up')
elif event.key == K_DOWN:
char.move('down')
# - object events ---
# char.handle_event(event)
# --- updates ---
# HERE put all functions which change elements
# (but don't depend on events) like:
# - chasing player,
# - moving mosters,
# - detecting collisions,
# - animating other elements (ie. fire, water, etc.)
# --- draws all elements ---
level.display(window)
window.blit(monst.direction , (monst.x , monst.y))
window.blit(char.direction , (char.x , char.y))
pygame.display.flip()
在Moster中我会使用char
(玩家实例)作为
def __init__(self , char, ...):
self.char = char
始终可以访问其位置
或至少
def chase(self, direction, char):
在您拨打chase()
BTW:而不是gameon = 0
更具可读性gameon = False
(和gameon = True
)
您可以在draw()
中创建Moster()
并执行
monst.draw(window)
而不是
window.blit(monst.direction , (monst.x , monst.y))
对变量(CamelCase
)的类(class Monster():
)和lower_case
名称使用moster = Moster()
名称。它使代码更具可读性。见PEP 8 -- Style Guide for Python Code
答案 1 :(得分:0)
就像上面提到的那样 - 你设置方向的缩进使得每次调用chase
时它都会调用所有4个赋值。这基本上总是设置direction = self.mdown
def chase(self,direction):
if self.sqrx > char.sqrx :
self.sqrx -= 1
self.x = self.sqrx * size_sprite
self.direction = self.mleft