Python 为什么tkinter小部件会在一段时间后消失?
我在制造小行星,我的飞船是个多边形。在飞船开始移动后,它会闪烁一整束光,然后由于某种原因从屏幕上消失 是否有我需要正确调用的方法 刷新东西什么的Python 为什么tkinter小部件会在一段时间后消失?,python,tkinter,tkinter-canvas,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
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()
导入tkinter
导入时间
从tkinter进口*
从日期时间导入日期时间
高度=600
宽度=1000
TITLE=“ASTERIODS”
可调整大小=错误
职业球员:
默认速度=.2
加速度=0
x=0
y=x
xSpeed=0
ySpeed=xSpeed
id=无
点数=[]
定义初始化(自、x、y、画布):
self.x=x
self.y=y
self.points=[0,01000,-200200,0,0,0]
对于范围内的i(len(self.points)):
自我点[i]/=4
self.id=canvas.create_多边形(self.points,width=3,outline='white')
canvas.move(self.id、self.x、self.y)
def更新(自我,画布):
canvas.move(self.id、self.xSpeed、self.ySpeed)
如果self.x=宽度:
canvas.move(self.id,-1*宽度,0)
self.x=宽度*-1
如果self.y=高度:
canvas.move(self.id,0,-1*高度)
self.y=高度*-1
self.y+=self.y速度
self.x+=self.xSpeed
def up(自我):
self.ySpeed-=self.DEFAULT\u速度
def关闭(自我):
self.ySpeed+=self.DEFAULT\u速度
定义权限(自我):
self.xSpeed+=self.DEFAULT_速度
def左(自身):
self.xSpeed-=self.DEFAULT_速度
def旋转(自身、鼠标、鼠标):
通过
def更新(画布):
对于roomObjects中的obi:
obi.update(画布)
def gameLoop():
更新(c)
root.update_idletasks()
root.after(10,gameLoop)
def keyLeft(事件):
player.left()
def keyRight(事件):
player.right()
def keyUp(事件):
player.up()
def按键关闭(事件):
玩家倒下()
情绪(事件)的定义:
玩家旋转(事件x,事件y)
##########################################
#开始干管
root=Tk()
root.title(title)
根目录。可调整大小(0,0)
c=画布(根,高度=高度,宽度=宽度)
c、 创建矩形(0,0,宽度,高度,填充=“黑色”)
c、 包()
root.bind(“”,keyLeft)
root.bind(“”,右键)
root.bind(“”,keyUp)
root.bind(“”,向下键)
root.bind(“”,mouseMotion)
roomObjects=[]
附加(播放器(宽度/2,高度/2,c))
player=roomObjects[0]
gameLoop()
root.mainloop()
感谢您实际格式化您的问题-欢迎使用SO:)您是否可能将播放器
移动到画布边界之外?我不完全理解你的代码,所以我可能是错的,但你似乎正在将画布的宽度和高度设置为width
和height
,然后将播放器移动width
和height
。我认为是这样,并进行了测试,看起来不是这样。但后来我用另一种方式测试了它,它超出了界限,谢谢。self.x=WIDTH*-1
这难道不会让它觉得它离屏幕太远吗?它不应该是self.x=0吗?或者self.x-=WIDTH
,因为您按宽度移动它,所以您应该按宽度更改self.x
。要将它标记为已回答,请将您为解决此问题所做的操作写入答案并接受答案。或者请@RobertR将他的答案作为答案而不是评论来写,然后接受。感谢您对您的问题进行了格式化-欢迎使用SO:)您是否可能将播放器
移动到画布边界之外?我不完全理解你的代码,所以我可能是错的,但你似乎正在将画布的宽度和高度设置为width
和height
,然后将播放器移动width
和height
。我认为是这样,并进行了测试,看起来不是这样。但后来我用另一种方式测试了它,它超出了界限,谢谢。self.x=WIDTH*-1
这难道不会让它觉得它离屏幕太远吗?它不应该是self.x=0吗?或者self.x-=WIDTH
,因为您按宽度移动它,所以您应该按宽度更改self.x
。要将它标记为已回答,请将您为解决此问题所做的操作写入答案并接受答案。或者让@RobertR把他的答案写成一个答案,而不是评论,然后接受。