穿过墙壁的字符

时间:2014-05-09 13:11:58

标签: python tkinter collision

我的迷宫游戏取得了一些进展。 但是,有一个问题非常持久:角色现在正在移动,但它也在穿过墙壁。 这很烦人,我想知道是否有一个简单的命令来阻止它这样做。 (我已经尝试过使用bbox方法和查找重叠但我必须对它们做错了...) 这是代码:

from tkinter import *
import pygame # Note : importation pygame pour pouvoir jouer des sons (musique)/ importing pygame Library to play music



pygame.mixer.init()
pygame.mixer.music.load("alerte.wav") # musique d'intro/ intro music
pygame.mixer.music.play() 


# Position / fonction that is in charge of moving the character

# Boucle principale / main loop
def Clavier(event) :
    global XP,YP
    touche = event.keysym
    # déplacement vers le haut / moving up
    if touche == 'Up':
        YP -= 20
    # déplacement vers le bas / moving down
    if touche == 'Down':
        YP += 20
    # déplacement vers la droite / moving right
    if touche == 'Right':
        XP += 20
    # déplacement vers la gauche / moving left
    if touche == 'Left':
        XP -= 20
    Fond.coords(Pion,XP -10, YP -10, XP +10, YP +10)
    Fond.update()



def lisDecors(fichier):
    """
    Fonction qui lis le contenu du fichier et la place dans
    la liste 2D Décors / function that reads a file that contains the level and place it in the list that con,tains the 2D background
    """
    filin = open(fichier,'r')
    R = [list(line.replace('\n','')) for line in filin]
    filin.close()
    return R

def dessine():
    """
    Fonction qui dessine le plateau de jeu avec les données de la liste Décors / Function that draws the game's background with the help of the list that contains the 2D background
    """
    ligne, colonne = 0, 0
    while ligne < 17 :
      if Decors[ligne][colonne] == 'X' :
        Fond.create_image(colonne*40, ligne*40, image=X, anchor=NW)
      if Decors[ligne][colonne] == 'T' :
        Fond.create_image(colonne*40, ligne*40, image=T, anchor=NW)
      colonne=colonne+1
      if colonne == 25 :
        colonne = 0
        ligne = ligne + 1


fenetre=Tk()
fenetre.resizable(width=False, height=False)

fenetre.title("Projet Deadalus - Prepare 4 F3AR")
fenetre.geometry("1200x680")
XP, YP = 80, 80

# Dessin de l'interface / Drawing main Canvas
Fond=Canvas(fenetre,width=1200,height=680,bg="#000000")
Pion = Fond.create_oval(XP-10,YP-10,XP+10,YP+10,width=2,outline='black',fill='red')
Fond.place(x=0,y=0)
Fond.focus_set()
Fond.bind('<Key>',Clavier)
Fond.pack(padx =5, pady =5)
Fond.create_rectangle(1000,0,1200,680,fill="black",width=5,outline="black")
Txt=Fond.create_text(1100,275,text="Projet Deadalus",font=("OCRAExtended","15"),fill="#0B0")
Texte=Fond.create_text(1100,300,text="Prepare 4 F3AR",font=("OCRAExtended","15"),fill="#0B0")


# Chargement des fichiers / loading files
T=PhotoImage(file="images/FondS.gif")
X=PhotoImage(file="images/brick.gif")




# On lis le décors. On garde les informations du décors dans une liste pour
# pouvoir tester si on tombe, si on peut monter, .... / Reading background file
Decors = lisDecors('niveaux/niv1.txt')
dessine()







fenetre.mainloop()

0 个答案:

没有答案
相关问题