From 5b50afd615347cd3db90ead33671ef708bf77e32 Mon Sep 17 00:00:00 2001 From: Frederic Aust <frederic.aust@hs-bochum.de> Date: Sun, 7 Mar 2021 19:31:42 +0100 Subject: [PATCH] # Einfuehrung der von oben nach unten fallenden Hindernisse * Koennen im Hauptmenue eingeschaltet werden * Die Geschwindigkeit ist vorerst noch im Code Obstacle.gd festeingestellt, wird in zukuenftigen Commits aber dynamisch gestaltet * Eine Einstellung ueber das SettingsWindow ist ebenfalls geplant * Sobald das Hindernisses den unteren Bildschirmrand vollstaendig verlassen hat, wird die der Block auf den Oberen Rand gesetzt, von wo aus das Hinternis sich wieder herunter bewegt --- Battle Pong/Ball.gd | 3 +- Battle Pong/GameSettings.gd | 1 + Battle Pong/Main.gd | 16 +++++++++ Battle Pong/Main.tscn | 13 ++++++- Battle Pong/MainMenu.gd | 6 ++++ Battle Pong/MainMenu.tscn | 69 ++++++++++++++++++++++++++----------- Battle Pong/Obstacle.gd | 63 +++++++++++++++++++++++++++++++++ Battle Pong/Obstacle.tscn | 10 +++--- 8 files changed, 154 insertions(+), 27 deletions(-) create mode 100644 Battle Pong/Obstacle.gd diff --git a/Battle Pong/Ball.gd b/Battle Pong/Ball.gd index ae70722..d56d1fd 100644 --- a/Battle Pong/Ball.gd +++ b/Battle Pong/Ball.gd @@ -41,7 +41,8 @@ func run(delta): ball_hit_paddle(get_tree().get_root().find_node("PlayerOne", true, false).position, get_tree().get_root().find_node("PlayerOne", true, false).get_shape(), true) elif collision.collider.name == "PlayerTwo": ball_hit_paddle(get_tree().get_root().find_node("PlayerTwo", true, false).position, get_tree().get_root().find_node("PlayerTwo", true, false).get_shape(), false) - + elif collision.collider.name =="Obstacle": + velocity = velocity.bounce(collision.normal) pass # velocity.x *=-1 # var localCollisionPos = collision.Position - collision.collider.Position; diff --git a/Battle Pong/GameSettings.gd b/Battle Pong/GameSettings.gd index a3e92d4..905409a 100644 --- a/Battle Pong/GameSettings.gd +++ b/Battle Pong/GameSettings.gd @@ -4,6 +4,7 @@ export var rendering_enabled = false export var learn_with_images = true export var trainings_mode_enabled = false export var local_two_player = false +export var obstacles_enabled = false # Constant values !!!! Keep always up to date !!! var display_window_width = 1024 diff --git a/Battle Pong/Main.gd b/Battle Pong/Main.gd index 43e7975..e85c991 100644 --- a/Battle Pong/Main.gd +++ b/Battle Pong/Main.gd @@ -17,6 +17,8 @@ var action_player_two = InputEventAction.new() var next_step_player_two = false var action_trainer_two = "" + + var Ball = preload("Ball.tscn") var ball = Ball.instance() var score_player_one = 0 @@ -75,10 +77,16 @@ func _ready(): set_ball() $PlayerOne.start($StartPositionPlayerOne.position) $PlayerTwo.start($StartPositionPlayerTwo.position) + if $"/root/GameSettings".obstacles_enabled: + set_obstacle() + display_message() update_score() pause() +func set_obstacle(): + + $Obstacle.start($StartPositionObstacleTop.position) func _input(_event): if Input.is_key_pressed(KEY_SPACE): @@ -93,6 +101,7 @@ func play(): update_score() playing = true ball.set_playing(playing) + $Obstacle.set_playing(playing) $DisplayMessage.visible = false @@ -130,6 +139,8 @@ func handle_score_event(): display_message() playing = false score_event = false + $Obstacle.set_playing(playing) + func update_score(): @@ -173,6 +184,7 @@ func handle_game_end(): + func _on_PlayerTwo_hit(): ball.ball_hit_paddle($PlayerTwo.position, $PlayerTwo/CollisionShape2D.shape.extents, false) pass # Replace with function body. @@ -383,12 +395,14 @@ func pause(): ball.set_pause(true) $PlayerOne.set_pause(true) $PlayerTwo.set_pause(true) + $Obstacle.set_pause(true) func unpause(): #print("unpause") ball.set_pause(false) $PlayerOne.set_pause(false) $PlayerTwo.set_pause(false) + $Obstacle.set_pause(false) func _on_LocalTwoPlayerTimer_timeout(): @@ -396,5 +410,7 @@ func _on_LocalTwoPlayerTimer_timeout(): $PlayerOne.run(game_playtime_per_step) $PlayerTwo.run(game_playtime_per_step) ball.run(game_playtime_per_step) + if $"/root/GameSettings".obstacles_enabled: + $Obstacle.run(game_playtime_per_step) timeout() pass # Replace with function body. diff --git a/Battle Pong/Main.tscn b/Battle Pong/Main.tscn index 1684f90..4b84899 100644 --- a/Battle Pong/Main.tscn +++ b/Battle Pong/Main.tscn @@ -1,8 +1,10 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=6 format=2] [ext_resource path="res://Player.tscn" type="PackedScene" id=1] [ext_resource path="res://Main.gd" type="Script" id=2] [ext_resource path="res://Wall.tscn" type="PackedScene" id=3] +[ext_resource path="res://Obstacle.tscn" type="PackedScene" id=4] +[ext_resource path="res://Obstacle.gd" type="Script" id=5] [node name="Node2D" type="Node2D"] pause_mode = 2 @@ -85,6 +87,15 @@ position = Vector2( 0, 600 ) [node name="LocalTwoPlayerTimer" type="Timer" parent="."] wait_time = 0.01 + +[node name="Obstacle" parent="." instance=ExtResource( 4 )] +script = ExtResource( 5 ) + +[node name="StartPositionObstacleTop" type="Position2D" parent="."] +position = Vector2( 512, 0 ) + +[node name="StartPositionObstacleBottom" type="Position2D" parent="."] +position = Vector2( 512, 600 ) [connection signal="hit" from="PlayerTwo" to="." method="_on_PlayerTwo_hit"] [connection signal="hit" from="PlayerOne" to="." method="_on_PlayerOne_hit"] [connection signal="timeout" from="LocalTwoPlayerTimer" to="." method="_on_LocalTwoPlayerTimer_timeout"] diff --git a/Battle Pong/MainMenu.gd b/Battle Pong/MainMenu.gd index 15e8915..201d080 100644 --- a/Battle Pong/MainMenu.gd +++ b/Battle Pong/MainMenu.gd @@ -71,3 +71,9 @@ func _on_but_trainer_with_position_pressed(): func _on_but_settings_pressed(): get_tree().change_scene("res://SettingsWindow.tscn") + + +func _on_ckb_Obstackles_pressed(): + $"/root/GameSettings".obstacles_enabled = $HBoxContainer/VBoxContainer/ckb_Obstackles.pressed + print($"/root/GameSettings".obstacles_enabled) + pass # Replace with function body. diff --git a/Battle Pong/MainMenu.tscn b/Battle Pong/MainMenu.tscn index 3e316c4..b1c8eb7 100644 --- a/Battle Pong/MainMenu.tscn +++ b/Battle Pong/MainMenu.tscn @@ -24,46 +24,53 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="vbox_two_learner" type="VBoxContainer" parent="."] +[node name="HBoxContainer" type="HBoxContainer" parent="."] anchor_left = 0.5 anchor_top = 0.5 anchor_right = 0.5 anchor_bottom = 0.5 margin_left = -132.5 -margin_top = -92.0 +margin_top = -80.0 margin_right = 132.5 -margin_bottom = 92.0 +margin_bottom = 80.0 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="vbox_two_learner" type="VBoxContainer" parent="HBoxContainer"] +margin_right = 265.0 +margin_bottom = 160.0 __meta__ = { "_edit_use_anchors_": false } -[node name="but_local_two_player" type="Button" parent="vbox_two_learner"] +[node name="but_local_two_player" type="Button" parent="HBoxContainer/vbox_two_learner"] margin_right = 265.0 margin_bottom = 20.0 text = "Local 2 Player" -[node name="HSeparator2" type="HSeparator" parent="vbox_two_learner"] +[node name="HSeparator2" type="HSeparator" parent="HBoxContainer/vbox_two_learner"] margin_top = 24.0 margin_right = 265.0 margin_bottom = 28.0 -[node name="but_two_re_with_images" type="Button" parent="vbox_two_learner"] +[node name="but_two_re_with_images" type="Button" parent="HBoxContainer/vbox_two_learner"] margin_top = 32.0 margin_right = 265.0 margin_bottom = 52.0 text = "2 RE with Images" -[node name="HBoxContainer" type="HBoxContainer" parent="vbox_two_learner"] +[node name="HBoxContainer" type="HBoxContainer" parent="HBoxContainer/vbox_two_learner"] margin_top = 56.0 margin_right = 265.0 margin_bottom = 80.0 -[node name="but_two_re_with_position" type="Button" parent="vbox_two_learner/HBoxContainer"] +[node name="but_two_re_with_position" type="Button" parent="HBoxContainer/vbox_two_learner/HBoxContainer"] margin_right = 126.0 margin_bottom = 24.0 text = "2 RE with Position" -[node name="cb_rendering_enabled" type="CheckBox" parent="vbox_two_learner/HBoxContainer"] +[node name="cb_rendering_enabled" type="CheckBox" parent="HBoxContainer/vbox_two_learner/HBoxContainer"] margin_left = 130.0 margin_right = 265.0 margin_bottom = 24.0 @@ -72,29 +79,50 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="HSeparator" type="HSeparator" parent="vbox_two_learner"] +[node name="HSeparator" type="HSeparator" parent="HBoxContainer/vbox_two_learner"] margin_top = 84.0 margin_right = 265.0 margin_bottom = 88.0 -[node name="but_trainer_with_images" type="Button" parent="vbox_two_learner"] +[node name="but_trainer_with_images" type="Button" parent="HBoxContainer/vbox_two_learner"] margin_top = 92.0 margin_right = 265.0 margin_bottom = 112.0 text = "RE Trainer with Images" -[node name="but_trainer_with_position" type="Button" parent="vbox_two_learner"] +[node name="but_trainer_with_position" type="Button" parent="HBoxContainer/vbox_two_learner"] margin_top = 116.0 margin_right = 265.0 margin_bottom = 136.0 text = "RE Trainer with Position" -[node name="but_settings" type="Button" parent="vbox_two_learner"] +[node name="but_settings" type="Button" parent="HBoxContainer/vbox_two_learner"] margin_top = 140.0 margin_right = 265.0 margin_bottom = 160.0 text = "Settings" +[node name="VSeparator" type="VSeparator" parent="HBoxContainer"] +margin_left = 269.0 +margin_right = 273.0 +margin_bottom = 160.0 + +[node name="VBoxContainer" type="VBoxContainer" parent="HBoxContainer"] +margin_left = 277.0 +margin_right = 421.0 +margin_bottom = 160.0 + +[node name="lbl_GameModifier" type="Label" parent="HBoxContainer/VBoxContainer"] +margin_right = 144.0 +margin_bottom = 14.0 +text = "Game Modifier" + +[node name="ckb_Obstackles" type="CheckButton" parent="HBoxContainer/VBoxContainer"] +margin_top = 18.0 +margin_right = 144.0 +margin_bottom = 58.0 +text = "Obstacles" + [node name="lbl_game_running" type="Label" parent="."] visible = false anchor_left = 0.5 @@ -107,10 +135,11 @@ text = "Game runs without rendering" __meta__ = { "_edit_use_anchors_": false } -[connection signal="pressed" from="vbox_two_learner/but_local_two_player" to="." method="_on_but_local_two_player_pressed"] -[connection signal="pressed" from="vbox_two_learner/but_two_re_with_images" to="." method="_on_but_two_re_with_images_pressed"] -[connection signal="pressed" from="vbox_two_learner/HBoxContainer/but_two_re_with_position" to="." method="_on_but_two_re_with_position_pressed"] -[connection signal="pressed" from="vbox_two_learner/HBoxContainer/cb_rendering_enabled" to="." method="_on_cb_rendering_enabled_pressed"] -[connection signal="pressed" from="vbox_two_learner/but_trainer_with_images" to="." method="_on_but_trainer_with_images_pressed"] -[connection signal="pressed" from="vbox_two_learner/but_trainer_with_position" to="." method="_on_but_trainer_with_position_pressed"] -[connection signal="pressed" from="vbox_two_learner/but_settings" to="." method="_on_but_settings_pressed"] +[connection signal="pressed" from="HBoxContainer/vbox_two_learner/but_local_two_player" to="." method="_on_but_local_two_player_pressed"] +[connection signal="pressed" from="HBoxContainer/vbox_two_learner/but_two_re_with_images" to="." method="_on_but_two_re_with_images_pressed"] +[connection signal="pressed" from="HBoxContainer/vbox_two_learner/HBoxContainer/but_two_re_with_position" to="." method="_on_but_two_re_with_position_pressed"] +[connection signal="pressed" from="HBoxContainer/vbox_two_learner/HBoxContainer/cb_rendering_enabled" to="." method="_on_cb_rendering_enabled_pressed"] +[connection signal="pressed" from="HBoxContainer/vbox_two_learner/but_trainer_with_images" to="." method="_on_but_trainer_with_images_pressed"] +[connection signal="pressed" from="HBoxContainer/vbox_two_learner/but_trainer_with_position" to="." method="_on_but_trainer_with_position_pressed"] +[connection signal="pressed" from="HBoxContainer/vbox_two_learner/but_settings" to="." method="_on_but_settings_pressed"] +[connection signal="pressed" from="HBoxContainer/VBoxContainer/ckb_Obstackles" to="." method="_on_ckb_Obstackles_pressed"] diff --git a/Battle Pong/Obstacle.gd b/Battle Pong/Obstacle.gd new file mode 100644 index 0000000..4cbc8b5 --- /dev/null +++ b/Battle Pong/Obstacle.gd @@ -0,0 +1,63 @@ +extends StaticBody2D + + +# Declare member variables here. Examples: +# var a = 2 +# var b = "text" +var min_speed = 300 +var max_speed = 700 +var speed=0 +var start_position +var playing = false +var is_paused = true +var obstacle_size +var top_position +var bottom_position +# Called when the node enters the scene tree for the first time. +func _ready(): + randomize() + #speed = rand_range(min_speed, max_speed) + speed = 500 + + obstacle_size =$CollisionShape2D.shape.extents + top_position = 0- obstacle_size.y + bottom_position = $"/root/GameSettings".display_window_height + obstacle_size.y + pass # Replace with function body. + + +func run(delta): + if playing and not is_paused: + self.rotation = 0 + #self.linear_velocity = Vector2(dx, dy) * delta * speed + #var move = velocity.normalized() * delta * speed + #print(delta) + #var collision = self.move(move, false) + + + var velocity = Vector2() # The player's movement vector. + # Hier kommt die Erweiterung hin um Obstacles von oben oder unten kommen zu lassen + #if true: + velocity.y += 1 + # else: + # velocity.y -= 1 + + if velocity.length() > 0: + velocity = velocity.normalized() * speed + + position += velocity * delta + if position.y >bottom_position: + position = start_position + +func start(pos): + start_position = pos + start_position.y = top_position + position = start_position + show() + +func set_playing(_playing): + playing = _playing + +func set_pause(value): + get_tree().paused=value + is_paused = value + diff --git a/Battle Pong/Obstacle.tscn b/Battle Pong/Obstacle.tscn index 0db91a4..690bd3d 100644 --- a/Battle Pong/Obstacle.tscn +++ b/Battle Pong/Obstacle.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=2 format=2] [sub_resource type="RectangleShape2D" id=1] -extents = Vector2( 19.9777, 20.0466 ) +extents = Vector2( 12.0822, 49.3651 ) [node name="Obstacle" type="StaticBody2D"] pause_mode = 1 @@ -12,10 +12,10 @@ collision_mask = 8 shape = SubResource( 1 ) [node name="ColorRect" type="ColorRect" parent="."] -margin_left = -20.0 -margin_top = -20.0 -margin_right = 20.0 -margin_bottom = 20.0 +margin_left = -12.0 +margin_top = -49.0 +margin_right = 12.0 +margin_bottom = 49.0 color = Color( 0, 0.0862745, 1, 1 ) __meta__ = { "_edit_use_anchors_": false -- GitLab