Update Pong with C# demo to Godot 3.1.2

This commit is contained in:
Aaron Franke
2020-01-31 01:25:21 -05:00
parent a5b8cd11d9
commit de57f45049
22 changed files with 226 additions and 187 deletions

View File

@@ -1,29 +0,0 @@
using Godot;
using System;
public class Ball : Area2D
{
private const int BallSpeed = 100;
private int speed = BallSpeed;
private Vector2 initialPos;
public Vector2 direction = new Vector2(-1, 0);
public void Reset()
{
SetPosition(initialPos);
speed = BallSpeed;
direction = new Vector2(-1, 0);
}
public override void _Ready()
{
initialPos = Position;
}
public override void _Process(float delta)
{
Position += direction * speed * delta;
}
}

View File

@@ -1,34 +0,0 @@
using Godot;
using System;
public class Paddle : Area2D
{
[Export]
private int ballDir = 1;
private const int MoveSpeed = 100;
public override void _Process(float delta)
{
String which = GetName();
// Move up and down based on input
if (Input.IsActionPressed(which + "_move_up") && Position.y > 0)
{
Position -= new Vector2(0, MoveSpeed * delta);
}
if (Input.IsActionPressed(which + "_move_down") && Position.y < GetViewportRect().Size.y)
{
Position += new Vector2(0, MoveSpeed * delta);
}
}
public void OnAreaEntered(Area2D area)
{
if (area is Ball ball)
{
// Assign new direction
ball.direction = new Vector2(ballDir, (float)new Random().NextDouble() * 2 - 1).Normalized();
}
}
}

View File

@@ -1,95 +0,0 @@
[gd_scene load_steps=13 format=2]
[ext_resource path="res://Paddle.cs" type="Script" id=1]
[ext_resource path="res://left_pallete.png" type="Texture" id=2]
[ext_resource path="res://right_pallete.png" type="Texture" id=3]
[ext_resource path="res://Ball.cs" type="Script" id=4]
[ext_resource path="res://ball.png" type="Texture" id=5]
[ext_resource path="res://separator.png" type="Texture" id=6]
[ext_resource path="res://Wall.cs" type="Script" id=7]
[ext_resource path="res://CeilingFloor.cs" type="Script" id=8]
[sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 4, 16 )
[sub_resource type="RectangleShape2D" id=2]
extents = Vector2( 4, 4 )
[sub_resource type="RectangleShape2D" id=3]
extents = Vector2( 10, 200 )
[sub_resource type="RectangleShape2D" id=4]
extents = Vector2( 320, 10 )
[node name="game" type="Node2D"]
[node name="left" type="Area2D" parent="."]
position = Vector2( 67.6285, 192.594 )
script = ExtResource( 1 )
[node name="sprite" type="Sprite" parent="left"]
texture = ExtResource( 2 )
[node name="collision" type="CollisionShape2D" parent="left"]
shape = SubResource( 1 )
[node name="right" type="Area2D" parent="."]
position = Vector2( 563.815, 188.919 )
script = ExtResource( 1 )
ballDir = -1
[node name="sprite" type="Sprite" parent="right"]
texture = ExtResource( 3 )
[node name="collision" type="CollisionShape2D" parent="right"]
shape = SubResource( 1 )
[node name="ball" type="Area2D" parent="."]
position = Vector2( 320.5, 191.124 )
script = ExtResource( 4 )
[node name="sprite" type="Sprite" parent="ball"]
texture = ExtResource( 5 )
[node name="collision" type="CollisionShape2D" parent="ball"]
shape = SubResource( 2 )
[node name="separator" type="Sprite" parent="."]
position = Vector2( 320, 200 )
texture = ExtResource( 6 )
[node name="left_wall" type="Area2D" parent="."]
position = Vector2( -10, 200 )
script = ExtResource( 7 )
[node name="collision" type="CollisionShape2D" parent="left_wall"]
shape = SubResource( 3 )
[node name="right_wall" type="Area2D" parent="."]
position = Vector2( 650, 200 )
script = ExtResource( 7 )
[node name="collision" type="CollisionShape2D" parent="right_wall"]
shape = SubResource( 3 )
[node name="ceiling" type="Area2D" parent="."]
position = Vector2( 320, -10 )
script = ExtResource( 8 )
[node name="collision" type="CollisionShape2D" parent="ceiling"]
shape = SubResource( 4 )
[node name="floor" type="Area2D" parent="."]
position = Vector2( 320, 410 )
script = ExtResource( 8 )
yDirection = -1
[node name="collision" type="CollisionShape2D" parent="floor"]
shape = SubResource( 4 )
[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="left_wall" to="left_wall" method="OnWallAreaEntered"]
[connection signal="area_entered" from="right_wall" to="right_wall" method="OnWallAreaEntered"]
[connection signal="area_entered" from="ceiling" to="ceiling" method="OnAreaEntered"]
[connection signal="area_entered" from="floor" to="floor" method="OnAreaEntered"]

