From eb153ce92870a811c00c8be79427ebb57b2bb1fb Mon Sep 17 00:00:00 2001 From: s464947 Date: Tue, 22 Mar 2022 11:55:01 +0100 Subject: [PATCH] move agent_movement to agent class --- agent.py | 28 ++++++++++++++++++++++------ main.py | 23 ++++++++++++++--------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/agent.py b/agent.py index 3a9e165..4adc04f 100644 --- a/agent.py +++ b/agent.py @@ -1,14 +1,30 @@ import pygame.image class trashmaster(pygame.sprite.Sprite): - def __init__(self,x,y,img,vel): + + def __init__(self,x,y,img): super().__init__() - self.x=x - self.y=y - self.img = img - self.velocity = vel + self.width=x + self.height=y + + self.x = 0 + self.y = 0 self.image = pygame.image.load(img) - self.image = pygame.transform.scale(self.image, (self.x,self.y)) + self.image = pygame.transform.scale(self.image, (self.width,self.height)) self.rect = self.image.get_rect() + + def movement(self, key, vel): + if key == pygame.K_LEFT: + self.x -= vel + + if key == pygame.K_RIGHT: + self.x += vel + + if key == pygame.K_UP: + self.y -= vel + + if key == pygame.K_DOWN: + self.y += vel + return (self.x, self.y) \ No newline at end of file diff --git a/main.py b/main.py index 443890b..42a0833 100644 --- a/main.py +++ b/main.py @@ -23,19 +23,20 @@ class WalleGame(): def update_window(self): pygame.display.update() - def draw_trashmaster(self, smieciara: trashmaster): - smieciara_list = pygame.sprite.Group() - smieciara_list.add(smieciara) - smieciara_list.draw(self.screen) + def draw_trashmaster(self, smieciara: trashmaster, pos): + # pos => (x, y) + self.screen.blit(smieciara.image, pos ) + + def reloadMap(self): + self.screen.fill(pygame.Color(self.BACKGROUND_COLOR)) + self.screen.blit(self.map, (0,0)) - - def main(): game = WalleGame() game.update_window() - smieciara_object = trashmaster(16,16,"resources/textures/trashmaster_blu.png",16) - game.draw_trashmaster(smieciara_object) + smieciara_object = trashmaster(16,16,"resources/textures/trashmaster_blu.png") + game.draw_trashmaster(smieciara_object, (0, 0)) game.update_window() @@ -45,7 +46,11 @@ def main(): for event in pygame.event.get(): if event.type == pygame.QUIT: running = False - pygame.quit() + if event.type == pygame.KEYDOWN: + game.reloadMap() + game.draw_trashmaster(smieciara_object, + smieciara_object.movement(event.key, 16)) + game.update_window() pygame.quit()