Question

I am writing a simple project with pygame and turtle graphics. They aren't integrated together. I want it so that when my turtle moves off the screen it bounces off. I looked for a while I tried using answers from some other posts but I could't make it work. It needs to always move forward. I get this error when I run it.

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
Était-ce utile?

La solution 2

I know this is my own question but I needed an answer that worked well so I added this to the character class:

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

Autres conseils

You can do something like:

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

You can check if you are off-screen, by:

if not screen_rect.contains(player_rect):
    print("turtle escaped screen")

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top