2

I'm pretty new to coding in python but I have started a small little script. Its a first person shooter and right now I'm working on ammo boxes you can pick up. This is the code:

from ursina import *
from random import uniform
from ursina.shaders import lit_with_shadows_shader
from ursina.prefabs.first_person_controller import FirstPersonController

app = Ursina()
Entity.default_shader = lit_with_shadows_shader
player = FirstPersonController(model='cube', color=color.orange, origin_y=-.5, speed=8, position = (-52, 0, 57))
player.collider = BoxCollider(player, Vec3(0,1,0))
player.visible = False
ammo = 10
editor_camera = EditorCamera(enabled=False, ignore_paused=True)           
ammoCounter = Text(text= ammo + ammo, position = (-0.4,-0.4), color = color.black)

def update():
    ammoCounter.text = ammo 
def pause_input(key):
    if key == 'tab':    # press tab to toggle edit/play mode
        editor_camera.position = player.position 
        editor_camera.rotation = (90,180,0)
        editor_camera.y = 50
        editor_camera.enabled = not editor_camera.enabled
        player.visible =True
        player.visible_self = editor_camera.enabled
        player.cursor.enabled = not editor_camera.enabled
        gun.enabled = not editor_camera.enabled
        mouse.locked = not editor_camera.enabled
        application.paused = editor_camera.enabled
pause_handler = Entity(ignore_paused=True, input=pause_input)
    

def input(key):
    global ammo 
    
    
    
    ## destroy ammo crates##
    if key == 'e':
        if mouse.hovered_entity == ammo1: 
            if distance(player, ammo1) <= 5: 
                ammo += 10
                destroy(ammo1)
        if mouse.hovered_entity == ammo2: 
            if distance(player, ammo2) <= 5:
                ammo += 10
                destroy(ammo2)
        if mouse.hovered_entity == ammo3: 
            if distance(player, ammo3) <= 5:
                ammo += 10
                destroy(ammo3)
        if mouse.hovered_entity == ammo4: 
            if distance(player, ammo4) <= 5:
                ammo += 10
                destroy(ammo4)
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   

   
    if key == 'k':
        print (player.position)
        print (ammo)

    if key=="left mouse down" and ammo > 0:
        ammo -= 1
        if mouse.hovered_entity and hasattr(mouse.hovered_entity, 'hp'):
            mouse.hovered_entity.hp -= 10
            mouse.hovered_entity.blink(color.red)
            print (ammo)
        Audio("gunshot")
        if gun.position == (0.45,-0.2,0):
            Animation("spark", parent=camera.ui, fps=5, scale=.15, position=(0.28, -0.01), loop=False)
            print (ammo)
        if gun.position == (0,-0.15,0):
            Animation("spark", parent=camera.ui, fps=5, scale=.15, position=(-0.005,0.08,0), loop=False)
            print (ammo)
        
    if held_keys["q"]:
            gun.position = (0,-0.15,0)
            gun.rotation = (0,0,0)
            Cursor = False
            player.speed = 1
            if held_keys["shift"]:
                player.speed = 100
            
    else: 
        gun.position = (0.45,-0.2,0)
        gun.rotation = (5,30,0)
        player.speed = 250 * time.dt
    if held_keys["shift"]:
        player.speed = 400 * time.dt
        

            


        
        
## ammo refill class#

class AmmoR(Entity):
    def __init__(self, **kwargs):
        super().__init__(model='cube', texture = 'ammobox', scale = 1, origin_y=-0.5, collider = "mesh", **kwargs)

## amount of amo refill boxes##

ammo1 = AmmoR() 
ammo2 = AmmoR()
ammo3 = AmmoR()
ammo4 = AmmoR()
ammo1.position = (-9,2,53)
ammo2.position = (50,1,21)
ammo3.position = (-17,0,31)
ammo4.position = (15,0,8)


class Enemy(Entity):
    def __init__(self, **kwargs):
        super().__init__(model='cube', scale_y=2, origin_y=-0.5, color=color.light_gray, collider='box', **kwargs)
        self.health_bar = Entity(parent=self, y=1.2, model='cube', color=color.red, world_scale=(1.5,.1,.1))
        self.max_hp = 100
        self.hp = self.max_hp

    def update(self):
        dist = distance_xz(player.position, self.position)
        if dist > 40:
            return

        self.health_bar.alpha = max(0, self.health_bar.alpha - time.dt)


        self.look_at_2d(player.position, 'y')
        hit_info = raycast(self.world_position + Vec3(0,1,0), self.forward, 30, ignore=(self,))
        if hit_info.entity == player:
            if dist > 1:
                self.position += self.forward * time.dt * 5

    @property
    def hp(self):
        return self._hp

    @hp.setter
    def hp(self, value):
        self._hp = value
        if value <= 0:
            destroy(self)
            return

        self.health_bar.world_scale_x = self.hp / self.max_hp * 1.5
        self.health_bar.alpha = 1

## ENEMYS FIRST LEVEL
enemie1 = Enemy()
enemie2 = Enemy()
enemie3 = Enemy()
enemie4 = Enemy()
enemie5 = Enemy()
enemie6 = Enemy()
enemie7 = Enemy()
enemie8 = Enemy()
enemie9 = Enemy()
enemie10 = Enemy()
enemie11 = Enemy()

enemie1.position = (10,0,-2)
enemie2.position = (51.9096, 0, -57.6378)
enemie3.position = (53.7879, 0, -57.4827)
enemie4.position = (53.5366, 0, -39.0536)
enemie5.position = (50,0,-2)
enemie6.position = (50,0,-5)
enemie7.position = (22,0,-20)
enemie8.position = (55,0,-4)
enemie9.position = (15,0,-2)

Sky()
maze=Entity(model="maze", scale=10, texture="BK", color= color.gold,collider="mesh")


gun=Entity(model="colt", parent=camera.ui, scale=0.07, texture='tx', position=(0.45,-0.2,0), rotation=(5,30,0))





app.run()

The AmmoR class is the class for the ammo boxes and ammo1,ammo2,ammo3,and ammo4 are the ammo boxes. When I go to pick up the ammo boxes it works fine but when I try to pick up ammo2 Ursina Crashes giving me this error:

(3 aux display modules not yet loaded.)
:prc(warning): changing default value for ConfigVariable paste-emit-keystrokes from '1' to '0'.
:pnmimage:png(warning): iCCP: known incorrect sRGB profile
read obj at: c:\Users\MoerkerkeThomas\Desktop\Python stuff\RIP URSINA SHIT\fps game\models_compressed\maze.obj
read obj at: c:\Users\MoerkerkeThomas\Desktop\Python stuff\RIP URSINA SHIT\fps game\models_compressed\colt.obj
info: psd-tools3 not installed
warning: missing texture: 'tx' 
screen resolution: (1920, 1080)
os: Windows
development mode: True
application successfully started
info: changed aspect ratio: 1.778 -> 1.778
PS C:\Users\MoerkerkeThomas\Desktop\Python stuff\RIP URSINA SHIT\fps game>  c:; cd 'c:\Users\MoerkerkeThomas\Desktop\Python stuff\RIP URSINA SHIT\fps game'; & 'C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\python.exe' 'c:\Users\MoerkerkeThomas\.vscode\extensions\ms-python.python-2022.18.2\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '50586' '--' 'c:\Users\MoerkerkeThomas\Desktop\Python stuff\RIP URSINA SHIT\fps game\main game fps.py'
package_folder: C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\ursina
asset_folder: c:\Users\MoerkerkeThomas\Desktop\Python stuff\RIP URSINA SHIT\fps game
:prc(warning): Invalid integer value for ConfigVariable win-size: 864.0 
:prc(warning): Invalid integer value for ConfigVariable win-size: 1536.0
Known pipe types:
  wglGraphicsPipe
(3 aux display modules not yet loaded.)
:prc(warning): changing default value for ConfigVariable paste-emit-keystrokes from '1' to '0'.
:pnmimage:png(warning): iCCP: known incorrect sRGB profile
read obj at: c:\Users\MoerkerkeThomas\Desktop\Python stuff\RIP URSINA SHIT\fps game\models_compressed\maze.obj
read obj at: c:\Users\MoerkerkeThomas\Desktop\Python stuff\RIP URSINA SHIT\fps game\models_compressed\colt.obj
info: psd-tools3 not installed
warning: missing texture: 'tx' 
screen resolution: (1920, 1080)
os: Windows
development mode: True
application successfully started
info: changed aspect ratio: 1.778 -> 1.778
Assertion failed: !is_empty() at line 1045 of panda/src/pgraph/nodePath.cxx
Traceback (most recent call last):
  File "C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\direct\showbase\EventManager.py", line 49, in eventLoopTask
    self.doEvents()
  File "C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\direct\showbase\EventManager.py", line 43, in doEvents     
    processFunc(dequeueFunc())
  File "C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\direct\showbase\EventManager.py", line 99, in processEvent 
    messenger.send(eventName, paramList)
  File "C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\direct\showbase\Messenger.py", line 337, in send
    self.__dispatch(acceptorDict, event, sentArgs, foundWatch)
  File "C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\direct\showbase\Messenger.py", line 422, in __dispatch     
    result = method (*(extraArgs + sentArgs))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\ursina\main.py", line 176, in input
    __main__.input(key)
  File "c:\Users\MoerkerkeThomas\Desktop\Python stuff\RIP URSINA SHIT\fps game\main game fps.py", line 52, in input
    if distance(player, ammo4) <= 5:
       ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\ursina\ursinamath.py", line 24, in distance
    if hasattr(b, 'position'): b = b.position
       ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\ursina\entity.py", line 466, in position
    return Vec3(*self.getPos())
                 ^^^^^^^^^^^^^
