Commit 17/03-24

Altered the image generation to now also generate terrain, however it is broken and has "red spots" where terrain fails to generate. Will look further into it and fix next time. Terrain regen also doesn't properly "blob" but has more of a tendency to form mesh-grids
This commit is contained in:
Hannah-Dagemark 2024-03-17 21:08:44 +01:00
commit 808e806b6a
11 changed files with 128 additions and 27 deletions

34
Main.py
View file

@ -11,7 +11,8 @@ screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock() clock = pygame.time.Clock()
running = True running = True
map = pygame.image.load( "./Map/sample.jpg") map = pygame.image.load( "./Map/Images/sample.jpg")
maprect = map.get_rect()
mapWidth = int(map.get_width()) mapWidth = int(map.get_width())
mapHeight = int(map.get_height()) mapHeight = int(map.get_height())
@ -30,6 +31,8 @@ while bX < mapWidth:
if y % 5 == 0: if y % 5 == 0:
map.set_at((x, y), (0,0,0,255)) map.set_at((x, y), (0,0,0,255))
y += 1 y += 1
screen.blit(map, maprect)
pygame.display.flip()
bX += 8 bX += 8
y = 0 y = 0
@ -43,6 +46,8 @@ while bY < mapHeight:
if y % 5 == 0: if y % 5 == 0:
map.set_at((x, y), (0,0,0,255)) map.set_at((x, y), (0,0,0,255))
y += 1 y += 1
screen.blit(map, maprect)
pygame.display.flip()
bY += 10 bY += 10
y = bY y = bY
@ -57,6 +62,8 @@ while bX < mapWidth:
if y % 5 == 0: if y % 5 == 0:
map.set_at((x, y), (0,0,0,255)) map.set_at((x, y), (0,0,0,255))
y += 1 y += 1
screen.blit(map, maprect)
pygame.display.flip()
bX += 8 bX += 8
y = 0 y = 0
@ -71,20 +78,29 @@ while bY < mapHeight:
if y % 5 == 0: if y % 5 == 0:
map.set_at((x, y), (0,0,0,255)) map.set_at((x, y), (0,0,0,255))
y += 1 y += 1
screen.blit(map, maprect)
pygame.display.flip()
bY += 10 bY += 10
y = bY y = bY
pygame.image.save(map, "./Map/blackstripes.png") pygame.image.save(map, "./Map/Images/blackstripes.png")
GameMap = MapManager.MapManager() GameMap = MapManager.MapManager()
GameMap.load(map) GameMap.load(map)
screen.blit(map, maprect)
pygame.display.flip()
GameMap.populate(map)
time2 = pygame.time.get_ticks() time2 = pygame.time.get_ticks()
print(str(time2 - time1)) print(str(time2 - time1))
pygame.image.save(map, "./Map/sampledone.png") pygame.image.save(map, "./Map/Images/sampledone.png")
timeX = pygame.time.get_ticks()
while running: while running:
# poll for events # poll for events
@ -92,16 +108,20 @@ while running:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
running = False running = False
# fill the screen with a color to wipe away anything from last frame # fill the screen with a color to wipe away anything from last frame
screen.fill("black") screen.fill("black")
screen.blit(map, maprect)
# RENDER YOUR GAME HERE # RENDER YOUR GAME HERE
# flip() the display to put your work on screen # flip() the display to put your work on screen
pygame.display.flip() pygame.display.flip()
clock.tick(60) # limits FPS to 60 clock.tick(120) # limits FPS to 120
running = False timeY = pygame.time.get_ticks()
if timeY % 1000 == 0:
print(f"{timeY-timeX}ms per tick\n{1000/(timeY-timeX)} Ticks per second")
timeX = pygame.time.get_ticks()
pygame.quit() pygame.quit()

View file

Before

Width:  |  Height:  |  Size: 5.4 KiB

After

Width:  |  Height:  |  Size: 5.4 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 9.9 KiB

After

Width:  |  Height:  |  Size: 9.9 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 360 B

After

Width:  |  Height:  |  Size: 360 B

Before After
Before After

BIN
Map/Images/sampledone.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

View file

