【发布时间】:2021-09-12 03:11:02
【问题描述】:
我有一个 box2d pygame 平台示例。您可以移动玩家并生成平台。除非玩家与关卡中的所有平台发生碰撞,否则我无法对玩家执行跳跃动作。这不是预期的行为,当您触摸一个平台而不是所有其他平台时,您希望能够跳跃。我正在使用 for 循环来循环播放玩家的所有碰撞,并根据需要设置 can_jump。
main.py
import pygame
from draw import Draw
from Box2D import (b2World,b2Vec2)
from box import Box
from player import Player
def Run():
PPM = 20
TARGET_FPS = 60
TIME_STEP = 1.0 / TARGET_FPS
SCREEN_WIDTH,SCREEN_HEIGHT = 640,480
CAPTION = ""
BGCOLOR = ((255,255,255))
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption(CAPTION)
clock = pygame.time.Clock()
world = b2World(gravity=(0, 30), doSleep=True)
closed = False
global player, hold_left, hold_right, can_jump, speed, deceleration
player = Player(world,200,200,PPM)
speed = 20
deceleration = 0.95
hold_left = False
hold_right = False
can_jump = False
while not closed:
def left():
player.body.ApplyForce(b2Vec2(-1*speed*speed,0),point=player.body.worldCenter,wake=True)
def right():
player.body.ApplyForce(b2Vec2(speed*speed,0),point=player.body.worldCenter,wake=True)
if hold_left:
left()
elif hold_right:
right()
if len(player.body.contacts) == 0:
can_jump = False
else:
for contact in player.body.contacts:
contact = contact.contact
print(contact)
if contact.manifold.localPoint == b2Vec2(0,1):
can_jump=True
elif contact.manifold.localPoint != b2Vec2(0,1):
can_jump=False
for event in pygame.event.get():
if event.type == pygame.QUIT:
closed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
hold_right = True
elif event.key == pygame.K_a:
hold_left = True
elif event.key == pygame.K_SPACE:
if can_jump:
player.body.linearVelocity.y = 0
player.body.ApplyLinearImpulse(b2Vec2(0,-150),point=player.body.worldCenter,wake=True)
if event.type == pygame.KEYUP:
if event.key == pygame.K_d:
hold_right = False
elif event.key == pygame.K_a:
hold_left = False
click = pygame.mouse.get_pressed()
if click[0] == 1:
x,y = pygame.mouse.get_pos()
Box(world, x, y, PPM)
player.body.linearVelocity.x *= deceleration
screen.fill(BGCOLOR)
Draw(screen,world.bodies,PPM)
world.Step(TIME_STEP, 10, 10)
pygame.display.flip()
clock.tick(TARGET_FPS)
pygame.quit()
if __name__ == '__main__':
Run()
player.py
from Box2D import (b2FixtureDef, b2PolygonShape)
class Player:
def __init__(self, world, x, y, PPM):
self.x = x / PPM
self.y = y / PPM
self.w = 1
self.h = 1
self.gh = 0.1
self.world = world
self.body = self.world.CreateDynamicBody(
position=(self.x, self.y),
fixtures=b2FixtureDef(
shape=b2PolygonShape(box=(self.w, self.h)), density=2.0, friction = 0.1))
self.body.fixedRotation = True
box.py
from Box2D import (b2FixtureDef, b2PolygonShape)
class Box:
def __init__(self, world, x, y, PPM):
self.x = x / PPM
self.y = y / PPM
self.w = 10
self.h = 1
self.world = world
self.body = self.world.CreateStaticBody(
position=(self.x, self.y),
fixtures=b2FixtureDef(
shape=b2PolygonShape(box=(self.w, self.h)), density=2.0, friction = 0.1))
draw.py
import pygame
from Box2D import b2PolygonShape
def Poly(screen,body,fixture,PPM):
shape = fixture.shape
vertices = [(body.transform * v) * PPM for v in shape.vertices]
pygame.draw.polygon(screen, (0, 255, 187), vertices)
pygame.draw.polygon(screen, (0,0,0), vertices,2)
def Draw(screen,bodies,PPM):
for body in bodies:
for fixture in body.fixtures:
if isinstance(fixture.shape, b2PolygonShape):
Poly(screen,body,fixture,PPM)
【问题讨论】:
-
听起来您可能需要学习如何使用调试器来单步调试您的代码。使用好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏差在哪里。如果您要进行任何编程,这是必不可少的工具。延伸阅读:How to debug small programs
-
我将使用调试器。
标签: python pygame game-physics collision