Update Pong with C# to Godot 4.2.1 (#966)

This commit is contained in:
behrooz bozorgchamy
2024-03-25 16:25:51 +01:00
committed by GitHub
parent 71eea49eba
commit 523c7d34c0
8 changed files with 72 additions and 61 deletions

View File

@@ -7,17 +7,17 @@ public partial class Ball : Area2D
public Vector2 direction = Vector2.Left; public Vector2 direction = Vector2.Left;
private Vector2 _initialPos; private Vector2 _initialPos;
private float _speed = DefaultSpeed; private double _speed = DefaultSpeed;
public override void _Ready() public override void _Ready()
{ {
_initialPos = Position; _initialPos = Position;
} }
public override void _Process(float delta) public override void _Process(double delta)
{ {
_speed += delta * 2; _speed += delta * 2;
Position += _speed * delta * direction; Position += (float)(_speed * delta) * direction;
} }
public void Reset() public void Reset()

View File

@@ -12,19 +12,19 @@ public partial class Paddle : Area2D
public override void _Ready() public override void _Ready()
{ {
string name = Name.ToLower(); string name = Name.ToString().ToLower();
_up = name + "_move_up"; _up = name + "_move_up";
_down = name + "_move_down"; _down = name + "_move_down";
_ballDir = name == "left" ? 1 : -1; _ballDir = name == "left" ? 1 : -1;
} }
public override void _Process(float delta) public override void _Process(double delta)
{ {
// Move up and down based on input. // Move up and down based on input.
float input = Input.GetActionStrength(_down) - Input.GetActionStrength(_up); float input = Input.GetActionStrength(_down) - Input.GetActionStrength(_up);
Vector2 position = Position; // Required so that we can modify position.y. Vector2 position = Position; // Required so that we can modify position.y.
position += new Vector2(0, input * MoveSpeed * delta); position += new Vector2(0, input * MoveSpeed * (float)delta);
position.y = Mathf.Clamp(position.y, 16, GetViewportRect().Size.y - 16); position = new(position.X, Mathf.Clamp(position.Y, 16, GetViewportRect().Size.X - 16));
Position = position; Position = position;
} }

View File

