extends KinematicBody2D #This line is used for keeping track of a score. var score : int = 0 #This section holds the values for speed, jumpforce, and gravity. #by writing codes with variables such as these "var" we can use a number later #and not have to write it over and over. Try filling these in with any number #you like, and try changing them later! var speed : int = 200 var jumpforce : int = 600 var gravity : int = 800 #This line is used to define a term we'll use lower in the script named "vel" #which we defined as a function of a vector, this is how we'll calculate speed var vel : Vector2 = Vector2() #These next few lines tell Godot that when we start the game we need it to #prepare the animations for the sprite, pull up our 'MainScene' #and show everything we put on it. onready var sprite = get_node("AnimatedSprite") onready var ui : Node = get_node("/root/MainScene/CanvasLayer/UI") #One last thing to define, how do we fail: when we fall off the side or run #into an enemy? func fail (): #This line says that when we tell the character to 'fail' #it will reload the scene. #The following comment ignores the fact that the returned value is not used # warning-ignore:return_value_discarded get_tree().reload_current_scene() #Now, we'll add the physics process. This will handle the movement and gravity. #By setting it to delta, we can store and change values! func _physics_process(delta): #To start, your speed is 0 - not moving on the x axis at all right or left vel.x = 0 #This 'if' statement says that if you fall below a certain point, your #character will fail, which we defined above if position.y > 1500: fail() #Movement inputs: This if statement says that if you press the button to #move left, make the player move negative (left) on the X axis at the rate #we called 'speed' if Input.is_action_pressed("move_left"): vel.x -= speed #This if statement says that if you press the button to #move right, make the player move positive (right) on the X axis at the rate #we called 'speed' if Input.is_action_pressed("move_right"): vel.x += speed #This section makes your character move #We're working with 'velocity' vectors (lines that are drawn with some value) vel = move_and_slide(vel, Vector2.UP) manage_animations() #Gravity vel.y += gravity * delta #Jump input: When you press jump, if your character is touching the floor #they will move negative on the Y axis, or up as fast as the variable #'jumpforce' at the top tells them to. if Input.is_action_just_pressed("jump") and is_on_floor(): vel.y -= jumpforce #Sprite Direction. This code looks to see if you're moving right or left, #and flips the character to match the direction. if vel.x < 0: sprite.flip_h = true elif vel.x > 0: sprite.flip_h = false #This part of the code helps us add points to our score to be displayed on screen #and plays a sound when we get a coin. func collect_coin (value): score += value ui.set_score_text(score) #Here's where we tell Godot what animation to play when func manage_animations (): #First, running - get the 'Run' animation if the player is not standing still #and is on the floor if vel.x != 0 and is_on_floor(): play_animation("run") #Otherwise, if the player is standing still get the 'Idle' animation elif vel.x == 0: play_animation("idle") #if however they're not on the floor, get the 'Jump' animation. if !is_on_floor(): play_animation("jump") #Now, play the animation you figured out you need. func play_animation (sprite_name): if sprite.animation !=sprite_name: sprite.play(sprite_name)