Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8964647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:38:08+00:00 2026-06-15T16:38:08+00:00

How would I go about loading a map file in Pygame / Python? I

  • 0

How would I go about loading a map file in Pygame / Python?

I would like to load a map file in this format;

http://pastebin.com/12DZhrtp

EDIT: I’m guessing it’s a for loop, but I’m not sure on how to do that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:38:09+00:00Added an answer on June 15, 2026 at 4:38 pm

    Though some people have helped open a file, I understand you are actually looking to import a text file as a map:
    I did not write this, but have been using it as an example for my game:

    # This code is in the Public Domain
    # -- richard@mechanicalcat.net
    
    
    
    class Map:
        def __init__(self, map, tiles):
            self.tiles = pygame.image.load(tiles)
    
            l = [line.strip() for line in open(map).readlines()]
            self.map = [[None]*len(l[0]) for j in range(len(l))]
            for i in range(len(l[0])):
                for j in range(len(l)):
                    tile = l[j][i]
                    tile = tile_coords[tile]
                    if tile is None:
                        continue
                    elif isinstance(tile, type([])):
                        tile = random.choice(tile)
                    cx, cy = tile
                    if random.choice((0,1)):
                        cx += 192
                    if random.choice((0,1)):
                        cy += 192
                    self.map[j][i] = (cx, cy)
    
        def draw(self, view, viewpos):
            '''Draw the map to the "view" with the top-left of "view" being at
               "viewpos" in the map.
            '''
            sx, sy = view.get_size()
            bx = viewpos[0]/64
            by = viewpos[1]/64
            for x in range(0, sx+64, 64):
                i = x/64 + bx
                for y in range(0, sy+64, 64):
                    j = y/64 + by
                    try:
                        tile = self.map[j][i]
                    except IndexError:
                        # too close to the edge
                        continue
                    if tile is None:
                        continue
                    cx, cy = tile
                    view.blit(self.tiles, (x, y), (cx, cy, 64, 64))
    
        def limit(self, view, pos):
            '''Limit the "viewpos" variable such that it defines a valid top-left
               rectangle of "view"'s size over the map.
            '''
            x, y = pos
            # easy
            x = max(x, 0)
            y = max(y, 0)
    
            # figure number of tiles in a view, hence max x and y viewpos
            sx, sy = view.get_size()
            nx, ny = sx/64, sy/64
            mx = (len(self.map[0]) - nx) * 64
            my = (len(self.map) - ny) * 64
            print y, my
    
            return (min(x, mx), min(y, my))
    
    def main():
        pygame.init()
        win = pygame.display.set_mode((640, 480))
    
        map = Map('map.txt', 'tiles.png')
        viewpos = (0,0)
        move = False
        clock = pygame.time.Clock()
        sx, sy = win.get_size()
        while 1:
            event = pygame.event.poll()
            while event.type != NOEVENT:
                if event.type in (QUIT, KEYDOWN):
                    sys.exit(0)
                elif event.type == MOUSEBUTTONDOWN:
                    x, y = viewpos
                    dx, dy = event.pos
                    x += dx - sx/2
                    y += dy - sy/2
                    viewpos = map.limit(win, (x, y))
                    move = True
                event = pygame.event.poll()
    
            win.fill((0,0,0))
            map.draw(win, viewpos)
            pygame.display.flip()
    
            clock.tick(30)
    
    
    if __name__ == '__main__':
        main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like 'about' to route to 'abouts/1' I tried this: match 'about' =>
In spring, how would I go about loading an XML file once such that
This is about loading youtube videos using latest embedded format (iframe) inside a webview.
Just a quick question about how you would go about implementing this. I want
I would like to know how I would go about altering the HTML that
everyone. Recently, we have faced one problem about the speed of loading google map
Does anyone know how one would go about loading a Word document into a
How would I go about loading my own data onto the $user object upon
Say I have like: $(function() { $('a').click(function(e) { e.preventDefault(); var h = $(this).attr('href'); $('#content').load(h);
using a file I want to create a map and I am wondering about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.