【发布时间】:2014-04-06 07:24:28
【问题描述】:
我正在编写一个带有 pygame 和海龟图形的简单项目。它们没有集成在一起。我想要它,这样当我的乌龟离开屏幕时它会反弹。我看了一会儿,尝试使用其他一些帖子的答案,但我无法让它发挥作用。它需要始终向前。运行时出现此错误。
Traceback (most recent call last):
File "F:\move.pyw", line 86, in <module>
main.fd(2)
File "C:\Python32\lib\turtle.py", line 1630, in forward
self._go(distance)
File "C:\Python32\lib\turtle.py", line 1598, in _go
self._goto(ende)
File "C:\Python32\lib\turtle.py", line 3151, in _goto
screen._pointlist(self.currentLineItem),
File "C:\Python32\lib\turtle.py", line 755, in _pointlist
cl = self.cv.coords(item)
File "<string>", line 1, in coords
File "C:\Python32\lib\tkinter\__init__.py", line 2221, in coords
self.tk.call((self._w, 'coords') + args))]
_tkinter.TclError: invalid command name ".50669168"
Here is me code:
#By Simon Harms
#2014
#import libs and other
import turtle, random, pygame, sys
from pygame.locals import *
#make turtle window
wn = turtle.Screen()
#set screensize
wn.screensize(1600,900)
#set turtle window title
wn.title("Move It!")
#make turtle
main = turtle.Turtle()
#make turtle shape
wn.register_shape("Main.gif")
main.shape('Main.gif')
#key functions
def isInScreen(wn,main):
leftBound = wn.window_width() / -2.0
rightBound = wn.window_width() / 2.0
bottomBound = wn.window_height() / -2.0
topBound = wn.window_height() / 2.0
turtlex = main.xcor()
turtley = main.ycor()
if turtlex < leftBound or turtlex > rightBound or turtley < bottomBound or turtley > topBound:
return False
return True
main.write("Hello to use this application please hit the keys wasd to move")
main.rt(90)
main.fd(10)
main.lt(90)
main.write("e to exit")
main.rt(90)
main.fd(10)
main.lt(90)
main.write("press x for swirling")
main.rt(90)
main.fd(10)
main.lt(90)
main.write("press r to reset")
def moveup ():
main.fd(10)
main.pencolor('red')
def movedown ():
main.bk(10)
main.pencolor('orange')
def moveleft ():
main.lt(10)
main.pencolor('purple')
def moveright ():
main.rt(10)
main.pencolor('blue')
def escape ():
wn.bye()
def randomSwirl ():
x = 10
for i in range(1,100):
main.fd(x)
main.rt(77)
x = x + 1
def speedup ():
main.pen(speed=10)
def speeddown ():
main.pen(speed=1)
def speedoff ():
main.pen(speed=0)
def reset ():
main.reset()
#if key pressed
wn.onkey(moveup, "w")
wn.onkey(moveleft, "a")
wn.onkey(moveright, "d")
wn.onkey(movedown, "s")
wn.onkey(escape, "e")
wn.onkey(speedup, "9")
wn.onkey(speedup, "0")
wn.onkey(randomSwirl, "x")
wn.onkey(speedoff, "i")
wn.onkey(reset, "r")
wn.listen()
gameloop = True
while gameloop == True:
main.fd(2)
while True:
counter = 0
while isInScreen(wn,main):
main.fd(2)
if isInScreen(wn,main) == False:
counter += 1
if counter == 1:
wn.reset()
wn.mainloop()
import moveItPyGame
【问题讨论】:
-
第一次通话时会发生这种情况吗?
main.fd(2)会把乌龟从屏幕边缘拿走吗? -
不,它不会,但它在一个while语句中,所以我认为它最终会。
-
我的意思是,这个错误是在海龟第一次尝试移动 2 时发生,还是在移动 2 将其移出屏幕时发生,或者介于两者之间?
-
当我关闭窗口时。
标签: python python-3.x window pygame turtle-graphics