gpt4 book ai didi

python - 如何让敌人跟随玩家? pygame

转载 作者:行者123 更新时间:2023-12-01 06:32:27 26 4
gpt4 key购买 nike

我知道以前有人问过这个问题,但我无法让它与我的代码一起工作。

我认为这才是让敌人跟随玩家的地方。我不确定具体如何实现。

我希望敌人跟随玩家,当他们遇到玩家时游戏结束。我还希望它能够与多个敌人一起工作。

# player class
class player:
def __init__(self, x, y, w, h, xChange, yChange, vel):
self.x = x # plater x value
self.y = y # player y value
self.w = w # player w (width)
self.h = h # player h (height)
self.xChange = xChange # player xChange (to add to x value to move player horizontally)
self.yChange = yChange # player yChange (to aad to y value to move player vertically)
self.vel = vel # velocity of player (needed for collision)

# enemy class
class enemy:
def __init__(self, x, y, w, h):
self.x = x # enemy x value
self.y = y # enemy y value
self.w = w # enemy w (width) value
self.h = h # enemy h (height) value

# ----------------------------------------------------------------

"""enemy's x value (random value) (we pick 750 because the enemy width is 50 and
the screen width is 800. If we set the random value to 800, then the enemy has a
chance of spawning outside of the screen.)"""
enemyX = random.randint(0, 700)

"""enemy's y value (random value) (we pick 540 because the enemy height is 60
and the screen height is 600. If we set the random value to 600, the enemy has a
chance of spawning outside of the screen.)"""
enemyY = random.randint(0, 540) # enemy's y value

score = 0 # score set to 0. Will update in while loop.

rec = player(50, 50, 24, 32, 0, 0, 5) # the player's values (x, y, w, h, xChange, yChange, vel)
redRec = enemy(enemyX, enemyY, 24, 32) # the enemy's values (x, y, w, h)

# mainloop #
def mainloop():
global running, score, intro, sprite, next_zombie_time

while running:
"""keeps filling window with the background image"""
window.blit(background, (0, 0))
pygame.time.delay(25) # delay
for event in pygame.event.get(): # for every event in game
if event.type == pygame.QUIT: # if I exit the game
quitGame()

if event.type == pygame.KEYUP: # if any keys are let go
if event.key == pygame.K_a: # if key a
rec.xChange = 0 # set xChange to 0 (stop moving rec)

if event.key == pygame.K_d: # if key d
rec.xChange = 0 # set xChange to 0 (stop moving rec)

if event.key == pygame.K_w: # if key w
rec.yChange = 0 # set xChange to 0 (stop moving rec)

if event.key == pygame.K_s: # if key s
rec.yChange = 0 # set xChange to 0 (stop moving rec)

if event.type == pygame.KEYDOWN: # if any keys are pressed
if event.key == pygame.K_F4: # if key F4
pygame.quit() # set running to false

if event.key == pygame.K_a: # if key a
rec.xChange += -5 # add -5 to xChange (move rec left)
sprite = spriteLeft

if event.key == pygame.K_d: # if key a
rec.xChange += 5 # adds 5 to xChange (move rec right)
sprite = spriteRight

if event.key == pygame.K_w: # if key a
#adds -5 to yChange (moves rec up). Yes, this is supposed to say up.
rec.yChange += -5
sprite = spriteUp

if event.key == pygame.K_s: # if key a
# adds 5 to yChange (moves rec down). Yes, this is supposed to say down.
rec.yChange += 5
sprite = spriteDown

# pause key to pause game
if event.key == pygame.K_o: # if key o
running = False # set running to false
intro = False # intro set to False
pauseMenu() # pauseMenu is called




rec.x += rec.xChange # add rec's xChange to x (to do the moving)
rec.y += rec.yChange # adds rec's yChange to y (to do the moving)


# ----------------BOUNDARIES------------------------------
if rec.x <= 0: # if rec's x is less than or equal to 0 (if tries to escape screen)
rec.x = 0 # rec's x is set to 0 so it won't go off screen.

"""(we pick 750 because the player width is 50 and the screen width is 800.
If we set it to 800, then the player can go outside of screen."""
if rec.x >= 750: # if rec's x is greater than or equal to 750 (if tries to escape screen)
rec.x = 750 # set rec's x to 750 so it won't go off screen

if rec.y <= 0: # if rec's y is less than or equal to 0 (if tries to escape screen)
rec.y = 0 # set rec's y to 0 so it won't go off screen
"""we pick 540 because the player height is 60 and the screen height is 600.
If we set it to 600, then the player can go outside of screen"""
if rec.y >= 540: # if rec'y is greater than or equal to 540 (if tries to escape screen)
rec.y = 540 # set rec's y to 540 so it won't go off screen

#enemy.update(delta_time, player)
collisions = detCollision(rec.x, rec.y, rec.w, rec.h, redRec.x, redRec.y, redRec.w, redRec.h)
# activate the redrawWin function
redrawWin(collisions)



最佳答案

您需要一些信息才能执行此操作。首先,您需要玩家和敌人之间的距离。您可以使用 math.hypot 函数计算出距离,如下所示 distance = (math.hypot(enemy.x - player.x, heaven.y - player.y) )。然后,您需要使用 math.atan2 函数计算出它们之间的角度(以弧度为单位)。请注意,此函数首先采用 y 位置参数。您可以按如下方式使用它

angle_radians = (math.atan2(enemy.y - player.y , enemy.x - player.x))

现在要让敌人朝玩家的方向移动,你可以这样做

     enemy.y += math.sin(angle_radians) 
enemy.x += math.cos(angle_radians)

看看为什么这有效 http://setosa.io/ev/sine-and-cosine/ 。您甚至可以通过设置玩家与敌人之间的距离条件来添加一个范围,规定当敌人开始跟随时您希望玩家与敌人的距离有多近。

if distance < range:

您还可以通过将正弦和余弦乘以相同的数字来控制速度,这是有效的,因为它们是比率。它看起来像这样

 enemy.y += math.sin(angle_radians) * speed # Note both speeds must be the same nnumber
enemy.x += math.cos(angle_radians) * speed

对于问题的最后一部分,如果您希望这对所有敌人都有效,我建议将您的游戏对象添加到列表中。例如,您可以将敌人添加到列表中并让他们跟随玩家。假设您通过这样做列出了所有敌人

all_enemies = []
for i in range(number of enemies you want):
all_enemies.append(enemy())

您可能会得到如下所示的最终结果:

 def Follow_player(self):
for e in all_enemies:
distance = (math.hypot(e.x - player.x, e.y - player.y) )
angle_radians = (math.atan2(e.y - player.y , e.x - player.x))

e.y += math.sin(angle_radians)
e.x += math.cos(angle_radians)

if distance < 1:
# they have collided

编辑这是一个非常好的 YouTube 视频的链接,它描述了这一切 https://www.youtube.com/watch?v=DVYDkHdsTIM

关于python - 如何让敌人跟随玩家? pygame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59828650/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com