SzybciorSmartTraktor/main.py

147 lines
5.1 KiB
Python
Raw Normal View History

import pygame
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
import math
2022-03-11 03:02:29 +01:00
possibleFields = {
'dirt': field.Dirt(),
'grass': field.Grass(),
'cobble': field.Cobble(),
'sand': field.Sand(),
'station': field.Station(),
}
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 = []
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)
if x < 80:
field_array_small.append(possibleFields['dirt'].tile.object)
field_array_small_2.append('dirt')
elif 80 < x < 90:
field_array_small.append(possibleFields['sand'].tile.object)
field_array_small_2.append('sand')
elif 90 < x < 100:
field_array_small.append(possibleFields['grass'].tile.object)
field_array_small_2.append('grass')
field_array_big.append(field_array_small)
field_array_big_2.append(field_array_small_2)
field_array_small = []
field_array_small_2 = []
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'
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))
elif current_field == possibleFields['station'].tile.object:
window.blit(possibleFields['station'].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()))
if agent.direction == 'east':
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))
elif agent.direction == 'west':
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))
elif agent.direction == 'north':
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))
elif agent.direction == 'south':
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-03-11 03:02:29 +01:00
common = common.Instance()
agent = agent.Instance(1000, 'east')
2022-04-07 18:44:33 +02:00
graph = graph.Instance()
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()
2022-04-07 18:44:33 +02:00
graph.config(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.rotate())
agent_action(agent.move())
pygame.quit()
if __name__ == "__main__":
2022-03-11 03:02:29 +01:00
main()