1
私は現在動いているスクエアをプログラミングしています。私は自分の鍵でそれを制御し、境界から外れないようにしたい。しかし、境界の外に出ないようにすることはできません(窓の側面)。ここに私のコードは次のとおりです。pygame moving squareはバウンダリー外に出る
import sys, pygame
pygame.init()
width, height = 700, 700
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
clock = pygame.time.Clock()
FPS = 120
x1 = 0
y1 = 0
xmb1 = False
xmf1 = False
ymb1 = False
ymf1 = False
squareh = 50
squarew = 50
squares = 3
BLACK = (0,0,0)
WHITE = (255,255,255)
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
xmb1 = True
if event.key == pygame.K_RIGHT:
xmf1 = True
if event.key == pygame.K_UP:
ymb1 = True
if event.key == pygame.K_DOWN:
ymf1 = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
xmb1 = False
if event.key == pygame.K_RIGHT:
xmf1 = False
if event.key == pygame.K_UP:
ymb1 = False
if event.key == pygame.K_DOWN:
ymf1 = False
if x1 == 0:
xmb1 = False
if y1 == 0:
ymb1 = False
if x1 == width - squarew:
xmfl = False
if y1 == height - squareh:
ymf1 == False
if xmb1:
x1 -= squares
if xmf1:
x1 += squares
if ymb1:
y1 -= squares
if ymf1:
y1 += squares
screen.fill(BLACK)
pygame.draw.rect(screen, WHITE, (x1, y1, squareh, squarew), 0)
pygame.display.flip()
clock.tick(FPS)
誰がこの問題を解決する方法を知っていますか?私は最近、pygameの学んだ
ありがとうございました! – 05bs001