2022-03-08 20:27:24 +01:00
|
|
|
import pygame
|
2022-03-09 20:55:12 +01:00
|
|
|
import random
|
2022-04-07 18:44:33 +02:00
|
|
|
import graph
|
2022-03-11 03:02:29 +01:00
|
|
|
import field
|
|
|
|
import settings
|
|
|
|
import common
|
|
|
|
import agent
|
2022-04-12 14:47:30 +02:00
|
|
|
import math
|
2022-04-28 09:21:32 +02:00
|
|
|
import time
|
2022-03-11 03:02:29 +01:00
|
|
|
|
2022-03-11 03:34:20 +01:00
|
|
|
possibleFields = {
|
|
|
|
'dirt': field.Dirt(),
|
|
|
|
'grass': field.Grass(),
|
|
|
|
'cobble': field.Cobble(),
|
|
|
|
'sand': field.Sand(),
|
2022-04-12 14:47:30 +02:00
|
|
|
'station': field.Station(),
|
2022-03-11 03:34:20 +01:00
|
|
|
}
|
2022-03-09 20:55:12 +01:00
|
|
|
|
2022-04-12 14:47:30 +02:00
|
|
|
|
2022-03-09 20:55:12 +01:00
|
|
|
def randomize_map():
|
2022-03-11 03:34:20 +01:00
|
|
|
fields_array = []
|
|
|
|
for i in possibleFields:
|
|
|
|
fields_array.append(possibleFields[i].tile.object)
|
2022-03-09 20:55:12 +01:00
|
|
|
field_array_big = []
|
|
|
|
field_array_small = []
|
2022-04-05 14:05:38 +02:00
|
|
|
field_array_big_2 = []
|
|
|
|
field_array_small_2 = []
|
2022-04-12 14:47:30 +02:00
|
|
|
width = settings.Field.horizontal_count()
|
|
|
|
height = settings.Field.vertical_count()
|
|
|
|
for i in range(width):
|
|
|
|
for j in range(height):
|
|
|
|
# k = random.choice(list(possibleFields.keys()))
|
|
|
|
x = random.uniform(0, 100)
|
2022-04-28 09:21:32 +02:00
|
|
|
if x < 4:
|
2022-04-12 14:47:30 +02:00
|
|
|
field_array_small.append(possibleFields['dirt'].tile.object)
|
|
|
|
field_array_small_2.append('dirt')
|
2022-04-28 09:21:32 +02:00
|
|
|
elif 4 < x < 55:
|
2022-04-12 14:47:30 +02:00
|
|
|
field_array_small.append(possibleFields['sand'].tile.object)
|
|
|
|
field_array_small_2.append('sand')
|
2022-04-28 09:21:32 +02:00
|
|
|
elif 55 < x < 100:
|
2022-04-12 14:47:30 +02:00
|
|
|
field_array_small.append(possibleFields['grass'].tile.object)
|
|
|
|
field_array_small_2.append('grass')
|
2022-03-09 20:55:12 +01:00
|
|
|
field_array_big.append(field_array_small)
|
2022-04-05 14:05:38 +02:00
|
|
|
field_array_big_2.append(field_array_small_2)
|
2022-03-09 20:55:12 +01:00
|
|
|
field_array_small = []
|
2022-04-05 14:05:38 +02:00
|
|
|
field_array_small_2 = []
|
2022-04-12 14:47:30 +02:00
|
|
|
|
|
|
|
for i in range(height):
|
|
|
|
field_array_big[math.floor(width / 2)][i] = possibleFields['cobble'].tile.object
|
|
|
|
field_array_big_2[math.floor(width / 2)][i] = 'cobble'
|
|
|
|
for i in range(width):
|
|
|
|
field_array_big[i][math.floor(height / 2)] = possibleFields['cobble'].tile.object
|
|
|
|
field_array_big_2[i][math.floor(height / 2)] = 'cobble'
|
|
|
|
field_array_big[0][0] = possibleFields['station'].tile.object
|
|
|
|
field_array_big_2[0][0] = 'station'
|
|
|
|
|
2022-04-05 14:05:38 +02:00
|
|
|
return field_array_big, field_array_big_2
|
2022-03-09 20:55:12 +01:00
|
|
|
|
2022-04-12 14:47:30 +02:00
|
|
|
|
2022-03-09 20:55:12 +01:00
|
|
|
def read_img(agent, fields):
|
2022-03-11 03:02:29 +01:00
|
|
|
window = common.get('window')
|
2022-03-11 03:34:20 +01:00
|
|
|
current_field = fields[agent.x()][agent.y()]
|
|
|
|
if current_field == possibleFields['grass'].tile.object:
|
|
|
|
window.blit(possibleFields['grass'].block.object, (0, 0))
|
|
|
|
elif current_field == possibleFields['dirt'].tile.object:
|
|
|
|
window.blit(possibleFields['dirt'].block.object, (0, 0))
|
|
|
|
elif current_field == possibleFields['sand'].tile.object:
|
|
|
|
window.blit(possibleFields['sand'].block.object, (0, 0))
|
|
|
|
elif current_field == possibleFields['cobble'].tile.object:
|
|
|
|
window.blit(possibleFields['cobble'].block.object, (0, 0))
|
2022-04-12 14:47:30 +02:00
|
|
|
elif current_field == possibleFields['station'].tile.object:
|
|
|
|
window.blit(possibleFields['station'].block.object, (0, 0))
|
2022-03-25 06:52:06 +01:00
|
|
|
|
2022-03-09 20:55:12 +01:00
|
|
|
pygame.display.update()
|
|
|
|
pygame.time.delay(2000)
|
2022-03-11 03:34:20 +01:00
|
|
|
common.set('state_imgShown', False)
|
|
|
|
|
2022-04-12 14:47:30 +02:00
|
|
|
|
2022-03-11 03:34:20 +01:00
|
|
|
def agent_action(action: str):
|
|
|
|
if action == 'open_window':
|
|
|
|
common.set('state_imgShown', True)
|
2022-03-09 20:55:12 +01:00
|
|
|
|
2022-04-12 14:47:30 +02:00
|
|
|
|
2022-03-11 03:02:29 +01:00
|
|
|
def draw_window(agent, fields):
|
|
|
|
window = common.get('window')
|
|
|
|
rect = agent.rect
|
2022-03-11 03:34:20 +01:00
|
|
|
for i in range(settings.Field.horizontal_count()):
|
|
|
|
for j in range(settings.Field.vertical_count()):
|
2022-03-11 03:02:29 +01:00
|
|
|
window.blit(fields[i][j], (i * settings.Field.size(), j * settings.Field.size()))
|
2022-04-28 09:21:32 +02:00
|
|
|
if agent.direction == 2:
|
2022-04-12 14:47:30 +02:00
|
|
|
AGENT_IMG = pygame.image.load('./assets/tracktor/tractor_east.png')
|
|
|
|
AGENT = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
|
|
|
|
window.blit(AGENT, (rect.x, rect.y))
|
2022-04-28 09:21:32 +02:00
|
|
|
elif agent.direction == 4:
|
2022-04-12 14:47:30 +02:00
|
|
|
AGENT_IMG = pygame.image.load('./assets/tracktor/tractor_west.png')
|
|
|
|
AGENT = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
|
|
|
|
window.blit(AGENT, (rect.x, rect.y))
|
2022-04-28 09:21:32 +02:00
|
|
|
elif agent.direction == 1:
|
2022-04-12 14:47:30 +02:00
|
|
|
AGENT_IMG = pygame.image.load('./assets/tracktor/tractor_north.png')
|
|
|
|
AGENT = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
|
|
|
|
window.blit(AGENT, (rect.x, rect.y))
|
2022-04-28 09:21:32 +02:00
|
|
|
elif agent.direction == 3:
|
2022-04-12 14:47:30 +02:00
|
|
|
AGENT_IMG = pygame.image.load('./assets/tracktor/tractor_south.png')
|
|
|
|
AGENT = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
|
|
|
|
window.blit(AGENT, (rect.x, rect.y))
|
2022-03-11 03:02:29 +01:00
|
|
|
pygame.display.update()
|
|
|
|
|
2022-04-12 14:47:30 +02:00
|
|
|
|
2022-03-11 03:02:29 +01:00
|
|
|
common = common.Instance()
|
2022-04-28 09:21:32 +02:00
|
|
|
agent = agent.Instance(1000, 2)
|
2022-03-08 20:27:24 +01:00
|
|
|
|
2022-04-12 14:47:30 +02:00
|
|
|
|
2022-03-08 20:27:24 +01:00
|
|
|
def main():
|
2022-03-11 03:02:29 +01:00
|
|
|
common.set('game_running', True)
|
2022-03-11 03:34:20 +01:00
|
|
|
common.set('state_imgShown', False)
|
2022-03-11 03:02:29 +01:00
|
|
|
common.set(
|
|
|
|
'window',
|
|
|
|
pygame.display.set_mode((
|
|
|
|
settings.Pygame.width(),
|
|
|
|
settings.Pygame.height())))
|
|
|
|
|
|
|
|
pygame.display.set_caption(settings.Pygame.display_name())
|
2022-04-05 14:05:38 +02:00
|
|
|
fields, fields_2 = randomize_map()
|
2022-04-28 09:21:32 +02:00
|
|
|
state = graph.State(2, 0, 0)
|
|
|
|
move_list = (graph.graphsearch([], [], [10,10] , state, graph.succ))
|
|
|
|
print(move_list)
|
|
|
|
x = True
|
2022-03-11 03:02:29 +01:00
|
|
|
while common.get('game_running'):
|
|
|
|
pygame.time.Clock().tick(settings.Pygame.fps())
|
2022-03-08 20:27:24 +01:00
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
2022-03-11 03:02:29 +01:00
|
|
|
common.set('game_running', False)
|
2022-03-11 03:34:20 +01:00
|
|
|
if common.get('state_imgShown'):
|
2022-03-11 03:02:29 +01:00
|
|
|
read_img(agent, fields)
|
|
|
|
else:
|
|
|
|
draw_window(agent, fields)
|
2022-04-28 09:21:32 +02:00
|
|
|
if x:
|
|
|
|
for action in move_list:
|
|
|
|
if action == 'l' or action == 'r':
|
|
|
|
agent_action(agent.rotate(action))
|
|
|
|
elif action == 'f':
|
|
|
|
agent_action(agent.move(action))
|
|
|
|
draw_window(agent, fields)
|
|
|
|
time.sleep(0.5)
|
|
|
|
x = False
|
|
|
|
agent_action(agent.rotate(None))
|
|
|
|
agent_action(agent.move(None))
|
2022-03-09 20:55:12 +01:00
|
|
|
|
2022-03-08 20:27:24 +01:00
|
|
|
pygame.quit()
|
|
|
|
|
2022-04-12 14:47:30 +02:00
|
|
|
|
2022-03-08 20:27:24 +01:00
|
|
|
if __name__ == "__main__":
|
2022-03-11 03:02:29 +01:00
|
|
|
main()
|