I'm designing a visual matrix in Python where the offset of the matrix rendering is altered by user key presses. At the moment, I'm working on the x-axis offset functionality, and it is almost working. The one issue I seem to be running into is that the key presses are only functioning as inputs while the cursor is in motion (or more technically, during every frame that the cursor is in a different position from that of the last). I'm using the Pygame tool and the event types. My code resembles an optimized composite of some techniques I've found, but does not fully function as I expected.
My source code is as follows:
import pygame
from pygame import key
pygame.init()
width = 1360
height = 705
screen = pygame.display.set_mode([width, height])
clock = pygame.time.Clock()
pygame.display.set_caption("Test Game Window")
x_offset = 0
y_offset = 0
running = True
while running:
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if keys[pygame.K_LEFT]:
x_offset -= 5
if keys[pygame.K_RIGHT]:
x_offset += 5
screen.fill((0, 0, 0))
pygame.draw.line(screen, (255, 255, 255), (int(width/2 + x_offset), int(0 + y_offset)),
(int(width/2 + x_offset), int(height + y_offset)), 3)
pygame.display.flip()
clock.tick(60)
pygame.quit()
Where might I have gone wrong, considering that I intend to have the offset update when I'm pressing the arrow keys, regardless of whether or not the mouse is in motion? Am I missing some simple but key element, or is another technique altogether required to solve the problem?