Plusieurs questions avec tkinter + Python 3.4, aidez-moi...

Fermé
Utilisateur anonyme - 10 mai 2015 à 23:00
 Utilisateur anonyme - 11 mai 2015 à 22:59
Bonjour à tous,

voilà, je suis étudiant au lycée avec l'option ISN (Informatique et Science du Numérique). Je dois rendre un projet pour le bac, et j'ai choisis un jeu en utilisant la bibliothèque tkinter, et la version 3.4 du logiciel. Voici un aperçu de mon petit programme ^^ :


from tkinter import*
from random import*

score=0

def Clavier(event):
global PosX, PosY, PX, PY, score
touche = event.char

#Déplacement vers le haut

if touche == 'z':
PosY -= 20

#Déplacement vers le bas

elif touche == 's':
PosY += 20

#Déplacement vers la droite

elif touche == 'd':
PosX += 20

#Déplacement vers la gauche

elif touche == 'q':
PosX -= 20

#Collision murs

if PosX < 0 or PosX > 1260 or PosY < 0 or PosY > 950:
print ("You loose!")
Snake.destroy()

#Manger un food

if PosX == PX and PosY == PY:

PX = randrange (20,391)
PX -= PX % 20

PY = randrange (20,391)
PY -= PY % 20

score += 1
print ("score :", score)

#On dessine le pion à sa nouvelle position

Canevas.coords(Pion,PosX, PosY, PosX+20, PosY+20)
Canevas.coords(Food,PX , PY, PX+20, PY+20)

#Fonction restart

def restart():
global PosX, PosY, PX, PY, score
score = 0
PosX = randrange (0,401)
PosX -= PosX % 20

PosY = randrange (0,401)
PosY -= PosY % 20

PX = randrange (20,391)
PX -= PX % 20

PY = randrange (20,391)
PY -= PY % 20

Canevas.coords (Pion, PosX, PosY, PosX + 20, PosY + 20)
Canevas.coords (Food, PX, PY, PX + 20, PY + 20)

print ("Nouvelle partie");

#Création de la fenêtre principale

Snake = Tk()

Snake.title ('Snake.version ALpha')

Snake.resizable (0,0)

#Position initiale du Snake

PosX = randrange (0,401)
PosX -= PosX % 20

PosY = randrange (0,401)
PosY -= PosY % 20

#Position initiale du Food

PX = randrange (20,391)
PX -= PX % 20

PY = randrange (20,391)
PY -= PY % 20

#Création d'un widget Canevas

Largeur = 1260

Hauteur = 950

Canevas = Canvas (Snake, width = Largeur, height = Hauteur, bg = 'white')

Pion = Canevas.create_rectangle (PosX, PosY, PosX+20, PosY+20, width=2, outline='black', fill='red')

Food = Canevas.create_oval (PX, PY, PX+20, PY+20, width=2, outline='black', fill='blue')

Canevas.focus_set()

Canevas.bind ('<KeyPress>', Clavier)

Canevas.pack (padx = 5, pady = 5)

#Création d'un widget "bouton Quitter"

Button (Snake, text = 'Quitter', command = Snake.destroy).pack (side = LEFT, padx = 5, pady = 5)
Button (Snake, text = 'Recommencer', command = restart).pack (side = RIGHT, padx = 5, pady = 5)

Snake.mainloop()


Alors, plusieurs choses:

-J'aimerais insérer un background, de la musique.
-J'aimerais que le score soit affiché dans mon canevas.

Voilà, en espérant que vous me répondiez au plus vite, merci :)
A voir également:

1 réponse

dsy73 Messages postés 9252 Date d'inscription dimanche 22 août 2010 Statut Contributeur Dernière intervention 23 octobre 2020 2 476
11 mai 2015 à 06:32
Salut
tu devrais utiliser la coloration syntaxique (bouton Code dans l'éditeur) pour afficher un code lisible et correct (indentations).
0
Utilisateur anonyme
11 mai 2015 à 22:59
dac, mais là c'est trop tard, non?
0