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
| from tkinter import*
from random import randint
def defile() :
global x4 , y4 , x1 , x2 , y2 , x
x1 = x1 +1
can.move('obstacle', 0,10)
can.move('mob', -5,0)
y4 = y4+13
collision()
fen.after(50, defile)
if (y3==50) :
apparition()
def droite(event):
global x1 , x2 , x3 , y2
print(x2)
if(x2>1160):
can.move('perso',0,0)
else :
can.move('perso',10 , 0)
x2=x2+10
collision()
def gauche(event):
global x1 , x2 , x3 , y2
print(x2)
if(x2<130):
can.move('perso',0,0)
else :
can.move('perso',-10 , 0)
x2=x2-10
collision()
def apparition():
global x4 , y4
x4 , y4 = randint(500, 700) , randint(0, 10)
obs1 = can.create_image(x4,y4, image = obs, anchor='center', tag = ['obstacle']) #Apparition obstacle au hasard
obs2 = can.create_image(x4,y4, image = obs, anchor='center', tag = ['obstacle'])
obs3 = can.create_image(x4,y4, image = obs, anchor='center', tag = ['obstacle'])
def collision () :
global x2 , x1 , x2 , y2 , x3 , y3 , x
if (y4>690) :
if (( x2 > x4) and (x2<x4+170)) :
print('Collision detecter')
can.move('perso', 0 , 0)
can.move('obstacle', 0 , 0)
exit()
elif ((x2 + 350>x4) and (x2+350< x4 + 170)) :
print('Collision detecter')
can.move('perso', 0 , 0)
can.move('obstacle', 0 , 0)
exit()
x1 , y1 = 850 , 500
x2 , y2 = 300 , 700
x3 , y3 = 1500 ,700
x4 , y4 = 1500, 1500
fen = Tk()
can = Canvas(fen , width = 1700 , height = 1000, bg = 'white')
can.pack()
Fond_jeu = PhotoImage(file = 'fond.png')
Image_Affichee = can.create_image(x1 , y1 , image= Fond_jeu, anchor='center' , tag = ['route'])
Perso = PhotoImage(file = 'perso.png')
Image_Affichee = can.create_image(x2 , y2 , image= Perso, anchor='center' , tag = ['perso'])
#Mob = PhotoImage(file = 'mob.png')
#Image_Affichee = can.create_image(x3 , y3 , image= Mob, anchor='center' , tag = ['mob'])
obs = PhotoImage(file='obstacle.png')
obs1 = can.create_image(x4,y4, image = obs, anchor='center', tag = ['obstacle'])
fen.bind("<Right>" , droite)
fen.bind("<Left>" , gauche)
apparition()
defile()
collision()
fen.mainloop() |
Partager