From 35dac32d41ca6e8ad546cf5b7cf62b4ee2048dcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Cwojdzi=C5=84ski?= Date: Wed, 9 Mar 2022 18:59:50 +0100 Subject: [PATCH] Add AI agent movement --- main.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 7fe6061..a7efd65 100644 --- a/main.py +++ b/main.py @@ -7,6 +7,16 @@ FPS = 30 AGENT_IMG = pygame.image.load("dot-22-433567.png") AGENT = pygame.transform.scale(AGENT_IMG, (50, 50)) +def agent_movement(keys_pressed, agent): + if keys_pressed[pygame.K_LEFT] and agent.x > 0: + agent.x -= 50 + if keys_pressed[pygame.K_RIGHT] and agent.x < 750: + agent.x += 50 + if keys_pressed[pygame.K_UP] and agent.y > 0: + agent.y -= 50 + if keys_pressed[pygame.K_DOWN] and agent.y < 750: + agent.y += 50 + def draw_window(agent): WIN.fill((255,255,255)) @@ -19,13 +29,16 @@ def main(): agent = pygame.Rect(0, 0, 50, 50) while run: + clock.tick(FPS) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False draw_window(agent) + keys_pressed = pygame.key.get_pressed() + agent_movement(keys_pressed, agent) pygame.quit() if __name__ == "__main__": - main() + main() \ No newline at end of file -- 2.20.1