如何使用Ursina实现敌人碰撞,并在Ursina中改变子弹方向?

2024-10-06 12:05:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我当时正在做一个Ursina游戏,annd当我去与敌人碰撞时,它犯了这样一个错误:

Traceback (most recent call last):
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\main.py", line 126, in _update
    __main__.update()
  File "C:/Users/Minerva Panganiban/OneDrive/Desktop/Python/Ursinale.py", line 128, in update
    destroy(bullet, enemy)
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\ursinastuff.py", line 48, in destroy
    s = Sequence(
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\sequence.py", line 42, in __init__
    self.generate()
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\sequence.py", line 52, in generate
    self.duration += arg.duration
TypeError: unsupported operand type(s) for +=: 'int' and 'Entity'
Traceback (most recent call last):
  File "C:/Users/Minerva Panganiban/OneDrive/Desktop/Python/Ursinale.py", line 136, in <module>
    app.run()
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\main.py", line 234, in run
    super().run()
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\direct\showbase\ShowBase.py", line 3325, in run
    self.taskMgr.run()
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\direct\task\Task.py", line 546, in run
    self.step()
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\direct\task\Task.py", line 500, in step
    self.mgr.poll()
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\main.py", line 126, in _update
    __main__.update()
  File "C:/Users/Minerva Panganiban/OneDrive/Desktop/Python/Ursinale.py", line 128, in update
    destroy(bullet, enemy)
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\ursinastuff.py", line 48, in destroy
    s = Sequence(
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\sequence.py", line 42, in __init__
    self.generate()
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\sequence.py", line 52, in generate
    self.duration += arg.duration
TypeError: unsupported operand type(s) for +=: 'int' and 'Entity'

查看错误,我找不到问题

以下是我正在制作的游戏代码:

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController

app = Ursina()

class Ground(Entity):
    def __init__(self):
        super().__init__(
            parent = scene,
            model = 'plane',
            texture = "Cube",
            scale = 500,
            position = Vec3(0, 0, 0),
            collider = 'plane')

class Cube(Entity):
    def __init__(self):
        super().__init__(
            parent = scene,
            model = 'cube',
            texture = "Sun",
            scale = 50,
            position = Vec3(250, 0, 250),
            collider = 'box')

class Cube2(Entity):
    def __init__(self):
        super().__init__(
            parent = scene,
            model = 'cube',
            texture = "Sun",
            scale = 50,
            position = Vec3(-250, 0, -250),
            collider = 'box')

class Cube3(Entity):
    def __init__(self):
        super().__init__(
            parent = scene,
            model = 'cube',
            texture = "Sun",
            scale = 50,
            position = Vec3(-250, 0, 250),
            collider = 'box')

class Cube4(Entity):
    def __init__(self):
        super().__init__(
            parent = scene,
            model = 'cube',
            texture = "Sun",
            scale = 50,
            position = Vec3(250, 0, -250),
            collider = 'box')

class Sky(Entity):
    def __init__(self):
        super().__init__(
            parent = scene,
            model = 'sphere',
            texture = "Bla",
            scale = 2050,
            double_sided = True)

class Weapon(Entity):
    def __init__(self):
        super().__init__(
            parent = camera.ui,
            model = 'cube',
            texture = "Untitled",
            scale = 0.5,
            rotation = Vec3(0, 0, 5),
            position = Vec3(0, 0, 0))

    def active(self):
        self.rotation = Vec3(0, 45, 5)
        self.position = Vec3(0, 0, 1)

    def passive(self):
        self.rotation = Vec3(0, 0, 5)
        self.position = Vec3(0, 0, 0)

player = FirstPersonController(model='cube',
                            texture = "face",
                            scale=0.75,
                            collider = 'box',
                              speed = 2.255)
camera.z = -5
ground = Ground()
cube = Cube()
cube2 = Cube2()
cube3 = Cube3()
cube4 = Cube4()
sky = Sky()
weapon = Weapon()

window.title = 'Ursinale'
window.borderless = False
window.fullscreen = False
window.exit_button.visible = False
window.fps_counter_enabled = True
window.color = color.black

enemy = Entity(parent = scene,
                 model = 'cube',
                 texture = "ArcataPonte1",
                 position = Vec3(2000, 2000, 2000),
                 scale = 25,
                 collider = 'box')
enemy.add_script(SmoothFollow(target=player,
                                offset=[0,0,0],
                                speed = 0.255))

bullet = None
def input(key):
    global bullet
    if key == 'left mouse down':
        bullet = Entity(parent=weapon, model='cube', scale=.1, color=color.black, collider='box')
        bullet.world_parent = scene
        bullet.animate_position(bullet.position+(bullet.forward*500), curve=curve.linear, duration=2)
        destroy(bullet, delay=2)

def update():
    global enemy
    if bullet and bullet.intersects(enemy).hit:
        hit_info = bullet.intersects(enemy)
        print('enemy hit')
        destroy(bullet, enemy)
        enemy = None

    if held_keys['left mouse']:
        weapon.active()
    else:
        weapon.passive()

app.run()

有什么错误吗,或者乌西娜被窃听了?另一个问题是如何将子弹的方向更改为玩家面对的方向


Tags: inpyselfinitliblocallineusers