created Field class and fieldsFactory
This commit is contained in:
parent
b8d1370438
commit
f860b1ce45
@ -1,11 +1,15 @@
|
||||
from typing import List
|
||||
from random import choices
|
||||
from fieldsFactory import FieldsFactory
|
||||
from field import Field
|
||||
|
||||
|
||||
def generate_field() -> List[List[int]]:
|
||||
return [choices(range(11), weights=[10, 10, 4, 4, 4, 4, 4, 4, 4, 4, 4], k=10) for _ in range(10)]
|
||||
def generate_field () -> List[List[Field]]:
|
||||
fieldsFactory = FieldsFactory()
|
||||
fieldsList = fieldsFactory.getFieldsList()
|
||||
return [choices (fieldsList, weights=[10, 10, 4, 4, 4, 4, 4, 4, 4, 4, 4], k=10) for _ in range (10)]
|
||||
|
||||
|
||||
class Environment:
|
||||
def __init__(self, field: List[List[int]] = None):
|
||||
def __init__(self, field: List[List[Field]] = None):
|
||||
self.field = field if field else generate_field()
|
||||
|
4
src/field.py
Normal file
4
src/field.py
Normal file
@ -0,0 +1,4 @@
|
||||
class Field:
|
||||
def __init__(self, number, isMined):
|
||||
self.number = number
|
||||
self.isMined = isMined
|
16
src/fieldsFactory.py
Normal file
16
src/fieldsFactory.py
Normal file
@ -0,0 +1,16 @@
|
||||
from typing import List
|
||||
|
||||
from field import Field
|
||||
|
||||
|
||||
class FieldsFactory:
|
||||
def __init__(self):
|
||||
pass
|
||||
def createField(self, number) -> Field:
|
||||
if number > 3:
|
||||
return Field(number, True)
|
||||
else:
|
||||
return Field(number, False)
|
||||
|
||||
def getFieldsList(self) -> List[Field]:
|
||||
return [self.createField(i) for i in range(11)]
|
@ -29,6 +29,7 @@ class GameUi:
|
||||
def update(self):
|
||||
for x in range(10):
|
||||
for y in range(10):
|
||||
self.screen.blit(IMAGES[self.env.field[y][x]], (x * 80, y * 80))
|
||||
print(IMAGES[self.env.field[y][x].number])
|
||||
self.screen.blit(IMAGES[self.env.field[y][x].number], (x * 80, y * 80))
|
||||
self.screen.blit(self.agent.img, (self.agent.x * 80, self.agent.y * 80))
|
||||
pg.display.update()
|
||||
|
@ -4,6 +4,7 @@ from agent import Agent
|
||||
from game_ui import GameUi
|
||||
from environment import Environment
|
||||
from const import ICON, IMAGES_MAPPING
|
||||
from fieldsFactory import FieldsFactory
|
||||
|
||||
|
||||
def main():
|
||||
@ -15,6 +16,8 @@ def main():
|
||||
agent = Agent()
|
||||
game_ui = GameUi(agent, env)
|
||||
game_ui.update()
|
||||
|
||||
factory = FieldsFactory()
|
||||
|
||||
running = True
|
||||
|
||||
@ -32,8 +35,8 @@ def main():
|
||||
elif event.key == pg.K_w and agent.y*80 > 0:
|
||||
game_ui.move('y', -1)
|
||||
elif event.key == pg.K_SPACE:
|
||||
if env.field[agent.y][agent.x] > 3:
|
||||
env.field[agent.y][agent.x] = IMAGES_MAPPING[env.field[agent.y][agent.x]]
|
||||
if env.field[agent.y][agent.x].number > 3:
|
||||
env.field[agent.y][agent.x] = factory.createField(IMAGES_MAPPING[env.field[agent.y][agent.x].number])
|
||||
game_ui.update()
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user