python - 粒子没有移动,那么问题出在哪里呢?

标签 python pygame pymunk

我关注了 YouTube 视频“https://www.youtube.com/watch?v=cCiXqK9c18g”,在视频中他创建了一个代表球的类,并使用 pymunk 制作了一个实体并将其添加到空间,之后他在球类中创建了一个方法,该方法将使用 pygame 来绘制球,我几乎喜欢他

import pygame
import pymunk

pygame.init()

fps = 60
dt = 1/fps
dsX = 800 # screen width
dsY = 500 # screen height

display = pygame.display.set_mode((dsX, dsY))
space = pymunk.Space()
clock = pygame.time.Clock()

def convert_cor(point): # convet the coordinates from pymunk to pygame coordinates
    return point[0], dsY - point[1]

class Particle: # v: velocity, pos: position[x, y], r: radius of particle(Circle)

    def __init__(self, pos = [0, 0], v = [0, 0], r = 10, color = (255, 0, 0)):

        self.pos = pos
        self.v = v
        self.r = r
        self.color = color
        self.body = pymunk.Body()
        self.body.position = self.pos
        self.body.velocity = self.v # this is the veclocity
        self.shape = pymunk.Circle(self.body, self.r)
        self.shape.dencity = 1
        self.shape.elasticity = 1
        space.add(self.body, self.shape)

    def draw(self):

        pygame.draw.circle(display, self.color, convert_cor(self.pos), self.r)

class Box: # thickness of the sides of the box and L1, L2, L3, L4 are the sides of the box

    def __init__(self, thickness, color):

        self.thickness = thickness
        self.color = color
        L1 = pymunk.Body(body_type = pymunk.Body.STATIC)
        L2 = pymunk.Body(body_type = pymunk.Body.STATIC)
        L3 = pymunk.Body(body_type = pymunk.Body.STATIC)
        L4 = pymunk.Body(body_type = pymunk.Body.STATIC)
        L1_shape = pymunk.Segment(L1, (0, 0), (dsX, 0), self.thickness)
        L2_shape = pymunk.Segment(L2, (dsX, 0), (dsX, dsY), self.thickness)
        L3_shape = pymunk.Segment(L3, (dsX, dsY), (0, dsY), self.thickness)
        L4_shape = pymunk.Segment(L4, (0, dsY), (0, 0), self.thickness)
        space.add(L1, L1_shape)
        space.add(L2, L2_shape)
        space.add(L3, L3_shape)
        space.add(L4, L4_shape)

    def draw(self):

        pygame.draw.line(display, self.color, convert_cor((0, 0)), convert_cor((dsX, 0)), self.thickness * 2)
        pygame.draw.line(display, self.color, convert_cor((dsX, 0)), convert_cor((dsX, dsY)), self.thickness * 2)
        pygame.draw.line(display, self.color, convert_cor((dsX, dsY)), convert_cor((0, dsY)), self.thickness * 2)
        pygame.draw.line(display, self.color, convert_cor((0, dsY)), convert_cor((0, 0)), self.thickness * 2)

def Sim(): # the infinite while loop as a function

    box = Box(2, (0, 255, 255))
    particle = Particle(pos =[dsX/2, dsY/2], v = [-200, 500]) # here i gave the position and the velocity

    while True:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                return

        display.fill((255, 255, 255))
        box.draw()
        particle.draw()
        clock.tick(fps)
        space.step(dt)
        pygame.display.update()

Sim()

pygame.quit()

问题是,我还做了一个类,它将为显示添加刚性侧面,并且我使用“draw”方法从 Box 类中绘制侧面 问题出现在时间 5:58视频中,他给了球速度,球开始移动,但在我的代码中它没有移动。知道为什么它不动吗? 注意:我在代码中调用了球粒子

最佳答案

您的错误既是拼写错误,又是使用了错误的变量。

在你的粒子绘制函数中...

# OLD
def draw(self):
        pygame.draw.circle(display, self.color, convert_cor(self.pos), self.r)

# New
def draw(self):
        pygame.draw.circle(display, self.color, convert_cor(self.body.position), self.r)

你必须使用 body 的位置,因为这是物理体在 pymunk 空间中的位置。

其次...

class Particle: # v: velocity, pos: position[x, y], r: radius of particle(Circle)

    def __init__(self, pos, v, r=10, color=(255, 0, 0)):
        ...
        # Old
        self.shape.dencity = 1
    
        # New
        self.shape.density = 1

由于密度未设置为任何值,Pymunk 存在除以零的错误,因此它不会更新 body 的位置。

关于python - 粒子没有移动,那么问题出在哪里呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70749184/

相关文章:

python - pandas 使用 LIKE 运算符加入条件

python - Pygame 移动 Sprite 上的鼠标点击检测

2d - 什么类型的速度-时间曲线模拟在水平表面上滑动到停止的物体?

python - 如何使用 pymunk 的碰撞处理程序调用函数?

python - 使用 Opennoise 在 Pymunk 中创建地形(形状)

python - 使用带有自制软件的 python 安装 selenium

python - 来自rabbitmq队列的base64 'payload'字符串无法正确解码

python - python中数据库连接池的最佳解决方案是什么?

python - Python 游戏 - 添加选项 "Restart game"

python - 如何在 Pygame 中添加背景图像