From 3305c2c14557f6659c2de4deb7381798e9e28a8f Mon Sep 17 00:00:00 2001 From: emil Date: Sun, 13 Oct 2024 06:04:43 +0300 Subject: [PATCH] Start remaking without numpy --- entities.py | 68 +++++++++++++++++++++++++---------------------------- main.py | 12 ++-------- map.py | 40 ++++++++++++++++++++++++------- tools.py | 27 +++++++++++++++++++++ 4 files changed, 93 insertions(+), 54 deletions(-) create mode 100644 tools.py diff --git a/entities.py b/entities.py index 8602993..05a5c43 100644 --- a/entities.py +++ b/entities.py @@ -1,45 +1,41 @@ -import numpy as np +from tools import * -class Entity: - position = np.array([0,0]) +class entity: + position = Vector2(0,0) name = "" - def __init__(self, InitialPosition, name): - self.position = InitialPosition + def __init__(self, initial_position, name): + self.position = initial_position self.name = name - - -class Actor(Entity): - perceptionRadius = 0 - percievedCells = None - def __init__(self, InitialPosition, name, perceptionRadius): - super().__init__(InitialPosition, name) - self.perceptionRadius = perceptionRadius - - -class Neo(Actor): - keyMakerPosition = np.array([0,0]) - def __init__(self, InitialPosition, perceptionRadius, keyMakerPosition): - super().__init__(InitialPosition, "Neo", perceptionRadius) - self.keyMakerPosition = keyMakerPosition -class Smith(Actor): - def __init__(self, InitialPosition, perceptionRadius): - super().__init__(InitialPosition, "Smith", perceptionRadius) - def Kill(): - print("Neo is killed.") #TODO: delete +class actor(entity): + perception_radius = 0 + perceived_cells = None + def __init__(self, initial_position, name, perception_radius): + super().__init__(initial_position, name) + self.perception_radius = perception_radius -class Sentinel(Actor): - def __init__(self, InitialPosition, perceptionRadius): - super().__init__(InitialPosition, "Sentinel", perceptionRadius) - def Kill(): +class neo(actor): + key_maker_position = Vector2(0,0) + def __init__(self, initial_position, perception_radius, key_maker_position): + super().__init__(initial_position, "neo", perception_radius) + self.key_maker_position = key_maker_position + +class smith(actor): + def __init__(self, initial_position, perception_radius): + super().__init__(initial_position, "smith", perception_radius) + def kill(): print("Neo is killed.") #TODO: delete -class Keymaker(Entity): - def __init__(self, InitialPosition): - super().__init__(InitialPosition, "Keymaker") - -class BackdoorKey(Entity): - def __init__(self, InitialPosition): - super().__init__(InitialPosition, "BackdoorKey") +class sentinel(actor): + def __init__(self, initial_position, perception_radius): + super().__init__(initial_position, "sentinel", perception_radius) + def kill(): + print("Neo is killed.") #TODO: delete +class key_maker(entity): + def __init__(self, initial_position): + super().__init__(initial_position, "key_maker") +class backdoor_key(entity): + def __init__(self, initial_position): + super().__init__(initial_position, "backdoor_key") diff --git a/main.py b/main.py index 93c179a..3bd52fb 100644 --- a/main.py +++ b/main.py @@ -1,14 +1,6 @@ -import pygame as pg +#import pygame as pg +#from tools import * from map import * -cell = Cell(coordinates=np.array([50,50])) -running = True - -while (running): - for event in pg.event.get(): - if event.type == pg.QUIT: - running = False - screen.fill('BLACK') - cell.draw() \ No newline at end of file diff --git a/map.py b/map.py index 3024e06..4b1bdd0 100644 --- a/map.py +++ b/map.py @@ -1,21 +1,45 @@ -import numpy as np import pygame as pg +from tools import * +from entities import * +pg.init() screen = pg.display.set_mode([512,512]) pg.display.set_caption("Matrix Universe") -pg.init() + + class Cell: - coordinates = np.array([0,0]) - position = np.array([0,0]) + coordinates = Vector2(0,0) + position = Vector2(0,0) size = 50 - piece = pg.draw.rect(screen, (255,255,255), (0, 0, size, size)) + piece = pg.Rect(0, 0, size, size) def __init__(self, coordinates): self.coordinates = coordinates self.position = coordinates // self.size + self.piece = pg.Rect(self.coordinates.x, self.coordinates.y, self.size, self.size) def draw(self): - screen.blit(screen, self.coordinates ,self.piece) + pg.draw.rect(screen, (255, 255, 255), self.piece) class Map: - cellGrid = None + cell_Grid = [] + margin = 5 + dimension = Vector2(8,8) + offset = Vector2(50, 50) def __init__(self): - pass \ No newline at end of file + for x in range(self.dimension.x): + for y in range(self.dimension.y): + self.cell_Grid.append(Cell()) #TODO: Finish it + pass + + + +cell = Cell(coordinates=Vector2(50,50)) + +running = True + +while (running): + for event in pg.event.get(): + if event.type == pg.QUIT: + running = False + screen.fill('BLACK') + cell.draw() + pg.display.update() diff --git a/tools.py b/tools.py new file mode 100644 index 0000000..4645e63 --- /dev/null +++ b/tools.py @@ -0,0 +1,27 @@ +class Vector2: + def __init__(self, x, y): + self.x = x + self.y = y + + def __add__(self, other): + return Vector2(self.x + other.x, self.y + other.y) + + def __sub__(self, other): + return Vector2(self.x - other.x, self.y - other.y) + + def __mul__(self, other): + return Vector2(self.x * other, self.y * other) + + def __truediv__(self, other): + return Vector2(self.x / other, self.y / other) + + def __floordiv__(self, other): + return Vector2(self.x // other, self.y // other) + + def magnitude(self): + return (self.x**2 + self.y**2)**0.5 + def to_array(vector): + return [vector.x, vector.y] + def normalize(self): + length = self.magnitude() + return Vector2(self.x / length, self.y / length)