@@ -1,6 +1,7 @@
<Project Sdk="Godot.NET.Sdk/4.0.0-dev5"> <Project Sdk="Godot.NET.Sdk/4.2.1">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net472</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
<RootNamespace>Pong</RootNamespace> <RootNamespace>Pong</RootNamespace>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@@ -2,7 +2,7 @@
importer="texture" importer="texture"
type="CompressedTexture2D" type="CompressedTexture2D"
uid="uid://dy8ya5bkuar1e" uid="uid://13ht46tyr4ae"
path="res://.godot/imported/ball.png-9a4ca347acb7532f6ae347744a6b04f7.ctex" path="res://.godot/imported/ball.png-9a4ca347acb7532f6ae347744a6b04f7.ctex"
metadata={ metadata={
"vram_texture": false "vram_texture": false

View File

@@ -2,7 +2,7 @@
importer="texture" importer="texture"
type="CompressedTexture2D" type="CompressedTexture2D"
uid="uid://bssbicievt24u" uid="uid://cgbjbhglgxf0k"
path="res://.godot/imported/paddle.png-0e798fb0912613386507c9904d5cc01a.ctex" path="res://.godot/imported/paddle.png-0e798fb0912613386507c9904d5cc01a.ctex"
metadata={ metadata={
"vram_texture": false "vram_texture": false

View File

@@ -1,24 +1,24 @@
[gd_scene load_steps=12 format=2] [gd_scene load_steps=12 format=3 uid="uid://bufr8nynnqrj"]
[ext_resource path="res://Logic/Paddle.cs" type="Script" id=1] [ext_resource type="Script" path="res://Logic/Paddle.cs" id="1"]
[ext_resource path="res://paddle.png" type="Texture2D" id=2] [ext_resource type="Texture2D" uid="uid://cgbjbhglgxf0k" path="res://paddle.png" id="2"]
[ext_resource path="res://Logic/Ball.cs" type="Script" id=4] [ext_resource type="Script" path="res://Logic/Ball.cs" id="4"]
[ext_resource path="res://ball.png" type="Texture2D" id=5] [ext_resource type="Texture2D" uid="uid://13ht46tyr4ae" path="res://ball.png" id="5"]
[ext_resource path="res://separator.png" type="Texture2D" id=6] [ext_resource type="Texture2D" uid="uid://cim6es185kfto" path="res://separator.png" id="6"]
[ext_resource path="res://Logic/Wall.cs" type="Script" id=7] [ext_resource type="Script" path="res://Logic/Wall.cs" id="7"]
[ext_resource path="res://Logic/CeilingFloor.cs" type="Script" id=8] [ext_resource type="Script" path="res://Logic/CeilingFloor.cs" id="8"]
[sub_resource type="RectangleShape2D" id=1] [sub_resource type="RectangleShape2D" id="1"]
extents = Vector2(4, 16) size = Vector2(4, 16)
[sub_resource type="RectangleShape2D" id=2] [sub_resource type="RectangleShape2D" id="2"]
extents = Vector2(4, 4) size = Vector2(4, 4)
[sub_resource type="RectangleShape2D" id=3] [sub_resource type="RectangleShape2D" id="3"]
extents = Vector2(10, 200) size = Vector2(10, 200)
[sub_resource type="RectangleShape2D" id=4] [sub_resource type="RectangleShape2D" id="4"]
extents = Vector2(320, 10) size = Vector2(320, 10)
[node name="Pong" type="Node2D"] [node name="Pong" type="Node2D"]
@@ -30,69 +30,69 @@ color = Color(0.141176, 0.152941, 0.164706, 1)
[node name="Left" type="Area2D" parent="."] [node name="Left" type="Area2D" parent="."]
modulate = Color(0, 1, 1, 1) modulate = Color(0, 1, 1, 1)
position = Vector2(67.6285, 192.594) position = Vector2(67.6285, 192.594)
script = ExtResource( 1 ) script = ExtResource("1")
[node name="Sprite2D" type="Sprite2D" parent="Left"] [node name="Sprite2D" type="Sprite2D" parent="Left"]
texture = ExtResource( 2 ) texture = ExtResource("2")
[node name="Collision" type="CollisionShape2D" parent="Left"] [node name="Collision" type="CollisionShape2D" parent="Left"]
shape = SubResource( 1 ) shape = SubResource("1")
[node name="Right" type="Area2D" parent="."] [node name="Right" type="Area2D" parent="."]
modulate = Color(1, 0, 1, 1) modulate = Color(1, 0, 1, 1)
position = Vector2(563.815, 188.919) position = Vector2(563.815, 188.919)
script = ExtResource( 1 ) script = ExtResource("1")
[node name="Sprite2D" type="Sprite2D" parent="Right"] [node name="Sprite2D" type="Sprite2D" parent="Right"]
texture = ExtResource( 2 ) texture = ExtResource("2")
[node name="Collision" type="CollisionShape2D" parent="Right"] [node name="Collision" type="CollisionShape2D" parent="Right"]
shape = SubResource( 1 ) shape = SubResource("1")
[node name="Ball" type="Area2D" parent="."] [node name="Ball" type="Area2D" parent="."]
position = Vector2(320.5, 191.124) position = Vector2(320.5, 191.124)
script = ExtResource( 4 ) script = ExtResource("4")
[node name="Sprite2D" type="Sprite2D" parent="Ball"] [node name="Sprite2D" type="Sprite2D" parent="Ball"]
texture = ExtResource( 5 ) texture = ExtResource("5")
[node name="Collision" type="CollisionShape2D" parent="Ball"] [node name="Collision" type="CollisionShape2D" parent="Ball"]
shape = SubResource( 2 ) shape = SubResource("2")
[node name="Separator" type="Sprite2D" parent="."] [node name="Separator" type="Sprite2D" parent="."]
position = Vector2(320, 200) position = Vector2(320, 200)
texture = ExtResource( 6 ) texture = ExtResource("6")
[node name="Node2D" type="Node2D" parent="."] [node name="Node2D" type="Node2D" parent="."]
[node name="LeftWall" type="Area2D" parent="."] [node name="LeftWall" type="Area2D" parent="."]
position = Vector2(-10, 200) position = Vector2(-10, 200)
script = ExtResource( 7 ) script = ExtResource("7")
[node name="Collision" type="CollisionShape2D" parent="LeftWall"] [node name="Collision" type="CollisionShape2D" parent="LeftWall"]
shape = SubResource( 3 ) shape = SubResource("3")
[node name="RightWall" type="Area2D" parent="."] [node name="RightWall" type="Area2D" parent="."]
position = Vector2(650, 200) position = Vector2(650, 200)
script = ExtResource( 7 ) script = ExtResource("7")
[node name="Collision" type="CollisionShape2D" parent="RightWall"] [node name="Collision" type="CollisionShape2D" parent="RightWall"]
shape = SubResource( 3 ) shape = SubResource("3")
[node name="Ceiling" type="Area2D" parent="."] [node name="Ceiling" type="Area2D" parent="."]
position = Vector2(320, -10) position = Vector2(320, -10)
script = ExtResource( 8 ) script = ExtResource("8")
[node name="Collision" type="CollisionShape2D" parent="Ceiling"] [node name="Collision" type="CollisionShape2D" parent="Ceiling"]
shape = SubResource( 4 ) shape = SubResource("4")
[node name="Floor" type="Area2D" parent="."] [node name="Floor" type="Area2D" parent="."]
position = Vector2(320, 410) position = Vector2(320, 410)
script = ExtResource( 8 ) script = ExtResource("8")
_bounceDirection = -1 _bounceDirection = -1
[node name="Collision" type="CollisionShape2D" parent="Floor"] [node name="Collision" type="CollisionShape2D" parent="Floor"]
shape = SubResource( 4 ) shape = SubResource("4")
[connection signal="area_entered" from="Left" to="Left" method="OnAreaEntered"] [connection signal="area_entered" from="Left" to="Left" method="OnAreaEntered"]
[connection signal="area_entered" from="Right" to="Right" method="OnAreaEntered"] [connection signal="area_entered" from="Right" to="Right" method="OnAreaEntered"]

View File

@@ -15,7 +15,7 @@ config/description="A simple Pong game. This demo shows best practices
for game development in Godot, including signals." for game development in Godot, including signals."
config/tags=PackedStringArray("2d", "demo", "official") config/tags=PackedStringArray("2d", "demo", "official")
run/main_scene="pong.tscn" run/main_scene="pong.tscn"
config/features=PackedStringArray("4.2") config/features=PackedStringArray("4.2", "C#")
config/icon="res://icon.webp" config/icon="res://icon.webp"
[display] [display]
@@ -23,40 +23,50 @@ config/icon="res://icon.webp"
window/size/viewport_width=640 window/size/viewport_width=640
window/size/viewport_height=400 window/size/viewport_height=400
window/stretch/mode="canvas_items" window/stretch/mode="canvas_items"
window/stretch/scale_mode="integer"
[dotnet]
project/assembly_name="Pong with C#"
[input] [input]
left_move_down={ left_move_down={
"deadzone": 0.5, "deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":90,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) "events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":1.0,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":1.0,"script":null) , Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":12,"pressure":0.0,"pressed":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":13,"pressure":0.0,"pressed":false,"script":null) , 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":83,"key_label":0,"unicode":115,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":83,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) , 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":68,"key_label":0,"unicode":100,"echo":false,"script":null)
] ]
} }
left_move_up={ left_move_up={
"deadzone": 0.5, "deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":65,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) "events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":-1.0,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":-1.0,"script":null) , Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":11,"pressure":0.0,"pressed":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":12,"pressure":0.0,"pressed":false,"script":null) , 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":87,"key_label":0,"unicode":122,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":87,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) , 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":65,"key_label":0,"unicode":113,"echo":false,"script":null)
] ]
} }
right_move_down={ right_move_down={
"deadzone": 0.5, "deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":16777234,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) "events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":1,"axis":1,"axis_value":1.0,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":1,"axis":1,"axis_value":1.0,"script":null) , Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":1,"button_index":12,"pressure":0.0,"pressed":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":1,"button_index":13,"pressure":0.0,"pressed":false,"script":null) , 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)
, 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)
] ]
} }
right_move_up={ right_move_up={
"deadzone": 0.5, "deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":16777232,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) "events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":1,"axis":1,"axis_value":-1.0,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":1,"axis":1,"axis_value":-1.0,"script":null) , Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":1,"button_index":11,"pressure":0.0,"pressed":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":1,"button_index":12,"pressure":0.0,"pressed":false,"script":null) , 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)
, 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)
] ]
} }
[rendering] [rendering]
textures/canvas_textures/default_texture_filter=0
renderer/rendering_method="gl_compatibility"
renderer/rendering_method.mobile="gl_compatibility"
viewport/default_clear_color=Color(0, 0, 0, 1) viewport/default_clear_color=Color(0, 0, 0, 1)

View File

@@ -2,7 +2,7 @@
importer="texture" importer="texture"
type="CompressedTexture2D" type="CompressedTexture2D"
uid="uid://bxqd5psne5h38" uid="uid://cim6es185kfto"
path="res://.godot/imported/separator.png-f981c8489b9148e2e1dc63398273da74.ctex" path="res://.godot/imported/separator.png-f981c8489b9148e2e1dc63398273da74.ctex"
metadata={ metadata={
"vram_texture": false "vram_texture": false