gpt4 book ai didi

python - 是什么导致这些物体与 pymunk 一起飞向左侧?

转载 作者:行者123 更新时间:2023-12-01 09:05:06 27 4
gpt4 key购买 nike

最近我一直在尝试用 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)

对我的非正式评论表示歉意。我尝试使用简单的变量名称,以便代码不太难以理解。我相信问题出在玩家类别上,因为它控制方 block 的行为。我的物理知识很差,而且我不太理解惯性的概念,所以如果我做了一个与物理学相关的基本概念有关的错误,我真的很感激任何可以帮助消除我的误解的解释。这就是它目前的行为方式。

This is how it currently behaves

PS。我正在尝试自学 pymunk/python,并且对这两者中的任何一个都没有接受过正规教育,因此我对任何“非正统”代码表示歉意,希望它不会太难理解。

谢谢:)

最佳答案

问题是玩家框没有居中。盒子的重心位于世界坐标中的 (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)))

然后将 body 位置设置为 x,y

    self.body.position = x,y

关于python - 是什么导致这些物体与 pymunk 一起飞向左侧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52127771/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com