为什么tkinter小部件会在一段时间后消失?

时间:2016-05-27 19:10:14

标签: python tkinter tkinter-canvas

我正在制造小行星而我的船是一个多边形。船开始移动后,它会闪现一大堆然后由于某种原因从屏幕上消失。

我需要一个正确调用的方法吗? 刷东西什么的?

import tkinter
import time
from tkinter import *
from datetime import datetime

HEIGHT = 600
WIDTH = 1000
TITLE = "ASTERIODS"
RESIZABLE = False

class player:
    DEFAULT_SPEED = .2

    acceleration = 0
    x = 0
    y = x
    xSpeed = 0
    ySpeed = xSpeed
    id = NONE
    points = []

    def __init__(self, x, y, canvas):

        self.x = x
        self.y = y
        self.points = [0,0, 100,-200,200,0,0,0]

        for i in range(len(self.points)):

            self.points[i] /= 4

        self.id = canvas.create_polygon(self.points, width=3, outline='white')
        canvas.move(self.id, self.x, self.y)

    def update(self, canvas):

        canvas.move(self.id, self.xSpeed, self.ySpeed)

        if self.x <= 0:
            canvas.move(self.id, WIDTH, 0)
            self.x = WIDTH

        elif self.x >= WIDTH:

            canvas.move(self.id, -1 * WIDTH, 0)
            self.x = WIDTH * -1

        if self.y <= 0:

            canvas.move(self.id, 0, HEIGHT)
            self.y = HEIGHT

        elif self.y >= HEIGHT:

            canvas.move(self.id, 0, -1 * HEIGHT)
            self.y = HEIGHT * -1

        self.y += self.ySpeed
        self.x += self.xSpeed


    def up(self):
        self.ySpeed -= self.DEFAULT_SPEED

    def down(self):
        self.ySpeed += self.DEFAULT_SPEED

    def right(self):
        self.xSpeed += self.DEFAULT_SPEED

    def left(self):
        self.xSpeed -= self.DEFAULT_SPEED

    def rotate(self, mouseX, mouseY):

        pass

def update(canvas):

    for obi in roomObjects:

        obi.update(canvas)


def gameLoop():

    update(c)
    root.update_idletasks()
    root.after(10, gameLoop)


def keyLeft(event):
    player.left()

def keyRight(event):
    player.right()

def keyUp(event):
    player.up()

def keyDown(event):
    player.down()

def mouseMotion(event):
    player.rotate(event.x, event.y)


##########################################
#Begin Main
root = Tk()
root.title(TITLE)
root.resizable(0, 0)
c = Canvas(root, height = HEIGHT, width = WIDTH)
c.create_rectangle(0 ,0, WIDTH, HEIGHT, fill = "black")
c.pack()
root.bind('<Left>', keyLeft)
root.bind('<Right>', keyRight)
root.bind('<Up>', keyUp)
root.bind('<Down>', keyDown)
root.bind('<Motion>', mouseMotion)

roomObjects = []
roomObjects.append(player(WIDTH / 2, HEIGHT / 2, c))
player = roomObjects[0]
gameLoop()
root.mainloop()

0 个答案:

没有答案