move agent_movement to agent class #4

Merged
s462072 merged 1 commits from movement into master 2022-03-22 11:55:53 +01:00
2 changed files with 36 additions and 15 deletions

View File

@ -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)

23
main.py
View File

@ -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()