0

我正在使用 Ursina 引擎创建一个 2D 游戏。在让玩家投篮时,投篮很好,但它根本不动。尝试使用playerShot.y += vel* time.dt没有奏效。尝试使用animate_position(),没有正常工作。

这是代码:

mainLibs.py(在 ursina_main.py 上导入)


    from ursina import *


def update():
    CreatePlayerShot()


def CreatePlayerShot(x, y, sx, sy, vel, ang):
   playerBullet = Entity(model = 'quad', scale = (sx, sy), position = (x, y))
   playerBullet.rotation_z = ang
   playerBullet.y += vel * time.dt
    yield

如果需要,生病发送视频!

ursina_main.py(主游戏脚本)


from ursina import *

from mainLibs import *

app = Ursina()

can_shoot = True
last_shot = 0

fullScreenBool = True
playerVel = float
player = Entity(model = 'quad', texture = 'reimuIdle.png', scale = (0.6, 1), position = (0, -4))
hitbox = Entity(model = 'circle', color = color.blue, scale = (0.15, 0.15), position = (0, 0))
ui = Entity(model = 'quad', texture = 'gameGUIStg.png', scale = (15, 11))

player.double_sided = True

window.title = "Touhou Test"
window.fullscreen = False
window.borderless = True

def update():
   movement()
   image()
   slow()
   shot()
   hitbox.x = player.x
   hitbox.y = player.y
   global can_shoot, last_shot
   if not can_shoot:
      last_shot += time.dt
      if last_shot > 600:  # 1 second
         can_shoot = True

    




def movement():
   if held_keys['left arrow'] and player.x > -4.25:
      player.x += -playerVel * time.dt

   if held_keys['right arrow'] and player.x < 3.90:
      player.x += playerVel * time.dt

   if held_keys['up arrow'] and player.y < 4.25:
      player.y += playerVel * time.dt

   if held_keys['down arrow']and player.y >-4.40:
      player.y += -playerVel * time.dt


def image():
   if held_keys['left arrow']:
      player.rotation = (0, 0, 0)
      player.texture = 'reimuMove.png'

   elif held_keys['right arrow']:
      player.rotation = (0, 180, 0)
      player.texture = 'reimuMove.png'

   else:
      player.texture = 'reimuIdle'


def slow():
   if held_keys['left shift']:
      global playerVel
      playerVel = 1.7

   else:
      playerVel = 3.3


def shot():
   if held_keys['z'] and can_shoot == True:
      CreatePlayerShot(player.x, player.y, 0.1, 0.2, 10, 0)
      

app.run()
4

1 回答 1

0

镜头不会移动,因为它们不是独立的对象 - 每次CreatePlayerShot()调用时您都会创建一个新对象。您必须跟踪项目符号并独立更新它们:

class Bullet(Entity):
    def __init__(self, x, y, sx, sy, vel, ang):
        super().__init__(model = 'quad', scale = (sx, sy), position = (x, y))
        self.vel = vel

    def update(self):
        self.y += self.vel * time.dt

bullets = []

def shot():
    if held_keys['z'] and can_shoot == True:
        bullet = Bullet(player.x, player.y, 0.1, 0.2, 10, 0)
        bullets.append(bullet)

    for bullet in bullets:
        bullet.update()

这会一次创建多个镜头。一旦子弹离开屏幕或击中某物,您还应该销毁它们。

于 2022-01-30T03:37:45.890 回答