View File

@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{4F9320CC-07E9-4CEA-AA42-A32C83303312}</ProjectGuid>
<ProjectGuid>{EBA5981B-C37E-48C5-A3B6-4390D9834F9A}</ProjectGuid>
<OutputType>Library</OutputType>
<OutputPath>.mono/temp/bin/$(Configuration)</OutputPath>
<RootNamespace>Pong with C#</RootNamespace>
<RootNamespace>PongwithC</RootNamespace>
<AssemblyName>Pong with C#</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<BaseIntermediateOutputPath>.mono/temp/obj</BaseIntermediateOutputPath>
@@ -14,25 +14,26 @@
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<DebugType>portable</DebugType>
<Optimize>false</Optimize>
<DefineConstants>DEBUG;</DefineConstants>
<DefineConstants>$(GodotDefineConstants);GODOT;DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>full</DebugType>
<DebugType>portable</DebugType>
<Optimize>true</Optimize>
<DefineConstants>$(GodotDefineConstants);GODOT;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Tools|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<DebugType>portable</DebugType>
<Optimize>false</Optimize>
<DefineConstants>DEBUG;TOOLS;</DefineConstants>
<DefineConstants>$(GodotDefineConstants);GODOT;DEBUG;TOOLS;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
@@ -49,11 +50,11 @@
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="Ball.cs" />
<Compile Include="CeilingFloor.cs" />
<Compile Include="Paddle.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Wall.cs" />
<Compile Include="Scripts\Ball.cs" />
<Compile Include="Scripts\CeilingFloor.cs" />
<Compile Include="Scripts\Paddle.cs" />
<Compile Include="Scripts\Wall.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

View File

@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pong with C#", "Pong with C#.csproj", "{4F9320CC-07E9-4CEA-AA42-A32C83303312}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pong with C#", "Pong with C#.csproj", "{EBA5981B-C37E-48C5-A3B6-4390D9834F9A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -9,11 +9,11 @@ Global
Tools|Any CPU = Tools|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4F9320CC-07E9-4CEA-AA42-A32C83303312}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F9320CC-07E9-4CEA-AA42-A32C83303312}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F9320CC-07E9-4CEA-AA42-A32C83303312}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F9320CC-07E9-4CEA-AA42-A32C83303312}.Release|Any CPU.Build.0 = Release|Any CPU
{4F9320CC-07E9-4CEA-AA42-A32C83303312}.Tools|Any CPU.ActiveCfg = Tools|Any CPU
{4F9320CC-07E9-4CEA-AA42-A32C83303312}.Tools|Any CPU.Build.0 = Tools|Any CPU
{EBA5981B-C37E-48C5-A3B6-4390D9834F9A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EBA5981B-C37E-48C5-A3B6-4390D9834F9A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EBA5981B-C37E-48C5-A3B6-4390D9834F9A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EBA5981B-C37E-48C5-A3B6-4390D9834F9A}.Release|Any CPU.Build.0 = Release|Any CPU
{EBA5981B-C37E-48C5-A3B6-4390D9834F9A}.Tools|Any CPU.ActiveCfg = Tools|Any CPU
{EBA5981B-C37E-48C5-A3B6-4390D9834F9A}.Tools|Any CPU.Build.0 = Tools|Any CPU
EndGlobalSection
EndGlobal

26
mono/pong/Scripts/Ball.cs Normal file
View File

@@ -0,0 +1,26 @@
using Godot;
public class Ball : Area2D
{
private const int BallSpeed = 100;
public Vector2 direction = Vector2.Left;
private Vector2 _initialPos;
public void Reset()
{
Position = _initialPos;
direction = Vector2.Left;
}
public override void _Ready()
{
_initialPos = Position;
}
public override void _Process(float delta)
{
Position += BallSpeed * delta * direction;
}
}

View File

