试图找到屏幕化并使对象与乌龟超过2的筛网
因此,我在Python Turtle上写了Tron游戏,我想让两辆自行车以屏幕宽度为/2和高度/2开始。 (另一个对另一件事,但负宽度)。可悲的是,这似乎不起作用,因为您无法将功能除以INT。有人知道该怎么做吗?
这就是我尝试的:
width = turtle.window_width
height = turtle.window_height
def tron():
#Drawing the starting turtles
blueplayer = turtle.Turtle()
redplayer = turtle.Turtle()
screen = turtle.Screen()
screen.setup(width, height)
screen.bgpic('TronBg.png')
screen.bgcolor('black')
screen.addshape('BlueBike.gif')
screen.addshape('RedBike.gif')
blueplayer.shape('BlueBike.gif')
redplayer.shape('RedBike.gif')
redplayer.pencolor("red")
redplayer.pensize(3)
blueplayer.pencolor("blue")
blueplayer.pensize(3)
redplayer.pu()
blueplayer.pu()
-> blueplayer.goto(width/2, height/2)
-> redplayer.goto(-1*(width)/2, height/2)**
redplayer.pd()
blueplayer.pd()
#Box border
#Border
box = Turtle()
box.ht()
box.color('purple')
box.speed('fastest')
box.pensize(10)
box.pu()
box.setpos(-355, -345)
box.pd()
for i in range(5):
box.forward(700)
box.left(90)
tron()
So I'm writing a tron game in python turtle and I want to make the two bikes start at whatever the screen width is/2, and height/2. (Same thing for the other but negative width). Sadly this doesn't seem to work because you can't divide a function by an int. Does anyone know how to do it?
This is what I tried:
width = turtle.window_width
height = turtle.window_height
def tron():
#Drawing the starting turtles
blueplayer = turtle.Turtle()
redplayer = turtle.Turtle()
screen = turtle.Screen()
screen.setup(width, height)
screen.bgpic('TronBg.png')
screen.bgcolor('black')
screen.addshape('BlueBike.gif')
screen.addshape('RedBike.gif')
blueplayer.shape('BlueBike.gif')
redplayer.shape('RedBike.gif')
redplayer.pencolor("red")
redplayer.pensize(3)
blueplayer.pencolor("blue")
blueplayer.pensize(3)
redplayer.pu()
blueplayer.pu()
-> blueplayer.goto(width/2, height/2)
-> redplayer.goto(-1*(width)/2, height/2)**
redplayer.pd()
blueplayer.pd()
#Box border
#Border
box = Turtle()
box.ht()
box.color('purple')
box.speed('fastest')
box.pensize(10)
box.pu()
box.setpos(-355, -345)
box.pd()
for i in range(5):
box.forward(700)
box.left(90)
tron()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如下更改代码,问题将消失:
PS获取窗口宽度,以防乌龟模块A功能。要调用功能,必须在功能名称之后提供
()
,elsewidth
将成为turtle.window_width_width
函数的另一个名称。如果您不在
turtle.window_width
inwidth = width = width = turtle.window_width
之后放置括号。 >在下一行。尝试一下,以查看它有效的目的是为了更好地了解分配操作员=
做什么。顺便说一句:您可以检查哪种类型和哪个值
width
具有print(类型(width),width)
。这样的打印调试通常很有帮助,因为有时需要使用value = module.Some_name
,有时value = module.some_name()
获得正确的值。Change your code as follows and the problem will go away:
P.S. to obtain window width you call in case of the turtle module a function. To call a function it is necessary to provide
()
after the function name, elsewidth
will become just another name forturtle.window_width
function.If you don't put the brackets after
turtle.window_width
inwidth = turtle.window_width
you can solve the problem by addingwidth = width()
in the next line. Try this out to see that it works in order to gain better understanding of what an assignment operator=
does.By the way: you can check which type and which value the variable
width
has withprint(type(width), width)
. Such print debugging is often helpful because sometimes it is necessary to usevalue = module.some_name
and sometimesvalue = module.some_name()
to get the right value.