-1

Alright I've tried to ask this before but I'm not really getting anywhere yet. I'm hoping i can explain what i want to do well enough. i will past all the code I have so far below.

  • Im trying to create a 2D game just as practice to get to know pygame and python better.
  • The game will be basically like the NES Zelda game (first one)
  • Right now I want to recreate a one of the topdown screens. Simple sprites of 16x16 pixels on a grid.
  • Now with the great help of you guys I have already gotten a clue about how to create the grid in a nice compact def with two 'for' statements (above).

    def drawMapArray(maparray):
    for x in range(0, xTile):
        for y in range(0, yTile):
            current_tile = tile_dict[maparray[x, y]]
            screen.blit(current_tile, (x*spritesize, y*spritesize))
    
  • Now what i want to do is from another file, take a map to map out tiles on the grid from a png file. so if my screen was 8 by 4 tiles:

     1 0 2 2 2 3 2 3
     3 0 0 0 0 1 0 0 
     4 4 4 4 4 1 1 1
     1 1 1 3 3 3 4 4
    
     1 = thing1.png
     2 = thing2.png
     3 = thing3.png
     4 = thing4.png
    
  • So then somehow I could import that into the maparray so each tile had the right .png file showing up on the grid. How would i go about getting this to happen? Here is my code below.

    import numpy
    import pygame
    import sys
    from pygame.locals import *
    
    pygame.init()
    
    fpsClock = pygame.time.Clock()
    
    #print help(numpy)
    
    resmulti=3
    spritesize=16*resmulti
    resolution=(256*resmulti,224*resmulti)
    screen = pygame.display.set_mode((resolution[0], resolution[1]))
    pygame.display.set_caption("testing.")
    
    xTile = 2
    yTile = 2
    
    gameRunning = True
    groundArray = numpy.ones((xTile,yTile))
    ###################################
    ######### Image Manipulation ######
    ###################################
    def pngLoad(imgloc, size, flipx, flipy):
        img = pygame.image.load(imgloc).convert_alpha()
        if size > 1:
            #pygame.transform.scale(Surface, (width, height), DestSurface = None): return Surface
            img = pygame.transform.scale(img, (img.get_width()*size, img.get_height()*size))
        if flipx == 1:
            #pygame.transform.flip(Surface, xbool, ybool)
            img = pygame.transform.flip(img, True, False)
        if flipy == 1:
            img = pygame.transform.flip(img, False, True)
    
        return img
    
    ###################################
    ######### All Image Tiles #########
    ###################################
    tile_dict = {3 : pngLoad("link1.png", resmulti,0,0),
                 2 : pngLoad("shrub_01.png", resmulti,0,0),
                 1 : pngLoad("tanback.png", resmulti,0,0)
                }
    
    def drawMapArray(maparray):
        for x in range(0, xTile):
            for y in range(0, yTile):
                #Determines tile type.
                current_tile = tile_dict[maparray[x, y]]
                screen.blit(current_tile, (x*spritesize, y*spritesize))
    
    
    while gameRunning:
        drawMapArray(groundArray)
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameRunning = False
                break
    
        #Updates display and then sets FPS to 30 FPS.
        pygame.display.update()
        fpsClock.tick(30)
    
    pygame.quit()
    
Quaternion
  • 10,380
  • 6
  • 51
  • 102
Paul Duncan
  • 302
  • 1
  • 5
  • 19

1 Answers1

0

This example that use a spritesheet for tiles, and numpy for a 2d array of tiles. https://stackoverflow.com/a/12665286/341744

Community
  • 1
  • 1
ninMonkey
  • 7,211
  • 8
  • 37
  • 66
  • Hey monkey, thanks. I downloaded and have been going over each line of code in those files to find out whats happening. Sorry, really new at this so excuse my stupid questions. Still in the middle of forcing these methods and new ideas into my head :) So as far as I see the def randomize is doing what im interested in self.tiles[x,y] = randint(0,TILE_ID_GROUND) ## But Now I am trying to figure out how to get the randint(0,TILE_ID_GROUND) to read from a txt file in my original post in the right order. if you have any tips on that id appreciate it while I reserch this. Thanks again – Paul Duncan Dec 01 '12 at 19:54
  • Alright mr Monkey Ive come up with this so far through trial and error I made I think(?) is a 2D array moo = [[1, 2, 0, 2, 1, 2], [0, 2, 0, 2, 0, 2], [1, 2, 0, 0, 0, 2], [0, 2, 0, 1, 2, 0], [2, 2, 0, 2, 1, 0], [2, 1, 0, 2, 0, 2]] (made tiles 6x6) and then plugged in self.tiles[x,y] = moo[x][y] Is this an ok way to do this or am I off? It seems to display alright corresponding to the numbers. Thanks for the original help in any case. – Paul Duncan Dec 01 '12 at 20:27