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
|
from tkinter import *
from timeit import default_timer
def start():
chrono()
deplacement()
def chrono():
now = default_timer() - start
minutes, seconds = divmod(now, 60)
hours, minutes = divmod(minutes, 60)
str_time = '%d:%02d:%02d' % (hours, minutes, seconds)
canvas.itemconfigure(text_clock, text=str_time)
fenetre.after(1000,chrono)
#DEPLACEMENT DES OBSTACLES
def deplacement():
global yobstacle,id_after
yobstacle += 5
canvas.coords(obstacle,640,yobstacle)
if yobstacle == 880:
yobstacle = 0
# Appel récursif de la fonction deplacement
id_after = canvas.after(100,deplacement)
#DEPLACER LA VOITURE
def touche_pressee(event):
global x,y
#DEPLACEMENT LORSQU'ON EST SUR LA VOIE DU MILIEU
if 540<x<740 and event.keysym == 'Left':
x -= 190
y == 650
if 540<x<740 and event.keysym == 'Right':
x += 190
y == 650
#DEPLACEMENT LORSQU'ON EST SUR LA VOIE DE GAUCHE
if 340<x<539 and event.keysym == 'Left':
x += 0
y == 650
if 340<x<539 and event.keysym == 'Right':
x += 190
y == 650
#DEPLACEMENT LORSQU'ON EST SUR LA VOIE DE DROITE
if 741<x<940 and event.keysym == 'Left':
x -= 190
y == 650
if 741<x<940 and event.keysym == 'Right':
x += 0
y == 650
canvas.coords(voiture,x,y)
#METTRE LE JEU EN PAUSE
def stop():
global id_after
canvas.after_cancel(id_after)
# =============================================================================
# PROGRAMME PRINCIPAL
# =============================================================================
# Initialisation des variables de la voiture déplacement
x,y = 670,650
xobstacle, yobstacle = 640,0
fenetre = Tk()
fenetre.title("Champion's race")
# Création du canvas
canvas = Canvas(fenetre,height=720,width=1280)
canvas.pack()
# Chargement de l'image de fond
photo_fond = PhotoImage(file='fond.gif')
canvas.create_image(640,360,image=photo_fond)
# Affichage de la voiture
photo_voiture = PhotoImage(file='voiturejaune.gif')
voiture = canvas.create_image(670,650,image=photo_voiture)
# Affichage du logo du jeu
photo_logo = PhotoImage(file='TEXTE.gif')
canvas.create_image(1140,100,image=photo_logo)
# Affichage des obstacles
#OBSTACLE 1
photo_obstacle = PhotoImage(file='obstacle.gif')
obstacle = canvas.create_image(640,360,image=photo_obstacle)
# Création du bouton démarrer
bouton_démarrer = Button(fenetre,text='Démarrer',command=start)
bouton_démarrer.pack(side=LEFT)
# Création du bouton pause
bouton_stop = Button(fenetre,text='Pause',command=stop)
bouton_stop.pack()
# Création du bouton quitter
bouton_quitter = Button(fenetre,text='Quitter',command=fenetre.destroy)
bouton_quitter.pack(side=RIGHT)
# Affichage du score (temps)
start = default_timer()
text_clock = canvas.create_text(1125,180,width=200,fill="white",font=("Arial",40))
# Gestion des touches enfoncées
fenetre.bind('<Key>',touche_pressee)
fenetre.mainloop() |
Partager