无法在 pygame.draw.circle 中使用变量?

发布于 2024-12-05 08:19:30 字数 982 浏览 1 评论 0原文

当我尝试使用变量创建形状时,我不断收到此错误消息:

TypeError:需要整数参数,得到浮点数

import pygame._view
import pygame, sys
from pygame.locals import *
import random

pygame.init()

......

barrel = pygame.image.load("images\Barrel.gif")
barrel_create = 0
barrelx = screen.get_height()- barrel.get_height()
barrely = screen.get_width()/2 - barrel.get_width()/2
barrel_exist = 0
explosion_delay = 0

while running:

    if barrel_exist == 0:
        if barrel_create == 500:
            barrely = 200
        barrelx = random.randint(0,400)
        barrel_exist = 1
    if barrel_exist == 1:
        barrely = barrely + 0.1
        if barrely > 400:
            barrel_exist = 0

    if explosion_delay > 0:
        pygame.draw.circle(screen, (0,255,0), (barrelx, barrely), 64, 0)
    explosion_delay = explosion_delay + 1

    if explosion_delay == 100:
    explosion_delay = 0

” 0 当枪管被“射击”时。

When I try to make a shape using variables, I keep getting this error message:

"TypeError: integer argument expected, got float"

import pygame._view
import pygame, sys
from pygame.locals import *
import random

pygame.init()

...

barrel = pygame.image.load("images\Barrel.gif")
barrel_create = 0
barrelx = screen.get_height()- barrel.get_height()
barrely = screen.get_width()/2 - barrel.get_width()/2
barrel_exist = 0
explosion_delay = 0

...

while running:

    if barrel_exist == 0:
        if barrel_create == 500:
            barrely = 200
        barrelx = random.randint(0,400)
        barrel_exist = 1
    if barrel_exist == 1:
        barrely = barrely + 0.1
        if barrely > 400:
            barrel_exist = 0

    if explosion_delay > 0:
        pygame.draw.circle(screen, (0,255,0), (barrelx, barrely), 64, 0)
    explosion_delay = explosion_delay + 1

    if explosion_delay == 100:
    explosion_delay = 0

The explosion_delay > 0 when the barrel is "shot".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

凉墨 2024-12-12 08:19:30

barrely = Barrely + 0.1

由于这一行,

barrely 在某个时刻必须是浮点数。我认为你应该这样做 pygame.draw.circle(screen, (0,255,0), (int(barrelx), int(barrely)), 64, 0) 将变量截断为整数功能需要。

barrely = barrely + 0.1

barrely must be a float at some point because of this line.

I think you should do pygame.draw.circle(screen, (0,255,0), (int(barrelx), int(barrely)), 64, 0) to truncate the variables to integers as the function requires.

妞丶爷亲个 2024-12-12 08:19:30

您没有说明哪一行给出了错误,但如果您使用的是 Python 3,/ 会给出 float 结果。使用 // 表示整数。

You don't say which line is giving the error, but if you're using Python 3, / gives a float result. Use // for an integer.

缘字诀 2024-12-12 08:19:30

我从 改为

pygame.draw.circle(win, self.color,(self.x, self.y), 
           self.radius)

pygame.draw.circle(win, self.color, (int(self.x), 
          int(self.y)), int(self.radius))

效果很好。我使用Python 2.7

I change from

pygame.draw.circle(win, self.color,(self.x, self.y), 
           self.radius)

to

pygame.draw.circle(win, self.color, (int(self.x), 
          int(self.y)), int(self.radius))

and it work fine. I use python 2.7

遥远的绿洲 2024-12-12 08:19:30

感谢所有的答案 - 只是想指出,我也遇到了同样的问题,关于 PyODE 教程 2(使用 pygame 完成);感谢该线程中的注释,我对其进行了修改,使其可以工作,它位于:pyode-pygame-example2.py

但请注意,在哪里转换为 int 确实很重要 - 如果您有一个计算坐标的函数,您需要“整数化”其返回值(“最终”坐标),而不是其参数:) (我在上面的脚本中犯的错误

Thanks for all the answers - just wanted to note, that I also bumped in the very same problem, in respect to PyODE Tutorial 2 (which is done with pygame); thanks to the notes in this thread, I modified it so it works, and it's here: pyode-pygame-example2.py.

Note, however, that is does matter where you cast to int - if you have a function calculating coordinates, you'd want to "integerize" its return (the 'final' coordinates), not its arguments :) (mistake I made in the script above)

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