python - Pygame 在点列表中移动?

标签 python math path pygame

我正在尝试创建一款塔防游戏,但我需要敌人在一条路径上移动。我以为我已经弄清楚了,但是当我去尝试我的代码时,它只能有时工作。

有时敌人会到达其预期的位置,有时则不会。它的工作原理是基于创建路径的一系列点,我让敌人穿过它们,当它到达一个点时,它就会转到下一个点。

我尝试了许多不同的测试来查看玩家是否接触到了点,但没有一个能够始终如一地工作。目前代码中的效果最好,但并非每次都有效。 (

except ZeroDivisionError:
    bullet_vector=''
if bullet_vector==(0,0):
    bullet_vector=''

)

据我所知,我只需要找到一个更好的测试来确定物体何时处于应该改变方向的位置。这是代码:

import pygame,math
from pygame.locals import *

pygame.init()
screen=pygame.display.set_mode((640,480))
run=True
clock=pygame.time.Clock()
def Move(t0,t1,psx,psy,speed):
    global mx
    global my

    speed = speed

    distance = [t0 - psx, t1 - psy]
    norm = math.sqrt(distance[0] ** 2 + distance[1] ** 2)
    try:
        direction = [distance[0] / norm, distance[1 ] / norm]
        bullet_vector = [int(direction[0] * speed), int(direction[1] * speed)]
    except ZeroDivisionError:
        bullet_vector=''
    if bullet_vector==(0,0):
        bullet_vector=''
    return bullet_vector

class AI(object):
    def __init__(self,x,y):
        self.x=x
        self.y=y
        self.path=[(144,114),(280,114),(280,301),(74,300),(74,400)]
    def update(self):
        self.move_vector=Move((self.path[0])[0],(self.path[0])[1],self.x,self.y,1)
        if self.move_vector != '':
            self.x += self.move_vector[0]
            self.y += self.move_vector[1]
        else:
            self.path=self.path[1:]
        pygame.draw.circle(screen,((255,0,0)),(self.x,self.y),3,0)
enemies=[AI(-5,114)]
while run:
    screen.fill((0,200,0))
    for e in enemies:
        e.update()
    for e in pygame.event.get():
        if e.type==QUIT:
            run=False
    clock.tick(99)
    pygame.display.flip()

如果有人能找出我错在哪里,我将不胜感激。

最佳答案

我自己找到了答案,但它只支持四个方向的移动(这正是我所需要的)它甚至允许可调速度!如果有人想要的话,这就是:

import pygame,math
from pygame.locals import *

pygame.init()
screen=pygame.display.set_mode((640,480))
run=True
themap=pygame.image.load('map1.png')
clock=pygame.time.Clock()

class AI(object):
    def __init__(self,x,y):
        self.x=x
        self.y=y
        self.path=[(144,114),(280,114),(280,300),(100,302)]
    def update(self):
        speed=2
        if self.x<(self.path[0])[0]:
            self.x+=speed
        if self.x>(self.path[0])[0]:
            self.x-=speed
        if self.y<(self.path[0])[1]:
            self.y+=speed
        if self.y>(self.path[0])[1]:
            self.y-=speed
        z=(self.x-(self.path[0])[0],self.y-(self.path[0])[1])
        if (z[0]/-speed,z[1]/-speed)==(0,0):
            self.path=self.path[1:]
        pygame.draw.circle(screen,((255,0,0)),(self.x,self.y),3,0)
enemies=[AI(-5,114)]
while run:
    screen.blit(themap,(0,0))
    for e in enemies:
        e.update()
    for e in pygame.event.get():
        if e.type==QUIT:
            run=False
    clock.tick(60)
    pygame.display.flip()

关于python - Pygame 在点列表中移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23045685/

相关文章:

algorithm - 确定由 2 个地理点(纬度、经度)构成的线是否与地理区域(圆)相交的公式?

syntax - 正确的 InstallUtil 文件语法路径?

python - 我们可以不使用 on_delete 只定义 models.CASCADE 吗?

python - 如何在保留 '\' 的同时替换 '\n'?

python - Pandas: reshape 数据框以将多列压缩为单行值

玛雅中的Python : listAttr with uiTypes

c++ - 计算所有可能的乘积为完全平方数对的有效算法

javascript - Jquery Load 全局函数部分工作

python - 本地主机网络应用程序。获取浏览器中文件的完整路径

python - 使用键字符串列表作为路径添加到字典