master
DJh2o2 2024-02-22 11:35:56 +07:00
parent 6f558eb49f
commit dc05a9b974
14 changed files with 150 additions and 22 deletions

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

@ -0,0 +1,22 @@
using System;
using Godot;
public partial class Area : Area2D
{
public override void _PhysicsProcess(double delta) {
// BodyEntered += OnBodyEntered;
// BodyExited += OnBodyExited;
}
private void OnBodyEntered(Node2D body) {
if (body is Ball) {
GD.Print("Ball entered the area!");
}
}
private void OnBodyExited(Node2D body) {
if (body is Ball) {
GD.Print("Ball left the area!");
}
}
}

@ -0,0 +1,19 @@
using Godot;
public partial class Ball : Node2D {
private int direction = 1;
[Export] private int speed = 500;
[Export] private Area2D area;
public override void _Ready() {
}
public override void _Process(double delta) {
Vector2 position = Position;
position.X += Mathf.RoundToInt(direction * speed * delta);
Position = position;
}
public void ChangeDirection() => direction *= -1;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 243 B

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://c5jcxyyyd0m60"
path="res://.godot/imported/Ball.png-3e5e22496e5c9e9b08e902070a717137.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://Ball.png"
dest_files=["res://.godot/imported/Ball.png-3e5e22496e5c9e9b08e902070a717137.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

Binary file not shown.

@ -0,0 +1,12 @@
using Godot;
using System;
public partial class GameManager : Node2D {
[Export] private Player player1;
[Export] private Player player2;
[Export] private Ball ball;
public override void _Ready() {
// ball.ChangeDirection();
}
}

@ -2,18 +2,16 @@ using Godot;
public partial class Player : Node2D {
[Export] private int playerNumber;
[Export] private int speed = 10;
public override void _UnhandledKeyInput(InputEvent @event) {
[Export] private int speed = 600;
public override void _Process(double delta) {
Vector2 position = Position;
if (playerNumber == 1) {
if (@event.IsAction("Player1Up")) position.Y -= speed;
else if (@event.IsAction("Player1Down")) position.Y += speed;
}
else {
if (@event.IsAction("Player2Up")) position.Y -= speed;
else if (@event.IsAction("Player2Down")) position.Y += speed;
}
if (Input.IsActionPressed("Player1Up") && playerNumber == 1) position.Y -= Mathf.RoundToInt(speed * delta);
else if (Input.IsActionPressed("Player1Down") && playerNumber == 1) position.Y += Mathf.RoundToInt(speed * delta);
if (Input.IsActionPressed("Player2Up") && playerNumber == 2) position.Y -= Mathf.RoundToInt(speed * delta);
else if (Input.IsActionPressed("Player2Down") && playerNumber == 2) position.Y += Mathf.RoundToInt(speed * delta);
if (position.Y > 520) position.Y = 520;
if (position.Y < 0) position.Y = 0;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.8 KiB

After

Width:  |  Height:  |  Size: 186 B

@ -2,7 +2,7 @@
importer="texture"
type="CompressedTexture2D"
uid="uid://4fir02w0nns3"
uid="uid://1aivwap6lvr4"
path="res://.godot/imported/Player.png-3b381e068d1d74cc2895fb9be41402b0.ctex"
metadata={
"vram_texture": false

Binary file not shown.

@ -1,11 +1,20 @@
[gd_scene load_steps=2 format=3 uid="uid://bxkg2ri346c2o"]
[gd_scene load_steps=3 format=3 uid="uid://bxkg2ri346c2o"]
[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_toecu"]
size = Vector2(30, 30)
[ext_resource type="Texture2D" uid="uid://c5jcxyyyd0m60" path="res://Ball.png" id="1_ihs8s"]
[ext_resource type="Script" path="res://Ball.cs" id="1_jhuit"]
[node name="Ball" type="Node2D"]
script = ExtResource("1_jhuit")
[node name="Sprite2D" type="Sprite2D" parent="."]
[node name="RigidBody2D" type="RigidBody2D" parent="."]
gravity_scale = 0.0
[node name="Sprite2D" type="Sprite2D" parent="RigidBody2D"]
position = Vector2(16, 16)
scale = Vector2(1, 1.14)
texture = ExtResource("1_ihs8s")
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="RigidBody2D"]
position = Vector2(16, 16)
scale = Vector2(1, 1.14)
texture = SubResource("PlaceholderTexture2D_toecu")
polygon = PackedVector2Array(16, -11.7, 16, 10, 10.7, 16, -11, 16, -16, 11.8, -16, -10, -10, -15.8, 10, -16)

@ -1,11 +1,18 @@
[gd_scene load_steps=3 format=3 uid="uid://dbgbygehmmopa"]
[ext_resource type="Script" path="res://Player.cs" id="1_cs5ta"]
[ext_resource type="Texture2D" uid="uid://4fir02w0nns3" path="res://Player.png" id="2_jdmud"]
[ext_resource type="Texture2D" uid="uid://1aivwap6lvr4" path="res://Player.png" id="2_jdmud"]
[node name="Player1" type="Node2D"]
script = ExtResource("1_cs5ta")
[node name="Sprite2D" type="Sprite2D" parent="."]
position = Vector2(16, 200)
[node name="RigidBody2D" type="RigidBody2D" parent="."]
gravity_scale = 0.0
[node name="Sprite2D" type="Sprite2D" parent="RigidBody2D"]
position = Vector2(0, 64)
texture = ExtResource("2_jdmud")
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="RigidBody2D"]
position = Vector2(0, 64)
polygon = PackedVector2Array(5, -63.7, 5, 64, -4, 64, -4, 63.3, -5, 61, -5, -61.1, -4, -63.4, -4, -64, 4.2, -64)

@ -1,12 +1,23 @@
[gd_scene load_steps=3 format=3 uid="uid://3acuhn3ykmnq"]
[gd_scene load_steps=6 format=3 uid="uid://3acuhn3ykmnq"]
[ext_resource type="PackedScene" uid="uid://dbgbygehmmopa" path="res://player.tscn" id="1_7bgs4"]
[ext_resource type="Script" path="res://GameManager.cs" id="1_284o0"]
[ext_resource type="PackedScene" uid="uid://bxkg2ri346c2o" path="res://ball.tscn" id="2_dcxy0"]
[ext_resource type="Script" path="res://Area.cs" id="4_gu0cb"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_r6erp"]
size = Vector2(1152, 648)
[node name="Pong" type="Node2D"]
[node name="GameManager" type="Node2D" parent="." node_paths=PackedStringArray("player1", "player2", "ball")]
script = ExtResource("1_284o0")
player1 = NodePath("../Player1")
player2 = NodePath("../Player2")
ball = NodePath("../Ball")
[node name="Player1" parent="." instance=ExtResource("1_7bgs4")]
position = Vector2(128, 256)
position = Vector2(64, 264)
playerNumber = 1
[node name="Player2" parent="." instance=ExtResource("1_7bgs4")]
@ -14,4 +25,14 @@ position = Vector2(1080, 256)
playerNumber = 2
[node name="Ball" parent="." instance=ExtResource("2_dcxy0")]
position = Vector2(560, 304)
position = Vector2(80, 216)
[node name="Area2D" type="Area2D" parent="."]
script = ExtResource("4_gu0cb")
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
position = Vector2(576, 324)
shape = SubResource("RectangleShape2D_r6erp")
[connection signal="body_entered" from="Area2D" to="Area2D" method="OnBodyEntered"]
[connection signal="body_exited" from="Area2D" to="Area2D" method="OnBodyExited"]