@ -1,4 +1,5 @@
import random import random
import TileTypes
class MapManager: class MapManager:
@ -74,42 +75,77 @@ class MapManager:
def load(self,mapObject): def load(self,mapObject):
print("Loading map") print("Loading map")
tiles = {} self.tiles = {}
tileprint = 0 tileprint = 0
currentVertical = 0
currentHorizontal = 0
currentTile = 0 currentTile = 0
for y in range(0, mapObject.get_height()-1): for y in range(0, mapObject.get_height()-1):
if (y-1) % 10 == 0:
currentVertical += 1
currentHorizontal = 0
for x in range(0, mapObject.get_width()-1): for x in range(0, mapObject.get_width()-1):
CurCol = mapObject.get_at((x, y)) CurCol = mapObject.get_at((x, y))
if CurCol == (255,255,255,255): if CurCol == (255,255,255,255):
tiles[f"{currentTile}"] = Tile(currentTile) self.tiles[f"{currentTile}"] = Tile(currentTile,(currentHorizontal,currentVertical))
goodtiles = self.tileChecker(x, y, mapObject) goodtiles = self.tileChecker(x, y, mapObject)
for pixel in goodtiles: for pixel in goodtiles:
tiles[f"{currentTile}"].add_pixel(pixel) self.tiles[f"{currentTile}"].add_pixel(pixel)
if tileprint < 20: if tileprint < 20:
print(f"Added tiles: {goodtiles}") print(f"Added tiles: {goodtiles}")
tileprint += 1 tileprint += 1
localId = tiles[f"{currentTile}"].getId() % (255*3) colour1 = 255
if localId < 255: colour2 = 0
colour1 = localId colour3 = 0
colour2 = 0 self.tiles[f"{currentTile}"].paint_pixels(mapObject, int(colour1),int(colour2),int(colour3))
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 currentTile += 1
currentHorizontal += 1
self.amountOfTiles = currentTile self.amountOfTiles = currentTile
print(f"{tiles.keys()}") self.Verticals = 59
self.Horizontals = 207
print(f"{self.tiles.keys()}")
def populate(self, mapObject):
print("Populating map")
print("Loading terrains...")
self.TileWorker = TileTypes.TerrainWorker()
self.TileWorker.generateTerrain()
tilesMapped = [["" for i in range(self.Verticals)] for j in range(self.Horizontals)]
types = self.TileWorker.get_terrain("types")
for tile in self.tiles.keys():
position = self.tiles[f"{tile}"].Position
print(f"{position}")
if position[1] == 0:
choice = random.randrange(1,7)
if choice != 6 and position[0] != 0:
tilesMapped[position[0]][position[1]] = tilesMapped[position[0]-1][position[1]]
else:
tilesMapped[position[0]][position[1]] = self.TileWorker.get_rand_tType()
else:
choice = random.randrange(1,12)
if choice <= 5:
tilesMapped[position[0]][position[1]] = tilesMapped[position[0]][position[1]-1]
elif choice != 11:
tilesMapped[position[0]][position[1]] = tilesMapped[position[0]-1][position[1]]
elif choice == 11:
tilesMapped[position[0]][position[1]] = self.TileWorker.get_rand_tType()
print(f"{tilesMapped}")
mappedColours = self.TileWorker.get_terrain("colours")
for x in self.tiles.keys():
tile = self.tiles[f"{x}"]
if tilesMapped[tile.Position[0]][tile.Position[1]] != "":
colour = mappedColours[f"{tilesMapped[tile.Position[0]][tile.Position[1]]}"]
print(f"{colour}")
colour1, colour2, colour3 = colour[0], colour[1], colour[2]
tile.paint_pixels(mapObject, int(colour1),int(colour2),int(colour3))
class Tile: class Tile:
def __init__(self, Id): def __init__(self, Id, Pos):
self.Id = Id self.Id = Id
self.Position = Pos
print(f"{Pos[0]},{Pos[1]}")
self.pixels = [] self.pixels = []
self.colour = (random.randint(0,255),random.randint(0,255),random.randint(0,255)) self.colour = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
@ -120,7 +156,6 @@ class Tile:
self.pixels.append(pixel) self.pixels.append(pixel)
def paint_pixels(self, map, r,g,b): def paint_pixels(self, map, r,g,b):
print(f"{r},{g},{b}")
for pixel in self.pixels: for pixel in self.pixels:
map.set_at((pixel[0],pixel[1]), (r,g,b,255)) map.set_at((pixel[0],pixel[1]), (r,g,b,255))

46
Map/TileTypes.py Normal file
View file

@ -0,0 +1,46 @@
import random
class TerrainWorker:
def generateTerrain(self):
self.terrains = []
ocean = Terrain("ocean", (0, 102, 255), 2, True)
self.terrains.append(ocean)
lake = Terrain("lake", (0, 204, 255), 1, True)
self.terrains.append(lake)
plains = Terrain("plains", (0, 200, 0), 1, False)
self.terrains.append(plains)
forest = Terrain("forest", (0, 100, 0), 2, False)
self.terrains.append(forest)
hills = Terrain("hills", (102, 102, 153), 2, False)
self.terrains.append(hills)
desert = Terrain("desert", (230, 230, 0), 1, False)
self.terrains.append(desert)
dune = Terrain("dune", (153, 153, 0), 2, False)
self.terrains.append(dune)
self.terrainNames = []
for terrain in self.terrains:
self.terrainNames.append(str(terrain.name))
print(f"Added {self.terrainNames}")
def get_terrain(self, action):
if action == "types":
return self.terrainNames
if action == "colours":
dictionary = {}
for terrain in self.terrains:
dictionary[terrain.name] = terrain.colour
return dictionary
def get_rand_tType(self):
tTypeNum = random.randrange(0, len(self.terrains))
return self.terrainNames[tTypeNum]
class Terrain:
def __init__(self, name, colour, moveC, water):
self.name = name
self.colour = colour
self.moveCost = moveC
self.isWater = water

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 269 KiB

Binary file not shown.