Commit 10/03-24

-- Began work on map generation.

-- Wrote functions to generate and color an image of hexagons.

-- Next step is to seed the map by generating tile clusters who designate what tile types are where
This commit is contained in:
Hannah-Dagemark 2024-03-10 20:04:25 +01:00
commit 1b4021f4ce
7 changed files with 233 additions and 0 deletions

107
Main.py
View file

@ -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()

View file

@ -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))

Binary file not shown.

BIN
Map/blackstripes.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

BIN
Map/sample.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 0 B

After

Width:  |  Height:  |  Size: 360 B

Before After
Before After

BIN
Map/sampledone.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 269 KiB