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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
from tkinter import *
import tkinter.messagebox
from tkinter import messagebox
from math import *
import random
import time
score_int = 0
def regles(trigger = 0):
if trigger == 1:
Tk().wm_withdraw()
tkinter.messagebox.showinfo("Règles","L'objectif est de toucher la cible en choisissant l'angle et la vitesse initiale du tir. Vous pouvez choisir la difficulté en saisissant les nombre 1, 2 ou 3 pour choisir parmis les difficultés facile, moyen, et difficile")
def tir_parabolique(trigger = 0):
if trigger == 1:
def choix_diff():
global switch
global background
global width_c
global base
switch = 0
if difficulte_temp.get() == "1":
width_c = 700
can.config(width = 700, height = 300)
background = PhotoImage(file="Assets\\Images\\Tkinter\\Background\\paysage_700.png")
can.create_image(0, 0, anchor=NW, image=background)
base = can.create_image(50, 260, image=canon_et_hero)
elif difficulte_temp.get() == "2":
width_c = 900
can.config(width = 900, height = 300)
background = PhotoImage(file="Assets\\Images\\Tkinter\\Background\\mer.png")
can.create_image(0, 0, anchor=NW, image=background)
base = can.create_image(50, 260, image=canon_et_hero)
elif difficulte_temp.get() == "3":
width_c = 1100
can.config(width = 1100, height = 300)
background = PhotoImage(file="Assets\\Images\\Tkinter\\Background\\interstellar.png")
can.create_image(0, 0, anchor=NW, image=background)
base = can.create_image(50, 260, image=canon_et_hero)
can.create_image(width_c - 40, y_cible, image=cible)
def changer_cible(can, cible, diff):
# can.delete(cible_r)
print(diff)
can.delete(tkinter.ALL)
if diff == "1":
background = PhotoImage(file="Assets\\Images\\Tkinter\\Background\\paysage_700.png")
can.create_image(0, 0, anchor=NW, image=background)
can.update()
elif diff == "2":
background = PhotoImage(file="Assets\\Images\\Tkinter\\Background\\mer.png")
can.create_image(0, 0, anchor=NW, image=background)
can.update()
elif diff == "3":
background = PhotoImage(file="Assets\\Images\\Tkinter\\Background\\interstellar.png")
can.create_image(0, 0, anchor=NW, image=background)
can.update()
can.update()
x_cible = random.randint(width_c - 200, width_c - 30) # Génération de l'abcise de la cible
y_cible = random.randint(60, 240) # Génération de l'ordonnée de la cible
base = can.create_image(50, 260, image=canon_et_hero)
cible_r = can.create_image(x_cible, y_cible, image=cible)
can.update()
def Lancer_Tir():
global score_int
global cible_r
b = float(in_angle.get())*pi/180
Vo = float(in_vitesse.get())
x = 130
y = 225
t = 0
a = difficulte_temp.get()
#global image
#x_cible = width_c - 40
score = Label(fenetre, text="score : {0}".format(score_int))
score.grid(row=0, column=1)
can.update()
##################################################
# chargement des nouveaux elements
can.delete(base)
can.create_image(50, 260, image=canon)
perso = can.create_image(130, 225, image=hc45)
can.delete(perso)
can.update()
##################################################
while not(y <= 0 or y >= height_c or x >= width_c or (x >= x_cible and y_cible >= y + 23 >= y_cible + 79)) : # Déplacement du personnage
can.delete((perso))
can.update()
perso = can.create_image(x, y, image=hc45)
can.update()
time.sleep(0.025)
t += 0.025
x += (Vo*cos(b)*t)
y -= ((-0.5*9.81*(t**2)) + (Vo*sin(b)*t))
#print(x >= x_cible and y_cible >= y-23 >= y_cible + 79)
#print(x, y)
#print(x, y)
#print(x_cible, y_cible,y_cible + 79)
#print(width_c)
can.delete(perso)
image = can.create_image(-50, -50, image=hc45)
can.update()
if x >= x_cible and y + 23 >= y_cible and y - 23 <= y_cible + 79 and y + 48 >= y_cible :
tkinter.messagebox.showinfo("Win", "Bravo tu touches la cible !")
changer_cible(can, cible, a)
can.delete((perso))
can.update()
x = 130
y = 225
score_int += 10
elif y <= 0 or y >= height_c or x >= width_c:
tkinter.messagebox.showinfo("Game Over", "Perdu")
changer_cible(can, cible, a)
score_int = 0
can.delete((perso))
can.update()
x = 130
y = 225
switch = 1
#Démarrage fenêtre
fenetre = Tk()
fenetre.title("ShotDown")
fenetre.resizable(width=False, height=False)
# initialisation:
cible = PhotoImage(file="Assets\\Images\\Tkinter\\Personnage\\cible.png")
canon = PhotoImage(file="Assets\\Images\\Tkinter\\Canon\\canon.png")
canon_et_hero = PhotoImage(file="Assets\\Images\\Tkinter\\Canon\\canontete.png")
if switch == 1:
background = PhotoImage(file="Assets\\Images\\Tkinter\\Background\\paysage_700.png")
hc45 = PhotoImage(file="Assets\\Images\\Tkinter\\Personnage\hc45.png")
#Déclaration variables initiales
t = 0
x = 130
y = 225
choix = 0
global width_c
global height_c
global base
height_c = 300
width_c = 700
# Definition de la fenetre graphique (Canvas)
can = Canvas(fenetre, width=width_c, height=height_c)
can.grid(row=0, column=4, rowspan=4)
hc45 = PhotoImage(file="Assets\\Images\\Tkinter\\Personnage\\hc45.png")
x_cible = random.randint(width_c - 200, width_c - 30) # Génération de l'abcise de la cible
y_cible = random.randint(60, 240) # Génération de l'ordonnée de la cible
can.create_image(0, 0, anchor=NW, image=background)
image = can.create_image(-50, -50, image=hc45)
# Creation des boutons "difficulté" et "Lancer"
bou_tir = Button(fenetre, text='Tirer', width=8, command=Lancer_Tir)
bou_tir.grid(row = 0, column = 2)
bou_diff = Button(fenetre, text='Changer Difficulté', width=15, command=choix_diff)
bou_diff.grid(row = 0, column = 3)
#Saisie de l'angle
New_angle = StringVar()
New_angle.set("")
text_angle=Label(fenetre, text = "Angle :")
text_angle.grid(row = 1, column = 2)
in_angle=Entry(fenetre, textvariable = New_angle)
in_angle.grid(row = 1, column = 3)
#Saisie de la vitesse
New_vitesse = StringVar()
New_vitesse.set("")
text_vitesse = Label(fenetre, text="Vitesse :")
in_vitesse = Entry(fenetre, textvariable = New_vitesse)
text_vitesse.grid(row = 2, column = 2)
in_vitesse.grid(row = 2, column = 3)
#Saisie de la difficulté
difficulte_temp = StringVar()
difficulte_temp.set("")
text_diff = Label(fenetre, text = "Difficulté :")
entree_difficulte = Entry(fenetre, textvariable = difficulte_temp)
text_diff.grid(row = 3, column = 2)
entree_difficulte.grid(row = 3, column = 3)
#Affichage du canon et de la cible
base = can.create_image(50, 260, image=canon_et_hero)
cible_r = can.create_image(x_cible, y_cible, image=cible)
# demarrage de la boucle principale
fenetre.mainloop() |
Partager