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
| #!/usr/bin/env python
# -*- coding: utf-8 -*-
from random import randrange
from Tkinter import *
# zone de définition des fonctions
def newgame(): # definition des parametres du jeu
global dep, direction
# initialisation de dep
dep = 3 # on démarre à 3 pixels
# création de l'objet à attraper
new_rond()
# création de l'obstacle
new_rect()
# 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
# 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)
new_rond()
new_rect()
# end if
# acceleration du carré possible
if dep <= 20:
dep +=0.1
# 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
# déplacement du carré
canevas.coords(id_carre, carreX, carreY, carreX+10, carreY+10)
# on reboucle dans un certain délai
fenetre.after(100, move)
# end def
def new_rect(): # faire apparaitre un nouvel obstacle
# création de l'obstacle
rectX = randrange(5, 495)
rectY = randrange(5, 495)
# NOTE:
# inutile de mémoriser ici l'ID de l'obstacle:
# si le carré joueur percute autre chose que id_rond alors
# c'est forcément un obstacle
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
# 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() |