因此,由于某种原因,我的对象/规则不会停留在我告诉它的同一位置,每次移动时,它都会朝与我相反的方向移动,并且每次与平台相撞并按a或某物时,它将向右移动,但是如果我在按d时与平台碰撞,它将向左移动。 https://gyazo.com/b3adcbe08a79760e1cd0ded940ee2373我希望它留在同一地点,但不要留在该地点。
我编写了这段代码,但仅适用于我的平台
let { parameter } = useParams()
我的完整代码
if keys[pygame.K_d]:
for Platform in platforms:
Platform.x -= playerman.speed
for Rule in rules:
Rule.x -= playerman.speed
if keys[pygame.K_a]:
for Platform in platforms:
Platform.x += playerman.speed
for Rule in rules:
Rule.x += playerman.speed
答案 0 :(得分:2)
您必须像移动Rule
对象一样移动Platform
对象。如果玩家撞到平台,则必须像对Rule
对象那样“撤消” Platform
对象的移动:
while run:
# [...]
# lets player move
keys = pygame.key.get_pressed()
px, py = playerman.x, playerman.y
platform_rect_list = [p.rect for p in platforms]
player_rect = playerman.get_rect()
player_rect.topleft = (px, py)
playerman.y = py
if player_rect.collidelist(platform_rect_list) < 0:
playerman.x = px
move_right = keys[pygame.K_d]
move_left = keys[pygame.K_a]
if move_right:
for Platform in platforms:
Platform.x -= playerman.speed
for Rule in rules:
Rule.x -= playerman.speed # <---
if move_left:
for Platform in platforms:
Platform.x += playerman.speed
for Rule in rules:
Rule.x += playerman.speed # <---
platform_rect_list = [p.get_rect() for p in platforms] # get_rect()
player_rect = playerman.get_rect()
player_rect.topleft = (px, py)
playerman.y = py
cI = player_rect.collidelist(platform_rect_list)
if cI >= 0:
# undo movement of platforms dependent on the direction and intersection distance
dx = 0
if move_right:
dx = platform_rect_list[cI].left - player_rect.right
if move_left:
dx = platform_rect_list[cI].right - player_rect.left
for Platform in platforms:
Platform.x -= dx
Platform.get_rect() # update rectangle
for Rule in rules:
Rule.x -= dx # <---