屏幕两侧有两种颜色。 我希望能够使用左箭头选择屏幕的左半部分,然后使用向上和向下箭头操作颜色,直到满意为止。
然而,我目前的代码不起作用。问题似乎是需要一系列关键命令,即左箭头,然后向上和向下操作颜色。
red = (100, 0, 0)
green = (0, 100, 0)
display_width = 800
display_height = 600
dw2 = display_width/2
calibration_exit = False
while not calibration_exit:
pygame.event.pump()
pygame.draw.rect( screen, red, [ 0 , 0, dw2, display_height ] )
pygame.draw.rect( screen, green, [ dw2 , 0, dw2, display_height ] )
pygame.display.update()
# Get Keys #
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
while not keys[K_RIGHT]:
keys = pygame.key.get_pressed()
if keys[K_UP]:
red = list(red)
red[0] += 10
red = tuple(red)
pygame.draw.rect( screen, red, [ 0 , 0, dw2, display_height ] )
pygame.display.update()
if keys[K_DOWN]:
red = list(red)
red[0] -= 10
red = tuple(red)
pygame.draw.rect( screen, red, [ 0 , 0, dw2, display_height ] )
pygame.display.update()
答案 0 :(得分:2)
可以把它想象成每帧只能召唤一次pygame.key.get_pressed()
,每帧就召唤一次pygame.display.update()
等等。如果那么逻辑,你就不能做你的标准,因为你开始'每次都在while
循环的根。这意味着您必须单独跟踪您的州。类似的东西:
calibration_exit = False
selected = None
while not calibration_exit:
pygame.event.pump()
pygame.draw.rect( screen, red, [ 0 , 0, dw2, display_height ] )
pygame.draw.rect( screen, green, [ dw2 , 0, dw2, display_height ] )
pygame.display.update()
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
selected = "left"
if keys[K_RIGHT]:
selected = "right"
if selected == "left":
if keys[K_UP]:
# modify 'left' by going up
if keys[K_DOWN]:
# modify 'left' by going down
elif selected == "right":
if keys[K_UP]:
# modify 'right' by going up
if keys[K_DOWN]:
# modify 'right' by going down