from tkinter import* from math import pi, sin, cos from random import randrange from random import* class Canon: def __init__(self, boss, x, y): self.boss = boss self.x1, self.y1 = x, y self.lbu = 50 self.x2, self.y2 = x + self.lbu, y self.buse = boss.create_line(self.x1, self.y1, self.x2, self.y2, width =10) r = 15 boss.create_oval(x-r, y-r, x+r, y+r, fill='red', width =3) self.rayon_obus = 10 self.obus =boss.create_oval(x, y, x, y, fill='red') self.anim =False self.explo = False self.xMax =int(boss.cget('width')) self.yMax =int(boss.cget('height')) def orienter(self, angle): self.angle = float(angle)*pi/180 self.x2 = self.x1 + self.lbu*cos(self.angle) self.y2 = self.y1 - self.lbu*sin(self.angle) self.boss.coords(self.buse, self.x1, self.y1, self.x2, self.y2) def feu(self): if not self.anim: self.anim =True self.boss.coords(self.obus, self.x2 -3, self.y2 -3, self.x2 +3, self.y2 +3) v = 15 self.vy = -v *sin(self.angle) self.vx = v *cos(self.angle) self.animer_obus() def animer_obus(self): if self.anim: self.boss.move(self.obus, int(self.vx), int(self.vy)) c = self.boss.coords(self.obus) xo, yo = c[0] +3, c[1] +3 if yo<60 or xo > 1227: self.anim =False self.boss.after(30, self.animer_obus) self.vy += 0.2 self.gerer_rebonds() ##self.explo = self.boss.create_oval(xo -12, yo -12, xo +12, yo +12, fill ='yellow', width =0) def gerer_rebonds(self): c = self.boss.coords(self.obus) # coord. résultantes # On en deduit le centre de l'obus xo = c[0] + self.rayon_obus yo = c[1] + self.rayon_obus # coord. du centre de l'obus if yo> 710 and yo<750 and xo > 1200 and xo<1250: self.anim = False self.explo = self.boss.create_oval(xo -12, yo -12, xo +12, yo +12, fill ='yellow', width =3) self.boss.after(350, self.fin_explosion) aleatoire4 = randint(0, 90) s1.set(aleatoire4) if yo < 0: self.vy = - self.vy def fin_explosion(self): self.boss.delete(self.explo) self.explo =False class Canon2: def __init__(self, boss, x, y): self.boss = boss self.x1, self.y1 = x, y self.lbu = 50 self.x2, self.y2 = x + self.lbu, y self.buse = boss.create_line(self.x1, self.y1, self.x2, self.y2, width =10) r = 15 boss.create_oval(x-r, y-r, x+r, y+r, fill='blue', width =3) self.rayon_obus = 10 self.obus =boss.create_oval(x, y, x, y, fill='red') self.anim =False self.explo = False self.xMax =int(boss.cget('width')) self.yMax =int(boss.cget('height')) def orienter(self, angle): self.angle = float(angle)*pi/180 self.x2 = self.x1 + self.lbu*cos(self.angle) self.y2 = self.y1 - self.lbu*sin(self.angle) self.boss.coords(self.buse, self.x1, self.y1, self.x2, self.y2) def feu(self): if not self.anim: self.anim =True self.boss.coords(self.obus, self.x2 -3, self.y2 -3, self.x2 +3, self.y2 +3) v = 15 self.vy = -v *sin(self.angle) self.vx = v *cos(self.angle) self.animer_obus() def animer_obus(self): if self.anim: self.boss.move(self.obus, int(self.vx), int(self.vy)) c = self.boss.coords(self.obus) xo, yo = c[0] +3, c[1] +3 if yo < 60 or xo < 10: self.anim =False self.vy += 0.2 self.boss.after(30, self.animer_obus) self.gerer_rebonds() def plus(): compte.configure(text=a) def gerer_rebonds(self): c = self.boss.coords(self.obus) # coord. résultantes # On en deduit le centre de l'obus xo = c[0] + self.rayon_obus yo = c[1] + self.rayon_obus # coord. du centre de l'obus if yo> 710 and yo<750 and xo > 10 and xo<60: self.anim = False self.explo=self.boss.create_oval(xo -12, yo -12, xo +12, yo +12, fill ='yellow', width =3) ## can.create_text(200, 100, text=(n+1)) ## n.remove ## n=1 ## Label(f, text ="points").pack() ## f.points =Label(f, text=(n+1), bg ='white') ## f.points.pack() ## self.boss.after(350, self.fin_explosion) self.hit =id aleatoire3 = randint(90, 180) s2.set(aleatoire3) if yo < 0: self.vy = - self.vy def fin_explosion(self): self.boss.delete(self.explo) self.explo =False class Score: def __init__(self, can): self.canvas = can self.add_score = 10 canvas.itemconfig(self.id, text='Your score = %s' % (self.scoreboard)) self.id = can.create_text(200,100, text='Your score = %s' %(self.scoreboard)) if __name__ == '__main__': f = Tk() f.title('salut') can = Canvas(f,width =1250, height =800, bg ='ivory', relief =SUNKEN) can.pack(padx =10, pady =10) ## my_image = PhotoImage(file='E:\\Capture2(2).gif') ## can.create_image(0, 50, anchor = NW, image=my_image) ## can.create_rectangle( 0, 50, 1250, 800, width = 8) c1 = Canon(can, 76, 720) c2 = Canon2(can, 1200, 720) aleatoire = randint(0, 90) aleatoire2 = randint(90, 190) s1 =Scale(f, label='angle', from_=90, to=0, command=c1.orienter, showvalue=0, activebackground='red') s1.pack(side=LEFT, pady =5, padx =20) s1.set(aleatoire) s2 =Scale(f, label='angle', from_=90, to=180, command=c2.orienter, showvalue=0, activebackground='red') s2.pack(side=RIGHT, pady =5, padx =20) s2.set(aleatoire2) Button(f, text='Feu !', command =c1.feu).pack(side=LEFT) Button(f, text='Feu !', command =c2.feu).pack(side=RIGHT) f.mainloop()