This commit is contained in:
Michal Zmudzinski 2021-06-13 15:14:55 +02:00
parent 15757a16a1
commit 5289c48fe2
11 changed files with 189 additions and 0 deletions

0
bfs.py Normal file
View File

5
colors.py Normal file
View File

@ -0,0 +1,5 @@
gray = (80, 80, 80)
white = (255, 255, 255)
blue = (0, 0, 255)
green = (0, 255, 0)
red = (255, 0, 0)

23
house.py Normal file
View File

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

16
houses.txt Normal file
View File

@ -0,0 +1,16 @@
11111111111111111111111111111111
10000000000000000000000000000001
10000000000000000000000000000001
10011111110001000010001111111001
10000000000001000010000000000001
10000000000001000010000000000001
11111111111111000011111111111111
10000000000000000000000000000001
10000000000000000000000000000001
11111111111111000011111111111111
10000000000001000010000000000001
10000000000001000010000000000001
10011111111001000010011111111001
10000000000000000000000000000001
10000000000000000000000000000001
11111111111111111111111111111111

BIN
img/house.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
img/trash.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

BIN
img/truck.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

81
main.py Normal file
View File

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

16
text.py Normal file
View File

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

16
trash.py Normal file
View File

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

32
truck.py Normal file
View File

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