Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3.2如何防止海龟离开屏幕以及何时重置?_Python_Python 3.x_Window_Pygame_Turtle Graphics - Fatal编程技术网

Python3.2如何防止海龟离开屏幕以及何时重置?

Python3.2如何防止海龟离开屏幕以及何时重置?,python,python-3.x,window,pygame,turtle-graphics,Python,Python 3.x,Window,Pygame,Turtle Graphics,我正在写一个简单的项目与pygame和海龟图形。它们没有整合在一起。我想要这样,当我的乌龟离开屏幕时,它会反弹回来。我找了一段时间,我试着使用其他帖子的答案,但我没能做到。它需要不断前进。当我运行它时,我得到了这个错误 Traceback (most recent call last): File "F:\move.pyw", line 86, in <module> main.fd(2) File "C:\Python32\lib\turtle.py", line

我正在写一个简单的项目与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
回溯(最近一次呼叫最后一次):
文件“F:\move.pyw”,第86行,在
main.fd(2)
文件“C:\Python32\lib\turtle.py”,第1630行,前进
自走(距离)
文件“C:\Python32\lib\turtle.py”,第1598行,在
自已.(完)
文件“C:\Python32\lib\turtle.py”,第3151行,在
屏幕._点列表(self.currentLineItem),
文件“C:\Python32\lib\turtle.py”,第755行,在点列表中
cl=自身cv坐标(项目)
文件“”,第1行,在坐标中
文件“C:\Python32\lib\tkinter\\uuuu init\uuuuu.py”,第2221行,在coords中
self.tk.call((self._w,'coords')+args))]
_tkinter.TclError:无效的命令名“.50669168”
这是我的密码:
#西蒙·哈姆斯
#2014
#导入LIB和其他
导入海龟,随机,pygame,系统
从pygame.locals导入*
#做乌龟窗
wn=tutle.Screen()
#设置屏幕大小
wn.屏幕尺寸(1600900)
#设置海龟窗口标题
wn.title(“移动它!”)
#做乌龟
main=turtle.turtle()
#做龟形
wn.register_形状(“Main.gif”)
main.shape('main.gif')
#关键功能
def ISINSCLEEN(北西,主):
leftBound=wn.window_width()/-2.0
rightBound=wn.window_width()/2.0
底部边界=wn.window_height()/-2.0
topBound=wn.window\u height()/2.0
turtlex=main.xcor()
turtley=main.ycor()
如果turtlexrighbound或turtleytopBound:
返回错误
返回真值
main.write(“您好,要使用此应用程序,请按wasd键移动”)
main.rt(90)
main.fd(10)
main.lt(90)
main.write(“e退出”)
main.rt(90)
main.fd(10)
main.lt(90)
main.write(“按x键旋转”)
main.rt(90)
main.fd(10)
main.lt(90)
main.write(“按r键重置”)
def moveup():
main.fd(10)
主要颜色(“红色”)
def movedown():
main.bk(10)
主要颜色(“橙色”)
def moveleft():
main.lt(10)
主色调铅笔(“紫色”)
def moveright():
main.rt(10)
主色调铅笔(“蓝色”)
def escape():
再见
def randomSwirl():
x=10
对于范围(1100)内的i:
main.fd(x)
main.rt(77)
x=x+1
def加速():
主笔(速度=10)
def减速():
main.pen(速度=1)
def快速起飞():
main.pen(速度=0)
def reset():
main.reset()
#如果按键
wn.onkey(上移,“w”)
wn.onkey(左移,“a”)
wn.onkey(右移,“d”)
wn.onkey(向下移动,“s”)
wn.onkey(转义,“e”)
wn.onkey(加速,“9”)
wn.onkey(加速,“0”)
wn.onkey(随机漩涡,“x”)
wn.onkey(快速起飞,“i”)
wn.ON键(复位,“r”)
听着
gameloop=True
当gameloop==True时:
main.fd(2)
尽管如此:
计数器=0
而ISInsScreen(wn,main):
main.fd(2)
如果ISINSCLEEN(wn,main)=False:
计数器+=1
如果计数器==1:
wn.reset()
wn.mainloop()
导入moveItPyGame

您可以执行以下操作:

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 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
如果self.x<0:
self.x=3
如果self.x>800:
self.x=790
如果self.y<0:
self.y=3
如果self.y>800:
self.y=790

这种情况是在第一次通话时发生的吗?
main.fd(2)
会让海龟离开屏幕边缘吗?不会,但它是在while语句中,所以我认为它最终会。我的意思是,当海龟第一次尝试移动2时,会发生这个错误吗?当移动2时,会让海龟离开屏幕,还是介于两者之间?当我关闭窗口时。
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