Naucz się programować tworząc gry – GDScript kompleksowy poradnik
# Gameextends Nodevar background: Panel@onreadyvar centerContainer: CenterContainer=CenterContainer.new()@onreadyvar mainContainer: VBoxContainer=VBoxContainer.new()@onreadyvar row1: HBoxContainer=HBoxContainer.new()@onreadyvar row2: HBoxContainer=HBoxContainer.new()@onreadyvar row3: HBoxContainer=HBoxContainer.new()var nextSignX: bool=truevar buttons: Array[OurButton] = []var game_over: bool=false# Called when the node enters the scene tree for the first time.func_ready(): background =Panel.new() background.set_anchors_preset(Control.PRESET_FULL_RECT) background.set_self_modulate(Color(1,0,0))add_child(background)generate_buttons() mainContainer.add_child(row1); mainContainer.add_child(row2); mainContainer.add_child(row3); centerContainer.add_child(mainContainer); centerContainer.set_anchors_preset(Control.PRESET_FULL_RECT)add_child(centerContainer);funcgenerate_buttons():for i inrange(9):var new_button: Button=OurButton.new() new_button.pressed.connect(on_click.bind(new_button)) buttons.append(new_button)if i <3: row1.add_child(new_button)elif i <6: row2.add_child(new_button)else: row3.add_child(new_button)funcon_click(button: Button):if button.text !=""or game_over:returnif nextSignX: button.text ="X"else: button.text ="O"ifcheck_win():handle_win()ifcheck_draw():restart() nextSignX =!nextSignXfuncwait(seconds: float) -> void:awaitget_tree().create_timer(seconds).timeoutfuncrestart(): game_over =trueawaitwait(1)get_tree().reload_current_scene()funchandle_win():var winLabel: Label=Label.new() winLabel.text ="X win"if nextSignX else"O win" winLabel.add_theme_font_size_override("font_size", 64)add_child(winLabel)restart()funccheck_draw():for button in buttons:if button.text =="":returnfalsereturntruefunccheck_win():if buttons[0].text == buttons[1].text and buttons[1].text == buttons[2].text and buttons[0].text !="" :returntrueif buttons[3].text == buttons[4].text and buttons[4].text == buttons[5].text and buttons[3].text !="":returntrueif buttons[6].text == buttons[7].text and buttons[7].text == buttons[8].text and buttons[6].text !="":returntrueif buttons[0].text == buttons[3].text and buttons[3].text == buttons[6].text and buttons[0].text !="":returntrueif buttons[1].text == buttons[4].text and buttons[4].text == buttons[7].text and buttons[1].text !="":returntrueif buttons[2].text == buttons[5].text and buttons[5].text == buttons[8].text and buttons[2].text !="":returntrueif buttons[0].text == buttons[4].text and buttons[4].text == buttons[8].text and buttons[0].text !="":returntrueif buttons[2].text == buttons[4].text and buttons[4].text == buttons[6].text and buttons[2].text !="":returntruereturnfalse
# OurButtonextends Buttonclass_nameOurButton# Called when the node enters the scene tree for the first time.func_ready():set_self_modulate(Color(0,1,0)) custom_minimum_size =Vector2(190,190)add_theme_font_size_override("font_size", 64)