My try at Clean Code Challenge

Tried to keep this as simple as possible

extends AnimatedSprite

func update(movement):
    if movement.y < 0:
        play("jump")
    elif movement.x != 0: # Moving
        play("run")
    else:
        play("idle")
	
    # Flip the sprite when going left
    flip_h = (movement.x < 0)
1 Like

And here’s my Bunny code at this stage:

extends KinematicBody2D

const UP = Vector2(0, -1)
const SPEED = 750
const JUMP_SPEED = 1000
const GRAVITY = 2000
var motion = Vector2()

func _process(delta):
	$AnimatedSprite.update_animation(motion)

func _physics_process(delta):
	update_motion(delta)

func update_motion(delta):
	motion.x = 0
	fall(delta)
	run()
	jump()
	move_and_slide(motion, UP)

func fall(delta):
	if is_on_floor():
		motion.y = 0
	else:
		motion.y += GRAVITY * delta

func run():
	if Input.is_action_pressed("ui_right"):
		motion.x += SPEED
	if Input.is_action_pressed("ui_left"):
		motion.x -= SPEED

func jump():
	if Input.is_action_pressed("ui_up") and is_on_floor():
		motion.y -= JUMP_SPEED

Privacy & Terms