@@ -1,16 +1,15 @@
using Godot;
using System;
public class CeilingFloor : Area2D
{
[Export]
private int yDirection = 1;
private int _bounceDirection = 1;
public void OnAreaEntered(Area2D area)
{
if (area is Ball ball)
{
ball.direction += new Vector2(0, yDirection);
ball.direction = (ball.direction + new Vector2(0, _bounceDirection)).Normalized();
}
}
}

View File

@@ -0,0 +1,39 @@
using Godot;
using System;
public class Paddle : Area2D
{
private const int MoveSpeed = 100;
// All three of these change for each paddle.
private int _ballDir;
private string _up;
private string _down;
public override void _Ready()
{
string name = Name.ToLower();
_up = name + "_move_up";
_down = name + "_move_down";
_ballDir = name == "left" ? 1 : -1;
}
public override void _Process(float delta)
{
// Move up and down based on input.
float input = Input.GetActionStrength(_down) - Input.GetActionStrength(_up);
Vector2 position = Position; // Required so that we can modify position.y.
position += new Vector2(0, input * MoveSpeed * delta);
position.y = Mathf.Clamp(position.y, 16, GetViewportRect().Size.y - 16);
Position = position;
}
public void OnAreaEntered(Area2D area)
{
if (area is Ball ball)
{
// Assign new direction
ball.direction = new Vector2(_ballDir, ((float)new Random().NextDouble()) * 2 - 1).Normalized();
}
}
}

View File

