0

I am making a platform game. So far I have a scrollable background and a grid(list) where I can input a number on that grid and the block will show up. However, I am trying to make these blocks scrollable so that they disappear when the character moves further in the game. For example, in Super Mario Bros the background and obstacles are both scrollable.

This is my code:

import pygame
from pygame.locals import *

pygame.init()




SCREEN_WIDTH = 879
SCREEN_HEIGHT = 597
LOWER_MARGIN = 0
SIDE_MARGIN = 0
level_limit = 6000
ROWS = 24
MAX_COLS = 110
tile_size = 35
scroll_right = False
scroll_left = False
scroll = 0
scroll_speed = 1


screen = pygame.display.set_mode((SCREEN_WIDTH + SIDE_MARGIN, SCREEN_HEIGHT + LOWER_MARGIN))
pygame.display.set_caption('Platformer')


#load images
pine1_img = pygame.image.load('/Users/gersh/PycharmProjects/Platformer/platformer/forest_tileset_lite/Sprites/Background/pine1.png').convert_alpha()
pine2_img = pygame.image.load('/Users/gersh/PycharmProjects/Platformer/platformer/forest_tileset_lite/Sprites/Background/pine2.png').convert_alpha()
sky_img = pygame.image.load('/Users/gersh/PycharmProjects/Platformer/platformer/forest_tileset_lite/Sprites/Background/sky_cloud.png').convert_alpha()
mountain = pygame.image.load('/Users/gersh/PycharmProjects/Platformer/platformer/forest_tileset_lite/Sprites/Background/mountain2.png').convert_alpha()


class World():
    def __init__(self, data):
        self.tile_list = []
        dirt_img = pygame.image.load('/Users/gersh/PycharmProjects/Platformer/platformer/tile/dirt1.png')
        blank_img = pygame.image.load('/Users/gersh/PycharmProjects/Platformer/platformer/tile/blank.png')
        row_count = 0
        for row in data:
            col_count = 0
            for tile in row:
                if tile == 1: #dirt
                    img = pygame.transform.scale(dirt_img, (tile_size, tile_size))
                    img_rect = img.get_rect()
                    img_rect.x = col_count * tile_size
                    img_rect.y = row_count * tile_size
                    tile = (img, img_rect)
                    self.tile_list.append(tile)
                if tile == 2: #blank
                    img = pygame.transform.scale(blank_img, (tile_size, tile_size))
                    img_rect = img.get_rect()
                    img_rect.x = col_count * tile_size
                    img_rect.y = row_count * tile_size
                    tile = (img, img_rect)
                    self.tile_list.append(tile)



                col_count += 1
            row_count += 1


    def draw(self):
        for tile in self.tile_list:
            screen.blit(tile[0], tile[1])



world_data = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,],
[2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,],
]


world = World(world_data)


def drawGrid():
    #vertical lines
    for c in range(MAX_COLS + 1):
        pygame.draw.line(screen, (255,255,255), (c * tile_size, 0), (c * tile_size, SCREEN_HEIGHT))
    for c in range(ROWS + 1):
        pygame.draw.line(screen, (255,255,255), (0,c * tile_size), (SCREEN_WIDTH, c * tile_size))

GREEN = (144, 201, 120)
WHITE = (255,255,255)
RED = (200, 25,25)
BLACK = (0,0,0)

def draw_bg():
    screen.fill(BLACK)
    width = sky_img.get_width()
    for x in range(100):
        screen.blit(sky_img, ((x * width) - scroll * 0.5,0))
        screen.blit(mountain, ((x * width) - scroll * 0.6, SCREEN_HEIGHT - mountain.get_height()-260))
        screen.blit(pine1_img, ((x * width) - scroll * 0.7, SCREEN_HEIGHT - pine1_img.get_height()-110))
        screen.blit(pine2_img, ((x * width) - scroll * 0.8, SCREEN_HEIGHT - pine2_img.get_height()+50))


run = True
while run == True:

    draw_bg()
    world.draw()
    if scroll_left == True and scroll > 0:
        scroll -= 7

    if scroll_right == True and scroll < level_limit:
        scroll += 7

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                scroll_left = True
            if event.key == pygame.K_RIGHT:
                scroll_right = True
            if event.key == pygame.K_RSHIFT:
                scroll_speed = 5
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                scroll_left = False
            if event.key == pygame.K_RIGHT:
                scroll_right = False
            if event.key == pygame.K_RSHIFT:
                scroll_speed = 1

    pygame.display.update()
pygame.quit()
Kingsley
  • 14,398
  • 5
  • 31
  • 53
gershinho
  • 33
  • 4

0 Answers0