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
| import tkinter as tk
import time
class EtudeSpaceInvanders(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.can = tk.Canvas(self, width=1920, height=1080, background='black')
self.can.pack()
self.image_id= dict()
self.photo_ovni = dict()
self.dx = 50
self.dy = 0
def init_ovni(self, image_path, indice_ovni, x_init, y_init,etiquette):
self.photo_ovni[indice_ovni] = tk.PhotoImage(file=image_path)
self.image_id[indice_ovni] = self.can.create_image(x_init,
y_init,anchor='center',image=self.photo_ovni[indice_ovni],
tags=etiquette)
def init_vaisseau_humain(self,image_path, indice, x_init, y_init,etiq):
self.photo_vaisseau_humain = tk.PhotoImage(file=image_path)
self.image_id[indice] = self.can.create_image(x_init,
y_init,anchor='center',image=self.photo_vaisseau_humain,
tags=etiq)
self.bind("<KeyPress-w>",self.gauche)
self.bind("<KeyPress-b>",self.droite)
self.update()
def gauche(self,event):
self.can.move("vaisseau_humain", -4, 0)
def droite(self,event):
self.can.move("vaisseau_humain", 4, 0)
def set_directions(self, dx, dy):
self.dx = dx
self.dy = dy
def deplacetag(self,etiquette):
self.can.move(etiquette, self.dx, self.dy)
self.update()
################################################################################
if __name__ == "__main__":
im = EtudeSpaceInvanders()
vaisseau = im.init_vaisseau_humain("vaisseau.png",
"a", 100, 900, "vaisseau_humain")
indice = 1
x = 60
for i in range(10):
"""création de 10 ovni sur deux lignes.
1er ligne --->indice, 2 lignes --->indice+100"""
photo1 = im.init_ovni("Invaders1.png",
indice, x, 60, "truc")
photo2 = im.init_ovni("Invaders1.png",
indice+100, x, 160, "truc")
x += 120
indice +=1
im.set_directions(1,0)
sens = 1
for i in range(30):
for i in range(700):
im.deplacetag("truc")
#time.sleep(1/400)
sens = -1 * sens
im.set_directions(sens,0) |
Partager