【发布时间】:2019-06-01 16:35:19
【问题描述】:
我试图以给定的速率生成无限数量的敌人,并让它们向玩家移动。但是,我只能设法让两个生成,一个用于它们生成的每个原始矩形。这是因为移动覆盖了它们生成的矩形,因此任何进一步的敌人只会在前两个中的一个之上生成。这是使用 move_ip() 函数,我认为这可能是问题所在,但尝试仅使用 move() 会导致根本没有移动。
def main():
global DISPLAYSURF, FPSCLOCK
pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
pygame.display.set_caption ('Ronin')
checkForQuit()
enemies = []
playerX = 730
enemyImage = pygame.image.load('enemyidle.png')
enemySurf = pygame.transform.scale(enemyImage, (PLAYERWIDTH, PLAYERHEIGHT))
leftRect = pygame.Rect(0, 735, PLAYERWIDTH, PLAYERHEIGHT)
rightRect = pygame.Rect(1500, 735, PLAYERWIDTH, PLAYERHEIGHT)
moveLeft = moveRight = moveUp = moveDown = False
enemycounter = 0
while True:
checkForQuit()
#Draw the background
DISPLAYSURF.blit(backgroundSurf, backgroundRect)
#draw the player
DISPLAYSURF.blit(playerSurf, playerRect)
#time the enemy spawn and draw the enemies
enemy = random.choice([leftRect, rightRect])
if enemycounter < ENEMYSPAWNRATE:
enemycounter += 1
if enemycounter == ENEMYSPAWNRATE:
enemycounter = 0
enemies.append(enemy)
enemyAI(enemies, playerRect)
for e in enemies:
DISPLAYSURF.blit(enemySurf, e)
pygame.display.update()
FPSCLOCK.tick(FPS)
def enemyAI(enemies, playerRect):
for e in enemies:
if e.left > playerRect.right:
e.move_ip(-1 * PLAYERSPEED +5, 0)
if e.right < playerRect.left:
e.move_ip(PLAYERSPEED -5, 0)
main()
我可以在调试器中看到,添加到敌人列表中的每个新敌人都与前两个现有敌人之一具有相同的坐标。但我希望它们出现在由 leftRect 或 rightRect 指定的原始坐标处。
【问题讨论】: