我正在编写一个带有pygame和turtle图形的简单项目。它们没有集成在一起。我想要它,以便当我的乌龟从屏幕上移开时它会反弹。我找了一会儿尝试使用其他帖子的答案,但我无法使其发挥作用。它需要始终向前发展。我运行时遇到此错误。
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
答案 0 :(得分:0)
您可以执行以下操作:
screen_rect = Rect(0, 0, 1600, 900)
player_rect = player.image.get_rect()
if player.rect.right > screen_rect.right:
player.xvel *= -1
else if player.rect.left < 0:
player.xvel *= -1
if player.rect.bottom > screen_rect.bottom:
player.yvel *= -1
else if player.rect.top < 0:
player.yvel *= -1
您可以通过以下方式检查您是否在屏幕外:
if not screen_rect.contains(player_rect):
print("turtle escaped screen")
答案 1 :(得分:0)
我知道这是我自己的问题,但我需要一个运作良好的答案,所以我将其添加到角色类中:
if self.x < 0:
self.x = 3
if self.x > 800:
self.x = 790
if self.y < 0:
self.y = 3
if self.y > 800:
self.y = 790