试图找到屏幕化并使对象与乌龟超过2的筛网

发布于 2025-02-08 14:32:37 字数 1173 浏览 1 评论 0原文

因此,我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆啦不做梦 2025-02-15 14:32:37

如下更改代码,问题将消失:

import turtle

width  = turtle.window_width()
height = turtle.window_height()

PS获取窗口宽度,以防乌龟模块A功能。要调用功能,必须在功能名称之后提供(),else width将成为turtle.window_width_width函数的另一个名称。
如果您不在turtle.window_width in width = 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:

import turtle

width  = turtle.window_width()
height = turtle.window_height()

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, else width will become just another name for turtle.window_width function.
If you don't put the brackets after turtle.window_width in width = turtle.window_width you can solve the problem by adding width = 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 with print(type(width), width). Such print debugging is often helpful because sometimes it is necessary to use value = module.some_name and sometimes value = module.some_name() to get the right value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文