From 528ad2b4d7fb2b694aae06f5dcae1f29218aa096 Mon Sep 17 00:00:00 2001 From: JianClash Date: Sat, 29 Oct 2022 13:53:08 +0530 Subject: [PATCH 1/3] Created a level editor: still needs a lot of improvment and functionalites --- levelEditor.py | 154 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 levelEditor.py diff --git a/levelEditor.py b/levelEditor.py new file mode 100644 index 00000000..b4906a8a --- /dev/null +++ b/levelEditor.py @@ -0,0 +1,154 @@ +import pygame +import sys +import json + +from classes.Sprites import Sprites + + +def loadBlocks(): + with open("./sprites/BackgroundSprites.json", "r") as file: + data = json.load(file) + + # gets the names of background sprites + blocks = [data["sprites"][i]["name"] for i in range(len(data["sprites"]))] + return blocks + + +def drawLines(): + for i in range(HEIGHT // cellSize): # Drawing lines verticaly + pygame.draw.line(screen, white, (cellSize * i, 0), (cellSize * i, WIDTH), 1) + + for i in range(WIDTH // cellSize): # Drawing lines horizontaly + pygame.draw.line(screen, white, (0, cellSize * i), (HEIGHT, cellSize * i), 1) + + +def drawSprite(): + mouseX, mouseY = pygame.mouse.get_pos() + gridLocationX, gridLocationY = mouseX // cellSize, mouseY // cellSize + placedBlocks[translatedBlocks[currentBlock]] = placedBlocks.get( + translatedBlocks[currentBlock], [] + ) + if translatedBlocks[currentBlock] == "pipe": + placedBlocks[translatedBlocks[currentBlock]].append( + [gridLocationX, gridLocationY, 0] + ) + else: + placedBlocks[translatedBlocks[currentBlock]].append( + [gridLocationX, gridLocationY] + ) + screen.blit( + Sprites().spriteCollection.get(currentBlock).image, + (gridLocationX * cellSize, gridLocationY * cellSize), + ) + + +def createJsonFile(): + try: + jsonData = { + "id": 2, + "length": 20, + "level": { + "objects": { + "bush": placedBlocks["bush"], + "sky": placedBlocks["sky"], + "cloud": placedBlocks["cloud"], + "pipe": placedBlocks["pipe"], + "ground": placedBlocks["ground"], + }, + "layers": { + "sky": {"x": [0, 60], "y": [0, 13]}, + "ground": {"x": [0, 60], "y": [14, 16]}, + }, + }, + } + + with open("./levels/Level1-3.json", "w") as file: + json.dump(jsonData, file, indent=4) + + except: + blocksNotPlaced = [] + for block in set(translatedBlocks.values()): + if block not in placedBlocks: + blocksNotPlaced.append(block) + print( + "Missing blocks {} place them to save the json file".format(blocksNotPlaced) + ) + + +def changeBlock(): + global currentBlock + + # Cycle through the blocks if reached the end then start again from the start + if blocks.index(currentBlock) < len(blocks) - 1: + currentBlock = blocks[blocks.index(currentBlock) + 1] + else: + currentBlock = blocks[0] + print("Current Block: {}".format(currentBlock)) + + +def keyPressed(): + pressedKeys = pygame.key.get_pressed() + + if pressedKeys[pygame.K_q]: + pygame.quit() + sys.exit() + + if pressedKeys[pygame.K_TAB]: + changeBlock() + + if pressedKeys[pygame.K_s]: + createJsonFile() + + +def translateBlocks(): + translatedBlocks = {} + for block in blocks: + translatedBlock = block.translate( + str.maketrans({"R": "", "L": "", "_": "", "1": "", "2": "", "3": ""}) + ) + translatedBlocks[block] = translatedBlock + + return translatedBlocks + + +# GLOBAL VARIABLES +HEIGHT, WIDTH = 640, 480 +cellSize = 32 + +screen = pygame.display.set_mode((HEIGHT, WIDTH)) +pygame.display.set_caption("Level Editor") + +white = (255, 255, 255) + +blocks = loadBlocks() +currentBlock = blocks[0] +placedBlocks = {} + +# Blocks +for block in block: + if block.toList() + +print("Current Block: {}".format(currentBlock)) + + +def main(): + pygame.init() + + while True: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + sys.exit() + + if event.type == pygame.KEYDOWN: + keyPressed() + + if event.type == pygame.MOUSEBUTTONDOWN: + drawSprite() + + drawLines() + pygame.display.update() + + +if __name__ == "__main__": + main() From 8179af8f124e58b8edc97f67d8128f9b4e1367f8 Mon Sep 17 00:00:00 2001 From: JianClash Date: Mon, 7 Nov 2022 16:28:27 +0530 Subject: [PATCH 2/3] Added cloud, pipe and bush draw functions * Fixed Bugs that made multiple clouds if you try to make a full one manually * Refactored the code --- levelEditor.py | 104 ++++++++++++++++++++++++++++++------------- levels/Level1-3.json | 86 +++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+), 31 deletions(-) create mode 100644 levels/Level1-3.json diff --git a/levelEditor.py b/levelEditor.py index b4906a8a..38ee86e6 100644 --- a/levelEditor.py +++ b/levelEditor.py @@ -11,7 +11,16 @@ def loadBlocks(): # gets the names of background sprites blocks = [data["sprites"][i]["name"] for i in range(len(data["sprites"]))] - return blocks + + translatedBlocks = [] + for block in blocks: + translatedBlock = block.translate( + str.maketrans({"R": "", "L": "", "_": "", "1": "", "2": "", "3": ""}) + ) + if translatedBlock not in translatedBlocks: + translatedBlocks.append(translatedBlock) + + return translatedBlocks def drawLines(): @@ -22,24 +31,69 @@ def drawLines(): pygame.draw.line(screen, white, (0, cellSize * i), (HEIGHT, cellSize * i), 1) +def drawCloudSprite(): + for yOff in range(0, 2): + for xOff in range(0, 3): + screen.blit( + Sprites() + .spriteCollection.get("cloud{}_{}".format(yOff + 1, xOff + 1)) + .image, + ((gridLocationX + xOff) * cellSize, (gridLocationY + yOff) * cellSize), + ) + + +def drawPipeSprite(): + placedBlocks[currentBlock][-1].append(0) + + screen.blit( + Sprites().spriteCollection.get("pipeL").image, + (gridLocationX * cellSize, gridLocationY * cellSize), + ) + screen.blit( + Sprites().spriteCollection.get("pipeR").image, + ((gridLocationX + 1) * cellSize, gridLocationY * cellSize), + ) + + for i in range(1, (HEIGHT // cellSize) - gridLocationY): + screen.blit( + Sprites().spriteCollection.get("pipe2L").image, + (gridLocationX * cellSize, (gridLocationY + i) * cellSize), + ) + for i in range(1, (HEIGHT // cellSize) - gridLocationY): + screen.blit( + Sprites().spriteCollection.get("pipe2R").image, + ((gridLocationX + 1) * cellSize, (gridLocationY + i) * cellSize), + ) + + +def drawBushSprite(): + for xOff in range(3): + screen.blit( + Sprites().spriteCollection.get("bush_{}".format(xOff + 1)).image, + ((gridLocationX + xOff) * cellSize, gridLocationY * cellSize), + ) + + def drawSprite(): + global gridLocationX, gridLocationY + mouseX, mouseY = pygame.mouse.get_pos() gridLocationX, gridLocationY = mouseX // cellSize, mouseY // cellSize - placedBlocks[translatedBlocks[currentBlock]] = placedBlocks.get( - translatedBlocks[currentBlock], [] - ) - if translatedBlocks[currentBlock] == "pipe": - placedBlocks[translatedBlocks[currentBlock]].append( - [gridLocationX, gridLocationY, 0] - ) + + placedBlocks[currentBlock] = placedBlocks.get(currentBlock, []) + placedBlocks[currentBlock].append([gridLocationX, gridLocationY]) + + if currentBlock == "cloud": + drawCloudSprite() + elif currentBlock == "pipe": + drawPipeSprite() + elif currentBlock == "bush": + drawBushSprite() else: - placedBlocks[translatedBlocks[currentBlock]].append( - [gridLocationX, gridLocationY] + screen.blit( + Sprites().spriteCollection.get(currentBlock).image, + (gridLocationX * cellSize, gridLocationY * cellSize), ) - screen.blit( - Sprites().spriteCollection.get(currentBlock).image, - (gridLocationX * cellSize, gridLocationY * cellSize), - ) def createJsonFile(): @@ -67,7 +121,7 @@ def createJsonFile(): except: blocksNotPlaced = [] - for block in set(translatedBlocks.values()): + for block in blocks: if block not in placedBlocks: blocksNotPlaced.append(block) print( @@ -77,10 +131,11 @@ def createJsonFile(): def changeBlock(): global currentBlock + currentBlockIndex = blocks.index(currentBlock) # Cycle through the blocks if reached the end then start again from the start - if blocks.index(currentBlock) < len(blocks) - 1: - currentBlock = blocks[blocks.index(currentBlock) + 1] + if currentBlockIndex < len(blocks) - 1: + currentBlock = blocks[currentBlockIndex + 1] else: currentBlock = blocks[0] print("Current Block: {}".format(currentBlock)) @@ -100,17 +155,6 @@ def keyPressed(): createJsonFile() -def translateBlocks(): - translatedBlocks = {} - for block in blocks: - translatedBlock = block.translate( - str.maketrans({"R": "", "L": "", "_": "", "1": "", "2": "", "3": ""}) - ) - translatedBlocks[block] = translatedBlock - - return translatedBlocks - - # GLOBAL VARIABLES HEIGHT, WIDTH = 640, 480 cellSize = 32 @@ -124,9 +168,7 @@ def translateBlocks(): currentBlock = blocks[0] placedBlocks = {} -# Blocks -for block in block: - if block.toList() +gridLocationX, gridLocationY = 0, 0 print("Current Block: {}".format(currentBlock)) diff --git a/levels/Level1-3.json b/levels/Level1-3.json new file mode 100644 index 00000000..215bb03b --- /dev/null +++ b/levels/Level1-3.json @@ -0,0 +1,86 @@ +{ + "id": 2, + "length": 20, + "level": { + "objects": { + "bush": [ + [ + 14, + 12 + ], + [ + 18, + 12 + ] + ], + "sky": [ + [ + 0, + 0 + ] + ], + "cloud": [ + [ + 4, + 2 + ], + [ + 16, + 4 + ], + [ + 1, + 6 + ] + ], + "pipe": [ + [ + 8, + 9, + 0 + ], + [ + 11, + 7, + 0 + ] + ], + "ground": [ + [ + 5, + 11 + ], + [ + 6, + 11 + ], + [ + 6, + 10 + ] + ] + }, + "layers": { + "sky": { + "x": [ + 0, + 60 + ], + "y": [ + 0, + 13 + ] + }, + "ground": { + "x": [ + 0, + 60 + ], + "y": [ + 14, + 16 + ] + } + } + } +} \ No newline at end of file From 561c22e743e431b448b649eada8ad6ddd1201d61 Mon Sep 17 00:00:00 2001 From: JianClash Date: Mon, 7 Nov 2022 16:35:34 +0530 Subject: [PATCH 3/3] Deleted Level1-3.json left from tests --- levels/Level1-3.json | 86 -------------------------------------------- 1 file changed, 86 deletions(-) delete mode 100644 levels/Level1-3.json diff --git a/levels/Level1-3.json b/levels/Level1-3.json deleted file mode 100644 index 215bb03b..00000000 --- a/levels/Level1-3.json +++ /dev/null @@ -1,86 +0,0 @@ -{ - "id": 2, - "length": 20, - "level": { - "objects": { - "bush": [ - [ - 14, - 12 - ], - [ - 18, - 12 - ] - ], - "sky": [ - [ - 0, - 0 - ] - ], - "cloud": [ - [ - 4, - 2 - ], - [ - 16, - 4 - ], - [ - 1, - 6 - ] - ], - "pipe": [ - [ - 8, - 9, - 0 - ], - [ - 11, - 7, - 0 - ] - ], - "ground": [ - [ - 5, - 11 - ], - [ - 6, - 11 - ], - [ - 6, - 10 - ] - ] - }, - "layers": { - "sky": { - "x": [ - 0, - 60 - ], - "y": [ - 0, - 13 - ] - }, - "ground": { - "x": [ - 0, - 60 - ], - "y": [ - 14, - 16 - ] - } - } - } -} \ No newline at end of file