I'm having trouble converting my code for multiplayer

Hi I am converting the single player mode of my game to the multiplayer mode of my game but I am encountering some errors in the code. If anyone can help me fix this it would be great.

extends Node2D

Variables pour les scores

var player1_score = 0
var player2_score = 0

Variables pour les choix des joueurs

var player1_choice = “”
var player2_choice = “”

Variable pour activer/désactiver les entrées

var inputs_enabled = false

Vérifie si un round est en cours

var round_in_progress = false

Chemins des ressources

const SPRITES = {
“attente_joueur”: preload(“res://attentejoueurun.png”),
“attente_joueur2”: preload(“res://attentejoueurdeux.png”),
“papier”: preload(“res://papierimage.png”),
“ciseaux”: preload(“res://ciseauximage.png”),
“roche”: preload(“res://rocheimage.png”),
“gun”: preload(“res://gunimage.png”)
}

const SONS = {
“papier”: preload(“res://smsound.wav”),
“ciseaux”: preload(“res://scissorsoundtwo.wav”),
“roche”: preload(“res://rockfallsoundtrois.wav”),
“gun”: preload(“res://gunsoundtrois.wav”)
}

Références aux nœuds de la scène

@onready var attentejoueurun = $Attentejoueurun
@onready var attentejoueurdeux = $Attentejoueurdeux
@onready var audio_player1 = $AudioStreamPlayer1
@onready var audio_player2 = $AudioStreamPlayer2
@onready var label_joueur1 = $Label1
@onready var label_joueur2 = $Label2

Fonction prête

func _ready():
print(“DEBUG: Jeu en mode 2 joueurs prêt.”)
enable_buttons(false)
inputs_enabled = false
reset_sprites()

# Connexion des boutons Joueur 1
$Papierbutton.pressed.connect(set_choice.bind(1, "papier"))
$Ciseaubutton.pressed.connect(set_choice.bind(1, "ciseaux"))
$Rochebutton.pressed.connect(set_choice.bind(1, "roche"))
$Gunbutton.pressed.connect(set_choice.bind(1, "gun"))

# Connexion des boutons Joueur 2
$PapierPlayer2.pressed.connect(set_choice.bind(2, "papier"))
$CiseauPlayer2.pressed.connect(set_choice.bind(2, "ciseaux"))
$RochePlayer2.pressed.connect(set_choice.bind(2, "roche"))
$GunPlayer2.pressed.connect(set_choice.bind(2, "gun"))

play_voice_off()

Fonction pour activer/désactiver les boutons

func enable_buttons(enabled):
print(“DEBUG: Boutons activés:”, enabled)
$Papierbutton.disabled = !enabled
$Ciseaubutton.disabled = !enabled
$Rochebutton.disabled = !enabled
$Gunbutton.disabled = !enabled
$PapierPlayer2.disabled = !enabled
$CiseauPlayer2.disabled = !enabled
$RochePlayer2.disabled = !enabled
$GunPlayer2.disabled = !enabled

Fonction pour réinitialiser les sprites des joueurs

func reset_sprites():
print(“DEBUG: Réinitialisation des sprites.”)
attentejoueurun.texture = SPRITES[“attente_joueur”]
attentejoueurdeux.texture = SPRITES[“attente_joueur2”]

Fonction pour jouer la voix off d’introduction

func play_voice_off():
if player1_score >= 8 or player2_score >= 8:
print(“DEBUG: Jeu terminé, voix off annulée.”)
return

print("DEBUG: Lecture de la voix off...")
var voice_off_path = preload("res://rockpaperscissorsbulletgun.mp3")

# Assigner le son au lecteur audio
audio_player1.stream = voice_off_path
audio_player1.play()

# Attendre la fin du son ou un délai max
await get_tree().create_timer(voice_off_path.get_length()).timeout

# Activer les entrées après le son
enable_buttons(true)
inputs_enabled = true

Fonction pour gérer le choix d’un joueur

func set_choice(player, choice):
if player == 1:
player1_choice = choice
print(“DEBUG: Joueur 1 a choisi”, choice)
else:
player2_choice = choice
print(“DEBUG: Joueur 2 a choisi”, choice)

# Vérifie si les deux joueurs ont choisi
if player1_choice != "" and player2_choice != "":
	start_round()

Fonction pour mettre à jour les sprites des joueurs en fonction de leur choix

func update_sprites(player1_choice, player2_choice):
print(“DEBUG: Mise à jour des sprites → Joueur 1:”, player1_choice, “| Joueur 2:”, player2_choice)
attentejoueurun.texture = SPRITES[player1_choice]
attentejoueurdeux.texture = SPRITES[player2_choice]

Fonction pour démarrer un round

func start_round():
if round_in_progress:
print(“DEBUG: Tentative de jouer pendant un round en cours, ignoré.”)
return

round_in_progress = true
inputs_enabled = false
enable_buttons(false)

print("DEBUG: Début du round -> Joueur 1:", player1_choice, "| Joueur 2:", player2_choice)

# Mise à jour des sprites
update_sprites(player1_choice, player2_choice)

# Jouer les sons
play_sound(player1_choice, audio_player1)
play_sound(player2_choice, audio_player2)

# Attendre la fin des sons ou un délai max
var max_sound_duration = 2.0
var timer1 = get_tree().create_timer(max_sound_duration)
var timer2 = get_tree().create_timer(max_sound_duration)

await timer1.timeout if not audio_player1.playing else audio_player1.finished
await timer2.timeout if not audio_player2.playing else audio_player2.finished

print("DEBUG: Les sons sont terminés, comparaison en cours...")

var result = compare_choices(player1_choice, player2_choice)
print("DEBUG: Résultat =", result)

update_scores(result)
check_win_condition()

# Réinitialisation des sprites après une pause
await get_tree().create_timer(0.5).timeout
reset_sprites()

# Réinitialiser les choix
player1_choice = ""
player2_choice = ""

play_voice_off()
round_in_progress = false

Fonction pour jouer un son

func play_sound(choice, player):
if choice in SONS:
player.stream = SONS[choice]
player.play()
else:
print(“ERREUR: Son introuvable pour”, choice)

Fonction pour comparer les choix

func compare_choices(player1_choice, player2_choice):
if player1_choice == player2_choice:
return “Égalité”

var win_conditions = {
	"papier": ["roche"],
	"ciseaux": ["papier"],
	"roche": ["ciseaux", "gun"],
	"gun": ["papier", "ciseaux"]
}

if player1_choice in win_conditions and player2_choice in win_conditions[player1_choice]:
	return "Joueur 1 gagne"
else:
	return "Joueur 2 gagne"

Fonction pour mettre à jour les scores

func update_scores(result):
if result == “Joueur 1 gagne”:
player1_score += 1
elif result == “Joueur 2 gagne”:
player2_score += 1

label_joueur1.text = str(player1_score)

Have you ever played “Where’s Waldo”? Pasting your entire script unformatted, with no hints as to what you’d like changed about it is a lot like that book/game. It’s exceedingly difficult, made even harder as I have no idea what to look for.

When posting for help, make sure to state your exact issue. What you expected to happen versus what really happens. Why you are in this erroneous situation, the “big picture” of your project.

And make sure to format code pastes properly, make sure to only paste relevant parts of the code.

1 Like

Also, can you tell us the errors you are getting?