rosnące pole, naprawiony ruch traktora

This commit is contained in:
Tomasz Adamczyk 2021-03-23 17:14:46 +01:00
parent 1f598d0896
commit c2a32568f7
13 changed files with 106 additions and 73 deletions

View File

@ -4,8 +4,9 @@
<list default="true" id="56453584-72bd-49f4-a39c-fccf91ab20c6" name="Default Changelist" comment=""> <list default="true" id="56453584-72bd-49f4-a39c-fccf91ab20c6" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/definitions.py" beforeDir="false" afterPath="$PROJECT_DIR$/definitions.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/definitions.py" beforeDir="false" afterPath="$PROJECT_DIR$/definitions.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/plant.py" beforeDir="false" afterPath="$PROJECT_DIR$/plant.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/py.py" beforeDir="false" afterPath="$PROJECT_DIR$/py.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/py.py" beforeDir="false" afterPath="$PROJECT_DIR$/py.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/soil.py" beforeDir="false" afterPath="$PROJECT_DIR$/soil.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/tractor.py" beforeDir="false" afterPath="$PROJECT_DIR$/tractor.py" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -83,6 +84,8 @@
<workItem from="1616322081205" duration="4362000" /> <workItem from="1616322081205" duration="4362000" />
<workItem from="1616327331805" duration="7576000" /> <workItem from="1616327331805" duration="7576000" />
<workItem from="1616428378038" duration="453000" /> <workItem from="1616428378038" duration="453000" />
<workItem from="1616510487017" duration="5321000" />
<workItem from="1616515878589" duration="110000" />
</task> </task>
<servers /> <servers />
</component> </component>
@ -90,22 +93,22 @@
<option name="version" value="1" /> <option name="version" value="1" />
</component> </component>
<component name="WindowStateProjectService"> <component name="WindowStateProjectService">
<state width="1879" height="295" key="GridCell.Tab.0.bottom" timestamp="1616428834194"> <state width="1879" height="295" key="GridCell.Tab.0.bottom" timestamp="1616515989422">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state width="1879" height="295" key="GridCell.Tab.0.bottom/0.0.1920.1080@0.0.1920.1080" timestamp="1616428834194" /> <state width="1879" height="295" key="GridCell.Tab.0.bottom/0.0.1920.1080@0.0.1920.1080" timestamp="1616515989422" />
<state width="1879" height="295" key="GridCell.Tab.0.center" timestamp="1616428834193"> <state width="1879" height="295" key="GridCell.Tab.0.center" timestamp="1616515989422">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state width="1879" height="295" key="GridCell.Tab.0.center/0.0.1920.1080@0.0.1920.1080" timestamp="1616428834193" /> <state width="1879" height="295" key="GridCell.Tab.0.center/0.0.1920.1080@0.0.1920.1080" timestamp="1616515989422" />
<state width="1879" height="295" key="GridCell.Tab.0.left" timestamp="1616428834193"> <state width="1879" height="295" key="GridCell.Tab.0.left" timestamp="1616515989422">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state width="1879" height="295" key="GridCell.Tab.0.left/0.0.1920.1080@0.0.1920.1080" timestamp="1616428834193" /> <state width="1879" height="295" key="GridCell.Tab.0.left/0.0.1920.1080@0.0.1920.1080" timestamp="1616515989422" />
<state width="1879" height="295" key="GridCell.Tab.0.right" timestamp="1616428834194"> <state width="1879" height="295" key="GridCell.Tab.0.right" timestamp="1616515989422">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state width="1879" height="295" key="GridCell.Tab.0.right/0.0.1920.1080@0.0.1920.1080" timestamp="1616428834194" /> <state width="1879" height="295" key="GridCell.Tab.0.right/0.0.1920.1080@0.0.1920.1080" timestamp="1616515989422" />
<state width="1879" height="364" key="GridCell.Tab.1.bottom" timestamp="1616326478876"> <state width="1879" height="364" key="GridCell.Tab.1.bottom" timestamp="1616326478876">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
@ -122,12 +125,12 @@
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state width="1879" height="364" key="GridCell.Tab.1.right/0.0.1920.1080@0.0.1920.1080" timestamp="1616326478875" /> <state width="1879" height="364" key="GridCell.Tab.1.right/0.0.1920.1080@0.0.1920.1080" timestamp="1616326478875" />
<state x="0" y="0" key="com.intellij.ide.util.TipDialog" timestamp="1616428379187"> <state x="0" y="0" key="com.intellij.ide.util.TipDialog" timestamp="1616515879771">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state x="0" y="0" key="com.intellij.ide.util.TipDialog/0.0.1920.1080@0.0.1920.1080" timestamp="1616428379187" /> <state x="0" y="0" key="com.intellij.ide.util.TipDialog/0.0.1920.1080@0.0.1920.1080" timestamp="1616515879771" />
</component> </component>
<component name="com.intellij.coverage.CoverageDataManagerImpl"> <component name="com.intellij.coverage.CoverageDataManagerImpl">
<SUITE FILE_PATH="coverage/SmartTractor$py.coverage" NAME="py Coverage Results" MODIFIED="1616428775958" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" /> <SUITE FILE_PATH="coverage/SmartTractor$py.coverage" NAME="py Coverage Results" MODIFIED="1616515897525" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
</component> </component>
</project> </project>

