SzybciorSmartTraktor/main.py

108 lines
3.1 KiB
Python
Raw Normal View History

import pygame
import random
import bfs
2022-03-11 03:02:29 +01:00
import field
import settings
import common
import agent
2022-03-24 15:12:58 +01:00
AGENT_IMG = pygame.image.load('./assets/tracktor/tractor.png')
AGENT = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
2022-03-11 03:02:29 +01:00
possibleFields = {
'dirt': field.Dirt(),
'grass': field.Grass(),
'cobble': field.Cobble(),
'sand': field.Sand(),
}
def randomize_map():
fields_array = []
for i in possibleFields:
fields_array.append(possibleFields[i].tile.object)
field_array_big = []
field_array_small = []
field_array_big_2 = []
field_array_small_2 = []
for i in range(settings.Field.horizontal_count()):
for j in range(settings.Field.vertical_count()):
k = random.choice(list(possibleFields.keys()))
field_array_small.append(possibleFields[k].tile.object)
field_array_small_2.append(k)
field_array_big.append(field_array_small)
field_array_big_2.append(field_array_small_2)
field_array_small = []
field_array_small_2 = []
return field_array_big, field_array_big_2
def read_img(agent, fields):
2022-03-11 03:02:29 +01:00
window = common.get('window')
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-03-25 06:52:06 +01:00
pygame.display.update()
pygame.time.delay(2000)
common.set('state_imgShown', False)
def agent_action(action: str):
if action == 'open_window':
common.set('state_imgShown', True)
2022-03-11 03:02:29 +01:00
def draw_window(agent, fields):
window = common.get('window')
rect = agent.rect
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()))
window.blit(AGENT, (rect.x, rect.y))
pygame.display.update()
common = common.Instance()
agent = agent.Instance(1000)
2022-03-09 18:59:50 +01:00
def main():
2022-03-11 03:02:29 +01:00
common.set('game_running', True)
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())
fields, fields_2 = randomize_map()
bfs.generate_all(fields_2)
2022-03-11 03:02:29 +01:00
while common.get('game_running'):
pygame.time.Clock().tick(settings.Pygame.fps())
for event in pygame.event.get():
if event.type == pygame.QUIT:
2022-03-11 03:02:29 +01:00
common.set('game_running', False)
if common.get('state_imgShown'):
2022-03-11 03:02:29 +01:00
read_img(agent, fields)
else:
draw_window(agent, fields)
agent_action(agent.move())
pygame.quit()
if __name__ == "__main__":
2022-03-11 03:02:29 +01:00
main()