-1

THE PROBLEM I have a problem with ursina, showing a number for how many bullets the player has but it stops working after the first bullet...THANKS!

def input(key):
all_ammo=100
round_ammo=100
ammo=Text(
color=color.red,
scale=1,
position=(0.8,0.5)
)

if key=="left mouse down":
    round_ammo-=1
    ammo.text=f"{all_ammo} {round_ammo}"
Someone
  • 1
  • 2
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 15 '22 at 05:30
  • @Someone You don't need to share the full code, if you don't want, you can create a [example] – Alexander Aug 15 '22 at 05:35
  • @Alexander Dear Alexander, this a FPS game that I'm working on, the player has got 100 ammo and each time that the player shoots he/she will lost a bullet and there is a number up-right corner on the screen that show how many bullet she/he has!!! – Someone Aug 15 '22 at 05:39
  • @Someone the code you shared doesn't really do anything, other than defining a function (which happens to shadow the builtin `input()`) and then updating two values if some variable `key` is some value - there's nothing to go on here. – Grismar Aug 15 '22 at 05:46
  • @Someone The picture isn't really helpful... also in addition to grismar statement your indentation is off, so i have no clue if the `if statement` is supposed to be part of the function or not – Alexander Aug 15 '22 at 05:46

1 Answers1

1

I see two issues here. First of all, the code is not properly indented and is not valid Python.

The second issue is that you're creating a new Text every time you press a key. It's better to create it once, outside of the function and just set .text to update it. You're also resetting all_ammo and round_ammo to 100 each time you press a key. Again, this should be set outside of the input function so it doesn't get reset every time you press a key.

Example:

from ursina import *

app = Ursina()

gun = Entity(ammo=10) # use this to store the variable so we don't have to use the global keyword
text_entity = Text(text=f'ammo: {gun.ammo}')

def input(key):
    if key == 'left mouse down':
        gun.ammo -= 1
        text_entity.text = f'ammo: {gun.ammo}'

app.run()

pokepetter
  • 1,383
  • 5
  • 8