pongodot

Tinkering with Godot, remaking pong
git clone git://git.hellocld.com/pongodot
Log | Files | Refs

commit 1464c1ff5590329b597b9c5170b0431e3148aaf7
parent c3780b6a6fd2809f894758ee7e35d3d43eed817b
Author: Christopher Ray Langford <chris@hellocld.com>
Date:   Tue,  5 Nov 2019 20:37:08 -0500

It's not very fun, but it's a game!

Keeps tabs on the score and stops resetting the ball once someone wins. Outputs the score to stdout.

Diffstat:
MBall.gd | 12+++++++++++-
MGame.gd | 35++++++++++++++++++++++++++++++++---
MGame.tscn | 17+++++++++++++----
3 files changed, 56 insertions(+), 8 deletions(-)

diff --git a/Ball.gd b/Ball.gd @@ -3,12 +3,21 @@ extends KinematicBody2D export var _speed = 10 var _dir = Vector2(1, 1) +func _ready(): + reset() + + func _physics_process(delta): var collision = move_and_collide(_dir * _speed * delta) if collision: - print(collision.normal) + print(collision.collider.name) _dir = _dir.bounce(collision.normal) if position.x >= get_viewport().size.x or position.x < 0: _dir.x *= -1 if position.y >= get_viewport().size.y or position.y < 0: _dir.y *= -1 + + +func reset(): + _dir = _dir.normalized() + position = get_viewport().size / 2+ \ No newline at end of file diff --git a/Game.gd b/Game.gd @@ -1,6 +1,35 @@ extends Node2D +export var game_score = 10 +var p1_score = 0 +var p2_score = 0 -func _on_Goal_body_entered(body): - print("Body " + body.name + " entered")- \ No newline at end of file + +func _ready(): + p1_score = 0 + p2_score = 0 + +func _print_score(): + print("P1 %d | P2 %d" % [p1_score, p2_score]) + +func _on_Goal_1_body_entered(area): + if area == $Ball: + p2_score += 1 + _print_score() + if p2_score >= game_score: + print("P2 Wins!") + $Ball.free() + else: + $Ball.reset() + + +func _on_Goal_2_body_entered(area): + if area == $Ball: + p1_score += 1 + _print_score() + if p1_score >= game_score: + print("P1 Wins!") + $Ball.free() + else: + $Ball.reset()+ \ No newline at end of file diff --git a/Game.tscn b/Game.tscn @@ -1,21 +1,30 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=5 format=2] [ext_resource path="res://Game.gd" type="Script" id=1] [ext_resource path="res://Ball.tscn" type="PackedScene" id=2] [ext_resource path="res://Paddle.tscn" type="PackedScene" id=3] +[ext_resource path="res://Goal.tscn" type="PackedScene" id=4] -[node name="Node2D" type="Node2D"] +[node name="Game" type="Node2D"] script = ExtResource( 1 ) +game_score = 3 [node name="Ball" parent="." instance=ExtResource( 2 )] position = Vector2( 320, 240 ) _speed = 128 [node name="Paddle 1" parent="." instance=ExtResource( 3 )] -position = Vector2( 39.7101, 255.408 ) +position = Vector2( 40, 240 ) _speed = 64 +[node name="Goal 1" parent="." instance=ExtResource( 4 )] + [node name="Paddle 2" parent="." instance=ExtResource( 3 )] -position = Vector2( 597.457, 255.408 ) +position = Vector2( 600, 240 ) _speed = 64 _player = "p2" + +[node name="Goal 2" parent="." instance=ExtResource( 4 )] +position = Vector2( 12, 240 ) +[connection signal="body_entered" from="Goal 1" to="." method="_on_Goal_1_body_entered"] +[connection signal="body_entered" from="Goal 2" to="." method="_on_Goal_2_body_entered"]