Finish coding_the_player of first 2D game

This commit is contained in:
Daniel Siepmann 2024-03-26 12:54:53 +01:00
parent c64931fd86
commit 8cdfeb2bf0
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
3 changed files with 86 additions and 1 deletions

57
first_2d_game/Player.gd Normal file
View file

@ -0,0 +1,57 @@
extends Area2D
signal hit
## How fast the player will move. In pixels/sec
@export var speed = 400
# Size of the game window.
var screen_size
# Called when the node enters the scene tree for the first time.
func _ready():
screen_size = get_viewport_rect().size
hide()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
var velocity = Vector2.ZERO
if Input.is_action_pressed('move_right'):
velocity.x += 1
if Input.is_action_pressed('move_left'):
velocity.x -= 1
if Input.is_action_pressed('move_down'):
velocity.y += 1
if Input.is_action_pressed('move_up'):
velocity.y -= 1
if velocity.length() > 0:
velocity = velocity.normalized() * speed
$AnimatedSprite2D.play()
else:
$AnimatedSprite2D.stop()
position += velocity * delta
position = position.clamp(Vector2.ZERO, screen_size)
if velocity.x != 0:
$AnimatedSprite2D.animation = 'walk'
$AnimatedSprite2D.flip_v = false
$AnimatedSprite2D.flip_h = velocity.x < 0
elif velocity.y != 0:
$AnimatedSprite2D.animation = 'up'
$AnimatedSprite2D.flip_v = velocity.y > 0
func _on_body_entered(body):
hide()
hit.emit()
$CollisionShape2D.set_deffered('disabled', true)
func start(pos):
position = pos
show()
$CollisionShape2D.disabled = false

View file

@ -1,6 +1,7 @@
[gd_scene load_steps=7 format=3 uid="uid://ckfhsdod4ghug"] [gd_scene load_steps=8 format=3 uid="uid://ckfhsdod4ghug"]
[ext_resource type="Texture2D" uid="uid://cj2a3ls4jlkjk" path="res://art/playerGrey_up1.png" id="1_fur7b"] [ext_resource type="Texture2D" uid="uid://cj2a3ls4jlkjk" path="res://art/playerGrey_up1.png" id="1_fur7b"]
[ext_resource type="Script" path="res://Player.gd" id="1_t1lbd"]
[ext_resource type="Texture2D" uid="uid://cx65oby2yhlus" path="res://art/playerGrey_up2.png" id="2_vhw1d"] [ext_resource type="Texture2D" uid="uid://cx65oby2yhlus" path="res://art/playerGrey_up2.png" id="2_vhw1d"]
[ext_resource type="Texture2D" uid="uid://d05l3yu0vp17w" path="res://art/playerGrey_walk1.png" id="3_wjp5t"] [ext_resource type="Texture2D" uid="uid://d05l3yu0vp17w" path="res://art/playerGrey_walk1.png" id="3_wjp5t"]
[ext_resource type="Texture2D" uid="uid://c2ndsc14p6skg" path="res://art/playerGrey_walk2.png" id="4_bmoxy"] [ext_resource type="Texture2D" uid="uid://c2ndsc14p6skg" path="res://art/playerGrey_walk2.png" id="4_bmoxy"]
@ -35,6 +36,8 @@ radius = 27.0
height = 68.0 height = 68.0
[node name="Player" type="Area2D"] [node name="Player" type="Area2D"]
position = Vector2(98, 106)
script = ExtResource("1_t1lbd")
metadata/_edit_group_ = true metadata/_edit_group_ = true
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."] [node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
@ -44,3 +47,5 @@ animation = &"walk"
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CapsuleShape2D_lftd4") shape = SubResource("CapsuleShape2D_lftd4")
[connection signal="body_entered" from="." to="." method="_on_body_entered"]

View file

@ -20,6 +20,29 @@ window/size/viewport_width=480
window/size/viewport_height=720 window/size/viewport_height=720
window/stretch/mode="canvas_items" window/stretch/mode="canvas_items"
[input]
move_right={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"echo":false,"script":null)
]
}
move_left={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194319,"key_label":0,"unicode":0,"echo":false,"script":null)
]
}
move_down={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194322,"key_label":0,"unicode":0,"echo":false,"script":null)
]
}
move_up={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194320,"key_label":0,"unicode":0,"echo":false,"script":null)
]
}
[rendering] [rendering]
renderer/rendering_method="gl_compatibility" renderer/rendering_method="gl_compatibility"