Tkinter帆布不显示我的“玩家”

时间:2018-03-20 20:44:59

标签: python tkinter

我是Python的初学者,想要制作一个红色正方形,通过键盘上的按键移动。但在我运行之后,只有绿色画布出现,没有红色方块。以下是代码请求尽快帮助:

进口:

try:
 import Tkinter
except:
 import tkinter as Tkinter

import time

这里只是一些常量

__author__ = "Brano"

print(__author__)

GAME_WIDTH = 800
GAME_HEIGHT = 600
GAME_BG = 'green'
MOVE_SPEED = 10

主要的课程

class Game(Tkinter.Tk):
  def __init__(self, *args, **kwargs):
    Tkinter.Tk.__init__(self, *args, **kwargs)
    # Trigger Of Other Functions 
    self.x = GAME_WIDTH/2
    self.y = GAME_HEIGHT/2
    self.create_board()
    self.create_men()
    self.bind('<Any-KeyPress>',self.move)

肌动蛋白板

  def create_board(self):
    self.board = Tkinter.Canvas(width=GAME_WIDTH, height=GAME_HEIGHT, 
bg=GAME_BG)
    self.board.pack(padx=10, pady=10)
    return

创建红色方块

def create_men(self):
    self.men = self.board.create_rectangle(300, 300, 310, 310, fill='red')
    return

按键后移动方块

def move(self, event=None):
    key = event.keysym
    if key=='Left':
        self.x = MOVE_SPEED
    elif key=='Right':
        self.x = -MOVE_SPEED
    elif key=='Up':
        self.y = MOVE_SPEED
    elif key=='Down':
        self.y = -MOVE_SPEED
    else:
        pass
    return

Just tkinter updates

def TkUpdate(self):
    self.update()
    self.update_idletasks()
    return

我的主要动作更新

def GameUpdate(self):
    self.board.move(self.men, self.x, self.y)
    return

检查是否已导入

if __name__ == '__main__':
 root=Game(className=" Snake Game ")
 while True:
  root.TkUpdate()
  root.GameUpdate()
  time.sleep(0.09)

else :
 print("U cannot import me !")

Here is the canvas i have

这是整个班级

class Game(Tkinter.Tk):
def __init__(self, *args, **kwargs):
    Tkinter.Tk.__init__(self, *args, **kwargs)
    # Trigger Of Other Functions 
    self.x = GAME_WIDTH/2
    self.y = GAME_HEIGHT/2
    self.create_board()
    self.create_men()
    self.bind('<Any-KeyPress>',self.move)

def create_board(self):
    self.board = Tkinter.Canvas(width=GAME_WIDTH, height=GAME_HEIGHT, bg=GAME_BG)
    self.board.pack(padx=10, pady=10)
    return


def create_men(self):
    self.men = self.board.create_rectangle(300, 300, 310, 310, fill='red')
    return

def move(self, event=None):
    key = event.keysym
    if key=='Left':
        self.x = MOVE_SPEED
    elif key=='Right':
        self.x = -MOVE_SPEED
    elif key=='Up':
        self.y = MOVE_SPEED
    elif key=='Down':
        self.y = -MOVE_SPEED
    else:
        pass
    return

def TkUpdate(self):
    self.update()
    self.update_idletasks()
    return

def GameUpdate(self):
    self.board.move(self.men, self.x, self.y)
    return

1 个答案:

答案 0 :(得分:0)

避免致电updatesleep。而是使用aftermainloop。您的代码应使用after安排游戏循环,并在设置完所有内容后进入主循环。然后继续使用after来调度游戏循环。

你的实际问题是你使用self.x和self.y作为设置中的位置感到困惑,但是然后将它们用作游戏循环中的相对移动。因此,当您第一次调用GameUpdate时,将矩形移动一半的画布大小,它会立即移出视图。下面是一个版本,它使用这些作为'man'位置,并且每次在游戏循环中使用coords来确定位置。

import tkinter as tk

GAME_WIDTH = 800
GAME_HEIGHT = 600
GAME_BG = 'green'
MOVE_SPEED = 10
MAN_SIZE = 20

class Game(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        # Trigger Of Other Functions 
        self.x = GAME_WIDTH/2
        self.y = GAME_HEIGHT/2
        self.create_board()
        self.create_men()
        self.bind('<Any-KeyPress>',self.move)

    def create_board(self):
        self.board = tk.Canvas(width=GAME_WIDTH, height=GAME_HEIGHT, bg=GAME_BG)
        self.board.pack(padx=10, pady=10)

    def create_men(self):
        self.men = self.board.create_rectangle((self.x, self.y, self.x + MAN_SIZE, self.y + MAN_SIZE), fill='red', outline='black')

    def move(self, event=None):
        key = event.keysym
        print(key)
        if key=='Left':
            self.x = self.x - MOVE_SPEED
        elif key=='Right':
            self.x = self.x + MOVE_SPEED
        elif key=='Up':
            self.y = self.y - MOVE_SPEED
        elif key=='Down':
            self.y = self.y + MOVE_SPEED
        else:
            pass
        return

    def game_loop(self):
        self.board.coords(self.men, (self.x, self.y, self.x + MAN_SIZE, self.y + MAN_SIZE))
        self.after(10, self.game_loop)

def main():
    game = Game(className="Snake")
    game.after(10, game.game_loop)
    game.mainloop()

if __name__ == '__main__':
    main()