我目前正在创建一个RPG游戏,现在我想让角色去某些地方。当它到达那些地方时,该窗口应该关闭并且应该出现End(它在不同的文件中)。这样做的最佳方法是什么?我目前遇到的问题是保持检查变量给我进入某个区域的状态是False
,如果它是真的,则应该执行其他代码。
以下是我目前的情况:
class player(object):
def __init__(self, x, y):
self.root = None
self.x = x
self.y = y
def zeichne(self, root):
self.root = root
self.l1 = root.create_line(self.x-5, self.y-5, self.x+5, self.y+5, width=2)
self.l2 = root.create_line(self.x+5, self.y-5, self.x-5, self.y+5, width=2)
def configure(self, x, y):
self.root.delete("all")
self.x = x
self.y = y
self.root.create_line(self.x-5, self.y-5, self.x+5, self.y+5, width=2)
self.root.create_line(self.x+5, self.y-5, self.x-5, self.y+5, width=2)
def go_fwd(event):
areal.focus_set()
me.configure(me.x, me.y-2)
def go_bwd(event):
areal.focus_set()
me.configure(me.x, me.y+2)
def go_left(event):
areal.focus_set()
me.configure(me.x-2, me.y)
def go_right(event):
areal.focus_set()
me.configure(me.x+2, me.y)
dw = tk.Tk()
dw.title('Erebos')
areal = tk.Frame(master=dw, width=20000, height=600, bg='black')
areal.pack_propagate(0)
areal.pack(fill=tk.BOTH, expand=bool(dw)-100)
areal.bind("<1>", lambda event: areal.focus_set())
dw.bind("<Up>", go_fwd)
dw.bind("<Down>", go_bwd)
dw.bind("<Left>", go_left)
dw.bind("<Right>", go_right)
playarea = tk.Canvas(width=800, height=500, master=areal, bg='white')
playarea.pack()
dw.mainloop()
我认为使用后台进程可能很有用,但有很多包,我真的很困惑。在这做什么,并使用背景包?
答案 0 :(得分:0)
我希望我的问题是正确的。你必须在每一步都检查球员的位置。我建议您在每次移动时定义区域(例如go_right
),您需要检查玩家是否在这些边界内。如果是True
,您可以加载另一个脚本。这是一个快速而肮脏的解决方案:
def go_fwd(event):
areal.focus_set()
me.configure(me.x, me.y-2)
check_if_player_enters_region(me)
def go_bwd(event):
areal.focus_set()
me.configure(me.x, me.y+2)
check_if_player_enters_region(me)
def go_left(event):
areal.focus_set()
me.configure(me.x-2, me.y)
check_if_player_enters_region(me)
def go_right(event):
areal.focus_set()
me.configure(me.x+2, me.y)
check_if_player_enters_region(me)
def check_if_player_enters_region(player):
## Define your Areas here
if(is_in_region(player, 20, 20, 10, 10)):
print "enter region 1, load script xxx"
if(is_in_region(player, 50, 50, 10, 10)):
print "enter region 2, load script xxx"
playarea.create_rectangle(20,20,30,30, fill='blue') ## this is just for visualisation, delete this line
playarea.create_rectangle(50,50,60,60, fill='blue') ## this is just for visualisation, delete this line
def is_in_region(player, x, y, width, height): # define region boundries
if((player.x >= x and player.x <= x+width) and ## check x boundries
(player.y >= y and player.y-2 <= y+height)): ## check y boundries
return True
return False
dw = tk.tk()
dw.title('Erebos')
me = player(0,0)
areal = tk.Frame(master=dw, width=20000, height=600, bg='black')
areal.pack_propagate(0)
areal.pack(fill=tk.BOTH, expand=bool(dw)-100)
areal.bind("<1>", lambda event: areal.focus_set())
dw.bind("<Up>", go_fwd)
dw.bind("<Down>", go_bwd)
dw.bind("<Left>", go_left)
dw.bind("<Right>", go_right)
playarea = tk.Canvas(width=800, height=500, master=areal, bg='white')
playarea.pack()
playarea.create_rectangle(20,20,30,30, fill='blue') ## this is just for visualisation, delete this line
playarea.create_rectangle(50,50,60,60, fill='blue') ## this is just for visualisation, delete this line
me.zeichne(playarea)
dw.mainloop()
我只检查播放器的中心,通常你会检查&#34;框&#34;玩家。