Binary file not shown.

Binary file not shown.

View File

@ -12,5 +12,15 @@ WIDTH, HEIGHT = 1000, 1000
WIN = pygame.display.set_mode((WIDTH, HEIGHT)) WIN = pygame.display.set_mode((WIDTH, HEIGHT))
TRACTOR = pygame.image.load(os.path.join('resources', 'tractor.png')) TRACTOR = pygame.image.load(os.path.join('resources', 'tractor.png'))
TRACTOR = pygame.transform.scale(TRACTOR, (BLOCK_SIZE, BLOCK_SIZE)) TRACTOR = pygame.transform.scale(TRACTOR, (BLOCK_SIZE, BLOCK_SIZE))
VEL = 100 WHEAT_GROW_TIME = 5
WHEAT_MAXIMUM_STATE = 5 WHEAT_MAXIMUM_STATE = 22
WHEATSTAGE1 = pygame.image.load(os.path.join('resources', 'wheat_stage1.png'))
WHEATSTAGE1 = pygame.transform.scale(WHEATSTAGE1, (BLOCK_SIZE, BLOCK_SIZE))
WHEATSTAGE2 = pygame.image.load(os.path.join('resources', 'wheat_stage2.png'))
WHEATSTAGE2 = pygame.transform.scale(WHEATSTAGE2, (BLOCK_SIZE, BLOCK_SIZE))
WHEATSTAGE3 = pygame.image.load(os.path.join('resources', 'wheat_stage3.png'))
WHEATSTAGE3 = pygame.transform.scale(WHEATSTAGE3, (BLOCK_SIZE, BLOCK_SIZE))
WHEATSTAGE4 = pygame.image.load(os.path.join('resources', 'wheat_stage4.png'))
WHEATSTAGE4 = pygame.transform.scale(WHEATSTAGE4, (BLOCK_SIZE, BLOCK_SIZE))
WHEATSTAGE5 = pygame.image.load(os.path.join('resources', 'wheat_stage5.png'))
WHEATSTAGE5 = pygame.transform.scale(WHEATSTAGE5, (BLOCK_SIZE, BLOCK_SIZE))

View File

@ -1,12 +1,7 @@
class Plant: class Plant:
def __init__(self, current_state, maximum_state): def __init__(self, state):
self.current_state = current_state self.state = state
self.maximum_state = maximum_state def get_state(self):
def get_current_state(self): return self.state
return self.current_state def set_state(self, state):
def set_current_state(self, current_state): self.state = state
self.current_state = current_state
def get_maximum_state(self):
return self.maximum_state
def set_maximum_state(self, maximum_state):
self.maximum_state = maximum_state

113
py.py
View File

