gpt4 book ai didi

python - Pymunk Pygame 对象位置不更新

转载 作者:太空宇宙 更新时间:2023-11-03 17:09:36 26 4
gpt4 key购买 nike

使用 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 时,它失败了。

最佳答案

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

您可以通过导入 python 3 分区来解决此问题来自 __future__ 导入部门

或者您可以除以 float 1/60.0

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

关于python - Pymunk Pygame 对象位置不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34237001/

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