微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pygame 在运行 pygame draw 函数时抛出错误

如何解决pygame 在运行 pygame draw 函数时抛出错误

我正在尝试使用 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 举报,一经查实,本站将立刻删除。