Skip to content

Commit 0af6d01

Browse files
committed
Add Mono conversion of the Pong demo
Quick and dirty port by a complete C# newbie, could probably be optimized a bit.
1 parent f8ca6d0 commit 0af6d01

File tree

12 files changed

+341
-0
lines changed

12 files changed

+341
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ export.cfg
44
export_presets.cfg
55
*.import
66

7+
# Mono-specific ignores
8+
.mono/
9+
Properties/
10+
*.csproj
11+
*.sln
12+
713
# System/tool-specific ignores
814
.directory
915
*~

mono/monkey_pong/Ball.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Godot;
2+
using System;
3+
4+
public class Ball : Area2D
5+
{
6+
private const int BALL_SPEED = 100;
7+
8+
private Vector2 direction = new Vector2(-1, 0);
9+
private int speed = BALL_SPEED;
10+
11+
private Vector2 initialPos;
12+
13+
public void SetDirection(Vector2 newDirection)
14+
{
15+
direction = newDirection;
16+
}
17+
public Vector2 GetDirection()
18+
{
19+
return direction;
20+
}
21+
22+
public void Reset()
23+
{
24+
SetPosition(initialPos);
25+
speed = BALL_SPEED;
26+
direction = new Vector2(-1, 0);
27+
}
28+
29+
public override void _Ready()
30+
{
31+
initialPos = GetPosition();
32+
}
33+
34+
public override void _Process(float delta)
35+
{
36+
SetPosition(GetPosition() + direction * speed * delta);
37+
}
38+
}

mono/monkey_pong/CeilingFloor.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Godot;
2+
using System;
3+
4+
public class CeilingFloor : Area2D
5+
{
6+
[Export]
7+
private int yDirection = 1;
8+
9+
public void OnAreaEntered(Area2D area)
10+
{
11+
if (area is Ball)
12+
{
13+
Ball ball = (Ball)area;
14+
ball.SetDirection(ball.GetDirection() + new Vector2(0, yDirection));
15+
}
16+
}
17+
}

mono/monkey_pong/Paddle.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Godot;
2+
using System;
3+
4+
public class Paddle : Area2D
5+
{
6+
[Export]
7+
private int ballDir = 1;
8+
9+
private const int MOVE_SPEED = 100;
10+
11+
public override void _Process(float delta)
12+
{
13+
String which = GetName();
14+
15+
// Move up and down based on input
16+
if (Input.IsActionPressed(which + "_move_up") && GetPosition().y > 0)
17+
{
18+
Vector2 pos = GetPosition();
19+
pos.y -= MOVE_SPEED * delta;
20+
SetPosition(pos);
21+
}
22+
if (Input.IsActionPressed(which + "_move_down") && GetPosition().y < GetViewportRect().Size.y)
23+
{
24+
Vector2 pos = GetPosition();
25+
pos.y += MOVE_SPEED * delta;
26+
SetPosition(pos);
27+
}
28+
}
29+
30+
public void OnAreaEntered(Area2D area)
31+
{
32+
if (area is Ball)
33+
{
34+
// Assign new direction
35+
Ball ball = (Ball)area;
36+
ball.SetDirection(new Vector2(ballDir, (float)new Random().NextDouble() * 2 - 1).normalized());
37+
}
38+
}
39+
}

mono/monkey_pong/Wall.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Godot;
2+
using System;
3+
4+
public class Wall : Area2D
5+
{
6+
public void OnWallAreaEntered(Area2D area)
7+
{
8+
if (area is Ball)
9+
{
10+
// Oops, ball went out of game place, reset
11+
Ball ball = (Ball)area;
12+
ball.Reset();
13+
}
14+
}
15+
}

mono/monkey_pong/ball.png

203 Bytes
Loading

mono/monkey_pong/icon.png

956 Bytes
Loading

mono/monkey_pong/left_pallete.png

200 Bytes
Loading

