This example that use a spritesheet for tiles, and numpy for a 2d array of tiles. https://stackoverflow.com/a/12665286/341744
Python Pygame 2D game times from a map
-
03-12-2021 - |
Question
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()
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow