@jackattack
here is my script example. sorry it took a while lol. its not very clean but i think it should be easy to follow. let me know if you have any questions. you should benable to use thenSprite class to create other objects and as you will see this approach doesnt use the update method at all or depend on position.
from scene import *
from time import sleep
import sound
import random
import math
A = Action
Character_Male='IMG_3039.jpg'
def get_texture(sheet, x, y, w, h):
'''
for cleaner Code
'''
return Texture(sheet).subtexture(Rect(x, y, w, h))
class Direction:
'''
simple Constants
'''
NORTH="north"
SOUTH="south"
EAST="east"
WEST="west"
class Sprite(SpriteNode):
'''
Use this class for game objects
'''
def __init__(self,sprite_sheet, *args, **kwargs):
self.sprite_sheet=sprite_sheet
self.animations=dict(
{"idle":get_texture(self.sprite_sheet, 0.00, 0.75, 0.33, 0.25)})
SpriteNode.__init__(self, texture=self.animations["idle"], *args, **kwargs)
self.movement_speed=50
self.active_direction=Direction.SOUTH
self.Texture_Group(
"move-north",
[
get_texture(self.sprite_sheet, 0.00, 0.75, 0.33, 0.25),
get_texture(self.sprite_sheet, 0.33, 0.75, 0.33, 0.25),
get_texture(self.sprite_sheet, 0.66, 0.75, 0.33, 0.25)
])
self.Texture_Group(
"move-south",
[
get_texture(self.sprite_sheet, 0.00, 0.50, 0.33, 0.25),
get_texture(self.sprite_sheet, 0.33, 0.50, 0.33, 0.25),
get_texture(self.sprite_sheet, 0.66, 0.50, 0.33, 0.25)
])
self.Texture_Group(
"move-east",
[
get_texture(self.sprite_sheet, 0.00, 0.25, 0.33, 0.25),
get_texture(self.sprite_sheet, 0.33, 0.25, 0.33, 0.25),
get_texture(self.sprite_sheet, 0.66, 0.25, 0.33, 0.25)
])
self.Texture_Group(
"move-west",
[
get_texture(self.sprite_sheet, 0.00, 0.00, 0.33, 0.25),
get_texture(self.sprite_sheet, 0.33, 0.00, 0.33, 0.25),
get_texture(self.sprite_sheet, 0.66, 0.00, 0.33, 0.25)
])
# decorator alows the use of time.sleep() without blocking
@ui.in_background
def Run_Animation(self, tag):
for x in self.animations[f"{tag}-{self.active_direction}"]:
self.texture = x
sleep(0.2)
self.texture=self.animations[f"{tag}-{self.active_direction}"][0]
def Texture_Group(self, tag, texture_list):
self.animations[tag]=texture_list
def Change_Direction(self, direction):
self.active_direction=direction
if direction == Direction.SOUTH:
self.velocity=(0.0, -self.movement_speed)
elif direction == Direction.NORTH:
self.velocity=(0.0, self.movement_speed)
elif direction == Direction.EAST:
self.velocity=(-self.movement_speed, 0.0)
elif direction == Direction.WEST:
self.velocity=(self.movement_speed, 0.0)
class Button(SpriteNode):
'''
Simple Button Object
'''
def __init__(self, text, meta, action=None, *args, **kwargs):
SpriteNode.__init__(self, Texture('pzl:Gray7'), *args, **kwargs)
self.texture=Texture('pzl:Gray7')
self.action=action
self.text=LabelNode(text, anchor_point=(0.5, 0.5), color="yellow", parent=self)
self.meta=meta
# Not used in this example but can be lol
def Pressed(self):
self.action(self)
class MyScene (Scene):
def setup(self):
self.screen="game"
self.background_color='black'
self.buttons=[]
self.player=Sprite(Character_Male, position=self.size/2, scale=4, parent=self)
self.n = -1
self.up_button=Button(
"↑",
Direction.NORTH,
position=Point(get_screen_size()[0]/4*3, 270),
scale=2,
parent=self)
self.buttons.append(self.up_button)
self.down_button=Button(
"↓",
Direction.SOUTH,
position=Point(get_screen_size()[0]/4*3, 130),
scale=2,
parent=self)
self.buttons.append(self.down_button)
self.right_button=Button(
"→",
Direction.WEST,
position=Point(get_screen_size()[0]/4*3+70, 200),
scale=2,
parent=self)
self.buttons.append(self.right_button)
self.left_button=Button(
"←",
Direction.EAST,
position=Point(get_screen_size()[0]/4*3-70, 200),
scale=2,
parent=self)
self.buttons.append(self.left_button)
def touch_began(self, touch):
for btn in self.buttons:
if touch.location in btn.bbox:
self.player.Change_Direction(btn.meta)
self.player.run_action(
Action.move_by(
self.player.velocity[0],
self.player.velocity[1]))
self.player.Run_Animation("move")
def touch_ended(self, touch):
pass
if __name__ == '__main__':
run(MyScene(), show_fps=True)