From 4681c2e62005d14fb7d558df4a8d301214555dab Mon Sep 17 00:00:00 2001 From: Fries Date: Tue, 16 May 2023 23:47:24 -0700 Subject: [PATCH] add randomness to the ball and make the paddle smaller. --- Scenes/Paddle.tscn | 16 ++++++++++------ Scripts/Ball.cs | 9 ++++++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/Scenes/Paddle.tscn b/Scenes/Paddle.tscn index 8b2f2ea..e631d5b 100644 --- a/Scenes/Paddle.tscn +++ b/Scenes/Paddle.tscn @@ -1,12 +1,17 @@ -[gd_scene load_steps=4 format=3 uid="uid://bklo6torhapa0"] +[gd_scene load_steps=5 format=3 uid="uid://bklo6torhapa0"] [ext_resource type="Script" path="res://Scripts/Paddle.cs" id="1_uv7s3"] -[ext_resource type="Texture2D" uid="uid://b186qihsjblv7" path="res://icon.svg" id="2_62eyv"] [sub_resource type="PhysicsMaterial" id="PhysicsMaterial_r5a55"] friction = 0.0 bounce = 1.0 +[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_iw3nx"] +size = Vector2(50, 75) + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_dbik4"] +size = Vector2(12, 150) + [node name="Paddle" type="RigidBody2D"] physics_material_override = SubResource("PhysicsMaterial_r5a55") gravity_scale = 0.0 @@ -15,8 +20,7 @@ script = ExtResource("1_uv7s3") [node name="Sprite" type="Sprite2D" parent="."] scale = Vector2(0.25, 2) -texture = ExtResource("2_62eyv") +texture = SubResource("PlaceholderTexture2D_iw3nx") -[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."] -scale = Vector2(0.25, 2) -polygon = PackedVector2Array(64, -60.8, 64, 59, 59.6, 64, -60.1, 64, -64, 60.7, -64, -59, -59, -63.8, 59.1, -64) +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_dbik4") diff --git a/Scripts/Ball.cs b/Scripts/Ball.cs index 66c541d..7e21005 100644 --- a/Scripts/Ball.cs +++ b/Scripts/Ball.cs @@ -7,15 +7,18 @@ public partial class Ball : CharacterBody2D [Export] private double _ballSpeed; private Vector2 _velocity; - + /// /// this property multiples the ballSpeed by the Meter constant. /// private double BallSpeed => _ballSpeed * Constants.Meter; - + public override void _Ready() { - _velocity = Vector2.Left * BallSpeed; + // this should dispose when this method goes out of scope (this means after the velocity variable is assigned) + using var rng = new RandomNumberGenerator(); + // set the velocity currently to the left of the screen with a random y angle. + _velocity = Vector2.Left * BallSpeed + new Vector2(0, rng.RandfRange(-0.5, 0.5) * BallSpeed); } public override void _PhysicsProcess(double delta)