我有一个tkinter框架和2个精灵,当我在墙的确切位置时,可以轻松地使vel = 0。问题是,在精灵内(我需要角色在碰到墙的边缘时停下来)。
import tkinter ##SETUP##
from tkinter import *
import threading
from threading import Timer
window = Tk() ##WINDOW SETUP##
window.title("Project Gladiator, Pre-Alpha 0.3")
window.config(bg = "white")
frame = Frame(window, width = 1000, height = 1000)
frame.grid(row = 0, column = 0)
frame.focus_set()
Px = 50 #char pos ##VARIABLES##
Py = 50 #char pos
X = 5 #char Xcel
Xchange = 5 #sets X
Direction = "Down" #different sprites based on movement
Level = 0 #used for collision (starts from 0)
PlayerF1 = PhotoImage(file = "Mainchar.png") #replace by each frame ##MAIN CHAR##
Playerd = Label(frame, image = PlayerF1)
Playerd.place(x = Px, y = Py)
w = False #movement button presses
a = False #movement button presses
s = False #movement button presses
d = False #movement button presses
##########################################################################---LEVEL---DRAWING---##########################################################################
#TEXTURES#
Wallimg = PhotoImage(file = "Lategame-tile.png")
#TESTLEVELONE#
Walltest = Label(window, image = Wallimg)
WalltestX = 30
WalltestY = 30
##########################################################################---MOVEMENT---##########################################################################
def keypress(event):
if event.char == "w":
global w
global a
global s
global d
global Xchange
w = True
elif event.char == "a":
a = True
elif event.char == "s":
s = True
elif event.char == "d":
d = True
elif event.char == "`":
Console = Tk()
Console.title("Console")
Console.config(bg = "green")
Output = Label(Console, text = "Output", bg = "green", fg = "yellow", font = "none 9")
#output goes here...
Input = Label(Console, text = "Input", bg = "green", fg = "yellow", font = "none 9")
Commandline = Entry(Console, bg = "white")
def SubmitCommand():
Command = Commandline.get()
print(f"Command Submitted \"{Command}\"")
Commandline.delete(0, END)
Commandsub = Button(Console, text = "Submit", bg = "green", fg = "yellow", command = SubmitCommand)
Output.grid(row = 0, column = 0)
Input.grid(row = 2, column = 0)
Commandline.grid(row = 3, column = 0)
Commandsub.grid(row = 3, column = 1)
def keyup(event):
if event.char == "w":
global w
global a
global s
global d
w = False
elif event.char == "a":
a = False
elif event.char == "s":
s = False
elif event.char == "d":
d = False
def wpress():
def wcheck():
if w == True:
global Direction
global Py
global Px
global X
global Xchange
X = Xchange
Direction = "Up"
Py = Py - X
if Level == 0 and Py == 30 and Px == 30:
Py = Py + X
Playerd.place(x = Px, y = Py)
elif w == False:
X = 0
if a == True:
X = Xchange
Direction = "Right"
Px = Px - X
if Level == 0 and Py == 30 and Px == 30:
Px = Px + X
Playerd.place(x = Px, y = Py)
elif w == False:
X = 0
if s == True:
X = Xchange
Direction = "Down"
Py = Py + X
if Level == 0 and Py == 30 and Px == 30:
Py = Py - X
Playerd.place(x = Px, y = Py)
elif w == False:
X = 0
if d == True:
X = Xchange
Direction = "Left"
Px = Px + X
if Level == 0 and Py == 30 and Px == 30:
Px = Px - X
Playerd.place(x = Px, y = Py)
elif w == False:
X = 0
wpress()
c = Timer(0.001, wcheck)
c.start()
frame.bind("<Key>", keypress)
frame.bind("<KeyRelease>", keyup)
wpress()
##########################################################################---END---MOVEMENT---##########################################################################
##########################################################################---LEVELS---##########################################################################
def DrawTEST1(): #wall test
Walltest.place(x = WalltestX, y = WalltestY)
DrawTEST1()
window.mainloop()
重点是这部分
def wpress():
def wcheck():
if w == True:
global Direction
global Py
global Px
global X
global Xchange
X = Xchange
Direction = "Up"
Py = Py - X
if Level == 0 and Py == 30 and Px == 30:
Py = Py + X
Playerd.place(x = Px, y = Py)
elif w == False:
X = 0
if a == True:
X = Xchange
Direction = "Right"
Px = Px - X
if Level == 0 and Py == 30 and Px == 30:
Px = Px + X
Playerd.place(x = Px, y = Py)
elif w == False:
X = 0
if s == True:
X = Xchange
Direction = "Down"
Py = Py + X
if Level == 0 and Py == 30 and Px == 30:
Py = Py - X
Playerd.place(x = Px, y = Py)
elif w == False:
X = 0
if d == True:
X = Xchange
Direction = "Left"
Px = Px + X
if Level == 0 and Py == 30 and Px == 30:
Px = Px - X
Playerd.place(x = Px, y = Py)
elif w == False:
X = 0
wpress()
c = Timer(0.001, wcheck)
c.start()
frame.bind("<Key>", keypress)
我已经在这个头发上拉了一段时间了。如果有人可以提供帮助,那就太好了!预先感谢!
答案 0 :(得分:0)
不确定您是否已解决问题,但是由于遇到了与此相同的徒劳的事情,因此,我想分享一下我的想法。 请记住,Tkinter不是专门为这种事情而制作的,而像Pygame这样的东西会是更好的选择。
简单方法: 如果您的精灵是一个简单的精灵(如正方形),则只需在每个方向上将其稍微指向您的精灵,然后在每次移动时检查该点是否与墙重叠。 示例:如果您的精灵为25x25,则您可以将中心设为x,y,然后将右侧用于碰撞检测的点设为x + 26,y。那么左边可能是x-26,y。 这里的问题是您在框架中使用标签,因此要么必须找出带有错误和错误的位置,要么使用.bbox方法。
稍微复杂一些的方法(但最终更简单): 如果您的子图形有些奇怪,并且您希望它更精确,则可以将其放在“画布”上而不是“框架”上。这使您可以选择绘制一个可以在精灵周围跟踪的多边形。然后让它随精灵一起移动,然后简单地检查它是否与墙,敌人,子弹,陷阱或您能想到的其他东西重叠。这将为您提供更大的灵活性。
如果您想做更复杂的事情,画布通常也是一个更好的选择,因为它使您可以更轻松地移动事物,绘制形状,设置标签等。如果需要,您仍然可以在画布上放置标签,按钮和所有好东西,除非您只是将东西包装在其中,否则无需使用框架。
最后,我强烈建议使用类,这将使您的生活变得更加轻松,这是该语言的主要优势之一。一开始他们可能会有些混乱,但是值得花很多时间来学习很多遍。一旦适应了它们,您会想知道如果没有它们,如何做任何事情。