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
|
from tkinter import *
# Quand on appuie sur une touche, on l'ajoute à la liste
def enfoncee(evt) :
if evt.keysym not in Touches :
Touches.append(evt.keysym)
# Quand on relache la touche, on la retire
def relachee(evt) :
if evt.keysym in Touches :
Touches.remove(evt.keysym)
#deplacement au clavier
def animation():
global x,y
if 'Right' in Touches:
# On regarde ce qu'il y a à droite du personnage 40x40
if decor[(y//40)][(x//40)+1] in (' ','H') :
x = x + 20
Fond.itemconfig(img,image=fichierimg2,anchor='nw')
print(x,y)
if 'Left'in Touches :
# On regarde ce qu'il y a à gauche du personnage 40x40
if decor[(y//40)][((x-20)//40)] in (' ','H') :
x = x - 20
Fond.itemconfig(img,image=fichierimg,anchor='nw')
print(x,y)
if 'Up' in Touches :
# On regarde si il y a une echelle au-dessus du personnage 40x40
if decor[(y+20)//40-1][x//40] == 'H' :
y = y - 20
print(x,y)
if 'Down' in Touches :
# On regarde si il y a une echelle en dessous du personnage 40x40
if decor[y//40+1][x//40] == 'H' :
y = y + 20
print(x,y)
if "space" in Touches :
# le personnage 40x40 saute
x = x+40
y = y-40
#le personnage 40x40 atterit
x = x+40
y = y+40
Fond.coords(img,x,y)
fenetre.after(80,animation)
#creation de la fenêtre graphique et du canvas
fenetre=Tk()
fenetre.title("Super Goomba Bros")
fenetre.geometry("1200x640")
Fond=Canvas(fenetre,width=1500,height=680,bg="white")
Fond.place(x=0,y=0)
#chargement du personnage
fichierimg=PhotoImage(file="goomba gauche.gif")
fichierimg2=PhotoImage(file="goomba droite.gif")
img=Fond.create_image(100,380,image=fichierimg2,anchor="nw")
#chargement du décor
F_Brique=PhotoImage(file="brique.gif")
F_Piege=PhotoImage(file="piege.gif")
F_Echelle=PhotoImage(file="echelle.gif")
#dessine le décor et l'enregistre dans une liste 2D
x, y = 0, 0
fichier = open("niveau2.txt")
decor=[]
for ligne in fichier :
decor.append(list(ligne))
for i in range(30) :
case=ligne[i]
if case == 'X' :
Fond.create_image(x,y,image=F_Brique,anchor="nw")
if case == 'P' :
Fond.create_image(x,y,image=F_Piege,anchor="nw")
if case == 'H' :
Fond.create_image(x,y,image=F_Echelle,anchor="nw")
x = x + 40
x = 0
y = y + 40
fichier.close()
#position initiale du personnage
x,y=40,80
Fond.coords(img,x,y)
print(decor[5][11])
# Surveillance des touches
Touches = []
fenetre.bind_all("<KeyPress>",enfoncee)
fenetre.bind_all("<KeyRelease>",relachee)
animation()
fenetre.mainloop() |
Partager