It's a proof of concept bit of code for creating 2d parralax scrolling.
<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/mVdMCNE_8xA&hl ... ram><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/mVdMCNE_8xA&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>[/youtube]
For those interested the code is here.
Code: Select all
import pygame
class BG_Item(pygame.sprite.Sprite):
def __init__(self,size=(64,64),position=(0,0),colour=(0,0,0),velocity=(0,0)):
## Call the __init__ function from the inherited class
pygame.sprite.Sprite.__init__(self)
## Save a rect of the screen.
self.screen = pygame.display.get_surface().get_rect()
## Create a surface to draw upon.
self.obj = pygame.Surface(size)
## Fill in the surface with a colour
self.obj.fill(colour)
## Create a rect from the size of our surface.
self.rect = self.obj.get_rect()
## Set the position of our rect on the display
self.rect.x = position[0]
self.rect.y = position[1]
## Create a variable for the old position of our rect.
## We will use this to fill it in with the background colour/details.
self.old_rect = (0,0,0,0)
##Set the speed at which the Surface scrolls
self.velocity = velocity
def update(self,amount):
self.old = self.rect
self.rect = self.rect.move(amount)
if (self.rect.x + self.rect.width) <= 0:
self.rect.x = self.rect.width + 400
RESOLUTION = (400,300)
pygame.init()
screen = pygame.display.set_mode(RESOLUTION,pygame.DOUBLEBUF)
pygame.key.set_repeat(1,1)
sky = BG_Item(RESOLUTION,(0,0),(0,0,200))
sun = BG_Item((48,48),(320,16),(200,200,0),(-1,0))
ground = BG_Item((400,64),(0,236),(0,230,0),(0,0))
cloud1 = BG_Item((32,16),(368,32),(240,240,240),(-3,0))
cloud2 = BG_Item((64,32),(232,64),(240,240,240),(-4,0))
cloud3 = BG_Item((128,64),(96,128),(240,240,240),(-5,0))
back_building1 = BG_Item((128,64),(0,180),(180,180,180),(-2,0))
back_building2 = BG_Item((128,96),(142,180-32),(180,180,180),(-2,0))
building1 = BG_Item((160,96),(0,204),(50,50,50),(-7,0))
building2 = BG_Item((160,128),(200,172),(50,50,50),(-7,0))
building3 = BG_Item((160,160),(380 ,300-160),(50,50,50),(-7,0))
objects = [
sky,sun,
ground,
back_building1,
back_building2,
cloud1,
cloud2,
cloud3,
building1,
building2,
building3,
]
def blit_to_screen():
for object in objects:
screen.blit(object.obj,object.rect)
def move_all():
for object in objects:
object.update(object.velocity)
blit_to_screen()
pygame.display.update()
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if pygame.key.get_pressed()[pygame.K_LEFT]:
move_all()
blit_to_screen()
clock.tick(30)
pygame.display.update()