mono/monkey_pong/pong.tscn

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
[gd_scene load_steps=13 format=2]
2+
3+
[ext_resource path="res://Paddle.cs" type="Script" id=1]
4+
[ext_resource path="res://left_pallete.png" type="Texture" id=2]
5+
[ext_resource path="res://right_pallete.png" type="Texture" id=3]
6+
[ext_resource path="res://Ball.cs" type="Script" id=4]
7+
[ext_resource path="res://ball.png" type="Texture" id=5]
8+
[ext_resource path="res://separator.png" type="Texture" id=6]
9+
[ext_resource path="res://Wall.cs" type="Script" id=7]
10+
[ext_resource path="res://CeilingFloor.cs" type="Script" id=8]
11+
12+
[sub_resource type="RectangleShape2D" id=1]
13+
14+
custom_solver_bias = 0.0
15+
extents = Vector2( 4, 16 )
16+
17+
[sub_resource type="RectangleShape2D" id=2]
18+
19+
custom_solver_bias = 0.0
20+
extents = Vector2( 4, 4 )
21+
22+
[sub_resource type="RectangleShape2D" id=3]
23+
24+
custom_solver_bias = 0.0
25+
extents = Vector2( 10, 200 )
26+
27+
[sub_resource type="RectangleShape2D" id=4]
28+
29+
custom_solver_bias = 0.0
30+
extents = Vector2( 320, 10 )
31+
32+
[node name="game" type="Node2D"]
33+
34+
[node name="left" type="Area2D" parent="."]
35+
36+
position = Vector2( 67.6285, 192.594 )
37+
input_pickable = true
38+
gravity_vec = Vector2( 0, 1 )
39+
gravity = 98.0
40+
linear_damp = 0.1
41+
angular_damp = 1.0
42+
audio_bus_override = false
43+
audio_bus_name = "Master"
44+
script = ExtResource( 1 )
45+
ballDir = 1
46+
47+
[node name="sprite" type="Sprite" parent="left"]
48+
49+
texture = ExtResource( 2 )
50+
_sections_unfolded = [ "Transform" ]
51+
52+
[node name="collision" type="CollisionShape2D" parent="left"]
53+
54+
shape = SubResource( 1 )
55+
56+
[node name="right" type="Area2D" parent="."]
57+
58+
position = Vector2( 563.815, 188.919 )
59+
input_pickable = true
60+
gravity_vec = Vector2( 0, 1 )
61+
gravity = 98.0
62+
linear_damp = 0.1
63+
angular_damp = 1.0
64+
audio_bus_override = false
65+
audio_bus_name = "Master"
66+
script = ExtResource( 1 )
67+
ballDir = -1
68+
69+
[node name="sprite" type="Sprite" parent="right"]
70+
71+
texture = ExtResource( 3 )
72+
_sections_unfolded = [ "Transform" ]
73+
74+
[node name="collision" type="CollisionShape2D" parent="right"]
75+
76+
shape = SubResource( 1 )
77+
78+
[node name="ball" type="Area2D" parent="."]
79+
80+
position = Vector2( 320.5, 191.124 )
81+
input_pickable = true
82+
gravity_vec = Vector2( 0, 1 )
83+
gravity = 98.0
84+
linear_damp = 0.1
85+
angular_damp = 1.0
86+
audio_bus_override = false
87+
audio_bus_name = "Master"
88+
script = ExtResource( 4 )
89+
90+
[node name="sprite" type="Sprite" parent="ball"]
91+
92+
texture = ExtResource( 5 )
93+
_sections_unfolded = [ "Transform" ]
94+
95+
[node name="collision" type="CollisionShape2D" parent="ball"]
96+
97+
shape = SubResource( 2 )
98+
99+
[node name="separator" type="Sprite" parent="."]
100+
101+
position = Vector2( 320, 200 )
102+
texture = ExtResource( 6 )
103+
104+
[node name="left_wall" type="Area2D" parent="."]
105+
106+
position = Vector2( -10, 200 )
107+
input_pickable = true
108+
gravity_vec = Vector2( 0, 1 )
109+
gravity = 98.0
110+
linear_damp = 0.1
111+
angular_damp = 1.0
112+
audio_bus_override = false
113+
audio_bus_name = "Master"
114+
script = ExtResource( 7 )
115+
_sections_unfolded = [ "Transform" ]
116+
117+
[node name="collision" type="CollisionShape2D" parent="left_wall"]
118+
119+
shape = SubResource( 3 )
120+
121+
[node name="right_wall" type="Area2D" parent="."]
122+
123+
position = Vector2( 650, 200 )
124+
input_pickable = true
125+
gravity_vec = Vector2( 0, 1 )
126+
gravity = 98.0
127+
linear_damp = 0.1
128+
angular_damp = 1.0
129+
audio_bus_override = false
130+
audio_bus_name = "Master"
131+
script = ExtResource( 7 )
132+
_sections_unfolded = [ "Transform" ]
133+
134+
[node name="collision" type="CollisionShape2D" parent="right_wall"]
135+
136+
shape = SubResource( 3 )
137+
138+
[node name="ceiling" type="Area2D" parent="."]
139+
140+
position = Vector2( 320, -10 )
141+
input_pickable = true
142+
gravity_vec = Vector2( 0, 1 )
143+
gravity = 98.0
144+
linear_damp = 0.1
145+
angular_damp = 1.0
146+
audio_bus_override = false
147+
audio_bus_name = "Master"
148+
script = ExtResource( 8 )
149+
_sections_unfolded = [ "Transform" ]
150+
yDirection = 1
151+
152+
[node name="collision" type="CollisionShape2D" parent="ceiling"]
153+
154+
shape = SubResource( 4 )
155+
156+
[node name="floor" type="Area2D" parent="."]
157+
158+
position = Vector2( 320, 410 )
159+
input_pickable = true
160+
gravity_vec = Vector2( 0, 1 )
161+
gravity = 98.0
162+
linear_damp = 0.1
163+
angular_damp = 1.0
164+
audio_bus_override = false
165+
audio_bus_name = "Master"
166+
script = ExtResource( 8 )
167+
_sections_unfolded = [ "Transform" ]
168+
yDirection = -1
169+
170+
[node name="collision" type="CollisionShape2D" parent="floor"]
171+
172+
shape = SubResource( 4 )
173+
174+
[connection signal="area_entered" from="left" to="left" method="OnAreaEntered"]
175+
176+
[connection signal="area_entered" from="right" to="right" method="OnAreaEntered"]
177+
178+
[connection signal="area_entered" from="left_wall" to="left_wall" method="OnWallAreaEntered"]
179+
180+
[connection signal="area_entered" from="right_wall" to="right_wall" method="OnWallAreaEntered"]
181+
182+
[connection signal="area_entered" from="ceiling" to="ceiling" method="OnAreaEntered"]
183+
184+
[connection signal="area_entered" from="floor" to="floor" method="OnAreaEntered"]
185+
186+

mono/monkey_pong/project.godot

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
; Engine configuration file.
2+
; It's best edited using the editor UI and not directly,
3+
; since the parameters that go here are not all obvious.
4+
;
5+
; Format:
6+
; [section] ; section goes between []
7+
; param=value ; assign values to parameters
8+
9+
config_version=3
10+
11+
[application]
12+
13+
config/name="Pong with C#"
14+
run/main_scene="pong.tscn"
15+
config/icon="res://icon.png"
16+
17+
[display]
18+
19+
window/size/width=640
20+
window/size/height=400
21+
window/stretch/mode="2d"
22+
23+
[gdnative]
24+
25+
singletons=[ ]
26+
27+
[input]
28+
29+
left_move_down=[ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":90,"unicode":0,"echo":false,"script":null)
30+
]
31+
left_move_up=[ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":65,"unicode":0,"echo":false,"script":null)
32+
]
33+
right_move_down=[ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777234,"unicode":0,"echo":false,"script":null)
34+
]
35+
right_move_up=[ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777232,"unicode":0,"echo":false,"script":null)
36+
]
37+
38+
[rendering]
39+
40+
viewport/default_clear_color=Color( 0, 0, 0, 1 )

0 commit comments

Comments
 (0)