X-Git-Url: https://harishankar.org/repos/?p=butaba-adventures.git;a=blobdiff_plain;f=leveleditor.py;h=bbfcbf74976510cfe7342e2b7669098d43e73ea2;hp=5b98c09e43f0397bbcb2257b6429c24aae0327e5;hb=8d8c32fdbd61956ccc4792524b621524d83b0467;hpb=9457edf26f8499b7fbeb9921fdd91a762bdfb8c4 diff --git a/leveleditor.py b/leveleditor.py index 5b98c09..bbfcbf7 100755 --- a/leveleditor.py +++ b/leveleditor.py @@ -8,6 +8,7 @@ import pygame import os.path import sys import cPickle +import math import utility @@ -25,13 +26,18 @@ def draw_level (screen, leveldata, tileset): j += 1 i += 1 -def display_menu (screen): +def display_menu (screen, tileset, tilerow, tilecol): utility.put_text (screen, 490, 10, 10, (255,255,0), "p to pick tile") utility.put_text (screen, 490, 50, 10, (255,255,0), "w to wall (solid)") - utility.put_text (screen, 490, 90, 10, (255,255,0), " to place") - utility.put_text (screen, 490, 130, 10,(255,255,0), "Arrows to move around") - utility.put_text (screen, 490, 170, 10, (255,255,0), "s to Save level") - utility.put_text (screen, 490, 210, 10, (255,255,0), "q to Quit editor") + utility.put_text (screen, 490, 90, 10, (255,255,0), " or ") + utility.put_text (screen, 490, 110, 10, (255,255,0), "to place") + utility.put_text (screen, 490, 150, 10,(255,255,0), "Arrows to move around") + utility.put_text (screen, 490, 190, 10, (255,255,0), "s to Save level") + utility.put_text (screen, 490, 230, 10, (255,255,0), "q to Quit editor") + + # currently selected tile + utility.put_text (screen, 490, 270, 10, (255,255,255), "Tile selected") + screen.blit (tileset, (490, 290), (tilecol*48, tilerow*48, 48, 48)) # draw the tile cursor def draw_cursor (screen, currow, curcol): @@ -48,16 +54,43 @@ def put_tile (leveldata, row, col, tilerow, tilecol): leveldata[row][col][1] = tilecol # picking a tile from the tileset -def pick_tile (screen, tileset): - selrow, selcol = 0, 0 +def pick_tile (screen, tileset, selrow, selcol): + # total number of rows and columns totalrows = tileset.get_height () / 48 totalcols = tileset.get_width () / 48 + # implement scrolling within the tileset image in a 480x480 viewport + + # set the current page + curpage_cols = selrow / 10 + curpage_rows = selcol / 10 + + # cursor row and column + cursor_row = selrow % 10 + cursor_col = selcol % 10 + + # the code for scrolling through the tileset is slightly complicated here are the steps + # logic goes like this + # e.g. horizontal movement + # left arrow key movement: + # 1. reduce the absolute column by 1 + # 2. if absolute column < 0 then set absolute column to last column as total columns - 1 + # 3. calculate the current page based on absolute column as absolute column / 10 + # 10 being the number of tile columns per page + # 4. calculate the relative column on screen as (abolute column modulo 10) giving a + # division remainder between 0 and 9. + + # same logic is used for all other movements + while 1: screen.fill (pygame.Color (0, 0, 0)) - screen.blit (tileset, (0, 0)) - draw_cursor (screen, selrow, selcol) + screen.blit (tileset, (0, 0), (curpage_cols * 480, curpage_rows * 480, 480, 480)) + draw_cursor (screen, cursor_row, cursor_col) + utility.put_text (screen, 490, 10,10, (255, 255, 0), " or ") + utility.put_text (screen, 490, 30,10, (255, 255, 0), "to select tile") + utility.put_text (screen, 490, 70,10, (255, 255, 0), " to return") + utility.put_text (screen, 10, 490,10, (255, 255, 0), "Selected: %d row, %d col" % (selrow, selcol)) pygame.display.update () for event in pygame.event.get (): if event.type == pygame.KEYDOWN: @@ -66,22 +99,50 @@ def pick_tile (screen, tileset): elif event.key == pygame.K_SPACE or event.key == pygame.K_RETURN: return selrow, selcol elif event.key == pygame.K_UP: + # reduce the selected row by 1 selrow -= 1 if selrow < 0: selrow = totalrows - 1 + + # calculate the current page + curpage_rows = selrow / 10 + + # calculate the current cursor row + cursor_row = selrow % 10 + elif event.key == pygame.K_DOWN: + # increase the selected row by 1 selrow += 1 if selrow >= totalrows: selrow = 0 + + # calculate the current row page + curpage_rows = selrow / 10 + + # calculate the cursor row + cursor_row = selrow % 10 elif event.key == pygame.K_LEFT: + # decrease the selected col by 1 selcol -= 1 if selcol < 0: selcol = totalcols - 1 + + # calculate the current column page + curpage_cols = selcol / 10 + + # calculate the cursor column + cursor_col = selcol % 10 elif event.key == pygame.K_RIGHT: + # increase the selected column by 1 selcol += 1 if selcol >= totalcols: selcol = 0 + # calculate the current column page + curpage_cols = selcol / 10 + + # calculate the cursor column + cursor_col = selcol % 10 # the actual level editor def level_editor (fname): @@ -92,13 +153,13 @@ def level_editor (fname): # load the tileset tileset = pygame.image.load (os.path.join ("background", "tileset.png")) screen = pygame.display.set_mode ((720, 512)) - pygame.display.set_caption ("Level editor") + pygame.display.set_caption ("Level editor: %s" % fname) currow, curcol = 0, 0 tilerow, tilecol = 0, 0 while 1: screen.fill (pygame.Color (0, 0, 0)) - display_menu (screen) + display_menu (screen, tileset, tilerow, tilecol) draw_level (screen, leveldata, tileset) draw_cursor (screen, currow, curcol) pygame.display.update () @@ -129,7 +190,7 @@ def level_editor (fname): elif event.key == pygame.K_SPACE or event.key == pygame.K_RETURN: put_tile (leveldata, currow, curcol, tilerow, tilecol) elif event.key == ord ("p"): - tile = pick_tile (screen, tileset) + tile = pick_tile (screen, tileset, tilerow, tilecol) if tile is not None: tilerow = tile[0] tilecol = tile[1]