AlpacinoNogeek
Messages postés12Date d'inscriptionlundi 16 avril 2018StatutMembreDernière intervention 2 mars 2020
-
Modifié le 17 avril 2018 à 16:05
critou -
17 avril 2018 à 19:46
Bonjour à tous et à toutes. Le problème du jour: Comment faire en sorte pour que les collisions (tirs du joueur - briques) fonctionnent ?
from tkinter import *
from random import *
from tkinter.messagebox import*
import sys
from random import shuffle
fenetre=Tk()
fenetre.title('Licorne Invaders')
fenetre.geometry("640x480")
#creation de la fenetre de jeu
Fond=Canvas(fenetre,width=800,height=600,bg='black')
fichier6=PhotoImage(file="images/image_de _début.gif")
Fond.create_image(330,230,image=fichier6)
Fond.place(x=0,y=0)
#creation du canvas
tirs = []
def creer_tir(event):
x = xplateforme1+30
tir = Fond.create_oval(x-5, 590, x+5, 600, fill='red', outline ='blue')
tirs.append(tir)
def deplacer_tirs():
for tir in tirs:
x, y, x2, y2 = Fond.coords(tir)
if y2 > 0:
Fond.move(tir, 0, -1)
else:
Fond.delete(tir)
tirs.remove(tir)
fenetre.after(3, deplacer_tirs)
def Creation_Abris():
global ListeAbris,CoordonneesBriques
ListeAbris=[]
CoordonneesBriques=[]
i=0
x=40
y=340
while i<3:
limX=x+120
limY=y+60
departx=x
while y<limY:
while x<limX:
ListeAbris.append(Fond.create_rectangle(x,y,x+20,y+20,fill='grey'))
CoordonneesBriques.append([x,y])
x+=20
x=departx
y+=20
i+=1
x+=220
y-=60
def creation_plateforme():
global plateforme,xplateforme1,yplateforme1
plateforme=[]
xplateforme1=307
yplateforme1=450
#definition de x et y
def steam2():
global bou2, fichier, bou4,bou9,plateforme,xplateforme1,yplateforme1
bou4.destroy()
bou2.destroy()
bou9.destroy()
fichier=PhotoImage(file="images/SPACE.gif")
Fond.create_image(307,155,image=fichier)
plateforme.append(Fond.create_rectangle(xplateforme1,yplateforme1,xplateforme1+59,yplateforme1+17,fill='green'))
ListeAbris=[]
CoordonneesBriques=[]
i=0
x=40
y=340
while i<3:
limX=x+120
limY=y+60
departx=x
while y<limY:
while x<limX:
ListeAbris.append(Fond.create_rectangle(x,y,x+20,y+20,fill='grey'))
CoordonneesBriques.append([x,y])
x+=20
x=departx
y+=20
i+=1
x+=220
y-=60
fenetre.bind('<Key-space>', creer_tir)
fenetre.after(3, deplacer_tirs)
#steam2 est creee a partir d'un autre ecran de jeux qui n'a pas les meme bouttons, donc pour ne pas beuger, on crée une deuxieme fonction steam
def steam():
global bou2, fichier, bou4, bou3
bou2.destroy()
bou3.destroy()
fichier=PhotoImage(file="images/SPACE.gif")
Fond.create_image(307,155,image=fichier)
plateforme.append(Fond.create_rectangle(xplateforme1,yplateforme1,xplateforme1+59,yplateforme1+17,fill='green'))
ListeAbris=[]
CoordonneesBriques=[]
i=0
x=40
y=340
while i<3:
limX=x+120
limY=y+60
departx=x
while y<limY:
while x<limX:
ListeAbris.append(Fond.create_rectangle(x,y,x+20,y+20,fill='grey'))
CoordonneesBriques.append([x,y])
x+=20
x=departx
y+=20
i+=1
x+=220
y-=60
fenetre.bind('<Key-space>', creer_tir)
fenetre.after(3, deplacer_tirs)
#creation de la fonction steam, fenetre de jeux principal
def liste():
global fichier3, bou3, bou2, bou4,bou7
bou1.destroy()
bou3.destroy()
bou7.destroy()
fichier3=PhotoImage(file="images/instru.gif")
Fond.create_image(320,240,image=fichier3)
bou4=Button(fenetre,text="Lancer le jeu !", command = start2)
bou4.pack()
#fonction liste pour les commandes
def credit():
global bou7, bou3, bou1, fichier5,bou9
bou7.destroy()
bou1.destroy()
bou3.destroy()
fichier5=PhotoImage(file="images/crédit.png")
Fond.create_image(320,240,image=fichier5)
bou9=Button(fenetre, text="Lancer le jeu !", command =start)
bou9.pack()
#fonction credits, pour nous, et une question de design (pour avoir 3 bouttons sur la premiere fenetre)
def start2():
global fichier2, bou2, bou1,bou3,bou7,fichier6,bou4
bou3.destroy()
bou1.destroy()
bou7.destroy()
bou4.destroy()
fichier2=PhotoImage(file="images/fond.png")
Fond.create_image(325,240,image=fichier2)
bou2=Button(fenetre,text="Je suis pret !", command = steam)
bou2.pack(side=BOTTOM)
#meme chose que pour la fonction steam2, on crée une deuxieme fonction strart
def start():
global fichier2, bou2, bou1,bou3,bou7,bou9
bou1.destroy()
bou3.destroy()
bou7.destroy()
bou9.destroy()
fichier2=PhotoImage(file="images/fond.png")
Fond.create_image(325,240,image=fichier2)
bou2=Button(fenetre,text="Je suis pret !", command = steam)
bou2.pack(side=BOTTOM)
#fonction start pour mettre le joueur dans une certaine ambiance
def start3():
global fichier2, bou2, bou1,bou3,bou7,bou9
bou1.destroy()
bou3.destroy()
bou7.destroy()
fichier2=PhotoImage(file="images/fond.png")
Fond.create_image(325,240,image=fichier2)
bou2=Button(fenetre,text="Je suis pret !", command = steam)
bou2.pack(side=BOTTOM)
bou7=Button(fenetre, text="Crédits",command=credit)
bou7.pack(side=TOP)
bou3=Button(fenetre, text="Comment jouer ?", command =liste)
bou3.pack(side=BOTTOM)
bou1=Button(fenetre, text="Lancer le jeu !", command =start3)
bou1.place(x=277,y=220)
#premiers bouttons du menu de base
def move_plateforme(dx):
global xplateforme1,yplateforme1,drapeau,plateforme
xplateforme1=xplateforme1+dx
if xplateforme1<=35:
xplateforme1=35
Fond.coords(plateforme[0],xplateforme1,yplateforme1,xplateforme1+60,yplateforme1+20)
elif xplateforme1>=550:
xplateforme1=550
Fond.coords(plateforme[0],xplateforme1,yplateforme1,xplateforme1+60,yplateforme1+20)
else:
Fond.coords(plateforme[0],xplateforme1,yplateforme1,xplateforme1+60,yplateforme1+20)
def right(event):
move_plateforme(10)
fenetre.bind("<Key-Right>",right)
creation_plateforme()
def left(event):
move_plateforme(-10)
fenetre.bind("<Key-Left>",left)
def up(event) :
move_1plateforme(-10)
fenetre.mainloop()
try:
fenetre.destroy()
except TclError:
pass
J'aimerais savoir à quoi servent ces duplications de fonctions ?
L'utilité première des fonctions est justement la réutilisation, faire des copier/coller de portions entières de code est une très très mauvaise pratique.
En ce qui concerne ton problème de collisions, il faut te renseigner sur la méthode find_overlapping du widget Canvas de tkinter qui te mâchera ce travail.