如何使用我创建的网格中的w,a,s,d键来修复玩家圈子移动的位置

时间:2014-12-10 04:08:05

标签: python tkinter

好的,所以我的最后一个项目我正在创建一个地牢爬虫,我有一个网格设置,玩家移动到目前为止,我正在努力让GUI完成,然后我将其余的文件链接在一起具有攻击功能。无论如何我的问题是我的玩家圈子不会在我创建的网格中移动,如果你不知道我的意思,这里是代码:

gui.py

try:
    from tkinter import *

except ImportError as err:
    print('a', err)

from player import *

class SimpleGUI:
    def __init__(self):
        self.window = Tk()
        self.window.title('Final Project')
        self.window.protocol('WM_DELETE_WINDOW',self.exit)
        self.canvas = Canvas(self.window,
                             width = 245,
                             height = 550,
                             bg = 'white')
        self.canvas.pack()

        self.player = Player()

        self.canvas.focus_set()
        self.canvas.bind('w', self.w_key)
        self.canvas.bind('a', self.a_key)
        self.canvas.bind('s', self.s_key)
        self.canvas.bind('d', self.d_key)


        self.running = True
        self.menu = Menu

        self.bt1 = Button(self.window,
                         text = 'Start',
                         command = self.menu)
        self.bt1.pack()




        self.mainloop()
        self.window.mainloop()

    def mainloop(self):
        while self.running:
            self.canvas.delete(ALL)
            #boss square
            boss_start = self.canvas.create_rectangle(100,0,150,100, outline = 'black', fill = 'black', width = 2)
            #player start square
            ply_start = self.canvas.create_rectangle(100,500,150,550, outline = 'black', fill = 'black', width = 2)
            #row 9
            self.canvas.create_rectangle(0,50,50,100, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(50,50,100,100, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(100,50,150,100, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(150,50,200,100, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(200,50,250,100, outline = 'black', fill = 'black', width = 2)
            #row 8
            self.canvas.create_rectangle(0,100,50,150, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(50,100,100,150, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(100,100,150,150, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(150,100,200,150, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(200,100,250,150, outline = 'black', fill = 'black', width = 2)
            #row 7
            self.canvas.create_rectangle(0,150,50,200, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(50,150,100,200, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(100,150,150,200, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(150,150,200,200, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(200,150,250,200, outline = 'black', fill = 'black', width = 2)
            #row 6
            self.canvas.create_rectangle(0,200,50,250, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(50,200,100,250, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(100,200,150,250, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(150,200,200,250, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(200,200,250,250, outline = 'black', fill = 'black', width = 2)
            #row 5
            self.canvas.create_rectangle(0,250,50,300, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(50,250,100,300, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(100,250,150,300, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(150,250,200,300, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(200,250,250,300, outline = 'black', fill = 'black', width = 2)
            #row 4
            self.canvas.create_rectangle(0,300,50,350, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(50,300,100,350, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(100,300,150,350, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(150,300,200,350, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(200,300,250,350, outline = 'black', fill = 'black', width = 2)
            #row 3
            self.canvas.create_rectangle(0,350,50,400, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(50,350,100,400, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(100,350,150,400, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(150,350,200,400, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(200,350,250,400, outline = 'black', fill = 'black', width = 2)
            #row 2
            self.canvas.create_rectangle(0,400,50,450, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(50,400,100,450, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(100,400,150,450, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(150,400,200,450, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(200,400,250,450, outline = 'black', fill = 'black', width = 2)
            #row 1
            self.canvas.create_rectangle(0,450,50,500, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(50,450,100,500, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(100,450,150,500, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(150,450,200,500, outline = 'black', fill = 'black', width = 2)
            self.canvas.create_rectangle(200,450,250,500, outline = 'black', fill = 'black', width = 2)
            self.player.draw(self.canvas)

            self.canvas.after(30)
            self.canvas.update()

    def button1(self):
        print('working')

    def a_key(self, event):
        self.player.x -= self.player.speed

    def w_key(self, event):
        self.player.y -= self.player.speed

    def s_key(self, event):
        self.player.y += self.player.speed

    def d_key(self, event):
        self.player.x += self.player.speed

    def exit(self):
        self.running = False
        self.window.destroy()


if __name__ == '__main__':
    SimpleGUI()

player.py

class Player:
    def __init__(self, hp = 100, defense = 10, strength = 10):
        self.hp = hp
        self.defense = defense
        self.strength = strength
        self.x = 0
        self.y = 0
        self.speed = 64

    def draw(self, canvas): 
        canvas.create_oval(self.x,self.y,self.x+45,self.y+45, fill='red') 

if __name__ == '__main__':
    player = Player()

2 个答案:

答案 0 :(得分:0)

这不仅仅是如何在tkinter中做动画。你不应该用你自己的while循环替换内置的mainloop

要做动画,请将您的纸板画一次。然后,创建一个更新电路板的功能。最后,定期使用after调用此函数。这将使您的UI能够在保持董事会最新的同时做出响应。

换句话说,而不是:

def mainloop():
    while self.running:
        ...
        self.canvas.after(30)
        self.canvas.update()

......你应该这样做:

def update_display():

    <update the display based on current information>

    if self.running:
        # update the screen again in 30 ms
        self.after(30, update_display)

对于&#34;更新显示&#34;另外,您可以使用画布move和/或coords方法移动椭圆。在每次迭代过程中都不需要删除和完全重绘 - 只需重绘已更改的内容。对于玩家来说也是如此 - 画一次,然后从那时开始移动它。

使用此架构,您的应用程序将表现更好。

答案 1 :(得分:-1)

我不确定你的问题是什么。我复制了你的代码,当我运行你的项目时,红色圆圈用wasd键适当地移动。是否缺乏适当的碰撞问题?