无法在 pygame.draw.circle 中使用变量?
当我尝试使用变量创建形状时,我不断收到此错误消息:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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.您没有说明哪一行给出了错误,但如果您使用的是 Python 3,
/
会给出float
结果。使用//
表示整数。You don't say which line is giving the error, but if you're using Python 3,
/
gives afloat
result. Use//
for an integer.我从 改为
,
效果很好。我使用Python 2.7
I change from
to
and it work fine. I use python 2.7
感谢所有的答案 - 只是想指出,我也遇到了同样的问题,关于 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)