Created
June 27, 2020 12:23
-
-
Save shorinji/88a7075945935cb7a7b1e819ee36e18b to your computer and use it in GitHub Desktop.
simple parallax scroll in pygame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import pygame | |
import math | |
from pygame.locals import * | |
TILE_SIZE = 16 | |
HORIZONTAL_TILES = 20 | |
VERTICAL_TILES = 10 | |
SCREEN_WIDTH = TILE_SIZE * HORIZONTAL_TILES | |
SCREEN_HEIGHT = TILE_SIZE * VERTICAL_TILES | |
BLACK = Color(0, 0, 0) | |
def getTile(tileset, tileX, tileY): | |
r = Rect(tileX * TILE_SIZE, tileY * TILE_SIZE, TILE_SIZE, TILE_SIZE) | |
return tileset.subsurface(r) | |
def createLayers(tileset): | |
layer1tile = getTile(tileset, 5, 1) | |
layer2tile = getTile(tileset, 5, 0) | |
layer3tile = getTile(tileset, 5, 2) | |
backgroundTile = getTile(tileset, 7, 3) | |
layer1 = pygame.Surface((SCREEN_WIDTH, TILE_SIZE), 0, screen) | |
layer2 = pygame.Surface((SCREEN_WIDTH, TILE_SIZE), 0, screen) | |
layer3 = pygame.Surface((SCREEN_WIDTH, TILE_SIZE), 0, screen) | |
background = pygame.Surface((SCREEN_WIDTH, TILE_SIZE), 0, screen) | |
for i in range(HORIZONTAL_TILES): | |
layer1.blit(layer1tile, (i * TILE_SIZE, 0)) | |
layer2.blit(layer2tile, (i * TILE_SIZE, 0)) | |
layer3.blit(layer3tile, (i * TILE_SIZE, 0)) | |
background.blit(backgroundTile, (i * TILE_SIZE, 0)) | |
return layer1, layer2, layer3, background | |
def handleEvents(): | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
sys.exit() | |
if event.type == pygame.KEYDOWN and event.key == K_q: | |
sys.exit() | |
pygame.init() | |
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT]) | |
# 128x224 px for ca 8x12 tiles | |
tileset = pygame.image.load("OverworldTileset_v03.png").convert() | |
layers = createLayers(tileset) | |
layer1surface, layer2surface, layer3surface, backgroundSurface = layers | |
layer1offset = 0 | |
layer2offset = 0 | |
layer3offset = 0 | |
backgroundOffset = 0 | |
while True: | |
# events | |
handleEvents() | |
# logic | |
layer1offset = (layer1offset - 4) % SCREEN_WIDTH | |
layer2offset = (layer2offset - 3) % SCREEN_WIDTH | |
layer3offset = (layer3offset - 2) % SCREEN_WIDTH | |
backgroundOffset = (backgroundOffset - 1) % SCREEN_WIDTH | |
# render | |
screen.blit(layer1surface, (layer1offset, 5 * TILE_SIZE)) | |
screen.blit(layer1surface, (layer1offset - SCREEN_WIDTH, 5 * TILE_SIZE)) | |
screen.blit(layer2surface, (layer2offset, 4 * TILE_SIZE)) | |
screen.blit(layer2surface, (layer2offset - SCREEN_WIDTH, 4 * TILE_SIZE)) | |
screen.blit(layer3surface, (layer3offset, 3 * TILE_SIZE)) | |
screen.blit(layer3surface, (layer3offset - SCREEN_WIDTH, 3 * TILE_SIZE)) | |
screen.blit(backgroundSurface, (backgroundOffset, 2 * TILE_SIZE)) | |
screen.blit(backgroundSurface, (backgroundOffset - SCREEN_WIDTH, 2 * TILE_SIZE)) | |
pygame.display.flip() | |
pygame.time.wait(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
depends on the image OverworldTileset_v03.png. It is not that important. Just replace it with your own image and change the dimensions