Jeu de pierre-feuilles-ciseaux contre l'ordinateur

Le_Pythoneur_Debutant Messages postés 3 Statut Membre -  
yg_be Messages postés 24281 Date d'inscription   Statut Contributeur Dernière intervention   -
Bonjour, je suis en train de coder un programme Python permettant de jouer au jeu du pierre feuille ciseaux contre l'ordinateur, malheureusement j'ai plusieurs bugs et je pense faire trop de lignes pour rien (je viens de débuter le python).
Pouvez-vous m'aidez a résoudre ces bugs ?
Merci !




import random as r

import time
from tkinter import *
import tkinter as tk


point_ordinateur=0
point_joueur=0


def unjoueur():
fen.destroy()
pseudo=input("Quel est ton pseudo? :\n")
fen2 = Tk()
fen2.geometry("200x200")
fen2.title("Choix")
fen2["bg"]= "blue"
fen2["relief"] = "raised"
B1 = Button(fen2, text="Pierre", command=pierre, width=10, height=2, bg="green", fg="black").place(x=10, y=40)
B2 = Button(fen2, text="Feuille", command=feuille, width=10, height=2, bg="yellow", fg="black").place(x=100, y=40)
B3 = Button(fen2, text="Ciseaux", command=ciseaux, width=10, height=2, bg="red", fg="black").place(x=100, y=100)


def rejouer():
fen2 = Tk()
fen2.geometry("200x200")
fen2.title("Choix")
fen2["bg"]= "blue"
fen2["relief"] = "raised"
B1 = Button(fen2, text="Pierre", command=pierre, width=10, height=2, bg="green", fg="black").place(x=10, y=40)
B2 = Button(fen2, text="Feuille", command=feuille, width=10, height=2, bg="yellow", fg="black").place(x=100, y=40)
B3 = Button(fen2, text="Ciseaux", command=ciseaux, width=10, height=2, bg="red", fg="black").place(x=100, y=100)


def pierre():
fen2.destroy()
print(pseudo+" a fait Pierre.")
ordi=r.randint(1,3)
if ordi== 1:
print("L\'ordinateur a fait :\n Pierre")
time.sleep(1)
print("Egalité !")
rejouer()

if ordi== 2:
print("L\'ordinateur a fait :\n Feuille")
time.sleep(1)
print("Perdu !")
point_ordinateur+=1
rejouer()

if ordi== 3:
print("L\'ordinateur a fait :\n Ciseaux")
time.sleep(1)
print("Gagné !")
point_joueur+=1
rejouer()


def feuille():
fen2.destroy()
print(pseudo+" a fait Feuille.")
ordi=r.randint(1,3)
if ordi== 1:
print("L\'ordinateur a fait :\n Pierre")
time.sleep(1)
print("Gagné !")
point_joueur+=1
rejouer()

if ordi== 2:
print("L\'ordinateur a fait :\n Feuille")
time.sleep(1)
print("Egalité !")
rejouer()

if ordi== 3:
print("L\'ordinateur a fait :\n Ciseaux")
time.sleep(1)
print("Perdu !")
point_ordinateur+=1
rejouer()



def ciseaux():
fen2.destroy()
print(pseudo+" a fait Ciseaux.")
ordi=r.randint(1,3)
if ordi== 1:
print("L\'ordinateur a fait :\n Pierre")
time.sleep(1)
print("Perdu !")
point_odinateur+=1
rejouer()

if ordi== 2:
print("L\'ordinateur a fait :\n Feuille")
time.sleep(1)
print("Gagné !")
point_joueur+=1
rejouer()

if ordi== 3:
print("L\'ordinateur a fait :\n Ciseaux")
time.sleep(1)
print("Egalité !")
rejouer()



fen = Tk()
fen.geometry("200x200")
fen.title("Choix")
fen["bg"]= "green"
fen["relief"] = "raised"

B1 = Button(fen, text="1 joueur", command=fen.quit and unjoueur, width=10, height=2, bg="blue", fg="black")
B1.place(x=10, y=40)
A voir également:

1 réponse

yg_be Messages postés 24281 Date d'inscription   Statut Contributeur Dernière intervention   Ambassadeur 1 585
 
bonjour, peux-tu utiliser les balises de code quand tu postes du code? https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code
peux-tu décrire le symptôme d'un des bugs?
0
Le_Pythoneur_Debutant Messages postés 3 Statut Membre
 
