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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
| # BIBLIOTHEQUE
from tkinter import *
from random import *
import time
# FONCTIONS
def change_couleur():
"""
changement aleatoire de la couleur de trace
"""
pal = ['purple', 'cyan', 'maroon', 'green', 'red', 'blue', 'orange', 'yellow']
c = randrange(8)
coul = pal[c]
return coul
def displayBoard(matrice):
global animAttaque, coord_tour, posx, posy, start_pos_X, start_pos_Y
ennemy = Ennemy()
size = 20
sizeEnnemy = 15
"""
fonction qui créer le canvas de fond
@param : matrice représentant le chemin
@return : rien
"""
a = 0
b = 0
for i in range(0, len(matrice)):
b = b + 40
a = 0
for j in range(len(matrice[i])):
a = a + 40
if (matrice[i][j]) == 0:
canvas.create_rectangle(a - size, b - size, a + size, b + size, fill='brown')
if (matrice[i][j]) == 1:
canvas.create_rectangle(a - size, b - size, a + size, b + size, fill='green')
if (matrice[i][j]) == 2 :
canvas.create_rectangle(a - size, b - size, a + size, b + size, fill='green')
canvas.create_oval(a - sizeEnnemy, b - sizeEnnemy, a + sizeEnnemy, b + sizeEnnemy, fill='blue')
if (matrice[i][j]) == 3:
canvas.create_rectangle(a - sizeEnnemy, b - sizeEnnemy, a + sizeEnnemy, b + sizeEnnemy, fill='black')
if (matrice[i][j]) == 2 and ennemy.life <= 0:
matrice[i][j] = 1
ennemy.posx = start_pos_X
ennemy.posy = start_pos_Y
ennemy.life = 100
matrice[start_pos_Y][start_pos_X] = 2
if (matrice[6][19]) == 2:
matrice[6][19] = 1
matrice[6][18] = 1
if animAttaque == TRUE:
canvas.create_rectangle(ennemy.posx * 40+20, ennemy.posy * 40+20,coord_tour[0],coord_tour[1], fill='purple')
animAttaque = FALSE
return ()
class Ennemy(object):
life = 100
valeur = 50
posx = 0
posy = 0
old_posx = 0
old_posy = 0
justCreated = TRUE
class tour (object) :
posx = 0
posy = 0
degats = 10
portee = 1
niveau = 1
quit = FALSE
isPlaying = FALSE
#myEnnemy = Ennemy()
lost = FALSE
animAttaque = FALSE
def checkEnd():
global isPlaying, lost, vie, board
if board[end_pos_Y][end_pos_X] == 2:
vie = vie - 1
if vie <= 0:
isPlaying = FALSE
lost = TRUE
return TRUE
return FALSE
def end():
global quit
quit = TRUE
champ_label = Label(fenetre, text = " vous avez perdu")
champ_label.pack(side = RIGHT)
def play(ennemy):
global argent
play = FALSE
check = 0
sv = StringVar()
champ_label1 = Label(fenetre, textvariable = sv, relief=RAISED)
champ_label = Label(fenetre, text="vous avez : ", relief=RAISED)
champ_label1.pack(side=RIGHT)
champ_label.pack(side=RIGHT)
while quit == FALSE:
time1 = time.clock()
if time1 - check > 0.1:
sv.set(str(argent))
play = TRUE
check = time.clock()
createEnnemy(ennemy)
#print(time1)
if isPlaying and play == TRUE and Ennemy.life > 0:
updateEnnemy(ennemy)
updateTurrets(ennemy)
play = FALSE
updateBoard()
fenetre.update()
if checkEnd():
end()
def checkIsOld(ennemy, y, x):
if ennemy.old_posy == y and ennemy.old_posx == x :
return TRUE
return FALSE
def getNewPos(ennemy, board):
if ennemy.posx + 1 <= len(board[0]):
if board[ennemy.posy][ennemy.posx + 1] == 1 or board[ennemy.posy][ennemy.posx + 1] == 2:
if checkIsOld(ennemy, ennemy.posy, ennemy.posx + 1) == FALSE:
ennemy.posx += 1
return
if ennemy.posx - 1 >= 0:
if board[ennemy.posy][ennemy.posx - 1] == 1 or board[ennemy.posy][ennemy.posx - 1] == 2:
if checkIsOld(ennemy, ennemy.posy, ennemy.posx - 1) == FALSE:
ennemy.posx -= 1
return
if ennemy.posy + 1 <= len(board):
if board[ennemy.posy + 1][ennemy.posx] == 1 or board[ennemy.posy + 1][ennemy.posx] == 2:
if checkIsOld(ennemy, ennemy.posy + 1, ennemy.posx) == FALSE:
ennemy.posy += 1
return
if ennemy.posy - 1 >= 0:
if board[ennemy.posy - 1][ennemy.posx] == 1 or board[ennemy.posy - 1][ennemy.posx] == 2:
if checkIsOld(ennemy, ennemy.posy - 1, ennemy.posx) == FALSE:
ennemy.posy -= 1
return
def Move(ennemy):
if ennemy.justCreated == TRUE:
ennemy.justCreated = FALSE
return
savey = ennemy.posy
savex = ennemy.posx
getNewPos(ennemy, board)
ennemy.old_posy = savey
ennemy.old_posx = savex
#print("Position y = " + str(ennemy.posy) + " Position x = " + str(ennemy.posx))
#print("OldPosition y = " + str(ennemy.old_posy) + " OldPosition x = " + str(ennemy.old_posx))
def createEnnemy(ennemy):
global isPlaying
if isPlaying == TRUE:
return
ennemy.posx = start_pos_X
ennemy.posy = start_pos_Y
def updateEnnemy(ennemy):
global board
board[ennemy.posy][ennemy.posx] = 1
Move(ennemy)
board[ennemy.posy][ennemy.posx] = 2
def updateTurrets(ennemy):
global board,animAttaque, argent, tour
for i in range (len(board)):
for j in range (len(board[i])):
if board[i][j] == 3:
for f in range (i-tour.portee,i+tour.portee):
for g in range (j-tour.portee,i+tour.portee):
if board[f][g] == 2 :
ennemy.life = ennemy.life - tour.degats
animAttaque = TRUE
if ennemy.life <= 0:
animAttaque = FALSE
argent = argent + ennemy.valeur
return()
def ameliorerTour () :
global tour, argent
if tour.niveau < 5 :
tour.portee = tour.portee + 1
tour.degats = tour.degats + 5
tour.niveau = tour.niveau + 1
argent = argent - 500
else :
champ_label = Label(fenetre, text = "portee max atteinte")
champ_label.pack(side = RIGHT)
tour.degats = tour.degats + 5
tour.niveau = tour.niveau + 1
argent = argent - 500
def ameliorerEnnemy ():
global Ennemy
Ennemy.life = Ennemy.life + 50
Ennemy.valeur = Ennemy.valeur + 5
def quitter(event):
global quit
quit = TRUE
def updateBoard():
global board
canvas.delete("all")
displayBoard(board)
def demarrer_manche():
global isPlaying
isPlaying = TRUE
return
def key_demarrer_manche(event):
demarrer_manche()
def poser_tour(event):
global canvas,board, argent, tour_posx, tour_posy, coord_tour
if event.x > 10 and event.x < 815 and event.y > 10 and event.y < 615:
cx = (event.x + 20) // 40 * 40 # coordonnees du centre du cercle
cy = (event.y + 20) // 40 * 40
coul = change_couleur()
a = cx//40
b = cy//40
a = a-1
b = b-1
x = event.x
y = event.y
if board [b][a] == 0 and argent > 0:
tour = canvas.create_oval(cx-15,cy-15,cx+15,cy+15,fill='yellow')
argent = argent - 100
else :
champ_label = Label(fenetre, text = 'impossible',fg= 'red')
champ_label.pack(side = TOP)
coord_tour = canvas.coords(tour)
x = (x - 25) / 40
y = (y - 25) / 40
tour_posx = int(x)
tour_posy = int(y)
board[int(y)][int(x)] = 3
return()
########
#
# programme principal
#
########
global board
board = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
global liste_tour
global argent
argent = 500
liste_tour = []
fenetre = Tk()
vie = 10
# creation des widgets "esclaves" de la fenetre principale
a = 40
b = 120
canvas = Canvas(fenetre, bg='dark grey', height=650, width=850)
champ_label = Label(fenetre, text="Tower Defense")
champ_label.pack(side=TOP)
bouton_start = Button(fenetre, text="Start", command=demarrer_manche) # fenetre.quit
bouton_start.pack(side=BOTTOM)
# Coordonnées de la balle au départ:
start_pos_X = 0
start_pos_Y = 2
# Coordonnées de la balle a la fin:
end_pos_X = 19
end_pos_Y = 6
# Déplacement de la balle au départ:
dx = 0
dy = 0
# poser tour
canvas.bind("<Button-1>", poser_tour)
canvas.pack(side = LEFT)
bouton = Button(fenetre, text="ameliorer tour pour 500", command=ameliorerTour)
bouton.pack()
# On cree une balle:
'''bouger(start_pos_X,start_pos_Y)'''
canvas.bind_all('<Escape>', quitter)
canvas.bind_all('<space>', key_demarrer_manche)
play(Ennemy) |
Partager