From 3e350cff23064e74da2ead6c65e46ccdf3e7fa2f Mon Sep 17 00:00:00 2001 From: Harishankar Date: Sun, 2 Oct 2011 08:33:30 +0530 Subject: [PATCH] Much simplified the tile picker in level editor Simplified the logic for scrolling through the tile picker in the level editor. Also added a bit of polish to the level editor with instructions. --- leveleditor.py | 144 +++++++++++++++++-------------------------------- 1 file changed, 48 insertions(+), 96 deletions(-) diff --git a/leveleditor.py b/leveleditor.py index 1fe96e1..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 @@ -28,13 +29,14 @@ def draw_level (screen, leveldata, tileset): 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, 250, 10, (255,255,255), "Tile selected") + 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 @@ -52,60 +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 by 480x480 - # first get the number of pages to span across. If the width is - # exactly fitting in the page use totalwidth/480 else totalwidth/480+1 - # (integer divison) - if tileset.get_width () % 480 == 0: - totalpages_cols = tileset.get_width () / 480 - else: - totalpages_cols = tileset.get_width () / 480 + 1 - - if tileset.get_height () % 480 == 0: - totalpages_rows = tileset.get_height () / 480 - else: - totalpages_rows = tileset.get_height () / 480 + 1 + # implement scrolling within the tileset image in a 480x480 viewport # set the current page - curpage_cols = 0 - curpage_rows = 0 + curpage_cols = selrow / 10 + curpage_rows = selcol / 10 # cursor row and column - cursor_row = 0 - cursor_col = 0 + cursor_row = selrow % 10 + cursor_col = selcol % 10 - # the code for scrolling through the tileset is quite complicated here are the steps - # first we determine the total number of pages (number of row pages and col pages - # then when scrolling we check for two things: - # first horizontal scrolling: + # 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: - # is the cursor col < 0 ? if so, is it the first page? if so, then set the - # cursor col to the last col of the last page. This unfortunately is not so easy - # because it need not be 10. So we determine the last col as follows: - # total width of the tileset image modulo 480 = this gives us a multiple of 48 - # since we are using 48x48 tiles and the tileset file size is a multiple of 48. - # then we divide that value by 48 which gives us a number between 0 and 9. - # this is the last col. If it is not the first page, we simply set the cursor - # column to 9 and reduce the page by 1. - # right arrow movement: - # simply if it is the last col and last page we simply reset the - # page to 0 and the cursor col to 0. Otherwise page is incremented by 1 - # and col is still set to 0. simple logic - - # vertical scrolling using the same logic + # 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), (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: @@ -119,21 +104,11 @@ def pick_tile (screen, tileset): if selrow < 0: selrow = totalrows - 1 - # reduce the cursor row by 1 - cursor_row -= 1 - # if cursor row is < 0 - if cursor_row < 0: - # reduce the page by 1 - curpage_rows -= 1 - if curpage_rows < 0: - curpage_rows = totalpages_rows - 1 - if tileset.get_height () % 480 > 0 : - # cursor row should be the last row - this is - cursor_row = (tileset.get_height () % 480) / 48 - 1 - else: - cursor_row = 9 - else: - cursor_row = 9 + # 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 @@ -141,56 +116,33 @@ def pick_tile (screen, tileset): if selrow >= totalrows: selrow = 0 - # increase the cursor row by 1 - cursor_row += 1 - # if cursor row is > 9 - if ((tileset.get_height () % 480 > 0 and curpage_rows >= totalpages_rows - 1 and cursor_row >= (tileset.get_height () % 480) / 48) - or cursor_row >= 10): - # increase the page by 1 - curpage_rows += 1 - # if the pages rows is >= totalpages_rows - if curpage_rows >= totalpages_rows: - curpage_rows = 0 - cursor_row = 0 - else: - cursor_row = 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 - # decrease the cursor col by 1 - cursor_col -= 1 - # if cursor col < 0 - if cursor_col < 0: - curpage_cols -= 1 - if curpage_cols < 0: - curpage_cols = totalpages_cols - 1 - if tileset.get_width () % 480 > 0: - cursor_col = (tileset.get_width () % 480) / 48 - 1 - else: - cursor_col = 9 - else: - cursor_col = 9 + # 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 - # increase the cursor col by 1 - cursor_col += 1 - # if cursor col > 9 - if ((tileset.get_width () % 480 > 0 and curpage_cols >= totalpages_cols - 1 and cursor_col >= (tileset.get_width () % 480) / 48) - or cursor_col >= 10): - curpage_cols += 1 - if curpage_cols >= totalpages_cols: - curpage_cols = 0 - cursor_col = 0 - else: - cursor_col = 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): @@ -201,7 +153,7 @@ 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 @@ -238,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] -- 2.20.1