123 lines
3.8 KiB
Python
Executable File
123 lines
3.8 KiB
Python
Executable File
#!linux_env/bin/python3
|
|
|
|
import pygame
|
|
import sys
|
|
from random import randint
|
|
from config import WINDOW_HEIGHT, WINDOW_WIDTH, GRID_HEIGHT, GRID_WIDTH, HOUSE_CAPACITY, FPS, GC_X, GC_Y
|
|
|
|
from PIL import Image,ImageDraw
|
|
|
|
from DataModels.Grass import Grass
|
|
from DataModels.House import House
|
|
from DataModels.Dump import Dump
|
|
from DataModels.Road import Road
|
|
from DataModels.GC import GC
|
|
|
|
pygame.init()
|
|
|
|
pygame_sprites = pygame.sprite.Group()
|
|
house_count=0
|
|
dump_count=0
|
|
FPS_CLOCK = pygame.time.Clock()
|
|
GAME_WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32)
|
|
|
|
map = open(sys.argv[1], 'r')
|
|
map.readline()
|
|
map.readline()
|
|
|
|
map_objects = [[None for y in range(0, GRID_HEIGHT)]
|
|
for x in range(0, GRID_WIDTH)]
|
|
|
|
|
|
def generate(letter):
|
|
key = 'D' if letter in ['B', 'G', 'Y'] else letter
|
|
letter_mapping = {
|
|
'E': lambda x, y: Grass(x, y),
|
|
'H': lambda x, y, max_rubbish, yellow, green, blue: House(x, y, max_rubbish, yellow, green, blue),
|
|
'D': lambda x, y, max_rubbish, dump_type: Dump(x, y, max_rubbish, dump_type),
|
|
'R': lambda x, y: Road(x, y)
|
|
}
|
|
|
|
return letter_mapping[key]
|
|
|
|
|
|
i = 0
|
|
for y in map.readlines():
|
|
for x in y.split():
|
|
|
|
x_coord = i % GRID_WIDTH
|
|
y_coord = (i-x_coord)//GRID_WIDTH
|
|
|
|
yellow, green, blue = [randint(0, HOUSE_CAPACITY // 2), randint(
|
|
0, HOUSE_CAPACITY // 2), randint(0, HOUSE_CAPACITY // 2)]
|
|
if x is 'E':
|
|
map_objects[x_coord][y_coord] = generate(x)(x_coord, y_coord)
|
|
elif x is 'H':
|
|
map_objects[x_coord][y_coord] = generate(x)(
|
|
x_coord, y_coord, HOUSE_CAPACITY, yellow, green, blue)
|
|
house_count+=1
|
|
elif x is 'B':
|
|
map_objects[x_coord][y_coord] = generate(
|
|
x)(x_coord, y_coord, 100, "Dump_Blue")
|
|
dump_count+=1
|
|
elif x is 'G':
|
|
map_objects[x_coord][y_coord] = generate(
|
|
x)(x_coord, y_coord, 100, "Dump_Green")
|
|
dump_count+=1
|
|
elif x is 'Y':
|
|
map_objects[x_coord][y_coord] = generate(
|
|
x)(x_coord, y_coord, 100, "Dump_Yellow")
|
|
dump_count+=1
|
|
elif x is 'R':
|
|
map_objects[x_coord][y_coord] = generate(x)(x_coord, y_coord)
|
|
i += 1
|
|
|
|
|
|
for line in map_objects:
|
|
for item in line:
|
|
pygame_sprites.add(item)
|
|
|
|
gc = GC(GC_X, GC_Y, 200)
|
|
print("GC: " + str(GC_X) + str(GC_Y))
|
|
pygame_sprites.add(gc)
|
|
|
|
while True:
|
|
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
pygame.quit()
|
|
sys.exit()
|
|
elif event.type == pygame.KEYUP:
|
|
if event.key == pygame.K_UP:
|
|
gc.move("up", map_objects)
|
|
elif event.key == pygame.K_DOWN:
|
|
gc.move("down", map_objects)
|
|
elif event.key == pygame.K_LEFT:
|
|
gc.move("left", map_objects)
|
|
elif event.key == pygame.K_RIGHT:
|
|
gc.move("right", map_objects)
|
|
elif event.key == pygame.K_SPACE:
|
|
gc.collect(map_objects)
|
|
elif event.key == pygame.K_0:
|
|
gc.find_houses(map_objects,house_count,dump_count, "DFS")
|
|
elif event.key == pygame.K_9:
|
|
gc.find_houses_BestFS(map_objects)
|
|
elif event.key == pygame.K_8:
|
|
gc.find_houses(map_objects,house_count,dump_count, "BFS")
|
|
|
|
gc.make_actions_from_list(map_objects)
|
|
pygame_sprites.update()
|
|
pygame_sprites.draw(GAME_WINDOW)
|
|
|
|
#draw GC moves
|
|
bg_rect = pygame.Surface((105,30), pygame.SRCALPHA)
|
|
bg_rect.fill((0,0,0,160))
|
|
GAME_WINDOW.blit(bg_rect, (0, WINDOW_HEIGHT-30))
|
|
|
|
font = pygame.font.SysFont("monospace", 15)
|
|
gc_moves = font.render("Moves: " + str(gc.get_moves_count()), 1, (255,255,255))
|
|
GAME_WINDOW.blit(gc_moves, (10, WINDOW_HEIGHT - 25))
|
|
|
|
pygame.display.flip()
|
|
FPS_CLOCK.tick(FPS)
|