1

最近我一直在尝试用 pymunk 制作一个简单的游戏。我的目标(目前)是让盒子受到重力的影响,并在它们的up()功能被调用时“跳跃”。

这是我的代码。

import pygame
from pygame.locals import *
from pygame.color import *
import pymunk
import pymunk.pygame_util

#constants
WALL_BOUNCINESS = 0
FLOOR_FRICTION = 1
accuracy=1
WALL_PADDING=5


#pygame initiation stuff
pygame.init()
width,height=1500,750
screen = pygame.display.set_mode((width, height))

#main loop initialisations
clock = pygame.time.Clock()
running = True

#physics things
space=pymunk.Space()
space.gravity=(0,-900)

#where the magic happens
draw_options = pymunk.pygame_util.DrawOptions(screen)

#boundaries
wall_body = space.static_body
walls = [pymunk.Segment(wall_body, (WALL_PADDING,height-WALL_PADDING), (width-WALL_PADDING,height-WALL_PADDING),1),
    pymunk.Segment(wall_body, (WALL_PADDING,height-WALL_PADDING), (WALL_PADDING,WALL_PADDING), 1),
    pymunk.Segment(wall_body, (WALL_PADDING,WALL_PADDING), (width-WALL_PADDING,WALL_PADDING), 1),
    pymunk.Segment(wall_body, (width-WALL_PADDING,WALL_PADDING), (width-WALL_PADDING,height-WALL_PADDING),1)]
for wall in walls:
    wall.elasticity = WALL_BOUNCINESS
    wall.friction = FLOOR_FRICTION
space.add(walls)

#keybindings
keybindings={}

#adding objects

class player(): #using a class for neatness, not instantiation purposes
    def __init__(self, starting_coords, keyset_LUR=None):

        x,y=starting_coords[0], starting_coords[1]

        mass=10
        inertia = pymunk.moment_for_box(mass, (100,100))

        self.body = pymunk.Body(mass, inertia)

        self.shape = pymunk.Poly(self.body,((x,y),(x+100,y),(x+100,y+100),(x,y+100)))
        self.shape.elasticity = 0
        self.shape.friction = 0.5

        space.add(self.body, self.shape)

        if keyset_LUR:
            funcs=iter((self.left, self.up, self.right))
            for key in keyset_LUR:
                keybindings[key]=next(funcs)

    def left(self):...
    def right(self):...
    def up(self):
        self.body.apply_impulse_at_local_point((0,100))


p1=player((100,100),(K_a,K_w,K_d))
p2=player((1300,100),(K_LEFT,K_UP,K_RIGHT))

#main løóp
while running:
    for event in pygame.event.get():
        if event.type == QUIT: running = False
        elif event.type == KEYDOWN:
            if event.key in keybindings: keybindings[event.key]()

    screen.fill(THECOLORS["white"])

    space.debug_draw(draw_options) #so happy this exists

    dt=1/(60*accuracy)
    for _ in range(accuracy):
        space.step(dt)

    pygame.display.flip()
    clock.tick(60)

为我的非正式评论道歉。我尝试使用简单的变量名,这样代码就不难理解了。 我相信问题出在玩家类上,因为它控制着方块的行为方式。我的物理知识很差,而且我并不真正理解惯性的概念,所以如果我在与物理学相关的基本概念方面犯了错误,我非常感谢任何可以帮助我消除误解的解释。这就是它当前的行为方式。

这是它当前的行为方式

PS。我正在尝试自学 pymunk/python,并且在两者中的任何一个方面都很少接受正规教育,所以我为任何“非正统”代码道歉,希望它不会太难遵循。

谢谢 :)

4

1 回答 1

1

问题是播放器框未居中。盒子的重心在世界坐标中的 (0,0) 处。

所以,你要做的就是把多边形变成一个盒子(-50,-50)到(50,50)。然后把它放在你想要的地方,你移动形状所附着的身体。

所以,首先更换

    self.shape = pymunk.Poly(self.body,((x,y),(x+100,y),(x+100,y+100),(x,y+100)))

    self.shape = pymunk.Poly(self.body,((-50,-50),(50,-50),(50,50),(-50,50)))

然后将身体位置设置为 x,y 之后

    self.body.position = x,y
于 2018-09-02T08:28:49.387 回答