Agent can rotate + changed map randomization

This commit is contained in:
jbiesek 2022-04-12 14:47:30 +02:00
parent 0db53c8892
commit 5096a289ca
10 changed files with 98 additions and 18 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
__pycache__/
__pycache__/*
/**/.idea/*

View File

@ -3,11 +3,12 @@ import settings
class Instance:
def __init__(self, tank_capacity):
def __init__(self, tank_capacity, direction):
size = settings.Field.size
self.rect = pygame.Rect(0, 0, size(), size())
self.action = ''
self.tank_capacity = tank_capacity
self.direction = direction
def coordinates(self):
return {
@ -20,27 +21,47 @@ class Instance:
def y(self):
return int(self.rect.y / settings.Field.size())
def get_tank_capacity(self):
return self.tank_capacity
def set_tank_capacity(self, fuel_units):
self.tank_capacity = fuel_units
def move(self):
key_pressed = pygame.key.get_pressed()
height = settings.Pygame.height()
width = settings.Pygame.width()
tile_size = settings.Field.size()
if key_pressed[pygame.K_LEFT] and self.rect.x > 0:
if key_pressed[pygame.K_UP] and self.direction == 'west' and self.rect.x > 0:
self.rect.x -= tile_size
elif key_pressed[pygame.K_RIGHT] and self.rect.x < width - tile_size:
elif key_pressed[pygame.K_UP] and self.direction == 'east' and self.rect.x < width - tile_size:
self.rect.x += tile_size
elif key_pressed[pygame.K_UP] and self.rect.y > 0:
elif key_pressed[pygame.K_UP] and self.direction == 'north' and self.rect.y > 0:
self.rect.y -= tile_size
elif key_pressed[pygame.K_DOWN] and self.rect.y < height - tile_size:
elif key_pressed[pygame.K_UP] and self.direction == 'south' and self.rect.y < height - tile_size:
self.rect.y += tile_size
elif key_pressed[pygame.K_SPACE]:
return 'open_window'
return 'none'
def rotate(self):
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_LEFT] and self.direction == 'east':
self.direction = 'north'
elif key_pressed[pygame.K_LEFT] and self.direction == 'north':
self.direction = 'west'
elif key_pressed[pygame.K_LEFT] and self.direction == 'west':
self.direction = 'south'
elif key_pressed[pygame.K_LEFT] and self.direction == 'south':
self.direction = 'east'
elif key_pressed[pygame.K_RIGHT] and self.direction == 'north':
self.direction = 'east'
elif key_pressed[pygame.K_RIGHT] and self.direction == 'west':
self.direction = 'north'
elif key_pressed[pygame.K_RIGHT] and self.direction == 'south':
self.direction = 'west'
elif key_pressed[pygame.K_RIGHT] and self.direction == 'east':
self.direction = 'south'
return 'none'

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

View File

@ -78,6 +78,7 @@ class Field:
class Dirt(Field):
type = Type.PLANT
name = 'dirt'
cost = 5
def __init__(self):
super(Dirt, self).__init__()
@ -86,6 +87,7 @@ class Dirt(Field):
class Cobble(Field):
type = Type.SPECIAL
name = 'cobble'
cost = 1
def __init__(self):
super(Cobble, self).__init__()
@ -94,6 +96,7 @@ class Cobble(Field):
class Grass(Field):
type = Type.DEFAULT
name = 'grass'
cost = 3
def __init__(self):
super(Grass, self).__init__()
@ -102,7 +105,17 @@ class Grass(Field):
class Sand(Field):
type = Type.DEFAULT
name = 'sand'
cost = 10
def __init__(self):
super(Sand, self).__init__()
class Station(Field):
type = Type.SPECIAL
name = 'station'
cost = 1
def __init__(self):
super(Station, self).__init__()
# Tile Types END

65
main.py
View File

@ -5,17 +5,19 @@ import field
import settings
import common
import agent
import math
AGENT_IMG = pygame.image.load('./assets/tracktor/tractor.png')
AGENT = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
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:
@ -24,17 +26,38 @@ def randomize_map():
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)
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):
window = common.get('window')
current_field = fields[agent.x()][agent.y()]
@ -46,28 +69,49 @@ def read_img(agent, fields):
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))
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)
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()):
window.blit(fields[i][j], (i * settings.Field.size(), j * settings.Field.size()))
window.blit(AGENT, (rect.x, rect.y))
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))
pygame.display.update()
common = common.Instance()
agent = agent.Instance(1000)
agent = agent.Instance(1000, 'east')
graph = graph.Instance()
def main():
common.set('game_running', True)
common.set('state_imgShown', False)
@ -92,10 +136,11 @@ def main():
read_img(agent, fields)
else:
draw_window(agent, fields)
agent_action(agent.rotate())
agent_action(agent.move())
pygame.quit()
if __name__ == "__main__":
main()