@ -13,7 +13,7 @@ def create_base_map():
for j in range(10): for j in range(10):
temp_rect = pygame.Rect(i * definitions.BLOCK_SIZE, j * definitions.BLOCK_SIZE, definitions.BLOCK_SIZE, definitions.BLOCK_SIZE) 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_soil = soil.Soil(False, False)
temp_plant = plant.Plant(0, definitions.WHEAT_MAXIMUM_STATE) temp_plant = plant.Plant(0)
temp_field = field.Field(temp_plant, temp_rect, temp_soil) temp_field = field.Field(temp_plant, temp_rect, temp_soil)
temp_map_field.append(temp_field) temp_map_field.append(temp_field)
fields.append(temp_map_field) fields.append(temp_map_field)
@ -22,64 +22,87 @@ def fill_map():
for j in range(10): for j in range(10):
field = fields[i][j] field = fields[i][j]
rect = field.get_rect() rect = field.get_rect()
state = field.get_soil().get_state() if field.get_plant().get_state() > 0 and field.get_plant().get_state() <= 1 * definitions.WHEAT_GROW_TIME + 1:
water_level = field.get_soil().get_water_level() block = definitions.WHEATSTAGE1
if state == False: elif field.get_plant().get_state() > 1 * definitions.WHEAT_GROW_TIME + 1 and field.get_plant().get_state() <= 2 * definitions.WHEAT_GROW_TIME + 1:
block = definitions.WHEATSTAGE2
elif field.get_plant().get_state() > 2 * definitions.WHEAT_GROW_TIME + 1 and field.get_plant().get_state() <= 3 * definitions.WHEAT_GROW_TIME + 1:
block = definitions.WHEATSTAGE3
elif field.get_plant().get_state() > 3 * definitions.WHEAT_GROW_TIME + 1 and field.get_plant().get_state() <= 4 * definitions.WHEAT_GROW_TIME + 1:
block = definitions.WHEATSTAGE4
elif field.get_plant().get_state() == 4 *definitions.WHEAT_GROW_TIME + 2:
block = definitions.WHEATSTAGE5
elif field.get_soil().get_state() is False:
block = definitions.DIRT block = definitions.DIRT
elif water_level == False: elif field.get_soil().get_state() is True and field.get_soil().get_water_level() is False:
block = definitions.FARMLAND block = definitions.FARMLAND
else: elif field.get_soil().get_state() is True and field.get_soil().get_water_level() is True:
block = definitions.FARMLANDMOIST block = definitions.FARMLANDMOIST
definitions.WIN.blit(block, (rect.x, rect.y)) definitions.WIN.blit(block, (rect.x, rect.y))
def do_work(tractor1_rectangle): def do_work(tractor1_rect):
x = int(tractor1_rectangle.x/100) x = int(tractor1_rect.x/100)
y = int(tractor1_rectangle.y/100) y = int(tractor1_rect.y/100)
field = fields[x][y] field = fields[x][y]
soil = field.get_soil() if field.get_soil().get_state() is False:
state = soil.get_state()
water_level = soil.get_water_level()
if state == False:
field.get_soil().set_state(True) field.get_soil().set_state(True)
elif water_level == False: elif field.get_soil().get_state() is True and field.get_soil().get_water_level() is False:
field.get_soil().set_water_level(True) field.get_soil().set_water_level(True)
def draw_window(tractor1_rectangle): 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):
fill_map() fill_map()
definitions.WIN.blit(definitions.TRACTOR, (tractor1_rectangle.x, tractor1_rectangle.y)) definitions.WIN.blit(definitions.TRACTOR, (tractor1_rect.x, tractor1_rect.y))
pygame.display.update() pygame.display.update()
def is_move_allowed(move, tractor1_rectangle): def grow_plants():
if ((move == 1) and (tractor1_rectangle.y + definitions.VEL + definitions.BLOCK_SIZE <= definitions.HEIGHT)): 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)):
return True return True
elif ((move == 2) and (tractor1_rectangle.x - definitions.VEL >= 0)): elif ((move == 2) and (tractor1_rect.x - definitions.BLOCK_SIZE >= 0)):
return True return True
elif ((move == 3) and (tractor1_rectangle.x + definitions.VEL + definitions.BLOCK_SIZE <= definitions.WIDTH)): elif ((move == 3) and (tractor1_rect.x + definitions.BLOCK_SIZE + definitions.BLOCK_SIZE <= definitions.WIDTH)):
return True return True
elif ((move == 4) and (tractor1_rectangle.y - definitions.VEL >= 0)): elif ((move == 4) and (tractor1_rect.y - definitions.BLOCK_SIZE >= 0)):
return True return True
else: else:
return False return False
def tractor1_handle_movement(tractor1, tractor1_rectangle): def tractor1_handle_movement(tractor1, tractor1_rect):
random1 = random.randint(1, 4) loop = True
do_work(tractor1_rectangle) while loop:
if ((random1 == 1) and (is_move_allowed(1, tractor1_rectangle) == True)): random1 = random.randint(1, 4)
tractor1.move_down() if ((random1 == 1) and (is_move_allowed(1, tractor1_rect) is True)):
tractor1_rectangle.x = tractor1.get_x() tractor1.move_down()
tractor1_rectangle.y = tractor1.get_y() tractor1_rect.x = tractor1.get_x()
elif ((random1 == 2) and (is_move_allowed(2, tractor1_rectangle) == True)): tractor1_rect.y = tractor1.get_y()
tractor1.move_left() loop = False
tractor1_rectangle.x = tractor1.get_x() elif ((random1 == 2) and (is_move_allowed(2, tractor1_rect) is True)):
tractor1_rectangle.y = tractor1.get_y() tractor1.move_left()
elif ((random1 == 3) and (is_move_allowed(3, tractor1_rectangle) == True)): tractor1_rect.x = tractor1.get_x()
tractor1.move_right() tractor1_rect.y = tractor1.get_y()
tractor1_rectangle.x = tractor1.get_x() loop = False
tractor1_rectangle.y = tractor1.get_y() elif ((random1 == 3) and (is_move_allowed(3, tractor1_rect) is True)):
elif ((random1 == 4) and (is_move_allowed(4, tractor1_rectangle) == True)): tractor1.move_right()
tractor1.move_up() tractor1_rect.x = tractor1.get_x()
tractor1_rectangle.x = tractor1.get_x() tractor1_rect.y = tractor1.get_y()
tractor1_rectangle.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
def main(): def main():
create_base_map() create_base_map()
tractor1 = tractor.Tractor(400, 400) tractor1 = tractor.Tractor(0, 0)
tractor1_rectangle = pygame.Rect(tractor1.get_x(), tractor1.get_y(), definitions.BLOCK_SIZE, definitions.BLOCK_SIZE) tractor1_rect = pygame.Rect(tractor1.get_x(), tractor1.get_y(), definitions.BLOCK_SIZE, definitions.BLOCK_SIZE)
draw_window(tractor1_rect)
clock = pygame.time.Clock() clock = pygame.time.Clock()
run = True run = True
while run: while run:
@ -87,8 +110,10 @@ def main():
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
run = False run = False
tractor1_handle_movement(tractor1, tractor1_rectangle) tractor1_handle_movement(tractor1, tractor1_rect)
draw_window(tractor1_rectangle) do_work(tractor1_rect)
grow_plants()
draw_window(tractor1_rect)
pygame.quit() pygame.quit()
if __name__ == "__main__": if __name__ == "__main__":
main() main()

BIN
resources/wheat_stage1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 765 B

BIN
resources/wheat_stage2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 832 B

BIN
resources/wheat_stage3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 843 B

BIN
resources/wheat_stage4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 867 B

BIN
resources/wheat_stage5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 879 B

View File

@ -12,10 +12,10 @@ class Tractor:
def set_y(self, y): def set_y(self, y):
self.y = y self.y = y
def move_down(self): def move_down(self):
self.y = self.y + definitions.VEL self.y = self.y + definitions.BLOCK_SIZE
def move_left(self): def move_left(self):
self.x = self.x - definitions.VEL self.x = self.x - definitions.BLOCK_SIZE
def move_right(self): def move_right(self):
self.x = self.x + definitions.VEL self.x = self.x + definitions.BLOCK_SIZE
def move_up(self): def move_up(self):
self.y = self.y - definitions.VEL self.y = self.y - definitions.BLOCK_SIZE