commit 0edf5ed31ce9ced6561aacc41e5e64c612be27b9 Author: CactiChameleon9 <51231053+CactiChameleon9@users.noreply.github.com> Date: Sat Aug 20 15:22:38 2022 +0100 Made PONG! in Godot diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..09e6732 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# ---> Godot +# Godot-specific ignores +.import/ +export.cfg +export_presets.cfg + +# Imported translations (automatically generated from CSV files) +*.translation + +# Mono-specific ignores +.mono/ +data_*/ + +# Builds +.exe +.x86_64 diff --git a/Ball.gd b/Ball.gd new file mode 100644 index 0000000..da7354d --- /dev/null +++ b/Ball.gd @@ -0,0 +1,37 @@ +extends KinematicBody2D + +export var inital_speed : float = 400.0 +export var speed_increase : float = 40.0 + +var speed : float = inital_speed + + +var velocity : Vector2 = Vector2.RIGHT + + + +func _draw(): + draw_circle(Vector2(0, 0), 20, Color("#c4c4c4")) + + +func _physics_process(delta): + velocity = velocity.normalized() * speed + + var collision = move_and_collide(velocity * delta) + if collision: + velocity = velocity.bounce(collision.normal) + speed += speed_increase + + if collision.collider.is_in_group("Paddle"): + velocity.x += velocity.x/abs(velocity.x) * speed_increase + + collision_mask = 2 + $DisableCollision.start() + + +func reset_speed(): + speed = inital_speed + + +func _on_DisableCollision_timeout(): + collision_mask = 3 diff --git a/Ball.tscn b/Ball.tscn new file mode 100644 index 0000000..5c26716 --- /dev/null +++ b/Ball.tscn @@ -0,0 +1,21 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://Ball.gd" type="Script" id=1] + +[sub_resource type="CircleShape2D" id=1] +radius = 20.0 + +[node name="Ball" type="KinematicBody2D"] +collision_layer = 4 +collision_mask = 3 +moving_platform_apply_velocity_on_leave = 2 +script = ExtResource( 1 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource( 1 ) + +[node name="DisableCollision" type="Timer" parent="."] +process_mode = 0 +wait_time = 0.2 + +[connection signal="timeout" from="DisableCollision" to="." method="_on_DisableCollision_timeout"] diff --git a/ExpressionPro.ttf b/ExpressionPro.ttf new file mode 100644 index 0000000..4600022 Binary files /dev/null and b/ExpressionPro.ttf differ diff --git a/Main.gd b/Main.gd new file mode 100644 index 0000000..4d25837 --- /dev/null +++ b/Main.gd @@ -0,0 +1,13 @@ +extends Node2D + +const ball := preload("res://Ball.tscn") + +onready var ball_position : Vector2 = $"%Ball".position + + +func reset_ball(): + yield(get_tree().create_timer(.5), "timeout") + + $"%Ball".reset_speed() + $"%Ball".position = ball_position + diff --git a/Main.tscn b/Main.tscn new file mode 100644 index 0000000..acb0e8f --- /dev/null +++ b/Main.tscn @@ -0,0 +1,100 @@ +[gd_scene load_steps=10 format=2] + +[ext_resource path="res://Paddle.tscn" type="PackedScene" id=1] +[ext_resource path="res://Ball.tscn" type="PackedScene" id=2] +[ext_resource path="res://ExpressionPro.ttf" type="DynamicFontData" id=3] +[ext_resource path="res://Score.gd" type="Script" id=4] +[ext_resource path="res://Main.gd" type="Script" id=5] + +[sub_resource type="DynamicFont" id=2] +size = 480 +font_data = ExtResource( 3 ) + +[sub_resource type="DynamicFont" id=3] +size = 200 +font_data = ExtResource( 3 ) + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 700, 10 ) + +[sub_resource type="RectangleShape2D" id=4] +extents = Vector2( 10, 540 ) + +[node name="Main" type="Node2D"] +script = ExtResource( 5 ) + +[node name="UI" type="CanvasLayer" parent="."] +layer = -1 + +[node name="Score" type="Label" parent="UI"] +anchor_top = 1.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_top = -420.0 +custom_colors/font_color = Color( 0.356863, 0.356863, 0.356863, 1 ) +custom_fonts/font = SubResource( 2 ) +text = "2 2" +align = 1 +script = ExtResource( 4 ) + +[node name="Title" type="Label" parent="UI"] +anchor_right = 1.0 +margin_bottom = 176.0 +custom_colors/font_color = Color( 0.356863, 0.356863, 0.356863, 1 ) +custom_fonts/font = SubResource( 3 ) +text = "PONG!" +align = 1 + +[node name="PlayerPaddle" parent="." instance=ExtResource( 1 )] +position = Vector2( 60, 142 ) +scale = Vector2( -15, 100 ) +is_player = true +acceleration = 25.0 +friction = 15.0 +speed = 1000.0 + +[node name="OppPaddle" parent="." instance=ExtResource( 1 )] +position = Vector2( 1220, 213 ) +speed = 1000.0 + +[node name="Ball" parent="." instance=ExtResource( 2 )] +unique_name_in_owner = true +position = Vector2( 619, 311 ) + +[node name="Walls" type="Node" parent="."] + +[node name="Wall1" type="StaticBody2D" parent="Walls"] +position = Vector2( 640, 0 ) +collision_layer = 2 +collision_mask = 0 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Walls/Wall1"] +shape = SubResource( 1 ) + +[node name="Wall2" type="StaticBody2D" parent="Walls"] +position = Vector2( 640, 720 ) +collision_layer = 2 +collision_mask = 0 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Walls/Wall2"] +shape = SubResource( 1 ) + +[node name="PlayerGoal" type="Area2D" parent="."] +position = Vector2( -30, 360 ) +collision_layer = 0 +collision_mask = 4 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="PlayerGoal"] +shape = SubResource( 4 ) + +[node name="OppGoal" type="Area2D" parent="."] +position = Vector2( 1310, 360 ) +collision_layer = 0 +collision_mask = 4 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="OppGoal"] +shape = SubResource( 4 ) + +[connection signal="goal_scored" from="UI/Score" to="." method="reset_ball"] +[connection signal="body_entered" from="PlayerGoal" to="UI/Score" method="increase_opp_score"] +[connection signal="body_entered" from="OppGoal" to="UI/Score" method="increase_player_score"] diff --git a/PONG!.x86_64 b/PONG!.x86_64 new file mode 100755 index 0000000..18abda9 Binary files /dev/null and b/PONG!.x86_64 differ diff --git a/Paddle.gd b/Paddle.gd new file mode 100644 index 0000000..56da6de --- /dev/null +++ b/Paddle.gd @@ -0,0 +1,35 @@ +extends KinematicBody2D + +export var is_player : bool = false +export var acceleration : float = 50.0 +export var friction : float = 55.0 +export var speed : float = 500.0 + +var velocity : Vector2 = Vector2.ZERO + +onready var inital_x = position.x + + +func _physics_process(_delta): + + var direction := Vector2.ZERO + + if is_player: + direction.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up") + else: + if get_node_or_null("%Ball"): + direction.y = 1 if $"%Ball".position.y >= self.position.y else -1 + else: + direction.y = 0 + + velocity += acceleration * direction + if direction.y == 0: + velocity -= velocity.normalized() * friction + if abs(velocity.length()) <= friction: + velocity = Vector2.ZERO + + velocity = velocity.limit_length(speed) + + velocity = move_and_slide(velocity) + + position.x = inital_x diff --git a/Paddle.tscn b/Paddle.tscn new file mode 100644 index 0000000..8b612c1 --- /dev/null +++ b/Paddle.tscn @@ -0,0 +1,15 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://Paddle.gd" type="Script" id=1] + +[node name="Paddle" type="KinematicBody2D" groups=["Paddle"]] +scale = Vector2( 15, 100 ) +collision_mask = 0 +script = ExtResource( 1 ) + +[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."] +polygon = PoolVector2Array( 1, 1, 1, -1, 0.333333, -1, -0.667, -0.75, -1.5, -0.25, -1.5, 0.25, -0.667, 0.75, 0.333333, 1 ) + +[node name="Polygon2D2" type="Polygon2D" parent="."] +color = Color( 0.768627, 0.768627, 0.768627, 1 ) +polygon = PoolVector2Array( 1, 1, 1, -1, -1.5, -1, -1.5, 1 ) diff --git a/Score.gd b/Score.gd new file mode 100644 index 0000000..7b5e88a --- /dev/null +++ b/Score.gd @@ -0,0 +1,22 @@ +extends Label + +signal goal_scored + +var player_score : int = 0 +var opp_score : int = 0 + +func _ready(): + update_label() + +func update_label(): + self.text = str(player_score) + " " + str(opp_score) + +func increase_player_score(_body = null): + player_score += 1 + update_label() + emit_signal("goal_scored") + +func increase_opp_score(_body = null): + opp_score += 1 + update_label() + emit_signal("goal_scored") diff --git a/default_env.tres b/default_env.tres new file mode 100644 index 0000000..20207a4 --- /dev/null +++ b/default_env.tres @@ -0,0 +1,7 @@ +[gd_resource type="Environment" load_steps=2 format=2] + +[sub_resource type="ProceduralSky" id=1] + +[resource] +background_mode = 2 +background_sky = SubResource( 1 ) diff --git a/icon.png b/icon.png new file mode 100644 index 0000000..9e4823e Binary files /dev/null and b/icon.png differ diff --git a/icon.png.import b/icon.png.import new file mode 100644 index 0000000..a4c02e6 --- /dev/null +++ b/icon.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" +metadata={ +"vram_texture": false +} + +[deps] + +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 +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +process/normal_map_invert_y=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/project.godot b/project.godot new file mode 100644 index 0000000..f655c77 --- /dev/null +++ b/project.godot @@ -0,0 +1,68 @@ +; Engine configuration file. +; It's best edited using the editor UI and not directly, +; since the parameters that go here are not all obvious. +; +; Format: +; [section] ; section goes between [] +; param=value ; assign values to parameters + +config_version=4 + +[application] + +config/name="PONG!" +run/main_scene="res://Main.tscn" +config/icon="res://icon.png" + +[display] + +window/size/width=1280 +window/size/height=720 +window/stretch/mode="2d" +window/stretch/aspect="keep_width" + +[gui] + +common/drop_mouse_on_gui_input_disabled=true + +[input] + +ui_left={ +"deadzone": 0.5, +"events": [ ] +} +ui_right={ +"deadzone": 0.5, +"events": [ ] +} +ui_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,"physical_scancode":0,"unicode":0,"echo":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":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777231,"physical_scancode":0,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":14,"pressure":0.0,"pressed":false,"script":null) + ] +} +ui_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,"physical_scancode":0,"unicode":0,"echo":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":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777233,"physical_scancode":0,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":15,"pressure":0.0,"pressed":false,"script":null) + ] +} + +[layer_names] + +2d_physics/layer_1="Paddle" +2d_physics/layer_2="Wall" +2d_physics/layer_3="Ball" + +[physics] + +common/enable_pause_aware_picking=true + +[rendering] + +environment/default_clear_color=Color( 0.168627, 0.168627, 0.168627, 1 ) +environment/default_environment="res://default_env.tres"