diff --git a/Main.py b/Main.py index e69de29..dd0b07d 100644 --- a/Main.py +++ b/Main.py @@ -0,0 +1,107 @@ +import pygame +import sys + +sys.path.append('./Map') + +import MapManager + +# pygame setup +pygame.init() +screen = pygame.display.set_mode((1280, 720)) +clock = pygame.time.Clock() +running = True + +map = pygame.image.load( "./Map/sample.jpg") + +mapWidth = int(map.get_width()) +mapHeight = int(map.get_height()) + +y = 0 +bY = 5 +bX = 4 + +time1 = pygame.time.get_ticks() + +while bX < mapWidth: + for x in range(bX, mapWidth): + if y < mapHeight: + map.set_at((x, y), (0,0,0,255)) + y += 1 + if y % 5 == 0: + map.set_at((x, y), (0,0,0,255)) + y += 1 + bX += 8 + y = 0 + +y = bY + +while bY < mapHeight: + for x in range(0, mapWidth): + if y < mapHeight: + map.set_at((x, y), (0,0,0,255)) + y += 1 + if y % 5 == 0: + map.set_at((x, y), (0,0,0,255)) + y += 1 + bY += 10 + y = bY + +bX = 4 +y = 0 + +while bX < mapWidth: + for x in range(bX, -1, -1): + if y < mapHeight: + map.set_at((x, y), (0,0,0,255)) + y += 1 + if y % 5 == 0: + map.set_at((x, y), (0,0,0,255)) + y += 1 + bX += 8 + y = 0 + +bY = 2 +y = bY + +while bY < mapHeight: + for x in range(mapWidth, 0, -1): + if y < mapHeight: + map.set_at((x, y), (0,0,0,255)) + y += 1 + if y % 5 == 0: + map.set_at((x, y), (0,0,0,255)) + y += 1 + bY += 10 + y = bY + +pygame.image.save(map, "./Map/blackstripes.png") + +GameMap = MapManager.MapManager() + +GameMap.load(map) + +time2 = pygame.time.get_ticks() + +print(str(time2 - time1)) + +pygame.image.save(map, "./Map/sampledone.png") + +while running: + # poll for events + # pygame.QUIT event means the user clicked X to close your window + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + + # fill the screen with a color to wipe away anything from last frame + screen.fill("black") + + # RENDER YOUR GAME HERE + + # flip() the display to put your work on screen + pygame.display.flip() + + clock.tick(60) # limits FPS to 60 + running = False + +pygame.quit() \ No newline at end of file diff --git a/Map/MapManager.py b/Map/MapManager.py index e69de29..0065070 100644 --- a/Map/MapManager.py +++ b/Map/MapManager.py @@ -0,0 +1,126 @@ +import random + +class MapManager: + + def tileChecker(self, x, y, map): + list = [] + CurY = y + CurX = x + RelX = 0 + RelY = 0 + floor = False + contact = False + while not floor: + if map.get_at((CurX, CurY)) == (255,255,255,255) and y != 0: + list.append((CurX,CurY)) + while not contact: + if CurX+RelX-1 >= 0: + RelX -= 1 + else: + contact = True + if map.get_at((CurX+RelX, CurY)) == (0,0,0,255): + contact = True + if map.get_at((CurX+RelX, CurY)) == (255,255,255,255): + list.append((CurX+RelX,CurY)) + contact = False + RelX = 0 + while not contact: + if CurX+RelX+1 < map.get_width(): + RelX += 1 + else: + contact = True + if map.get_at((CurX+RelX, CurY)) == (0,0,0,255): + contact = True + if map.get_at((CurX+RelX, CurY)) == (255,255,255,255): + list.append((CurX+RelX,CurY)) + if CurY+1 < map.get_height(): + CurY += 1 + else: + floor = True + contact = False + RelX = 0 + elif map.get_at((CurX, CurY)) == (255,255,255,255) and y == 0: + list.append((CurX,CurY)) + contact = False + contactB = False + RelX = 0 + RelY = 0 + while not contact: + if map.get_at((CurX+RelX, CurY)) == (0,0,0,255): + contact = True + if map.get_at((CurX+RelX, CurY)) == (255,255,255,255): + list.append((CurX+RelX,CurY)) + while not contactB: + if CurY+RelY+1 < map.get_height(): + RelY += 1 + else: + contactB = True + if map.get_at((CurX+RelX, CurY+RelY)) == (255,255,255,255): + list.append((CurX+RelX,CurY+RelY)) + else: + contactB = True + if CurX+RelX+1 < map.get_width(): + RelX += 1 + else: + contact = True + contactB = False + RelY = 0 + contact = False + floor = True + RelX = 0 + else: + floor = True + return list + + def load(self,mapObject): + print("Loading map") + tiles = {} + tileprint = 0 + currentTile = 0 + for y in range(0, mapObject.get_height()-1): + for x in range(0, mapObject.get_width()-1): + CurCol = mapObject.get_at((x, y)) + if CurCol == (255,255,255,255): + tiles[f"{currentTile}"] = Tile(currentTile) + goodtiles = self.tileChecker(x, y, mapObject) + for pixel in goodtiles: + tiles[f"{currentTile}"].add_pixel(pixel) + if tileprint < 20: + print(f"Added tiles: {goodtiles}") + tileprint += 1 + localId = tiles[f"{currentTile}"].getId() % (255*3) + if localId < 255: + colour1 = localId + colour2 = 0 + colour3 = 0 + elif localId >= 255 and localId <= 510: + colour1 = 255 + colour2 = localId - 255 + colour3 = 0 + else: + colour1 = 255 + colour2 = 255 + colour3 = localId - 510 + tiles[f"{currentTile}"].paint_pixels(mapObject, int(colour1),int(colour2),int(colour3)) + currentTile += 1 + self.amountOfTiles = currentTile + print(f"{tiles.keys()}") + +class Tile: + + def __init__(self, Id): + self.Id = Id + self.pixels = [] + self.colour = (random.randint(0,255),random.randint(0,255),random.randint(0,255)) + + def getId(self): + return self.Id + + def add_pixel(self,pixel): + self.pixels.append(pixel) + + def paint_pixels(self, map, r,g,b): + print(f"{r},{g},{b}") + for pixel in self.pixels: + map.set_at((pixel[0],pixel[1]), (r,g,b,255)) + diff --git a/Map/__pycache__/MapManager.cpython-312.pyc b/Map/__pycache__/MapManager.cpython-312.pyc new file mode 100644 index 0000000..faa6a58 Binary files /dev/null and b/Map/__pycache__/MapManager.cpython-312.pyc differ diff --git a/Map/blackstripes.png b/Map/blackstripes.png new file mode 100644 index 0000000..02c772a Binary files /dev/null and b/Map/blackstripes.png differ diff --git a/Map/sample.jpg b/Map/sample.jpg new file mode 100644 index 0000000..6cd3941 Binary files /dev/null and b/Map/sample.jpg differ diff --git a/Map/sample.png b/Map/sample.png index e69de29..042bc81 100644 Binary files a/Map/sample.png and b/Map/sample.png differ diff --git a/Map/sampledone.png b/Map/sampledone.png new file mode 100644 index 0000000..0474507 Binary files /dev/null and b/Map/sampledone.png differ