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
|
from tkinter import*
from math import*
def Clic(event):
""" Gestion de l'événement Clic gauche """
global DETECTION_CLIC_SUR_OBJET
# position du pointeur de la souris
X = event.x
Y = event.y
if monstre.X-TailleMob/2<=X<=monstre.X+TailleMob/2 and monstre.Y-TailleMob/2<=Y<=monstre.Y+TailleMob/2:
DETECTION_CLIC_SUR_OBJET = True
else:
DETECTION_CLIC_SUR_OBJET = False
def Drag(event):
""" Gestion de l'événement bouton gauche enfoncé """
X = event.x
Y = event.y
if DETECTION_CLIC_SUR_OBJET == True:
# limite de l'objet dans la zone graphique
if X<0: X=0
if X>Largeur_Fen: X=Largeur_Fen
if Y<0: Y=0
if Y>Hauteur_Fen: Y=Hauteur_Fen
# mise à jour de la position de l'objet (drag)
monstre.déplacement(X,Y)
def Distance(x1,y1,x2,y2):
l=sqrt((x2-x1)**2+(y2-y1)**2)
return l
def Déplacement(x_mob,y_mob):
[x_mob,y_mob] = Canevas.coords(mob)
xB=(40/Distance(Centre_Tour[0],Centre_Tour[1],x_mob,y_mob)*(x_mob-Centre_Tour[0])+Centre_Tour[0])
yB=(40/Distance(Centre_Tour[0],Centre_Tour[1],x_mob,y_mob)*(y_mob-Centre_Tour[1])+Centre_Tour[1])
Canevas.coords(Canon, 200+TailleTour/2, 200+TailleTour/2, xB, yB)
class Tour(object):
def __init__(self, X, Y):
self.X = X
self.Y = Y
self._range = 150
self.damage = 1
self._dps = 10
#Créer la tour
X = self.X
Y = self.Y
range = self._range
Centre_Tour = [X,Y]
Tourelle = Canevas.create_image(Centre_Tour, image=imgTour)
Canevas.create_oval(X-range, Y+range, X+range, Y-range)
Ext_Canon=[X, Y-25]
xy_Canon = [Centre_Tour,Ext_Canon]
self.Canon = Canevas.create_line(xy_Canon,fill='black',width=4)
def Canon1(self,X_mob,Y_mob):
X = self.X
Y = self.Y
Canon = self.Canon
Centre_Tour = [X,Y]
[x_mob,y_mob] = [X_mob,Y_mob]
xB=(25/Distance(Centre_Tour[0],Centre_Tour[1],x_mob,y_mob)*(x_mob-Centre_Tour[0])+Centre_Tour[0])
yB=(25/Distance(Centre_Tour[0],Centre_Tour[1],x_mob,y_mob)*(y_mob-Centre_Tour[1])+Centre_Tour[1])
Canevas.coords(Canon,X,Y,xB,yB)
def Portée(self,cible):
[x_mob,y_mob] = [cible.X,cible.Y]
d = (x_mob-self.X)**2 + (y_mob-self.Y)**2
if (d<140**2):
self.Canon1(x_mob,y_mob)
self.attaque(cible)
def attaque(self, cible):
if cible.vie > 0:
cible.vie = cible.vie - self.damage
else:
Canevas.delete(fenetre,cible.mob)
del cible
print(cible.vie)
class Mob(object):
def __init__(self, X, Y):
self.X = X
self.Y = Y
self.vie = 150
self.mob = Canevas.create_image(X, Y, image=imgmob)
def déplacement(self, X, Y):
self.X = X
self.Y = Y
mob = self.mob
Canevas.coords(mob,X,Y)
a.Portée(monstre)
b.Portée(monstre)
c.Portée(monstre)
fenetre = Tk()
imgTour = PhotoImage(file='Tour.gif')
imgmob = PhotoImage(file='Mob.gif')
TailleMob = 20
Largeur_Fen = 600
Hauteur_Fen = 600
pv_mob = 3
Canevas = Canvas(fenetre,width=Largeur_Fen,height=Hauteur_Fen,bg ='white')
Canevas.pack(padx=10,pady=10)
monstre = Mob(40,40)
vie=Canevas.create_text(10,10, text=monstre.vie)
a=Tour(200,200)
b=Tour(320,200)
c=Tour(260,310)
Canevas.bind('<Button-1>',Clic)
Canevas.bind('<B1-Motion>',Drag)
fenetre.mainloop() |
Partager