diff --git a/bfs.py b/bfs.py new file mode 100644 index 0000000..e69de29 diff --git a/colors.py b/colors.py new file mode 100644 index 0000000..1b85afc --- /dev/null +++ b/colors.py @@ -0,0 +1,5 @@ +gray = (80, 80, 80) +white = (255, 255, 255) +blue = (0, 0, 255) +green = (0, 255, 0) +red = (255, 0, 0) diff --git a/house.py b/house.py new file mode 100644 index 0000000..68374ee --- /dev/null +++ b/house.py @@ -0,0 +1,23 @@ +class House: + def __init__(self, position, grid_size): + self.pos = position + self.size = grid_size + + +def is_house(position, houses): + for house in houses: + if house.pos == position: + return True + return False + + +def create_houses(grid_size): + houses = [] + with open(file='houses.txt', mode='r', encoding='utf-8') as file: + for l_index, line in enumerate(file): + for h_index, if_house in enumerate(line): + if if_house == '1': + house = House( + [h_index*grid_size, l_index*grid_size], grid_size) + houses.append(house) + return houses diff --git a/houses.txt b/houses.txt new file mode 100644 index 0000000..7eb87fa --- /dev/null +++ b/houses.txt @@ -0,0 +1,16 @@ +11111111111111111111111111111111 +10000000000000000000000000000001 +10000000000000000000000000000001 +10011111110001000010001111111001 +10000000000001000010000000000001 +10000000000001000010000000000001 +11111111111111000011111111111111 +10000000000000000000000000000001 +10000000000000000000000000000001 +11111111111111000011111111111111 +10000000000001000010000000000001 +10000000000001000010000000000001 +10011111111001000010011111111001 +10000000000000000000000000000001 +10000000000000000000000000000001 +11111111111111111111111111111111 \ No newline at end of file diff --git a/img/house.png b/img/house.png new file mode 100644 index 0000000..cf9c5be Binary files /dev/null and b/img/house.png differ diff --git a/img/trash.png b/img/trash.png new file mode 100644 index 0000000..5677e49 Binary files /dev/null and b/img/trash.png differ diff --git a/img/truck.png b/img/truck.png new file mode 100644 index 0000000..7860105 Binary files /dev/null and b/img/truck.png differ diff --git a/main.py b/main.py new file mode 100644 index 0000000..23c1690 --- /dev/null +++ b/main.py @@ -0,0 +1,81 @@ +import pygame + +from text import msg_display +from colors import gray +from house import create_houses +from truck import Truck +from trash import Trash + +from bfs import bfs + + +pygame.init() + + +def update_images(gameDisplay, truck, trash, houses, score): + gameDisplay.fill(gray) + for house in houses: + gameDisplay.blit(pygame.image.load('./img/house.png'), house.pos) + gameDisplay.blit(pygame.image.load('./img/trash.png'), trash.pos) + gameDisplay.blit(truck.image, truck.pos) + msg_display(gameDisplay, f'Trash collected: {score}') + pygame.display.update() + + +def game_loop(): + game_w = 1280 # 32 + game_h = 640 # 16 + grid_size = 40 + + gameDisplay = pygame.display.set_mode((game_w, game_h)) + pygame.display.set_caption('Garbage truck') + clock = pygame.time.Clock() + + gameExit = False + + score = 0 + truck = Truck(game_w, game_h, grid_size) + trash = Trash(game_w, game_h, grid_size) + houses = create_houses(grid_size) + + trash.new_pos(truck.pos, houses) + + while not gameExit: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + quit() + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_ESCAPE: + pygame.quit() + quit() + if (event.key == pygame.K_b): + bfs() + break + if (event.key == pygame.K_w or event.key == pygame.K_UP): + if truck.test_crash(houses): + break + truck.move() + print('↑') + if truck.pos == trash.pos: + score += 1 + trash.new_pos(truck.pos, houses) + break + elif (event.key == pygame.K_a or event.key == pygame.K_LEFT): + print('←') + truck.rotate(-1) + truck.rotate_image(90) + break + elif (event.key == pygame.K_d or event.key == pygame.K_RIGHT): + print('→') + truck.rotate(1) + truck.rotate_image(-90) + break + + update_images(gameDisplay, truck, trash, houses, score) + clock.tick(60) + + +if __name__ == '__main__': + game_loop() +pygame.quit() diff --git a/text.py b/text.py new file mode 100644 index 0000000..2ad8549 --- /dev/null +++ b/text.py @@ -0,0 +1,16 @@ +from colors import white +import pygame + + +def text_objects(text, font): + textSurface = font.render(text, True, white) + return textSurface, textSurface.get_rect() + + +def msg_display(gameDisplay, msg): + msg_size = 35 + pos = (1100, 16) + largeText = pygame.font.Font('freesansbold.ttf', msg_size) + TestSurf, TextRect = text_objects(msg, largeText) + TextRect.center = (pos) + gameDisplay.blit(TestSurf, TextRect) diff --git a/trash.py b/trash.py new file mode 100644 index 0000000..9ac4c9a --- /dev/null +++ b/trash.py @@ -0,0 +1,16 @@ +import random +from house import is_house + + +class Trash: + def __init__(self, grid_w, grid_h, grid_size): + self.grid_w = grid_w + self.grid_h = grid_h + self.size = grid_size + + def new_pos(self, truck_pos, houses): + while True: + self.pos = [random.randrange(0, self.grid_w, self.size), + random.randrange(0, self.grid_h, self.size)] + if self.pos != truck_pos and not is_house(self.pos, houses): + break diff --git a/truck.py b/truck.py new file mode 100644 index 0000000..cd227f9 --- /dev/null +++ b/truck.py @@ -0,0 +1,32 @@ +import pygame +from house import is_house + + +class Truck: + def __init__(self, grid_w, grid_h, grid_size): + self.grid_w = grid_w + self.grid_h = grid_h + self.size = grid_size + self.pos = [self.grid_w // 2, self.grid_h // 2] + self.direction = [1, 0] + self.dir_control = 0 + self.image = pygame.image.load('./img/truck.png') + + def move(self): + self.pos[0] += self.direction[0] * self.size + self.pos[1] += self.direction[1] * self.size + + def test_crash(self, houses): + new_pos = [] + new_pos.append(self.pos[0] + (self.direction[0]*self.size)) + new_pos.append(self.pos[1] + (self.direction[1]*self.size)) + return is_house(new_pos, houses) + + def rotate(self, direction): + all_dir = [[1, 0], [0, 1], [-1, 0], [0, -1]] + self.dir_control = (direction + self.dir_control) % 4 + self.direction = all_dir[self.dir_control] + + def rotate_image(self, angle): + rotated_image = pygame.transform.rotate(self.image, angle) + self.image = rotated_image