Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting Turtle to Bounce Off the Walls
    text
    copied!<p>I am making a program for somebody, so they can exercise their eyes, and I am using the turtle, so it will move randomly. I want the turtle to bounce off the walls, so that it does not disappear off the sides, never to be seen again! I have looked at: <a href="https://stackoverflow.com/questions/1457332/turtlegraphics-python-bouncing-turtle-off-the-walls/1457659#1457659">TurtleGraphics Python: Bouncing turtle off the walls?</a> and used some code from jnylen, so some credit goes to him! Anyway, here is my old, and terrible code (This is kept for reference):</p> <pre><code>from random import * from turtle import * penup() # Get rid of those lines it leaves behind! def main(): while True: # I want this to be infinite a = randint(1,600) print a b = randint(1,359) print b c = randint(1,600) print c d = randint(1,359) print d #Checking my randoms forward(a) edge() # Keep calling edge, so it will bounce on the edge. left(b) edge() # Keep calling edge, so it will bounce on the edge. forward(c) edge() # Keep calling edge, so it will bounce on the edge. right(d) edge() # Keep calling edge, so it will bounce on the edge. def edge(): x, y = position() top = window_height()/2 bottom = -top right = window_width()/2 left = -right if (x &lt;= left and 90 &lt;= heading() &lt;= 270) or (right &lt;= x and not 90 &lt;= heading() &lt;= 270): f = 178 * h left(f) main() elif (y &lt;= bottom and heading() &gt;= 180) or (top &lt;= y and heading &lt;= 180): f = -2 * heading() left(f) main() else: main() main() </code></pre> <p>However, When I run it, I get the output: </p> <pre><code>189 199 553 152 26 175 597 263 119 201 582 329 231 267 344 28 Traceback (most recent call last): File "C:/Users/George/Desktop/Eyes.py", line 39, in &lt;module&gt; main() File "C:/Users/George/Desktop/Eyes.py", line 15, in main edge() File "C:/Users/George/Desktop/Eyes.py", line 38, in edge main() File "C:/Users/George/Desktop/Eyes.py", line 15, in main edge() File "C:/Users/George/Desktop/Eyes.py", line 38, in edge main() File "C:/Users/George/Desktop/Eyes.py", line 15, in main edge() File "C:/Users/George/Desktop/Eyes.py", line 38, in edge main() File "C:/Users/George/Desktop/Eyes.py", line 15, in main edge() File "C:/Users/George/Desktop/Eyes.py", line 31, in edge left(f) TypeError: 'int' object is not callable </code></pre> <p>Please, can somebody help me fix this. I have tried setting <code>f</code> to a <code>float</code>, a <code>string</code> and an <code>int</code>, but nothing works! This is probably something really obvious, so if it is, sorry.</p> <p>Here is my new, and hopefully, correct code: </p> <pre><code>from random import * from turtle import * penup() # Get rid of those lines it leaves behind! def main(): while True: # I want this to be infinite a = randint(1,600) print a b = randint(1,359) print b c = randint(1,600) print c d = randint(1,359) print d #Checking my randoms forward(a) x, y = position() top = window_height()/2 bottom = -top right = window_width()/2 l = -right if (x &lt;= l and 90 &lt;= heading() &lt;= 270) or (right &lt;= x and not 90 &lt;= heading() &lt;= 270): f = 178 * heading() left(f) print "1" elif (y &lt;= bottom and heading() &gt;= 180) or (top &lt;= y and heading &lt;= 180): f = -2 * heading() left(f) print "2" else: print "3" left(b) x, y = position() top = window_height()/2 bottom = -top right = window_width()/2 l = -right if (x &lt;= l and 90 &lt;= heading() &lt;= 270) or (right &lt;= x and not 90 &lt;= heading() &lt;= 270): f = 178 * heading() left(f) print "1" elif (y &lt;= bottom and heading() &gt;= 180) or (top &lt;= y and heading &lt;= 180): f = -2 * heading() left(f) print "2" else: print "3" forward(c) x, y = position() top = window_height()/2 bottom = -top right = window_width()/2 l = -right if (x &lt;= l and 90 &lt;= heading() &lt;= 270) or (right &lt;= x and not 90 &lt;= heading() &lt;= 270): f = 178 * heading() left(f) print "1" elif (y &lt;= bottom and heading() &gt;= 180) or (top &lt;= y and heading &lt;= 180): f = -2 * heading() left(f) print "2" else: print "3" right(d) x, y = position() top = window_height()/2 bottom = -top right = window_width()/2 l = -right if (x &lt;= l and 90 &lt;= heading() &lt;= 270) or (right &lt;= x and not 90 &lt;= heading() &lt;= 270): f = 178 * heading() left(f) print "1" elif (y &lt;= bottom and heading() &gt;= 180) or (top &lt;= y and heading &lt;= 180): f = -2 * heading() left(f) print "2" else: print "3" main() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload