From 9f20733cc10d010d9998fa7ffa315c351b5c15ae Mon Sep 17 00:00:00 2001 From: Dragos Stefan Cirstian Date: Sat, 6 Jul 2024 12:04:06 +0300 Subject: [PATCH] Upload files to "/" --- main.gd | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.tscn | 52 +++++++++++++++++++++++++++++ project.godot | 20 ++++++++++++ 3 files changed, 162 insertions(+) create mode 100644 main.gd create mode 100644 main.tscn create mode 100644 project.godot diff --git a/main.gd b/main.gd new file mode 100644 index 0000000..bfa70f5 --- /dev/null +++ b/main.gd @@ -0,0 +1,90 @@ +extends Node + +@export var circle_scene : PackedScene +@export var cross_scene : PackedScene +var player : int +var winner : int +var temp_marker # marker temporar/ cel din dreapta pt next player +var player_panel_pos : Vector2i # pozitie chenar dreapta in care apare ce jucator urmeaza +var board_size : int +var cell_size : int +var grid_pos : Vector2i +var grid_data : Array # matrice 3 pe 3 +var row_sum : int +var col_sum : int +var diagonal1_sum : int +var diagonal2_sum : int + +# Called when the node enters the scene tree for the first time. +func _ready(): # ruleaza instant cand nodul maine este utilizat + board_size = $Board.texture.get_width() + # imparte board_size la 3 pentru a obtine dimensiunea fiecarei celule + cell_size = board_size / 3 + # obtinere coordonate ale panoului mic din dreapta + player_panel_pos = $PlayerPanel.get_position() + new_game() + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta): + pass + +func _input(event): + if event is InputEventMouseButton: + if event.button_index == MOUSE_BUTTON_LEFT and event.pressed : + #verifica daca mouse-ul e pe tabla de joc + if event.position.x < board_size: + #convertire pozitie mouse in locatie grid + grid_pos = Vector2i(event.position / cell_size) + if grid_data[grid_pos.y][grid_pos.x] == 0 : + grid_data[grid_pos.y][grid_pos.x] = player # player e variabila sa astim ce salvam in memorie + #plasare simbol jucator pe tabla + create_marker(player, grid_pos * cell_size + Vector2i(cell_size /2, cell_size /2 )) + if check_win() != 0 : + print("game over") + if check_win() == 1 : print("Jucator 1 a castigat") + else: print("Jucator 2 a castigat") + get_tree().paused = true + player *= -1 + #update player marcker din dreapta + #temp_marker.delete() + remove_child(temp_marker) + create_marker(player, player_panel_pos + Vector2i(cell_size /2, cell_size /2 ), true) + +func new_game(): + player = 1 + winner = 0 + grid_data = [ + [0, 0, 0], + [0, 0, 0], + [0, 0, 0] + ] + #creare marker pentru a arata care player incepe + create_marker(player, player_panel_pos + Vector2i(cell_size / 2, cell_size /2), true) + +func create_marker(player, position, temp = false) : + #creaza un marcker node si il adauga ca un child + if player == -1 : + var circle = circle_scene.instantiate() + circle.position = position + add_child(circle) + if temp : temp_marker = circle + if player == 1 : + var cross = cross_scene.instantiate() + cross.position = position + add_child(cross) + if temp : temp_marker = cross + +func check_win() : + for i in len(grid_data): + row_sum = grid_data[i][0] + grid_data[i][1] + grid_data[i][2] + col_sum = grid_data[0][i] + grid_data[1][i] + grid_data[2][i] + diagonal1_sum = grid_data[0][0] + grid_data[1][1] + grid_data[2][2] + diagonal2_sum = grid_data[0][2] + grid_data[1][1] + grid_data[2][0] + + #verific daca un player are toate markurile pe o linie / a castigat + if row_sum == 3 or col_sum == 3 or diagonal1_sum ==3 or diagonal2_sum == 3 : + winner = 1 + elif row_sum == -3 or col_sum == -3 or diagonal1_sum == -3 or diagonal2_sum == -3 : + winner = -1 + + return winner diff --git a/main.tscn b/main.tscn new file mode 100644 index 0000000..6446f62 --- /dev/null +++ b/main.tscn @@ -0,0 +1,52 @@ +[gd_scene load_steps=7 format=3 uid="uid://b1tee71w8k0kg"] + +[ext_resource type="Script" path="res://main.gd" id="1_5p2h0"] +[ext_resource type="PackedScene" uid="uid://b0skica3w7850" path="res://board.tscn" id="1_cnr5t"] +[ext_resource type="PackedScene" uid="uid://nn6wgmlux55i" path="res://circle.tscn" id="2_rpogs"] +[ext_resource type="PackedScene" uid="uid://c7w4rinv7meho" path="res://cross.tscn" id="3_rntru"] + +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_tarn6"] +bg_color = Color(0.349786, 0.654526, 0.701248, 1) +border_width_left = 5 +border_color = Color(0.00820203, 0.00820202, 0.00820201, 1) + +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_ccu3p"] +bg_color = Color(1, 1, 1, 1) +border_width_left = 5 +border_width_top = 5 +border_width_right = 5 +border_width_bottom = 5 +border_color = Color(0.125911, 0.125911, 0.125911, 1) + +[node name="Main" type="Node"] +script = ExtResource("1_5p2h0") +circle_scene = ExtResource("2_rpogs") +cross_scene = ExtResource("3_rntru") + +[node name="Board" parent="." instance=ExtResource("1_cnr5t")] +visible = true +position = Vector2(300, 300) + +[node name="SidePanel" type="Panel" parent="."] +custom_minimum_size = Vector2(300, 600) +offset_left = 600.0 +offset_right = 900.0 +offset_bottom = 600.0 +theme_override_styles/panel = SubResource("StyleBoxFlat_tarn6") + +[node name="PlayerLabel" type="Label" parent="."] +offset_left = 600.0 +offset_right = 900.0 +offset_bottom = 70.0 +theme_override_font_sizes/font_size = 34 +text = "Next player: +" +horizontal_alignment = 1 +vertical_alignment = 1 + +[node name="PlayerPanel" type="Panel" parent="."] +offset_left = 650.0 +offset_top = 150.0 +offset_right = 850.0 +offset_bottom = 350.0 +theme_override_styles/panel = SubResource("StyleBoxFlat_ccu3p") diff --git a/project.godot b/project.godot new file mode 100644 index 0000000..47fb254 --- /dev/null +++ b/project.godot @@ -0,0 +1,20 @@ +; 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=5 + +[application] + +config/name="X si O" +config/features=PackedStringArray("4.2", "Forward Plus") +config/icon="res://icon.svg" + +[display] + +window/size/viewport_width=900 +window/size/viewport_height=600