add field class. To fix: tractor renders under field
This commit is contained in:
parent
9a58e4047d
commit
2c996cc86f
29
main.py
29
main.py
@ -61,7 +61,32 @@ class Tractor:
|
|||||||
self.move(self.direction)
|
self.move(self.direction)
|
||||||
|
|
||||||
|
|
||||||
|
class Field:
|
||||||
|
def __init__(self, parent_screen):
|
||||||
|
self.parent_screen = parent_screen
|
||||||
|
self.block = pygame.image.load(r'resources\field.png').convert()
|
||||||
|
|
||||||
|
def place_field(self, field_matrix):
|
||||||
|
for m, posY in enumerate(field_matrix):
|
||||||
|
for n, posX in enumerate(posY):
|
||||||
|
if field_matrix[m][n] == 1:
|
||||||
|
self.parent_screen.blit(self.block, (n * 50, m * 50))
|
||||||
|
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
|
field_matrix = [[0 for m in range(10)] for n in range(10)]
|
||||||
|
|
||||||
|
for i in range(10):
|
||||||
|
while True:
|
||||||
|
field_posX = random.randint(0, 9)
|
||||||
|
field_posY = random.randint(0, 9)
|
||||||
|
|
||||||
|
if field_matrix[field_posY][field_posX] == 0:
|
||||||
|
field_matrix[field_posY][field_posX] = 1
|
||||||
|
break
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pygame.init()
|
pygame.init()
|
||||||
self.screenWidth = 500
|
self.screenWidth = 500
|
||||||
@ -74,6 +99,9 @@ class Game:
|
|||||||
|
|
||||||
self.lines = Lines(self.surface)
|
self.lines = Lines(self.surface)
|
||||||
|
|
||||||
|
self.field = Field(self.surface)
|
||||||
|
self.field.place_field(self.field_matrix)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
running = True
|
running = True
|
||||||
last_time = datetime.now()
|
last_time = datetime.now()
|
||||||
@ -101,6 +129,7 @@ class Game:
|
|||||||
if (time_now - last_time).total_seconds() > 1: # tractor moves every 1 sec
|
if (time_now - last_time).total_seconds() > 1: # tractor moves every 1 sec
|
||||||
last_time = datetime.now()
|
last_time = datetime.now()
|
||||||
self.tractor.walk()
|
self.tractor.walk()
|
||||||
|
self.field.place_field(self.field_matrix)
|
||||||
self.lines.draw_lines()
|
self.lines.draw_lines()
|
||||||
print(f'x, y = ({int(self.tractor.x / 50)}, {int(self.tractor.y / 50)})')
|
print(f'x, y = ({int(self.tractor.x / 50)}, {int(self.tractor.y / 50)})')
|
||||||
|
|
||||||
|
BIN
resources/field.png
Normal file
BIN
resources/field.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 577 B |
Loading…
Reference in New Issue
Block a user