import random as r
import time
from tkinter import *
import tkinter as tk


point_ordinateur=0
point_joueur=0


def unjoueur():
    fen.destroy()
    pseudo=input("Quel est ton pseudo? :\n")
    fen2 = Tk() 
    fen2.geometry("200x200") 
    fen2.title("Choix")
    fen2["bg"]= "blue"
    fen2["relief"] = "raised"
    B1 = Button(fen2, text="Pierre", command=pierre, width=10, height=2, bg="green", fg="black").place(x=10, y=40)
    B2 = Button(fen2, text="Feuille", command=feuille, width=10, height=2, bg="yellow", fg="black").place(x=100, y=40)
    B3 = Button(fen2, text="Ciseaux", command=ciseaux, width=10, height=2, bg="red", fg="black").place(x=100, y=100)


def rejouer():
    fen2 = Tk() 
    fen2.geometry("200x200") 
    fen2.title("Choix")
    fen2["bg"]= "blue"
    fen2["relief"] = "raised"
    B1 = Button(fen2, text="Pierre", command=pierre, width=10, height=2, bg="green", fg="black").place(x=10, y=40)
    B2 = Button(fen2, text="Feuille", command=feuille, width=10, height=2, bg="yellow", fg="black").place(x=100, y=40)
    B3 = Button(fen2, text="Ciseaux", command=ciseaux, width=10, height=2, bg="red", fg="black").place(x=100, y=100)
    

def pierre():
    fen2.destroy()
    print(pseudo+" a fait Pierre.")
    ordi=r.randint(1,3)
    if ordi== 1:
        print("L\'ordinateur a fait :\n      Pierre")
        time.sleep(1)
        print("Egalité !")
        rejouer()
        
    if ordi== 2:
        print("L\'ordinateur a fait :\n      Feuille")
        time.sleep(1)
        print("Perdu !")
        point_ordinateur+=1
        rejouer()
        
    if ordi== 3:
        print("L\'ordinateur a fait :\n      Ciseaux")
        time.sleep(1)
        print("Gagné !")
        point_joueur+=1
        rejouer()


def feuille():
    fen2.destroy()
    print(pseudo+" a fait Feuille.")
    ordi=r.randint(1,3)
    if ordi== 1:
        print("L\'ordinateur a fait :\n      Pierre")
        time.sleep(1)
        print("Gagné !")
        point_joueur+=1
        rejouer()
        
    if ordi== 2:
        print("L\'ordinateur a fait :\n      Feuille")
        time.sleep(1)
        print("Egalité !")
        rejouer()
        
    if ordi== 3:
        print("L\'ordinateur a fait :\n      Ciseaux")
        time.sleep(1)
        print("Perdu !")
        point_ordinateur+=1
        rejouer()



def ciseaux():
    fen2.destroy()
    print(pseudo+" a fait Ciseaux.")
    ordi=r.randint(1,3)
    if ordi== 1:
        print("L\'ordinateur a fait :\n      Pierre")
        time.sleep(1)
        print("Perdu !")
        point_odinateur+=1
        rejouer()
        
    if ordi== 2:
        print("L\'ordinateur a fait :\n      Feuille")
        time.sleep(1)
        print("Gagné !")
        point_joueur+=1
        rejouer()
        
    if ordi== 3:
        print("L\'ordinateur a fait :\n      Ciseaux")
        time.sleep(1)
        print("Egalité !")
        rejouer()



fen = Tk() 
fen.geometry("200x200") 
fen.title("Choix")
fen["bg"]= "green"
fen["relief"] = "raised"

B1 = Button(fen, text="1 joueur", command=fen.quit and unjoueur, width=10, height=2, bg="blue", fg="black")
B1.place(x=10, y=40)



0
Le_Pythoneur_Debutant Messages postés 3 Statut Membre
 
Le bug est que quand je sélectionne pierre feuille ou ciseaux, il me dit que fen2 n'est pas défini.
0
yg_be Messages postés 24281 Date d'inscription   Statut Contributeur Dernière intervention   1 585 > Le_Pythoneur_Debutant Messages postés 3 Statut Membre
 
je pense que tu devrais commencer par des exercices plus simples.
lecture utile: https://www.pierre-giraud.com/python-apprendre-programmer-cours/portee-variable/
0