125 lines
4.3 KiB
Python
125 lines
4.3 KiB
Python
import pygame
|
|
from time import sleep
|
|
|
|
from colors import gray
|
|
from house import create_houses
|
|
from truck import Truck
|
|
from trash import Trash
|
|
from TSP import tsp, tspmove
|
|
from bfs import bfs
|
|
|
|
multi_trash = []
|
|
pygame.init()
|
|
|
|
|
|
def game_keys(truck, multi_trash, houses, auto=False):
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.KEYDOWN:
|
|
if auto:
|
|
sleep(.2)
|
|
if (event.key == pygame.K_w or event.key == pygame.K_UP):
|
|
if truck.test_crash(houses):
|
|
break
|
|
truck.move()
|
|
print('↑')
|
|
for tindex, trash in enumerate(multi_trash):
|
|
if truck.pos == trash.pos:
|
|
multi_trash.pop(tindex)
|
|
|
|
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
|
|
|
|
|
|
def update_images(gameDisplay, truck, multi_trash, houses):
|
|
gameDisplay.fill(gray)
|
|
for house in houses:
|
|
gameDisplay.blit(pygame.image.load('./img/house.png'), house.pos)
|
|
for trash in multi_trash:
|
|
gameDisplay.blit(pygame.image.load('./img/trash.png'), trash.pos)
|
|
gameDisplay.blit(truck.image, truck.pos)
|
|
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
|
|
|
|
truck = Truck(game_w, game_h, grid_size)
|
|
houses = create_houses(grid_size)
|
|
|
|
|
|
for _ in range(7):
|
|
trash = Trash(game_w, game_h, grid_size)
|
|
trash.new_pos(truck.pos, houses, multi_trash)
|
|
multi_trash.append(trash)
|
|
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):
|
|
trash = multi_trash[0]
|
|
actions = bfs(truck.pos, truck.dir_control,
|
|
trash.pos, houses)
|
|
if not actions:
|
|
print('Path couldn\'t be found')
|
|
break
|
|
|
|
print('##################################################')
|
|
while actions:
|
|
action = actions.pop()
|
|
pygame.event.post(pygame.event.Event(
|
|
pygame.KEYDOWN, {'key': action}))
|
|
|
|
game_keys(truck, multi_trash, houses, True)
|
|
update_images(gameDisplay, truck, multi_trash, houses)
|
|
if (event.key == pygame.K_t):
|
|
solution = tsp(multi_trash, truck)
|
|
actions = tspmove(solution, truck, multi_trash)
|
|
if not actions:
|
|
print('Path couldn\'t be found')
|
|
break
|
|
|
|
|
|
for i in actions:
|
|
arr=i
|
|
#print(arr)
|
|
print('##################################################')
|
|
while arr:
|
|
action = arr.pop()
|
|
pygame.event.post(pygame.event.Event(
|
|
pygame.KEYDOWN, {'key': action}))
|
|
|
|
game_keys(truck, multi_trash, houses, True)
|
|
update_images(gameDisplay, truck, multi_trash, houses)
|
|
else:
|
|
pygame.event.post(event)
|
|
game_keys(truck, multi_trash, houses)
|
|
update_images(gameDisplay, truck, multi_trash, houses)
|
|
clock.tick(60)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
game_loop()
|
|
pygame.quit()
|