【问题标题】:Python3.2 How to keep turtle from going off the screen and when it does reset?Python3.2 如何防止乌龟离开屏幕以及何时重置?
【发布时间】: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


【解决方案1】:

你可以这样做:

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")

http://www.pygame.org/docs/ref/rect.html

【讨论】:

    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      相关资源
      最近更新 更多