Pymunk Pygame对象位置不会更新

时间:2015-12-12 06:25:26

标签: python python-2.7 pygame pymunk

使用python 2.7尝试以下代码。基本上它是一个圆圈,悬挂在一个酒吧和一个苹果,你可以通过按空格键冲动。还有一个广场。

import time
import pygame
import pymunk
import pymunk.pygame_util
import sys

pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()

space = pymunk.Space()
space.gravity = 0, -1000

ball_body = pymunk.Body(100, 100)
ball_body.position = 400.0, 300.0
ball_body.angular_velocity = 10.0
ball_shape = pymunk.Circle(ball_body, 15)
ball_shape.friction = 0.5
ball_shape.elasticity = 0.9
ball_shape.color = (0,0,0,0)
space.add(ball_body, ball_shape)


static_lines = [pymunk.Segment(space.static_body, (20,20), (780,20), 2),
                pymunk.Segment(space.static_body, (20,580), (780,580), 2),
                pymunk.Segment(space.static_body, (20,20), (20,580), 2),
                pymunk.Segment(space.static_body, (780,20), (780,580), 2)]

for static_line in static_lines:
    static_line.color = (255,255,255)
    static_line.elasticity = 0.9
    static_line.friction = 1

space.add(static_lines)


i = 0
prev_body = pymunk.Body(10, 10000)
prev_body.position = (300, 580)
chain_fix_point = pymunk.Body()
chain_fix_point.position = (300, prev_body.position[1])

# Another

i = 0
prev_body = pymunk.Body(10, 10000)
prev_body.position = (600, 580)
chain_fix_point = pymunk.Body()
chain_fix_point.position = (600, prev_body.position[1])

while i < 20:
    # rotation_center_body = pymunk.Body()
    # rotation_center_body.position = (400, prev_body.position[1] - 20)

    body = pymunk.Body(1, 1)
    body.position = (600, prev_body.position[1] - 10)

    line = pymunk.Circle(body, 5)
    line.elasticity = 0
    line.friction = 1
    if i == 0:
        rotation_center_joint = pymunk.PinJoint(body, chain_fix_point)
    else:
        rotation_center_joint = pymunk.PinJoint(body, prev_body)

    space.add(line, body, rotation_center_joint)

    prev_body = body

    i += 1

blob_body = pymunk.Body(5, 1)
blob_body.position = prev_body.position[0], prev_body.position[1] - 40
blob_shape = pymunk.Circle(blob_body, 20)
rotation_center_joint = pymunk.SlideJoint(blob_body, prev_body,(0,0),(0,0),0,40)
space.add(blob_body, blob_shape, rotation_center_joint)

appleimg = pygame.image.load('apple.png')


box_body = pymunk.Body(10,10000)
box_body.position = 600, 300
box_vertices = [(570, 270),(570, 330),(630,330),(630,270)]
box_shape = pymunk.Poly(box_body, box_vertices, offset=(0, 0), radius=1).create_box(box_body, size = (60,60))
box_shape.friction = 0.5
box_shape.elasticity = 0.9
box_shape.color = (255,0,0)
space.add(box_body, box_shape)


def main():
    running = True
    angle = 0;
    while running == True:
        print "game loop"
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    ball_body.apply_impulse(j = (100000, 100000))

        screen.fill((0,0,0))
        pymunk.pygame_util.draw(screen, space)
        if ball_body.angle != 0:
            angle += ball_body.angular_velocity
        img = pygame.transform.rotate(appleimg, angle)
        screen.blit(img, (ball_body.position[0] - 20, 580 - ball_body.position[1]))
        pygame.display.flip()
        clock.tick(60)
        space.step(1/60)


    pygame.quit()
    quit()

main()

游戏循环运行但位置不会更新。

这段代码适用于python 3.5。但当我切换到2.7时,它失败了。

1 个答案:

答案 0 :(得分:1)

问题是在python 2.x中,当你调用step函数时,将1除以60得到0,因为/在2.x中进行整数除法。

您可以通过导入python 3分区来解决此问题 from __future__ import division

或者你可以用浮点除1/60.0

检查此问题以获取详细信息:In Python 2, what is the difference between '/' and '//' when used for division?