1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| #!/usr/bin/env python
# -*- coding: utf-8 -*-
from random import randrange
from Tkinter import *
# zone de définition des fonctions
def newgame():#definition des aprametres du jeu
global rondX, rondY, rectX, rectY, carreX, carreY, dep, direction, id_rond, id_rect, id_carre, nb_rond
# initialisation de dep
dep = 3 # on démarre à 3 pixels
nb_rond=0 #compteur de ronds avalés
# création de l'objet à attraper
rondX = randrange(5, 495)
rondY = randrange(5, 495)
id_rond=canevas.create_oval(rondX, rondY, rondX+5, rondY+5, outline='white', fill='green')
# création de l'obstacle
rectX = randrange(5, 495)
rectY = randrange(5, 495)
id_rect=canevas.create_rectangle(rectX, rectY, rectX+20, rectY+10, outline='black', fill='black')
# mise en mouvement possible du carré
direction = ''
move ()
# utilisation des flèches du clavier
fenetre.bind("<Right>", right)
fenetre.bind('<Left>', left)
fenetre.bind('<Up>', up)
fenetre.bind('<Down>', down)
# end def
def move():#definitions des mouvements du carré
global carreX, carreY, dep, direction, id_carre, nb_rond
# mise en mouvement
if direction == 'gauche':
carreX -= dep
elif direction == 'droite':
carreX += dep
elif direction == 'haut':
carreY -= dep
elif direction == 'bas':
carreY += dep
# end if
# détection de collisions
d = dep//4
collisions = canevas.find_overlapping(carreX-d, carreY-d, carreX+10+d, carreY+10+d)
# collision avec le rond ?
if id_rond in collisions:
canevas.delete(id_rond)
nb_rond += 1
#nouveau_rond()
new_rond()
#nouvel_obstacle()
new_rect()
elif id_rect in collisions:
game_over()
move.stop()
#end if
#acceleration du carré possible
if dep <= 20:
dep +=0.1
# déplacement du carré
canevas.coords(id_carre, carreX, carreY, carreX+10, carreY+10)
# on reprend le prochain pas dans 100 ms
fenetre.after(100, move)
# Si sortie d'un côté, entrée de l'autre côté
if carreY > 500:
carreY = 0
elif carreY < 0:
carreY = 500
# end if
if carreX > 500:
carreX = 0
elif carreX < 0:
carreX = 500
# end if
if (carreX, carreY) == (rondX, rondY):
canevas.delete(id_carre)
# end def
def new_rect():#faire apparaitre un nouvel obstacle
# création de l'obstacle
rectX = randrange(5, 495)
rectY = randrange(5, 495)
canevas.create_rectangle(rectX, rectY, rectX+20, rectY+10, outline='black', fill='black')
# end def
def new_rond ():#faire apparaitre un nouveau rond
global rondX, rondY, id_rond
# création de l'objet à attraper
rondX = randrange(5, 495)
rondY = randrange(5, 495)
id_rond = canevas.create_oval(rondX, rondY, rondX+5, rondY+5, outline='white', fill='green')
# end def
def game_over():
global nb_rond
fenetre2 = Tk()
fenetre2.title('Cest fini !')
Label(fenetre2, text="Vous avez perdu. Vous avez mangé " + str(nb_rond)+".").pack(padx=0, pady=11)#vous avez donc maintenant +str(nb de rond+ nb bonnerep)
Button(fenetre2, text='Cancel', command=fenetre2.destroy).pack(side=RIGHT, padx=5, pady=5)
#Label2(fenetre2, text="Voulez-vous creer votre propre QCM ?").pack(padx=0, pady=11)
#Button(fenetre2, text='oui', command=).pack(side=LEFT, padx=5, pady=5)
# directions
def left (event):#mouvement vers la gauche
global direction
direction = 'gauche'
# end def
def right (event):#mouvement vers la droite
global direction
direction = 'droite'
# end def
def up (event):#mouvement vers le haut
global direction
direction = 'haut'
# end def
def down (event):#mouvement vers le bas
global direction
direction = 'bas'
# end def
# création de l'interface graphique
fenetre = Tk()
fenetre.title('Jeu Final')
# canevas graphique
canevas = Canvas(fenetre, width=500, height=500, bg='#046380')
canevas.pack(side=TOP, padx=5, pady=5)
# init coordonnées du carré
carreX, carreY = (235, 250)
# dessin du carré
id_carre = canevas.create_rectangle(carreX, carreY, carreX+10, carreY+10, outline='white', fill='#C03000')
# init boutons
Button(fenetre, text='New Game', command=newgame).pack(side=LEFT, padx=5, pady=5)
Button(fenetre, text='Quitter', command=fenetre.destroy).pack(side=RIGHT, padx=5, pady=5)
#infos
Label(fenetre, text="Cliquez sur 'New Game' pour commencer").pack(padx=0, pady=11)
# on lance la boucle principale
fenetre.mainloop() |