+# place a tile at current spot
+def put_tile (leveldata, row, col, tilerow, tilecol):
+ leveldata[row][col][0] = tilerow
+ leveldata[row][col][1] = tilecol
+
+# picking a tile from the tileset
+def pick_tile (screen, tileset):
+ selrow, selcol = 0, 0
+
+ # 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
+
+ # set the current page
+ curpage_cols = 0
+ curpage_rows = 0
+
+ # cursor row and column
+ cursor_row = 0
+ cursor_col = 0
+
+ # 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:
+ # logic goes like this
+ # 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
+
+ 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)
+ pygame.display.update ()
+ for event in pygame.event.get ():
+ if event.type == pygame.KEYDOWN:
+ if event.key == pygame.K_ESCAPE:
+ return None
+ 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
+
+ # 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
+
+ elif event.key == pygame.K_DOWN:
+ # increase the selected row by 1
+ selrow += 1
+ 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
+
+ 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
+ elif event.key == pygame.K_RIGHT:
+ 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
+
+