python-3.x - 我的 Python 游戏敌人正在循环移动

标签 python-3.x pygame

在我的 pygame 代码中,我制作了自己的简单敌人 AI,让它们在屏幕上跟随玩家,朝那个方向移动。但我遇到了一个问题,导致它们在角色移动时开始奇怪地循环,并且仅在玩家保持静止时才跟随玩家。进行跟踪的类是“calcSpeeds()”。如果有人能帮助我弄清楚我需要做什么,那就太好了。

import pygame
import random

import math

from fractions import Fraction

pygame.init()

swidth = int(1920/1.5)
sheight = int(1080/1.5)

groundheight = sheight*.9



win = pygame.display.set_mode((swidth,sheight))

pygame.display.set_caption("first time coding in pythong for realzies")

mx, my = pygame.mouse.get_pos()

class Player:
    def __init__(self,x,y,width,height):
        self.px = x
        self.py = y
        self.pwidth = width
        self.pheight = height
        self.psx = 0
        self.psy = 0
        self.masterspeed = 2
        # 4
        self.isjump = False

    def updatePos(self):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                self.psy = self.masterspeed
                self.py -= self.psy
            else:
                self.psy = 0

            if event.key == pygame.K_DOWN:
                self.psy = self.masterspeed
                self.py += self.psy
            else:
                self.psy = 0

            if event.key == pygame.K_LEFT:
                self.psx = self.masterspeed
                self.px -= self.psx
            else:
                self.psx = 0

            if event.key == pygame.K_RIGHT:
                self.psx = self.masterspeed
                self.px += self.psx
            else:
                self.psx = 0


            if event.key == pygame.K_SPACE:
                print("Hey, you pressed the key, 'space bar'!")

            if event.key == pygame.K_x:
                print("Hey, you pressed the key, 'x'!")

    def drawCharacter(self):
        pygame.draw.rect(win, (222,2,2), (self.px-self.pwidth/2,self.py-self.pheight/2,self.pwidth,self.pheight))


class EnemyBird:
    def __init__(self,x,y,width,height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.sx = 0
        self.sy = 0
        self.vel = 1

    def drawEnemyBird(self):
        pygame.draw.ellipse(win, (245, 218, 17), (self.x,self.y,self.width,self.height))

    def calcSpeeds(self):

        self.slope = (me.py - self.y)/(me.px - self.x)

        # this variable holds the angle between the yellow birds and the player
        self.pointangle = math.degrees(math.atan2((me.py - self.y),(me.px - self.x)))

        self.sy = self.vel*math.sin(self.pointangle)
        self.sx = self.vel*math.cos(self.pointangle)

        print(self.sx,self.sy)

    def moveBird(self):
        self.x += self.sx
        self.y += self.sy

me = Player(swidth/2 ,sheight*.9,20,20)

birdies = []

for i in range(1):
    birdies.append(EnemyBird(random.randint(0,swidth), random.randint(0,sheight*.9), 20,20))

run = True
while run:
    pygame.time.delay(10)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    mx, my = pygame.mouse.get_pos()
    win.fill((135,206,235))
    pygame.draw.rect(win, (48, 110, 16), (0,groundheight,swidth,sheight*.2))

    me.updatePos()
    me.drawCharacter()

    for i in range(len(birdies)):

        birdies[i].moveBird()
        birdies[i].calcSpeeds()
        birdies[i].drawEnemyBird()

    pygame.display.update()
pygame.quit()

最佳答案

根据 Asocia 的评论,在计算 self.pointangle 时删除 math. Degrees() 可以解决您遇到的问题。

    def calcSpeeds(self):

        self.slope = (me.py - self.y)/(me.px - self.x)

        # this variable holds the angle between the yellow birds and the player
        self.pointangle = math.atan2((me.py - self.y),(me.px - self.x))

        self.sy = self.vel*math.sin(self.pointangle)
        self.sx = self.vel*math.cos(self.pointangle)

        print(self.sx,self.sy)

让敌人跟踪玩家时可以使用的另一种方法是:

    def calcSpeeds(self):

        y = (me.py - self.y)
        x = (me.px - self.x)
        direction = pygame.Vector2(x, y)
        direction.scale_to_length(self.vel)

        self.sx, self.sy = direction

        print(self.sx,self.sy)

首先找到从敌人到玩家的向量 x, y。然后,设置一个 pygame Vector2 ,以便您可以将向量缩放到您想要的任何长度。这将决定敌人向玩家移动的速度。

关于python-3.x - 我的 Python 游戏敌人正在循环移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61779736/

相关文章:

python - 在 pandas 中使用 groupby 进行 bool 运算

python - Pygame 填充和 blit 不编辑屏幕

python - python 中的鸡蛋和篮子游戏

python - 在 Python 中重现随机生成关卡的最简单方法是什么?

Python错误: 'None Type' object does not support item assignment

python - 从 Python 中的元组列表中获取配对元素

python - 有断开连接按钮和连接按钮吗?

python - 如何从文本文件向 OptionMenu 添加选项

python - 如何使用 selenium 将文本嵌入到 div 类中

python-3.x - python - add() 不会将类的实例添加到组中,因为它不可迭代,但它正在迭代