【问题标题】:PyGame border collision detection not workingPyGame边界碰撞检测不起作用
【发布时间】:2021-03-14 15:05:48
【问题描述】:

我是 pygame 的新手,我做了一个屏幕边框检测,但它不起作用

screen = pygame.display.set_mode([854, 480])

并检查它是否接触到边缘

pressed = pygame.key.get_pressed()

if playerY > 0:
    if pressed[pygame.K_w]: playerY -= 1
if playerY > 854:
    if pressed[pygame.K_s]: playerY += 1
if playerX > 0:
    if pressed[pygame.K_a]: playerX -= 1
if playerX > 480:
    if pressed[pygame.K_d]: playerX += 1

但如果我们使用它会中断/玩家会卡住

整个代码是

import pygame

pygame.init()

screen = pygame.display.set_mode([854, 480])

running = True

playerX = 70
playerY = 400

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    pressed = pygame.key.get_pressed()

    if playerY > 0:
        if pressed[pygame.K_w]: playerY -= 1
    if playerY > 854:
        if pressed[pygame.K_s]: playerY += 1
    if playerX > 0:
        if pressed[pygame.K_a]: playerX -= 1
    if playerX > 480:
        if pressed[pygame.K_d]: playerX += 1
    
    screen.fill((255, 255, 255))

    player = pygame.draw.rect(screen, (0, 0, 0), pygame.Rect(playerX, playerY, 10, 25))

    pygame.display.flip()

【问题讨论】:

  • playerY < 854playerX < 480 有帮助吗?
  • @Ynjxsjmh nope 它卡在右侧 70% 并且地面碰撞不起作用
  • 我想帮忙,你能通过编辑你的帖子提供Minimal, Reproducible Example吗?
  • 你已经交换了 854 和 480。宽度是 854,高度是 480。因此playerY < 480playerX < 854
  • @ARandomPro 在pygame.display.set_mode([854, 480])中,X的最大值是854,Y的最大值是480。@Rabbid76是对的。

标签: python pygame


【解决方案1】:

窗口的宽度是854,高度是480。你必须测试playerY < 480playerX < 854。此外,您应该考虑播放器的大小:

import pygame
pygame.init()

screen = pygame.display.set_mode([854, 480])
running = True
playerX = 70
playerY = 400

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    pressed = pygame.key.get_pressed()
    if playerY > 0:
        if pressed[pygame.K_w]: playerY -= 1
    if playerY < 480 - 25:
        if pressed[pygame.K_s]: playerY += 1
    if playerX > 0:
        if pressed[pygame.K_a]: playerX -= 1
    if playerX < 854 - 10:
        if pressed[pygame.K_d]: playerX += 1
    
    screen.fill((255, 255, 255))
    player = pygame.draw.rect(screen, (0, 0, 0), pygame.Rect(playerX, playerY, 10, 25))
    pygame.display.flip()

我建议使用move_ipclamp_ip 简化代码:

import pygame
pygame.init()

screen = pygame.display.set_mode([854, 480])
player = pygame.Rect(70, 40, 10, 25)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    pressed = pygame.key.get_pressed()
    player.move_ip(
        pressed[pygame.K_d] - pressed[pygame.K_a],
        pressed[pygame.K_s] - pressed[pygame.K_w]
    )
    player.clamp_ip(screen.get_rect())    
    
    screen.fill((255, 255, 255))
    player = pygame.draw.rect(screen, (0, 0, 0), player)
    pygame.display.flip()

【讨论】:

  • 谢谢!它修复了为我的游戏添加重力的时间再见!
【解决方案2】:

应该是

pressed = pygame.key.get_pressed()

if playerY > 0:
    if pressed[pygame.K_w]: playerY -= 1
if playerY < 480:
    if pressed[pygame.K_s]: playerY += 1
if playerX > 0:
    if pressed[pygame.K_a]: playerX -= 1
if playerX < 854:
    if pressed[pygame.K_d]: playerX += 1

编辑:@rabbid76 是对的!你已经交换了你的 x 和 y!

【讨论】:

  • 卡在右侧 70%,地面碰撞不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多