0% found this document useful (0 votes)
36 views

Learning in Programming

Uploaded by

api-464640560
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Learning in Programming

Uploaded by

api-464640560
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CTRL + K to comment a line

lerp= learning interpolation


Motion.x = lerp(from, to, percentage)

Learned that you can make an animated sprite without making a children called “animated
sprite” just make a new child to the sprite called “animation ” and it accomplishes the same
thing.

Left and right running animations


Falling and jumping animations.

extends KinematicBody2D

const UP = Vector2 (0,-1)


const GRAVITY = 20
const MAXFALLSPEED = 200
const MAXSPEED = 80
const JUMP = 300
const ACCELERATION = 10

var motion = Vector2()


var facing_right = true

func _ready():
pass

func _physics_process(delta):

motion.y += GRAVITY
if motion.y > MAXFALLSPEED:
motion.y = MAXFALLSPEED

if facing_right == true:
$Sprite.scale.x = 1
else:
$Sprite.scale.x = -1

motion.x = clamp(motion.x, -MAXSPEED,MAXSPEED)

if Input.is_action_pressed("right"):
motion.x += ACCELERATION
facing_right = true
$AnimationPlayer.play("run")
elif Input.is_action_pressed("left"):
motion.x -= ACCELERATION
facing_right = false
$AnimationPlayer.play("run")
else:
motion.x = lerp(motion.x,0,0.2)
$AnimationPlayer.play("idle")

if is_on_floor():
if Input.is_action_just_pressed("jump"):
motion.y = -JUMP

if !is_on_floor():
if motion.y < 0:
$AnimationPlayer.play("jump")
elif motion.y > 0:
$AnimationPlayer.play("falling")

motion = move_and_slide(motion, UP)

___________________________________________

extends KinematicBody2D

const UP = Vector2 (0,-1)


const GRAVITY = 20
const MAXFALLSPEED = 200
const MAXSPEED = 80
const JUMP = 300
const ACCELERATION = 10
const WALL_SLIDE_ACCELERATION = 10
const MAX_WALL_SLIDE_SPEED = 120

var motion = Vector2()


var facing_right = true
var can_jump = false
var double_jump = 0
var max_double_jump = 1

func _ready():
pass

func _physics_process(delta):

motion.y += GRAVITY
if motion.y > MAXFALLSPEED:
motion.y = MAXFALLSPEED

if facing_right == true:
$Sprite.scale.x = 1
else:
$Sprite.scale.x = -1

motion.x = clamp(motion.x, -MAXSPEED,MAXSPEED)

if Input.is_action_pressed("right"):
motion.x += ACCELERATION
facing_right = true
$AnimationPlayer.play("run")
elif Input.is_action_pressed("left"):
motion.x -= ACCELERATION
facing_right = false
$AnimationPlayer.play("run")
else:
motion.x = lerp(motion.x,0,0.2)
$AnimationPlayer.play("idle")

if is_on_floor():
can_jump = true
if Input.is_action_just_pressed("jump"):
motion.y = -JUMP

if !is_on_floor():
if motion.y < 0:
$AnimationPlayer.play("jump")
elif motion.y > 0:
$AnimationPlayer.play("falling")

if is_on_wall() && (Input.is_action_pressed("right") || Input.is_action_pressed("left")):


can_jump = true
double_jump = max_double_jump
if motion.y >= 0:
motion.y = min(motion.y + WALL_SLIDE_ACCELERATION,
MAX_WALL_SLIDE_SPEED)
else:
motion.y =+ GRAVITY
else:
motion.y =+ GRAVITY

motion = move_and_slide(motion, UP)

Snake Game

Creating an Atlus to make sprites

randi() to generate a random number


Using randomize() will ensure you dont get the same random number twice

You might also like