我如何为这种运动风格实施边界或碰撞?
How can I implement boundaries or collisions for this movement style?
我正在尝试制作一款简单的游戏并且运行良好。运动就像俄罗斯方块的运动(瞬间,像传送),这就是我想要的。我不要流畅的动作,我要这种动作风格。
但是有一个问题...当我尝试为我的角色和矩形添加边界或碰撞时,它不起作用,但是如果我编写这段代码它只工作一次然后左右都工作箭头键不再起作用。我该如何解决这个问题?
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()
pygame.K_LEFT = False
<-- 不要那样做。您在这里覆盖了 pygame.K_LEFT
常量(实际上是一个整数 276
),这意味着您不能再在程序中使用它(pygame 将不再识别这些事件).
如果球向左或向右移动得太远,只需将 top_x
设置为最小值或最大值:
if top_x < 6:
top_x = 6
if top_x > 104:
top_x = 104
我正在尝试制作一款简单的游戏并且运行良好。运动就像俄罗斯方块的运动(瞬间,像传送),这就是我想要的。我不要流畅的动作,我要这种动作风格。
但是有一个问题...当我尝试为我的角色和矩形添加边界或碰撞时,它不起作用,但是如果我编写这段代码它只工作一次然后左右都工作箭头键不再起作用。我该如何解决这个问题?
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()
pygame.K_LEFT = False
<-- 不要那样做。您在这里覆盖了 pygame.K_LEFT
常量(实际上是一个整数 276
),这意味着您不能再在程序中使用它(pygame 将不再识别这些事件).
如果球向左或向右移动得太远,只需将 top_x
设置为最小值或最大值:
if top_x < 6:
top_x = 6
if top_x > 104:
top_x = 104