2021-03-21 12:40:13 +01:00
|
|
|
import definitions
|
|
|
|
import field
|
|
|
|
import plant
|
2021-03-13 22:30:18 +01:00
|
|
|
import pygame
|
2021-03-21 12:40:13 +01:00
|
|
|
import random
|
|
|
|
import soil
|
|
|
|
import tractor
|
2021-03-13 22:30:18 +01:00
|
|
|
pygame.display.set_caption("Smart Tractor")
|
2021-03-21 15:17:34 +01:00
|
|
|
fields = []
|
2021-03-21 14:10:08 +01:00
|
|
|
def create_base_map():
|
|
|
|
for i in range(10):
|
|
|
|
temp_map_field = []
|
|
|
|
for j in range(10):
|
2021-03-21 15:17:34 +01:00
|
|
|
temp_rect = pygame.Rect(i * definitions.BLOCK_SIZE, j * definitions.BLOCK_SIZE, definitions.BLOCK_SIZE, definitions.BLOCK_SIZE)
|
|
|
|
temp_soil = soil.Soil(False, False)
|
2021-03-23 17:14:46 +01:00
|
|
|
temp_plant = plant.Plant(0)
|
2021-03-21 15:17:34 +01:00
|
|
|
temp_field = field.Field(temp_plant, temp_rect, temp_soil)
|
|
|
|
temp_map_field.append(temp_field)
|
|
|
|
fields.append(temp_map_field)
|
2021-03-21 14:10:08 +01:00
|
|
|
def fill_map():
|
|
|
|
for i in range(10):
|
|
|
|
for j in range(10):
|
2021-03-21 15:17:34 +01:00
|
|
|
field = fields[i][j]
|
|
|
|
rect = field.get_rect()
|
2021-03-23 17:54:39 +01:00
|
|
|
if field.get_plant().get_state() > 0 and field.get_plant().get_state() <= 1 * definitions.WHEAT_GROW_TIME:
|
2021-03-23 17:14:46 +01:00
|
|
|
block = definitions.WHEATSTAGE1
|
2021-03-23 17:54:39 +01:00
|
|
|
elif field.get_plant().get_state() > 1 * definitions.WHEAT_GROW_TIME and field.get_plant().get_state() <= 2 * definitions.WHEAT_GROW_TIME:
|
2021-03-23 17:14:46 +01:00
|
|
|
block = definitions.WHEATSTAGE2
|
2021-03-23 17:54:39 +01:00
|
|
|
elif field.get_plant().get_state() > 2 * definitions.WHEAT_GROW_TIME and field.get_plant().get_state() <= 3 * definitions.WHEAT_GROW_TIME:
|
2021-03-23 17:14:46 +01:00
|
|
|
block = definitions.WHEATSTAGE3
|
2021-03-23 17:54:39 +01:00
|
|
|
elif field.get_plant().get_state() > 3 * definitions.WHEAT_GROW_TIME and field.get_plant().get_state() <= 4 * definitions.WHEAT_GROW_TIME:
|
2021-03-23 17:14:46 +01:00
|
|
|
block = definitions.WHEATSTAGE4
|
2021-03-23 17:54:39 +01:00
|
|
|
elif field.get_plant().get_state() == 4 *definitions.WHEAT_GROW_TIME + 1:
|
2021-03-23 17:14:46 +01:00
|
|
|
block = definitions.WHEATSTAGE5
|
|
|
|
elif field.get_soil().get_state() is False:
|
2021-03-21 15:17:34 +01:00
|
|
|
block = definitions.DIRT
|
2021-03-23 17:14:46 +01:00
|
|
|
elif field.get_soil().get_state() is True and field.get_soil().get_water_level() is False:
|
2021-03-22 17:02:12 +01:00
|
|
|
block = definitions.FARMLAND
|
2021-03-23 17:14:46 +01:00
|
|
|
elif field.get_soil().get_state() is True and field.get_soil().get_water_level() is True:
|
2021-03-22 17:02:12 +01:00
|
|
|
block = definitions.FARMLANDMOIST
|
2021-03-21 15:17:34 +01:00
|
|
|
definitions.WIN.blit(block, (rect.x, rect.y))
|
2021-03-23 17:14:46 +01:00
|
|
|
def do_work(tractor1_rect):
|
|
|
|
x = int(tractor1_rect.x/100)
|
|
|
|
y = int(tractor1_rect.y/100)
|
2021-03-22 17:02:12 +01:00
|
|
|
field = fields[x][y]
|
2021-03-23 17:14:46 +01:00
|
|
|
if field.get_soil().get_state() is False:
|
2021-03-22 17:02:12 +01:00
|
|
|
field.get_soil().set_state(True)
|
2021-03-23 17:14:46 +01:00
|
|
|
elif field.get_soil().get_state() is True and field.get_soil().get_water_level() is False:
|
2021-03-22 17:02:12 +01:00
|
|
|
field.get_soil().set_water_level(True)
|
2021-03-23 17:14:46 +01:00
|
|
|
elif field.get_plant().get_state() == 0:
|
|
|
|
field.get_plant().set_state(1)
|
|
|
|
elif field.get_plant().get_state() == definitions.WHEAT_MAXIMUM_STATE:
|
|
|
|
field.get_plant().set_state(0)
|
|
|
|
field.get_soil().set_water_level(False)
|
|
|
|
field.get_soil().set_state(False)
|
|
|
|
def draw_window(tractor1_rect):
|
2021-03-21 14:10:08 +01:00
|
|
|
fill_map()
|
2021-03-23 17:14:46 +01:00
|
|
|
definitions.WIN.blit(definitions.TRACTOR, (tractor1_rect.x, tractor1_rect.y))
|
2021-03-13 22:30:18 +01:00
|
|
|
pygame.display.update()
|
2021-03-23 17:14:46 +01:00
|
|
|
def grow_plants():
|
|
|
|
for i in range(10):
|
|
|
|
for j in range(10):
|
|
|
|
field = fields[i][j]
|
|
|
|
if field.get_plant().get_state() > 0 and field.get_plant().get_state() < definitions.WHEAT_MAXIMUM_STATE:
|
|
|
|
field.get_plant().set_state(field.get_plant().get_state() + 1)
|
|
|
|
def is_move_allowed(move, tractor1_rect):
|
|
|
|
if ((move == 1) and (tractor1_rect.y + definitions.BLOCK_SIZE + definitions.BLOCK_SIZE <= definitions.HEIGHT)):
|
2021-03-21 13:01:38 +01:00
|
|
|
return True
|
2021-03-23 17:14:46 +01:00
|
|
|
elif ((move == 2) and (tractor1_rect.x - definitions.BLOCK_SIZE >= 0)):
|
2021-03-21 13:01:38 +01:00
|
|
|
return True
|
2021-03-23 17:14:46 +01:00
|
|
|
elif ((move == 3) and (tractor1_rect.x + definitions.BLOCK_SIZE + definitions.BLOCK_SIZE <= definitions.WIDTH)):
|
2021-03-21 13:01:38 +01:00
|
|
|
return True
|
2021-03-23 17:14:46 +01:00
|
|
|
elif ((move == 4) and (tractor1_rect.y - definitions.BLOCK_SIZE >= 0)):
|
2021-03-21 13:01:38 +01:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2021-03-23 17:14:46 +01:00
|
|
|
def tractor1_handle_movement(tractor1, tractor1_rect):
|
|
|
|
loop = True
|
|
|
|
while loop:
|
|
|
|
random1 = random.randint(1, 4)
|
|
|
|
if ((random1 == 1) and (is_move_allowed(1, tractor1_rect) is True)):
|
|
|
|
tractor1.move_down()
|
|
|
|
tractor1_rect.x = tractor1.get_x()
|
|
|
|
tractor1_rect.y = tractor1.get_y()
|
|
|
|
loop = False
|
|
|
|
elif ((random1 == 2) and (is_move_allowed(2, tractor1_rect) is True)):
|
|
|
|
tractor1.move_left()
|
|
|
|
tractor1_rect.x = tractor1.get_x()
|
|
|
|
tractor1_rect.y = tractor1.get_y()
|
|
|
|
loop = False
|
|
|
|
elif ((random1 == 3) and (is_move_allowed(3, tractor1_rect) is True)):
|
|
|
|
tractor1.move_right()
|
|
|
|
tractor1_rect.x = tractor1.get_x()
|
|
|
|
tractor1_rect.y = tractor1.get_y()
|
|
|
|
loop = False
|
|
|
|
elif ((random1 == 4) and (is_move_allowed(4, tractor1_rect) is True)):
|
|
|
|
tractor1.move_up()
|
|
|
|
tractor1_rect.x = tractor1.get_x()
|
|
|
|
tractor1_rect.y = tractor1.get_y()
|
|
|
|
loop = False
|
2021-03-13 22:30:18 +01:00
|
|
|
def main():
|
2021-03-21 14:10:08 +01:00
|
|
|
create_base_map()
|
2021-03-23 17:14:46 +01:00
|
|
|
tractor1 = tractor.Tractor(0, 0)
|
|
|
|
tractor1_rect = pygame.Rect(tractor1.get_x(), tractor1.get_y(), definitions.BLOCK_SIZE, definitions.BLOCK_SIZE)
|
2021-03-13 22:30:18 +01:00
|
|
|
clock = pygame.time.Clock()
|
|
|
|
run = True
|
|
|
|
while run:
|
2021-03-21 12:40:13 +01:00
|
|
|
clock.tick(definitions.FPS)
|
2021-03-13 22:30:18 +01:00
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
run = False
|
2021-03-23 17:54:39 +01:00
|
|
|
draw_window(tractor1_rect)
|
|
|
|
grow_plants()
|
2021-03-23 17:14:46 +01:00
|
|
|
tractor1_handle_movement(tractor1, tractor1_rect)
|
|
|
|
do_work(tractor1_rect)
|
2021-03-13 22:30:18 +01:00
|
|
|
pygame.quit()
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|