0

I'm creating a 2D game using Ursina Engine. While making the player shot, the shot intantiates fine, but it simply doesn't move. Tried using playerShot.y += vel* time.dt didn't worked. Tried using animate_position(), didn't worked correctly.

Here's the code:

mainLibs.py(imported on 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

If needed, ill send videos!

ursina_main.py(main game script)


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()

1 Answers1

0

The shots don't move because they aren't independent objects - you create a new one every time CreatePlayerShot() is called. You have to keep track of the bullets and update them independently:

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()

This creates a number of shots at once. You should also destroy the bullets once they leave the screen or hit something.

Jan Wilamowski
  • 3,308
  • 2
  • 10
  • 23