问题描述
我正在尝试使用 pygame 在 pyBox2d 世界中绘制所有主体,并且在运行代码时收到此错误消息:
pygame.draw.circle(screen,(255,0),position,radius)
TypeError: integer argument expected,got float
import pygame
from circle import Circle
from draw import Draw
from Box2D import b2World
PPM = 20
TARGET_FPS = 60
TIME_STEP = 1.0 / TARGET_FPS
pygame.init()
screen = pygame.display.set_mode((600,480))
pygame.display.set_caption("Physics")
clock = pygame.time.Clock()
# A list for all of our rectangles
world = b2World(gravity=(0,30),doSleep=True)
close = False
while not close:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close = True
screen.fill((255,255,255))
click,_,_ = pygame.mouse.get_pressed()
if click == 1:
x,y = pygame.mouse.get_pos()
Box = Circle(world,x,y,PPM)
for Box in world.bodies:
if Box.worldCenter.y * PPM > 520:
world.DestroyBody(Box)
Draw(screen,world.bodies,PPM)
world.Step(TIME_STEP,10,10)
pygame.display.flip()
clock.tick(TARGET_FPS)
pygame.quit()
Box.py
from Box2D import (b2FixtureDef,b2polygonShape)
class Box:
def __init__(self,world,PPM):
self.x = x / PPM
self.y = y / PPM
self.w = 1
self.h = 1
self.world = world
self.body = self.world.CreateDynamicBody(
position=(self.x,self.y),fixtures=b2FixtureDef(
shape=b2polygonShape(Box=(self.w,self.h)),density=2.0,friction = 0.5))
circle.py
from Box2D import (b2FixtureDef,b2CircleShape)
class Circle:
def __init__(self,PPM):
self.x = x / PPM
self.y = y / PPM
self.r = 1
self.world = world
self.body = self.world.CreateDynamicBody(
position=(self.x,fixtures=b2FixtureDef(
shape=b2CircleShape(radius = self.r),friction = 0.5))
绘制.py
import pygame
def poly(screen,body,fixture,PPM):
shape = fixture.shape
vertices = [(body.transform * v) * PPM for v in shape.vertices]
pygame.draw.polygon(screen,vertices)
pygame.draw.polygon(screen,(0,vertices,1)
def Circle(screen,PPM):
shape = fixture.shape
radius = shape.radius
position = (int(body.position.x * PPM),int(body.position.y * PPM))
pygame.draw.circle(screen,radius)
def Draw(screen,PPM,bodies):
for body in bodies:
for fixture in body.fixtures:
try:
poly(screen,PPM)
except: pass
try:
Circle(screen,PPM)
except: pass
Circle(screen,fixture)
我觉得这很简单,但我不明白这个错误信息。 我还从这个 stackoverflow 页面获得了大部分代码: In pybox2d physics simulation bodies are not falling 这段代码很可能没有优化,所以我也不知道如何解决这个问题。 预先感谢您提供任何可能的帮助。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)