私は誰かのためにプログラムを作っているので、彼らは目を動かすことができます.私はカメを使っているので、ランダムに動きます. カメが壁に跳ね返って、側面から消えないようにして、二度と見られないようにしたいのです! 私は見てきました: TurtleGraphics Python: タートルを壁から跳ね返しますか? jnylen のコードを使用したので、彼の功績が認められました。とにかく、これが私の古くてひどいコードです(これは参照用に保持されています):
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 <= left and 90 <= heading() <= 270) or (right <= x and not 90 <= heading() <= 270):
f = 178 * h
left(f)
main()
elif (y <= bottom and heading() >= 180) or (top <= y and heading <= 180):
f = -2 * heading()
left(f)
main()
else:
main()
main()
ただし、実行すると、次の出力が得られます。
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 <module>
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
誰かがこれを修正するのを手伝ってくれませんか。f
、 a float
、string
およびに設定しようとしましたint
が、何も機能しません! これはおそらく本当に明白なことなので、そうであれば申し訳ありません。
これが私の新しい、できれば正しいコードです。
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 <= l and 90 <= heading() <= 270) or (right <= x and not 90 <= heading() <= 270):
f = 178 * heading()
left(f)
print "1"
elif (y <= bottom and heading() >= 180) or (top <= y and heading <= 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 <= l and 90 <= heading() <= 270) or (right <= x and not 90 <= heading() <= 270):
f = 178 * heading()
left(f)
print "1"
elif (y <= bottom and heading() >= 180) or (top <= y and heading <= 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 <= l and 90 <= heading() <= 270) or (right <= x and not 90 <= heading() <= 270):
f = 178 * heading()
left(f)
print "1"
elif (y <= bottom and heading() >= 180) or (top <= y and heading <= 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 <= l and 90 <= heading() <= 270) or (right <= x and not 90 <= heading() <= 270):
f = 178 * heading()
left(f)
print "1"
elif (y <= bottom and heading() >= 180) or (top <= y and heading <= 180):
f = -2 * heading()
left(f)
print "2"
else:
print "3"
main()