SprytnyTraktor/py.py

94 lines
3.7 KiB
Python
Raw Normal View History

2021-03-21 12:40:13 +01:00
import definitions
import field
import plant
import pygame
2021-03-21 12:40:13 +01:00
import random
import soil
import tractor
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)
temp_plant = plant.Plant(0, definitions.WHEAT_MAXIMUM_STATE)
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()
state = field.get_soil().get_state()
water_level = field.get_soil().get_water_level()
if state == False:
2021-03-21 15:17:34 +01:00
block = definitions.DIRT
elif water_level == False:
block = definitions.FARMLAND
else:
block = definitions.FARMLANDMOIST
2021-03-21 15:17:34 +01:00
definitions.WIN.blit(block, (rect.x, rect.y))
def do_work(tractor1_rectangle):
x = int(tractor1_rectangle.x/100)
y = int(tractor1_rectangle.y/100)
field = fields[x][y]
soil = field.get_soil()
state = soil.get_state()
water_level = soil.get_water_level()
if state == False:
field.get_soil().set_state(True)
elif water_level == False:
field.get_soil().set_water_level(True)
2021-03-21 12:40:13 +01:00
def draw_window(tractor1_rectangle):
2021-03-21 14:10:08 +01:00
fill_map()
2021-03-21 12:40:13 +01:00
definitions.WIN.blit(definitions.TRACTOR, (tractor1_rectangle.x, tractor1_rectangle.y))
pygame.display.update()
2021-03-21 12:40:13 +01:00
def is_move_allowed(move, tractor1_rectangle):
2021-03-21 15:17:34 +01:00
if ((move == 1) and (tractor1_rectangle.y + definitions.VEL + definitions.BLOCK_SIZE <= definitions.HEIGHT)):
2021-03-21 13:01:38 +01:00
return True
elif ((move == 2) and (tractor1_rectangle.x - definitions.VEL >= 0)):
return True
2021-03-21 15:17:34 +01:00
elif ((move == 3) and (tractor1_rectangle.x + definitions.VEL + definitions.BLOCK_SIZE <= definitions.WIDTH)):
2021-03-21 13:01:38 +01:00
return True
elif ((move == 4) and (tractor1_rectangle.y - definitions.VEL >= 0)):
return True
else:
return False
2021-03-21 12:40:13 +01:00
def tractor1_handle_movement(tractor1, tractor1_rectangle):
random1 = random.randint(1, 4)
do_work(tractor1_rectangle)
2021-03-21 13:01:38 +01:00
if ((random1 == 1) and (is_move_allowed(1, tractor1_rectangle) == True)):
2021-03-21 12:40:13 +01:00
tractor1.move_down()
tractor1_rectangle.x = tractor1.get_x()
tractor1_rectangle.y = tractor1.get_y()
2021-03-21 13:01:38 +01:00
elif ((random1 == 2) and (is_move_allowed(2, tractor1_rectangle) == True)):
2021-03-21 12:40:13 +01:00
tractor1.move_left()
tractor1_rectangle.x = tractor1.get_x()
tractor1_rectangle.y = tractor1.get_y()
2021-03-21 13:01:38 +01:00
elif ((random1 == 3) and (is_move_allowed(3, tractor1_rectangle) == True)):
2021-03-21 12:40:13 +01:00
tractor1.move_right()
tractor1_rectangle.x = tractor1.get_x()
tractor1_rectangle.y = tractor1.get_y()
2021-03-21 13:01:38 +01:00
elif ((random1 == 4) and (is_move_allowed(4, tractor1_rectangle) == True)):
2021-03-21 12:40:13 +01:00
tractor1.move_up()
tractor1_rectangle.x = tractor1.get_x()
tractor1_rectangle.y = tractor1.get_y()
def main():
2021-03-21 14:10:08 +01:00
create_base_map()
2021-03-21 13:45:20 +01:00
tractor1 = tractor.Tractor(400, 400)
2021-03-21 15:17:34 +01:00
tractor1_rectangle = pygame.Rect(tractor1.get_x(), tractor1.get_y(), definitions.BLOCK_SIZE, definitions.BLOCK_SIZE)
clock = pygame.time.Clock()
run = True
while run:
2021-03-21 12:40:13 +01:00
clock.tick(definitions.FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
2021-03-21 12:40:13 +01:00
tractor1_handle_movement(tractor1, tractor1_rectangle)
draw_window(tractor1_rectangle)
pygame.quit()
if __name__ == "__main__":
main()