我正在尝试做一个简单的游戏,它运行良好。这个动作就像是俄罗斯方块的移动(就像瞬移一样),这就是我想要的。我不想流畅的运动,我想要这种运动风格。如何为这种运动风格实现边界或碰撞?
但有一个问题...当我尝试为我的角色和矩形添加边界或碰撞时,它不起作用,但是如果我编写此代码,它只能工作一次,然后左右两个箭头钥匙不再工作了。我怎么解决这个问题?
import pygame
pygame.init()
ekranx = 160
ekrany = 310
window = pygame.display.set_mode((ekranx, ekrany))
white = (255,255,255)
black = (0,0,0)
blue = (0,0,200)
pygame.display.set_caption("TOP")
clock = pygame.time.Clock()
ball = pygame.image.load("top.png")
def top(x,y):
window.blit(ball,(x,y))
top_x = (55)
top_y = (250)
cikis = False
while not cikis:
for event in pygame.event.get():
if event.type == pygame.QUIT:
cikis = True
if event.type == pygame.KEYDOWN:
#--SOL
if event.key == pygame.K_LEFT:
top_x += -50
#--SAĞ
if event.key == pygame.K_RIGHT:
top_x += 50
#print(event)
window.fill(white)
top(top_x,top_y)
pygame.draw.rect(window,blue,(5,5,150,300),5)
if top_x < 6:
pygame.K_LEFT = False
if top_x > 104:
pygame.K_RIGHT = False
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
不要发布您的代码的截图,直接的形式添加代码后[最小,完整和可验证的示例](https://stackoverflow.com/help/mcve)。只需将代码粘贴到问题中,选择它并按下Ctrl-K即可。 – skrx