Add set_content and some other functionalities
This commit is contained in:
@@ -24,7 +24,6 @@ class Vector2:
|
|||||||
|
|
||||||
def __floordiv__(self, other):
|
def __floordiv__(self, other):
|
||||||
return Vector2(self.x // other, self.y // other)
|
return Vector2(self.x // other, self.y // other)
|
||||||
|
|
||||||
def magnitude(self):
|
def magnitude(self):
|
||||||
return (self.x**2 + self.y**2)**0.5
|
return (self.x**2 + self.y**2)**0.5
|
||||||
def to_array(vector):
|
def to_array(vector):
|
||||||
@@ -32,6 +31,9 @@ class Vector2:
|
|||||||
def normalize(self):
|
def normalize(self):
|
||||||
length = self.magnitude()
|
length = self.magnitude()
|
||||||
return Vector2(self.x / length, self.y / length)
|
return Vector2(self.x / length, self.y / length)
|
||||||
|
def set_from_tuple(self,tup):
|
||||||
|
self.x = tup[0]
|
||||||
|
self.y = tup[1]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -39,15 +41,24 @@ class Vector2:
|
|||||||
|
|
||||||
#ENTITIES
|
#ENTITIES
|
||||||
class Entity:
|
class Entity:
|
||||||
position = Vector2(0,0)
|
coordinates = Vector2(0,0) ##PIXELS
|
||||||
|
position = Vector2(0,0) ##CELLS
|
||||||
name = ""
|
name = ""
|
||||||
def __init__(self, initial_position, name):
|
def __init__(self, initial_position:tuple, name):
|
||||||
self.position = initial_position
|
self.position.set_from_tuple(initial_position)
|
||||||
|
#self.set_cell(initial_position[0], initial_position[1])
|
||||||
|
self.coordinates = map.cell_grid[initial_position[0]][initial_position[1]].coordinates
|
||||||
|
map.cell_grid[initial_position[0]][initial_position[1]].set_content(self)
|
||||||
self.name = name
|
self.name = name
|
||||||
def draw(self):
|
def set_cell(self, x, y):
|
||||||
|
map.get_cell(self.position.x,self.position.y).set_content(None)
|
||||||
|
self.position.set_from_tuple((x,y))
|
||||||
|
self.coordinates = map.cell_grid[x][y].coordinates
|
||||||
|
map.cell_grid[x][y].set_content(self)
|
||||||
|
def draw(self, color):
|
||||||
font = pg.font.Font(None, 32)
|
font = pg.font.Font(None, 32)
|
||||||
text = font.render(self.name, True, (0, 255, 255))
|
text = font.render(self.name, True, color)
|
||||||
screen.blit(text, (self.position + Vector2(0, 0)).to_array())
|
screen.blit(text, (self.coordinates + Vector2(0, 0)).to_array())
|
||||||
|
|
||||||
class Actor(Entity):
|
class Actor(Entity):
|
||||||
perception_radius = 0
|
perception_radius = 0
|
||||||
@@ -94,31 +105,37 @@ class Cell:
|
|||||||
position = Vector2(0,0)
|
position = Vector2(0,0)
|
||||||
size = 50
|
size = 50
|
||||||
piece = pg.Rect(0, 0, size, size)
|
piece = pg.Rect(0, 0, size, size)
|
||||||
|
content = None
|
||||||
def __init__(self, coordinates):
|
def __init__(self, coordinates):
|
||||||
self.coordinates = coordinates
|
self.coordinates = coordinates
|
||||||
self.position = coordinates // self.size
|
|
||||||
self.piece = pg.Rect(self.coordinates.x, self.coordinates.y, self.size, self.size)
|
self.piece = pg.Rect(self.coordinates.x, self.coordinates.y, self.size, self.size)
|
||||||
def draw(self):
|
def draw(self):
|
||||||
pg.draw.rect(screen, (255, 255, 255), self.piece)
|
pg.draw.rect(screen, (255, 255, 255), self.piece)
|
||||||
|
def set_content(self, content):
|
||||||
|
self.content = content
|
||||||
|
|
||||||
|
|
||||||
class Map:
|
class Map:
|
||||||
cellSize = CELLSIZE
|
cellSize = CELLSIZE
|
||||||
cell_Grid = []
|
cell_grid = []
|
||||||
margin = 5
|
margin = 5
|
||||||
dimension = Vector2(8,8)
|
dimension = Vector2(8,8)
|
||||||
offset = Vector2(50, 50)
|
offset = Vector2(50, 50)
|
||||||
def __init__(self):
|
def __init__(self, dimension:tuple):
|
||||||
|
self.dimension.set_from_tuple(dimension)
|
||||||
for x in range(self.dimension.x):
|
for x in range(self.dimension.x):
|
||||||
col = []
|
col = []
|
||||||
for y in range(self.dimension.y):
|
for y in range(self.dimension.y):
|
||||||
col.append(Cell(Vector2(self.offset.x + x * (self.cellSize + self.margin), self.offset.y + y * (self.cellSize + self.margin))))
|
cell = Cell(Vector2(self.offset.x + x * (self.cellSize + self.margin), self.offset.y + y * (self.cellSize + self.margin)))
|
||||||
self.cell_Grid.append(col)
|
cell.position = Vector2(x,y)
|
||||||
|
col.append(cell)
|
||||||
|
self.cell_grid.append(col)
|
||||||
|
def get_cell(self, x, y):
|
||||||
|
return self.cell_grid[x][y]
|
||||||
def draw(self):
|
def draw(self):
|
||||||
for y in range(self.dimension.y):
|
for y in range(self.dimension.y):
|
||||||
for x in range(self.dimension.x):
|
for x in range(self.dimension.x):
|
||||||
print(self.cell_Grid[x][y].coordinates.y)
|
self.cell_grid[x][y].draw()
|
||||||
self.cell_Grid[x][y].draw()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -127,10 +144,15 @@ screen = pg.display.set_mode([512,512])
|
|||||||
pg.display.set_caption("Matrix Universe")
|
pg.display.set_caption("Matrix Universe")
|
||||||
|
|
||||||
cell = Cell(coordinates=Vector2(50,50))
|
cell = Cell(coordinates=Vector2(50,50))
|
||||||
map = Map()
|
map = Map((8, 8))
|
||||||
|
|
||||||
|
neo = Neo((0,0), 1, (0,0))
|
||||||
|
neo.set_cell(4,5)
|
||||||
|
|
||||||
|
#smith = Smith((0,0),2)
|
||||||
|
print(map.get_cell(4,5).content.name)
|
||||||
|
print(map.get_cell(0,0).content)
|
||||||
|
|
||||||
neo = Neo(map.cell_Grid[0][0].coordinates, 1, Vector2(0,0))
|
|
||||||
neo.position = map.cell_Grid[1][0].coordinates
|
|
||||||
running = True
|
running = True
|
||||||
|
|
||||||
while (running):
|
while (running):
|
||||||
@@ -139,5 +161,6 @@ while (running):
|
|||||||
running = False
|
running = False
|
||||||
screen.fill('BLACK')
|
screen.fill('BLACK')
|
||||||
map.draw()
|
map.draw()
|
||||||
neo.draw()
|
neo.draw("blue")
|
||||||
|
#smith.draw("red")
|
||||||
pg.display.update()
|
pg.display.update()
|
||||||
|
|||||||
Reference in New Issue
Block a user