How to scroll the map in a 2d game with Pygame

I get this code from https://gamedev.stackexchange.com/questions/117678/how-to-figure-out-which-tiles-are-within-view-and-where-to-draw-them-in-the-gri

And I think it can be interesting for the people that wants to learn pygame.

import pygame
pygame.init()
#
display = pygame.display.set_mode((600,600))
clock = pygame.time.Clock()
#
scene = [
    "XXXXXXXXXXXXXXXXXXXXXX",
    "X--------------------X",
    "X--------------------X",
    "X--------------------X",
    "XXXXXXXX-------------X",
    "X--------------------X",
    "X------X-------------X",
    "X--XX--XXXXXXXXXXXXXXX",
    "X--XX--X-------------X",
    "X--------------------X",
    "X------X-------------X",
    "XXXXXXXXXXXXXXXXXXXXXX"]
#
mappa = pygame.Surface((len(scene[0])*64,len(scene)*64)) 
x,y = 0,0
for row in scene:
    for tile in row:
        if tile in "-":
            pygame.draw.rect(mappa,(0,155,0),((x,y),(64,64)))
        elif tile in "X":
            pygame.draw.rect(mappa,(125,125,125),((x,y),(64,64)))
        else:
            pygame.draw.rect(mappa,(255,128,122),((x,y),(64,64)))
        x += 64
    y += 64
    x = 0
#
class Player:
    def __init__(self):
        self.image = pygame.Surface((32,32))
        self.image.fill((255,0,0))
        self.rect = pygame.Rect((284,284),(32,32))
        self.map_pos = (0,0)
        self.moveBox = (100,100,500,500)
    def move(self):
        mx,my = self.map_pos
        key = pygame.key.get_pressed()
        if key[pygame.K_w]:
            self.rect.y -= 8
        if key[pygame.K_a]:
            self.rect.x -= 8
        if key[pygame.K_s]:
            self.rect.y += 8
        if key[pygame.K_d]:
            self.rect.x += 8
        if player.rect.x <= self.moveBox[0]:
            self.rect.x += 8
            mx += 8
        elif player.rect.x >= self.moveBox[2]-32:
            self.rect.x -= 8
            mx -= 8
        if player.rect.y <= self.moveBox[1]:
            self.rect.y += 8
            my += 8
        elif player.rect.y >= self.moveBox[3]-32:
            self.rect.y -= 8
            my -= 8
        self.map_pos = (mx,my)
    def render(self,display):
        display.blit(self.image,(self.rect.x,self.rect.y))
#
player = Player()
#
RUNNING = True
while RUNNING:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            RUNNING = False
    #
    player.move()
    #
    display.fill((0,155,0))
    display.blit(mappa,player.map_pos)
    player.render(display)
    #
    pygame.display.flip()
#
pygame.quit()

Video


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.