AssertionError: !is_empty() at line 1045 of panda/src/pgraph/nodePath.cxx
:task(error): Exception occurred in PythonTask eventManager
Traceback (most recent call last):
  File "c:\Users\MoerkerkeThomas\Desktop\Python stuff\RIP URSINA SHIT\fps game\main game fps.py", line 197, in <module>
    app.run()
  File "C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\ursina\main.py", line 237, in run
    super().run()
  File "C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\direct\showbase\ShowBase.py", line 3330, in run
    self.taskMgr.run()
  File "C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\direct\task\Task.py", line 553, in run
    self.step()
  File "C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\direct\task\Task.py", line 504, in step
    self.mgr.poll()
  File "C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\direct\showbase\EventManager.py", line 49, in eventLoopTask
    self.doEvents()
  File "C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\direct\showbase\EventManager.py", line 43, in doEvents
    processFunc(dequeueFunc())
  File "C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\direct\showbase\EventManager.py", line 99, in processEvent
    messenger.send(eventName, paramList)
  File "C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\direct\showbase\Messenger.py", line 337, in send
    self.__dispatch(acceptorDict, event, sentArgs, foundWatch)
  File "C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\direct\showbase\Messenger.py", line 422, in __dispatch
    result = method (*(extraArgs + sentArgs))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\ursina\main.py", line 176, in input
    __main__.input(key)
  File "c:\Users\MoerkerkeThomas\Desktop\Python stuff\RIP URSINA SHIT\fps game\main game fps.py", line 52, in input
    if distance(player, ammo4) <= 5:
       ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\ursina\ursinamath.py", line 24, in distance
    if hasattr(b, 'position'): b = b.position
       ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MoerkerkeThomas\AppData\Local\Programs\Python\Python311\Lib\site-packages\ursina\entity.py", line 466, in position
    return Vec3(*self.getPos())
                 ^^^^^^^^^^^^^
AssertionError: !is_empty() at line 1045 of panda/src/pgraph/nodePath.cxx
PS C:\Users\MoerkerkeThomas\Desktop\Python stuff\RIP URSINA SHIT\fps game>

I have tried changing the the if function to be structured differently but I think the statement is correct. The only thing I could think of being the issue is the distance between objects variable.

Onewaypie
  • 31
  • 4
  • 1
    I think it is because of your update function. Have you tried using a try statement before your destroy and before using Amo, because it is destroyed – Lixt Dec 06 '22 at 18:54
  • Hey @Lixt, I'm pretty new so I don't exactly know how the try statement works, but it seems to be to test out code. I've run some other troubleshooting on it. What is don't understand is its crashing because ammo is the amount of ammo you have and Ammo1,2,3, and 4 are the separate ammo boxes. So, there shouldn't be any conflict in the code right? – Onewaypie Dec 07 '22 at 12:20
  • 1
    Hi onewaypie, I couldn't find where you defined ammo 1, 2, 3 and 4 so it may be the problem, but I will debug your code soon I get home. (I am on my phone) – Lixt Dec 07 '22 at 12:30
  • Here is where I think I defined it: ` ## amount of amo refill boxes## ammo1 = AmmoR() ammo2 = AmmoR() ammo3 = AmmoR() ammo4 = AmmoR() ammo1.position = (-9,2,53) ammo2.position = (50,1,21) ammo3.position = (-17,0,31) ammo4.position = (15,0,8) ` So I'm not sure. I may have defined it wrong. Thank you for helping me in this. – Onewaypie Dec 07 '22 at 12:34
  • 1
    Ok, I will check the full code when I get home – Lixt Dec 07 '22 at 12:48
  • 1
    couldn't reproduce the problem, but here is a possible solution: https://pastebin.com/2YLmGQNz – Lixt Dec 07 '22 at 20:06
  • @Lixt So what you sent seems to help. I notice I still have issues with your modified code but it acts normal and won't crash, I just get this message in the terminal: `Assertion failed: !is_empty() at line 1045 of panda/src/pgraph/nodePath.cxx !is_empty() at line 1045 of panda/src/pgraph/nodePath.cxx` – Onewaypie Dec 07 '22 at 20:30
  • 1
    yes. so the way try if statements work is you put the code you want to run and could fail in the try statement and after the code, put one tab down and put except Exception as e, and that way you can have the error but the program don't crash. – Lixt Dec 07 '22 at 20:47
  • Alright, thank you for helping me out with this! Really appreciate it. Now I am going to look to see if I can make a flashlight of sorts to my game but I've been working the whole day on that and can't get that to work so I'll have to work on that a bit more. Thanks for helping out! – Onewaypie Dec 07 '22 at 21:08

0 Answers0