@@ -1,5 +1,4 @@
using Godot;
using System;
public class Wall : Area2D
{
@@ -7,7 +6,7 @@ public class Wall : Area2D
{
if (area is Ball ball)
{
// Oops, ball went out of game place, reset
// Ball went off the side of the screen, reset it.
ball.Reset();
}
}

View File

Before

Width:  |  Height:  |  Size: 149 B

After

Width:  |  Height:  |  Size: 149 B

View File

@@ -3,16 +3,21 @@
importer="texture"
type="StreamTexture"
path="res://.import/ball.png-9a4ca347acb7532f6ae347744a6b04f7.stex"
metadata={
"vram_texture": false
}
[deps]
source_md5="4219e9084f5485c8d1812297700f317d"
source_file="res://ball.png"
dest_files=[ "res://.import/ball.png-9a4ca347acb7532f6ae347744a6b04f7.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
@@ -22,6 +27,7 @@ flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true

View File

Before

Width:  |  Height:  |  Size: 583 B

After

Width:  |  Height:  |  Size: 583 B

View File

@@ -3,16 +3,21 @@
importer="texture"
type="StreamTexture"
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
metadata={
"vram_texture": false
}
[deps]
source_md5="9be177e863d37787c0836d64924b3503"
source_file="res://icon.png"
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
@@ -22,6 +27,7 @@ flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true

View File

Before

Width:  |  Height:  |  Size: 138 B

After

Width:  |  Height:  |  Size: 138 B

View File

@@ -3,16 +3,21 @@
importer="texture"
type="StreamTexture"
path="res://.import/left_pallete.png-bc33611074a0f886142e37c77bd2545a.stex"
metadata={
"vram_texture": false
}
[deps]
source_md5="df76627349499ad47ebf48a7ca947cca"
source_file="res://left_pallete.png"
dest_files=[ "res://.import/left_pallete.png-bc33611074a0f886142e37c77bd2545a.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
@@ -22,6 +27,7 @@ flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true

95
mono/pong/pong.tscn Normal file
View File

@@ -0,0 +1,95 @@
[gd_scene load_steps=13 format=2]
[ext_resource path="res://Scripts/Paddle.cs" type="Script" id=1]
[ext_resource path="res://left_pallete.png" type="Texture" id=2]
[ext_resource path="res://right_pallete.png" type="Texture" id=3]
[ext_resource path="res://Scripts/Ball.cs" type="Script" id=4]
[ext_resource path="res://ball.png" type="Texture" id=5]
[ext_resource path="res://separator.png" type="Texture" id=6]
[ext_resource path="res://Scripts/Wall.cs" type="Script" id=7]
[ext_resource path="res://Scripts/CeilingFloor.cs" type="Script" id=8]
[sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 4, 16 )
[sub_resource type="RectangleShape2D" id=2]
extents = Vector2( 4, 4 )
[sub_resource type="RectangleShape2D" id=3]
extents = Vector2( 10, 200 )
[sub_resource type="RectangleShape2D" id=4]
extents = Vector2( 320, 10 )
[node name="Pong" type="Node2D"]
[node name="Left" type="Area2D" parent="."]
position = Vector2( 67.6285, 192.594 )
script = ExtResource( 1 )
[node name="Sprite" type="Sprite" parent="Left"]
texture = ExtResource( 2 )
[node name="Collision" type="CollisionShape2D" parent="Left"]
shape = SubResource( 1 )
[node name="Right" type="Area2D" parent="."]
position = Vector2( 563.815, 188.919 )
script = ExtResource( 1 )
[node name="Sprite" type="Sprite" parent="Right"]
texture = ExtResource( 3 )
[node name="Collision" type="CollisionShape2D" parent="Right"]
shape = SubResource( 1 )
[node name="Ball" type="Area2D" parent="."]
position = Vector2( 320.5, 191.124 )
script = ExtResource( 4 )
[node name="Sprite" type="Sprite" parent="Ball"]
texture = ExtResource( 5 )
[node name="Collision" type="CollisionShape2D" parent="Ball"]
shape = SubResource( 2 )
[node name="Separator" type="Sprite" parent="."]
position = Vector2( 320, 200 )
texture = ExtResource( 6 )
[node name="Node2D" type="Node2D" parent="."]
[node name="LeftWall" type="Area2D" parent="."]
position = Vector2( -10, 200 )
script = ExtResource( 7 )
[node name="Collision" type="CollisionShape2D" parent="LeftWall"]
shape = SubResource( 3 )
[node name="RightWall" type="Area2D" parent="."]
position = Vector2( 650, 200 )
script = ExtResource( 7 )
[node name="Collision" type="CollisionShape2D" parent="RightWall"]
shape = SubResource( 3 )
[node name="Ceiling" type="Area2D" parent="."]
position = Vector2( 320, -10 )
script = ExtResource( 8 )
[node name="Collision" type="CollisionShape2D" parent="Ceiling"]
shape = SubResource( 4 )
[node name="Floor" type="Area2D" parent="."]
position = Vector2( 320, 410 )
script = ExtResource( 8 )
_bounceDirection = -1
[node name="Collision" type="CollisionShape2D" parent="Floor"]
shape = SubResource( 4 )
[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="LeftWall" to="LeftWall" method="OnWallAreaEntered"]
[connection signal="area_entered" from="RightWall" to="RightWall" method="OnWallAreaEntered"]
[connection signal="area_entered" from="Ceiling" to="Ceiling" method="OnAreaEntered"]
[connection signal="area_entered" from="Floor" to="Floor" method="OnAreaEntered"]

View File

@@ -35,21 +35,29 @@ singletons=[ ]
left_move_down={
"deadzone": 0.5,
"events": [ 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)
, 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":13,"pressure":0.0,"pressed":false,"script":null)
]
}
left_move_up={
"deadzone": 0.5,
"events": [ 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)
, 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)
]
}
right_move_down={
"deadzone": 0.5,
"events": [ 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)
, 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":13,"pressure":0.0,"pressed":false,"script":null)
]
}
right_move_up={
"deadzone": 0.5,
"events": [ 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)
, 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)
]
}

View File

Before

Width:  |  Height:  |  Size: 138 B

After

Width:  |  Height:  |  Size: 138 B

View File

@@ -3,16 +3,21 @@
importer="texture"
type="StreamTexture"
path="res://.import/right_pallete.png-fc6e4a6a7c8197834656482b94708e47.stex"
metadata={
"vram_texture": false
}
[deps]
source_md5="d46f647d3f045dbee4d786089c309868"
source_file="res://right_pallete.png"
dest_files=[ "res://.import/right_pallete.png-fc6e4a6a7c8197834656482b94708e47.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
@@ -22,6 +27,7 @@ flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true

View File

Before

Width:  |  Height:  |  Size: 142 B

After

Width:  |  Height:  |  Size: 142 B

View File

@@ -3,16 +3,21 @@
importer="texture"
type="StreamTexture"
path="res://.import/separator.png-f981c8489b9148e2e1dc63398273da74.stex"
metadata={
"vram_texture": false
}
[deps]
source_md5="b6234b89455156532bbe1256249fcfd4"
source_file="res://separator.png"
dest_files=[ "res://.import/separator.png-f981c8489b9148e2e1dc63398273da74.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
@@ -22,6 +27,7 @@ flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true