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