[英]PyGame border collision detection not working
I am new to pygame and I made a screen border detection but it doesn't work我是 pygame 的新手,我进行了屏幕边框检测,但它不起作用
screen = pygame.display.set_mode([854, 480])
and to check if its touching the edge并检查它是否接触边缘
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
but if we use it breaks / the player gets stuck但是如果我们使用它会中断/播放器会卡住
and the entire code is整个代码是
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()
The width of the window is 854 and the height is 480.You have to test if playerY < 480
and playerX < 854
. window 的宽度为 854,高度为 480。您必须测试playerY < 480
和playerX < 854
。 Additionally you should consider the size of the player:此外,您应该考虑播放器的大小:
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()
I suggest simplifying the code with move_ip
and clamp_ip
:我建议使用move_ip
和clamp_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()
It should be它应该是
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
Edit: @rabbid76 was right!编辑: @rabbid76是对的! You have swapped your x and y!你已经交换了你的 x 和 y!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.