Python 穿墙的角色

Python 穿墙的角色,python,tkinter,collision,Python,Tkinter,Collision,我在迷宫游戏方面取得了一些进展。 然而,有一个问题是相当持久的:角色现在正在正确移动,但它也在穿墙移动。 这很烦人,我想知道是否有一个简单的命令来阻止它这样做。(我已经尝试过bbox方法和查找重叠,但我一定是做错了什么…) 代码如下: from tkinter import * import pygame # Note : importation pygame pour pouvoir jouer des sons (musique)/ importing pygame Library to p

我在迷宫游戏方面取得了一些进展。 然而,有一个问题是相当持久的:角色现在正在正确移动,但它也在穿墙移动。 这很烦人,我想知道是否有一个简单的命令来阻止它这样做。(我已经尝试过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()
从tkinter导入*
导入pygame#注意:导入pygame pour pouvoir jouer des sons(音乐)/导入pygame库以播放音乐
pygame.mixer.init()
pygame.mixer.music.load(“alert.wav”)#musique d'intro/intro music
pygame.mixer.music.play()
#负责移动角色的位置/功能
#主回路/主回路
def键盘(事件):
全球XP,YP
touche=event.keysym
#dévers le haut/向上移动
如果touche==“向上”:
YP-=20
#déverse le bas/向下移动
如果触地==‘向下’:
YP+=20
#dévers la droite/向右移动
如果touche=='Right':
XP+=20
#dévers la gauche/左移
如果触摸==‘左’:
XP-=20
坐标系(π介子,XP-10,YP-10,XP+10,YP+10)
fund.update()
def lisDecors(菲舍尔):
"""
菲舍尔和丹斯广场酒店
la liste 2D Décors/函数,用于读取包含标高的文件并将其放置在包含2D背景的列表中
"""
菲林=开放(菲希尔,'r')
R=[文件中行的列表(行.替换('\n','')]
菲林·克洛斯
返回R
def dessine():
"""
在包含2D背景的列表帮助下绘制游戏背景的功能
"""
林,科隆=0,0
当ligne<17时:
如果花色[ligne][colonne]=“X”:
创建图像(colonne*40,ligne*40,图像=X,锚定=NW)
如果花色[ligne][colonne]=“T”:
创建图像(colonne*40,ligne*40,图像=T,锚定=NW)
科隆=科隆+1
如果colonne==25:
科隆=0
ligne=ligne+1
fenetre=Tk()
fenetre.可调整大小(宽度=假,高度=假)
fenetre.标题(“项目设计-准备4 F3AR”)
费内特几何(“1200x680”)
XP,YP=80,80
#Desin de l’界面/绘图主画布
Fond=画布(fenetre,宽度=1200,高度=680,背景=“#000000”)
Pion=Fond.创建椭圆(XP-10,YP-10,XP+10,YP+10,宽度=2,轮廓为黑色,填充为红色)
地点(x=0,y=0)
found.focus_set()
found.bind(“”,键盘)
Fond.pack(padx=5,pady=5)
创建一个矩形(1000,01200680,fill=“black”,width=5,outline=“black”)
Txt=Fond.create_text(1100275,text=“Projet Deadalus”,font=(“OCRAExtended”,“15”),fill=“#0B0”)
Texte=Fond。创建_文本(1100300,text=“Prepare 4 F3AR”,font=(“OCRAExtended”,“15”),fill=“#0B0”)
#文件计费/加载文件
T=PhotoImage(file=“images/FondS.gif”)
X=PhotoImage(file=“images/brick.gif”)
#在丽斯·勒代科斯。关于你所需要的信息
#pouvoir测试仪si在tombe上,si在peut monter上,…/读取背景文件
花色=lisDecors('niveaux/niv1.txt')
德辛()
fenetre.mainloop()

请您将其减少到最低限度,好吗?试图阻止角色穿墙的代码在哪里?到目前为止,您认为您的尝试有什么错误?您必须实现碰撞检测。没有“简单的命令可以这么做”,但是您提到的使用边界框来查找重叠区域的方法是一个很好的开始,因此您可能希望在这方面寻求帮助-否则,这个问题可能太广泛了。是的,我想了解一些关于bbox方法的决定。我的理论是Tkinter不“喜欢”。gifs(这个角色拒绝移动,直到我用一个椭圆形替换了它的.gif…@MaxFR68:你说Tkinter不“喜欢”gifs是没有道理的。图像是Tkinter的头等公民。如果你使用得当,它们可以像其他任何东西一样轻松移动。