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
| # -*- coding: utf-8 -*-
"""
Created on Tue Nov 26 17:49:04 2019
@author: mihai
"""
#!/usr/bin/env python3
# coding: utf-8
from tkinter import *
def KeyBoard(event):
global tank
Key=event.keysym.lower()
print(Key)
for t in tank:
for (k, v) in t["key"].items():
if Key == k and v!="tirer":
(lig, col)=(t["pos"][0] + move[v][0], t["pos"][1] + move[v][1])
if Verification(t, lig, col):
LC[t["pos"][0]][t["pos"][1]]=0
(t["pos"][0], t["pos"][1])=(lig, col)
LC[t["pos"][0]][t["pos"][1]]=2
MyCanvas.coords(t["canvas"], t["pos"][0]*20, t["pos"][1]*20,
t["pos"][0]*20+20, t["pos"][1]*20+20)
break
elif v == "tirer":
bullets.append([MyCanvas.create_oval(t["pos"][0]*20, t["pos"][1]*20,
t["pos"][0]*20+20,t["pos"][1]*20+20,fill="black"),
dx,dy])
def play():
for b in bullets:
MyCanvas.move(b[0],b[1],b[2])
MyWindow.after(dt,play)
def Verification(tank, lig, col):
if LC[lig][col]:
return False
return True
dx,dy=5,5
dt=50
move={
"haut" : (0, -1),
"bas" : (0, 1),
"gauche" : (-1, 0),
"droite" : (1, 0),
}
tank=({ "nom" : "Tank1",
"pos" : [10, 7],
"fill" : "red",
"key" : {"up" : "haut", "left" : "gauche", "down" : "bas", "right" : "droite","m":"tirer"},},
{ "nom" : "Tank2",
"pos" : [5, 2],
"fill" : "green",
"key" : {"z" : "haut", "q" : "gauche", "s" : "bas", "d" : "droite","space":"tirer"},})
LC=[[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,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0],
[0,0,0,0,1,1,0,0,0,0,0,0,0,0,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,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,0,0,0,0,0],
[0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0],
[0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0],
[0,0,0,0,1,1,0,0,0,0,0,0,0,0,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,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,0,0,0,0,0],
[0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0],
[0,0,0,0,1,1,0,0,0,0,0,0,0,0,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,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,0,0,0,0,0]]
bullets = []
MyWindow=Tk()
MyWindow.title('Piece')
MyCanvas=Canvas(MyWindow, width=400, height=400, bg='white')
for t in tank:
t["canvas"]=MyCanvas.create_oval(
t["pos"][0]*20,
t["pos"][1]*20,
t["pos"][0]*20+20,
t["pos"][1]*20+20,
width=2,
outline='black',
fill=t["fill"],
)
MyCanvas.focus_set()
MyCanvas.bind('<Key>',KeyBoard)
MyCanvas.pack(padx=50, pady=50)
#MyCanvas.coords(tank[0]["canvas"])
#MyCanvas.coords(tank[1]["canvas"])
Button(MyWindow, text='Exit', command=MyWindow.destroy).pack(side=LEFT,padx=5,pady=5)
Button(MyWindow, text='Play', command=play).pack(side=LEFT,padx=5,pady=5)
for j in range(20):
for i in range(20):
if LC[j][i]==1:
MyCanvas.create_rectangle(j*20,i*20,j*20+20,i*20+20,fill='brown')
"""
i=0
j=0
while j < 15:
while i<20:
if LC[j][i]==1:
MyCanvas.create_rectangle(j*20,i*20,j*20+20,i*20+20,fill='black')
i=i+1
i=0
j=j+1"""
MyWindow.mainloop() |