SI_projekt_smieciarka/main.py

133 lines
4.7 KiB
Python
Raw Normal View History

2021-06-13 15:14:55 +02:00
import pygame
2021-06-15 19:48:34 +02:00
from time import sleep
2021-06-13 15:14:55 +02:00
from colors import gray
from house import create_houses
from truck import Truck
from trash import Trash
2021-06-16 02:04:55 +02:00
from TSP import tsp, tspmove
2021-06-13 15:14:55 +02:00
from bfs import bfs
2021-06-16 02:04:55 +02:00
multi_trash = []
2021-06-13 15:14:55 +02:00
pygame.init()
2021-06-15 20:24:06 +02:00
def game_keys(truck, multi_trash, houses, auto=False):
2021-06-15 19:48:34 +02:00
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if auto:
2021-06-16 11:53:54 +02:00
sleep(.04)
2021-06-15 19:48:34 +02:00
if (event.key == pygame.K_w or event.key == pygame.K_UP):
if truck.test_crash(houses):
break
truck.move()
print('')
2021-06-15 20:24:06 +02:00
for tindex, trash in enumerate(multi_trash):
if truck.pos == trash.pos:
multi_trash.pop(tindex)
2021-06-15 19:48:34 +02:00
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
2021-06-15 20:24:06 +02:00
def update_images(gameDisplay, truck, multi_trash, houses):
2021-06-13 15:14:55 +02:00
gameDisplay.fill(gray)
for house in houses:
gameDisplay.blit(pygame.image.load('./img/house.png'), house.pos)
2021-06-15 20:24:06 +02:00
for trash in multi_trash:
gameDisplay.blit(pygame.image.load('./img/trash.png'), trash.pos)
2021-06-13 15:14:55 +02:00
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)
2021-06-16 02:04:55 +02:00
2021-06-15 20:24:06 +02:00
for _ in range(7):
trash = Trash(game_w, game_h, grid_size)
trash.new_pos(truck.pos, houses, multi_trash)
multi_trash.append(trash)
2021-06-13 15:14:55 +02:00
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):
2021-06-15 20:24:06 +02:00
trash = multi_trash[0]
2021-06-15 19:48:34 +02:00
actions = bfs(truck.pos, truck.dir_control,
trash.pos, houses)
if not actions:
print('Path couldn\'t be found')
2021-06-13 15:14:55 +02:00
break
2021-06-15 19:48:34 +02:00
print('##################################################')
while actions:
action = actions.pop()
pygame.event.post(pygame.event.Event(
pygame.KEYDOWN, {'key': action}))
2021-06-15 19:59:23 +02:00
2021-06-15 20:24:06 +02:00
game_keys(truck, multi_trash, houses, True)
update_images(gameDisplay, truck, multi_trash, houses)
2021-06-16 02:04:55 +02:00
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)
2021-06-16 11:53:54 +02:00
truck.direction = [1, 0]
truck.dir_control = 0
truck.image = pygame.image.load('./img/truck.png')
if not multi_trash:
for _ in range(7):
trash = Trash(game_w, game_h, grid_size)
trash.new_pos(truck.pos, houses, multi_trash)
multi_trash.append(trash)
2021-06-15 19:48:34 +02:00
else:
pygame.event.post(event)
2021-06-15 20:24:06 +02:00
game_keys(truck, multi_trash, houses)
update_images(gameDisplay, truck, multi_trash, houses)
2021-06-13 15:14:55 +02:00
clock.tick(60)
if __name__ == '__main__':
game_loop()
pygame.quit()