[Python] Why are other objects that should not move moving in the Python/Ursina engine?

Discussão em 'Python' iniciado por Stack, Agosto 16, 2025.

  1. Stack

    Stack Membro Participativo

    I am creating a game using the Ursina engine. I added a simple smoothfollow to my character. With Smoothfollow, I noticed that objects fixed to the camera's x and y coordinates (health bars, level counters, etc.) shifted slightly in the direction the character was moving, but when the character stopped, the objects returned to their original positions. it can be a simple problem but this problem really makes the game ugly. Here is the python code:

    from ursina import *
    import random

    app = Ursina()

    Mermiler = []
    back = Entity(model="quad", z = 4, scale=(70,40,1), texture="Back.png")
    mermiat = True
    mertim = 0
    can = 5
    kill = 0
    level = 0
    dushiz = 4

    class Dusman(Entity):
    def __init__(self,x,y):
    super().__init__()
    self.model = "quad"
    self.scale = (2.08,1.04)
    self.x = x
    self.y = y
    self.collider = "box"
    self.texture = "Dusman.png"

    def update(self):
    global can, oyun, Ens, kill, dushiz

    if oyun == True:
    if self.x > Player.x and self.y > Player.y:
    self.x -= dushiz * time.dt
    self.y -= dushiz * time.dt

    elif self.x > Player.x and self.y < Player.y:
    self.x -= dushiz * time.dt
    self.y += dushiz * time.dt

    elif self.x < Player.x and self.y > Player.y:
    self.x += dushiz * time.dt
    self.y -= dushiz * time.dt

    elif self.x < Player.x and self.y < Player.y:
    self.x += dushiz * time.dt
    self.y += dushiz * time.dt

    for mermi in Mermiler[:]:
    if self.intersects(mermi).hit:
    destroy(self)
    destroy(mermi)
    Mermiler.remove(mermi)
    Ens -= 1
    kill += 1

    if self.intersects(Player).hit:
    can -= 1
    destroy(self)
    Ens -=1

    class Mermi(Entity):
    def __init__(self):
    super().__init__()
    self.model = "quad"
    self.position = Player.position
    self.rotation_z = Player.rotation_z
    self.scale = (0.25,0.5)
    self.collider = "box"
    self.texture = "Mermi.png"

    class particle(Entity):
    def __init__(self,x,y):
    super().__init__()
    self. model = "quad"
    self.scale = 0.293
    self.x = x
    self.y = y
    self.z = -3
    self.texture = "Duman.png"

    def update(self):
    self.scale -= 0.19 * time.dt
    ax = random.uniform(-0.5,0.5)
    ay = random.uniform(-0.5,0.5)
    self.x += ax * 3.4 * time.dt
    self.y += ay * 3.4 * time.dt
    self.rotation_z += 2 * time.dt
    if self.scale <= 0.00005:
    destroy(self)

    class particle2(Entity):
    def __init__(self,x,y):
    super().__init__()
    self. model = "quad"
    self.scale = 0.293
    self.x = x
    self.y = y
    self.z = -3
    self.texture = "particle2.png"

    def update(self):
    self.scale -= 0.19 * time.dt
    ax = random.uniform(-0.5,0.5)
    ay = random.uniform(-0.5,0.5)
    self.x += ax * 3.4 * time.dt
    self.y += ay * 3.4 * time.dt
    self.rotation_z += 2 * time.dt
    if self.scale <= 0.00005:
    destroy(self)

    class Karakter(Entity):
    def __init__(self):
    super().__init__()
    self.model = "quad"
    self.position = (0,0)
    self.scale = (1.5,1.5)
    self.collider = "box"
    self.texture = "player.png"

    def update(self):
    global mermiat, mertim, oyun, a, b
    mertim += time.dt
    camera.x = self.x
    camera.y = self.y
    if oyun == True:
    if held_keys["w"]:
    self.y += 18 * time.dt
    self.rotation_z = 0

    elif held_keys["s"]:
    self.y -= 18 * time.dt
    self.rotation_z = 180

    elif held_keys["d"]:
    self.x += 18 * time.dt
    self.rotation_z = 90

    elif held_keys["a"]:
    self.x -= 18 * time.dt
    self.rotation_z = 270

    if held_keys["space"] and mermiat:
    mermi = Mermi()
    Mermiler.append(mermi)
    mermiat = False
    if self.rotation_z == 0:
    particle(self.x,self.y+1.2)
    particle(self.x,self.y+1.2)
    particle(self.x,self.y+1.2)
    particle(self.x,self.y+1.2)
    particle(self.x,self.y+1.2)
    particle(self.x,self.y+1.2)
    particle(self.x,self.y+1.2)
    particle(self.x,self.y+1.2)
    particle(self.x,self.y+1.2)
    particle2(self.x,self.y+1.2)
    particle2(self.x,self.y+1.2)
    particle2(self.x,self.y+1.2)

    elif self.rotation_z == 90:
    particle(self.x+1.2,self.y)
    particle(self.x+1.2,self.y)
    particle(self.x+1.2,self.y)
    particle(self.x+1.2,self.y)
    particle(self.x+1.2,self.y)
    particle(self.x+1.2,self.y)
    particle(self.x+1.2,self.y)
    particle(self.x+1.2,self.y)
    particle(self.x+1.2,self.y)
    particle2(self.x+1.2,self.y)
    particle2(self.x+1.2,self.y)
    particle2(self.x+1.2,self.y)

    elif self.rotation_z == 180:
    particle(self.x,self.y-1.2)
    particle(self.x,self.y-1.2)
    particle(self.x,self.y-1.2)
    particle(self.x,self.y-1.2)
    particle(self.x,self.y-1.2)
    particle(self.x,self.y-1.2)
    particle(self.x,self.y-1.2)
    particle(self.x,self.y-1.2)
    particle(self.x,self.y-1.2)
    particle2(self.x,self.y-1.2)
    particle2(self.x,self.y-1.2)
    particle2(self.x,self.y-1.2)

    elif self.rotation_z == 270:
    particle(self.x-1.2,self.y)
    particle(self.x-1.2,self.y)
    particle(self.x-1.2,self.y)
    particle(self.x-1.2,self.y)
    particle(self.x-1.2,self.y)
    particle(self.x-1.2,self.y)
    particle(self.x-1.2,self.y)
    particle(self.x-1.2,self.y)
    particle(self.x-1.2,self.y)
    particle2(self.x-1.2,self.y)
    particle2(self.x-1.2,self.y)
    particle2(self.x-1.2,self.y)

    if self.x > 34 and held_keys["d"]:
    self.x = 30

    elif self.x < -34 and held_keys["a"]:
    self.x = -30

    elif self.y > 19 and held_keys["w"]:
    self.y = 15

    elif self.y < -19 and held_keys["s"]:
    self.y = -15

    if mermiat == False and mertim >= 1.0:
    mermiat = True
    mertim = 0

    elif mermiat == True and mertim >= 1.0:
    mertim = 0

    if mermiat == True:
    BulletBar.scale_x = 12

    if mermiat == False:
    BulletBar.scale_x = mertim * 10

    def SpawnEnemy1():
    global oyun, Ens
    if oyun == True:
    rx = random.uniform(-35.0,35.0)
    Dusman(x=rx,y=20)
    Ens += 1

    def SpawnEnemy2():
    global oyun, Ens
    if oyun == True:
    rx = random.uniform(-35.0,35.0)
    Dusman(x=rx,y=-20)
    Ens += 1

    def SpawnEnemy3():
    global oyun, Ens
    if oyun == True:
    ry = random.uniform(-20.0,20.0)
    Dusman(x=35,y=ry)
    Ens +=1

    def SpawnEnemy4():
    global oyun, Ens
    if oyun == True:
    ry = random.uniform(-20.0,20.0)
    Dusman(x=-35,y=ry)
    Ens += 1

    spwn1 = 0
    spwn2 = 0
    spwn3 = 0
    spwn4 = 0

    Player = Karakter()
    BulletBar = Entity(model = "quad", position = (-13,-9,-2),scale=(12,0.5,1), color="FFFF00", origin=(-0.5,0,0))
    HealthBar = Entity(model = "quad", position = (-13,-7.5,-2),scale=(12,1,1), color="FF0000", origin=(-0.5,0,0))
    levelgos = Entity(model="quad", position=(-16,10,-2),scale=(12.3,3.3,1), texture="level-1.png")
    gamewon = Entity(model="quad", position=(0,0), texture="Bink.png")
    oyun = True
    Ens = 0

    spawnspeed = 6

    def update():
    global spwn1, spwn2, spwn3, spwn4, can, oyun, Ens, kill, level, spawnspeed, dushiz
    BulletBar.x = camera.x - 13
    BulletBar.y = camera.y - 9
    HealthBar.x = camera.x - 13
    HealthBar.y = camera.y - 7.5

    HealthBar.scale_x = can * 2.4

    spwn1 += time.dt
    spwn2 += time.dt
    spwn3 += time.dt
    spwn4 += time.dt

    level = (kill // 20) + 1

    levelgos.x = camera.x - 15
    levelgos.y = camera.y + 10.2

    for mermi in Mermiler[:]:
    if abs(mermi.x) > (abs(Player.x) + 5) or abs(mermi.y) > (abs(Player.y) + 5):
    Mermiler.remove(mermi)
    destroy(mermi)

    else:
    if mermi.rotation_z == 0:
    mermi.y += 4 * time.dt

    elif mermi.rotation_z == 180:
    mermi.y -= 4 * time.dt

    elif mermi.rotation_z == 90:
    mermi.x += 4 * time.dt

    elif mermi.rotation_z == 270:
    mermi.x -= 4 * time.dt

    if spwn1 >= spawnspeed:
    spwn1 = 0
    if Ens <= 36:
    SpawnEnemy1()

    if spwn2 >= spawnspeed:
    if Ens <= 36:
    SpawnEnemy2()
    spwn2 = 0

    if spwn3 >= spawnspeed:
    if Ens<= 36:
    SpawnEnemy3()
    spwn3 = 0

    if spwn4 >= spawnspeed:
    if Ens <= 36:
    SpawnEnemy4()
    spwn4 = 0

    if can <= 0:
    gamewon.x = camera.x
    gamewon.y = camera.y - 2
    gamewon.texture="game-over.png"
    gamewon.scale = (18,12,1)
    oyun = False

    else:
    oyun = True

    if level == 1:
    spawnspeed = 6
    dushiz = 4
    levelgos.texture = "level-1.png"

    elif level == 2:
    spawnspeed = 5.9
    dushiz = 4.1
    levelgos.texture = "level-2.png"

    elif level == 3:
    spawnspeed = 5.8
    dushiz = 4.2
    levelgos.texture = "level-3.png"

    elif level == 4:
    spawnspeed = 5.7
    dushiz = 4.3
    levelgos.texture = "level-4.png"

    elif level == 5:
    spawnspeed = 5.6
    dushiz = 4.4
    levelgos.texture = "level-5.png"

    elif level == 6:
    spawnspeed = 5.5
    dushiz = 4.5
    levelgos.texture = "level-6.png"

    elif level == 7:
    spawnspeed = 5.4
    dushiz = 4.6
    levelgos.texture = "level-7.png"

    elif level == 8:
    spawnspeed = 5.3
    dushiz = 4.7
    levelgos.texture = "level-8.png"

    elif level == 9:
    spawnspeed = 5.2
    dushiz = 4.8
    levelgos.texture = "level-9.png"

    elif level == 10:
    spawnspeed = 5
    dushiz = 5
    levelgos.texture = "level-10.png"

    elif level == 11:
    gamewon.x = camera.x
    gamewon.y = camera.y - 2
    gamewon.texture="you-won.png"
    gamewon.scale = (18,12,1)
    oyun = False

    camera.z = -60

    app.run()

    Continue reading...

Compartilhe esta Página