|
From: João <lv...@us...> - 2009-12-09 04:29:41
|
This is an automated email from the git, the tree hashes are:
via 03fe38a92960e0df882a7dc16564deeb321b4014 (commit)
from df68da5eea203fc158360dc9efbf564361c04e3f (commit)
- Log -----------------------------------------------------------------
commit 03fe38a92960e0df882a7dc16564deeb321b4014
Author: João <jcorrea@marvim.(none)>
Date: Wed Dec 9 02:28:13 2009 -0200
Added multilayer background
diff --git a/code/background.py b/code/background.py
index 291f2f0..ad2caf7 100644
--- a/code/background.py
+++ b/code/background.py
@@ -2,8 +2,9 @@
# -*- coding: utf-8 -*-
#----------------------------------------------------------------------
-# Author:
+# Authors:
# Bruno Dilly <bru...@br...>
+# Joao Correa <jo...@li...>
#
# Copyright (C) 2009 Bruno Dilly
#
@@ -15,48 +16,109 @@ import pygame
from pygame.locals import *
from collections import deque
+class Image:
+ def __init__(self, image):
+ self.image = image
+ self.size = self.image.get_size()
+
+class Layer:
+ def __init__(self, image, width, layer):
+ self.default_image = image
+ self.pos = [0, 0]
+ self.width = width
+ self.layer = layer
+ self.L = deque()
+ self.w = width * image.size[0]
+ self.h = image.size[1]
+
+ def subLayer(self, screen):
+ s = screen.get_size()
+ self.pos = [0, s[1] - self.default_image.size[1]]
+
+ def move(self):
+ self.pos[0] -= 1
+
+ def build(self):
+ self.back = pygame.Surface((self.w, self.h), SRCALPHA)
+ # checks if the number of images is OK
+ while len(self.L) < self.width:
+ self.L.append(self.default_image)
+
+ lw = 0
+ self.back.blit(self.L[0].image, (0, self.default_image.size[1] - self.L[0].size[1]))
+ for i in range(1,self.width):
+ lw += self.L[i-1].size[0]
+ self.back.blit(self.L[i].image, (lw , self.h - self.L[i].size[1]))
+
+ self.screen = self.back
+
+ # remove the first image on the queue
+ self.L.popleft()
+
+
class Background:
"""
It's the animated game background created with tiles.
"""
image = None
- loaded_imgs = {}
pos = None
L = deque()
+ loaded_imgs = {}
+ ms = 0
+ size = [0, 0]
- def __init__(self, image):
+ def __init__(self, image0, image1):
"""
Creates a surface with tiles repeated until cover the entire screen.
"""
# to work "perfectly" all images in the tile must have the same size
- image = self.loadImage(image)
- self.default_image = image
- self.size = image.get_size()
- self.pos = [0, 0]
+ self.layer0 = Layer(Image(self.loadImage(image0)), 1, 0)
+ self.size = self.layer0.default_image.size
screen = pygame.display.get_surface()
screen_size = screen.get_size()
- from math import ceil
+ image = self.loadImage(image1)
+ size = image.get_size()
+ self.layer1 = Layer(Image(image), screen_size[0] / size[0] + 1, 1)
+ self.layer1.subLayer(screen)
- # ceil returns the ceiling of x as a float
- # we want the size of the screen plus a tile
- self.w = (ceil(float(screen_size[0]) / self.size[0]) + 1) * self.size[0]
- self.h = (ceil(float(screen_size[1]) / self.size[1]) + 1) * self.size[1]
+ self.layer0.build()
+ self.layer1.build()
- # creates a surface with width w and height h
- self.back = pygame.Surface((self.w, self.h))
+ def nextTile(self, next, layer):
+ """
+ Adds a tile to the queue and makes it default
+ """
+ self.layer1.default_image = Image(self.loadImage(next))
+ self.layer1.L.append(self.layer1.default_image)
+ #The tile added becomes the new default tile
- for i in range((self.back.get_size()[0]/self.size[0])):
- self.L.append(image)
+ def update(self, ms):
+ """
+ Moves the background to the left
+ """
+ self.ms += ms
+ if self.ms > 10:
+ self.ms = 0
+ self.layer1.move()
+ # when it reaches the end, moves the background for the start point
+ if (self.layer1.pos[0] <= -self.layer1.default_image.size[0]):
+
self.layer1.pos[0] += self.layer1.default_image.size[0]
+ self.layer1.build()
- self.build()
+ def draw(self,screen):
+ """
+ Draws the background
+ """
+ screen.blit(self.layer0.screen, (0,0))
+ screen.blit(self.layer1.screen, (self.layer1.pos))
def loadImage(self, image_path):
"""
Loads the image that is going to be used on the tile
"""
-
+
# tries to load from memory for better performance
# need to check if it is really better!
if image_path in self.loaded_imgs:
@@ -64,55 +126,8 @@ class Background:
else:
image = os.path.join('graphic', image_path)
# loads the image
- image = pygame.image.load(image)
- # disables alpha
- image.set_alpha(None, RLEACCEL)
- # converts the image so it won't be done every blit
- # it improves the performance
- image = image.convert()
+ image = pygame.image.load(image).convert_alpha()
+ # image is saved in memory, for no file reading
self.loaded_imgs[image_path] = image
return image
- def build(self):
- """
- Build the whole image in the background
- """
-
- # checks if the number of images is OK
- if len(self.L) < self.w:
- self.L.append(self.default_image)
-
- for i in range((self.back.get_size()[0]/self.size[0])):
- for j in range((self.back.get_size()[1]/self.size[1])):
- self.back.blit(self.L[i], (i * self.size[0], j * self.size[1]))
-
- self.image = self.back
-
- # remove the first image on the queue
- self.L.popleft()
-
-
- def nextTile(self, next):
- """
- Adds a tile to the queue
- """
- self.default_image = self.loadImage(next)
- #The tile added becomes the new default tile
- self.L.append(self.default_image)
-
- def update(self, dt):
- """
- Moves the background to the left
- """
- self.pos[0] -= 1
- # when it reaches the end, moves the background for the start point
- if (self.pos[0] < -self.size[0]):
- self.pos[0] += self.size[0]
- self.build()
-
- def draw(self,screen):
- """
- Draws the background
- """
- screen.blit(self.image, self.pos)
-
diff --git a/code/game.py b/code/game.py
index 4e983d8..8915c11 100644
--- a/code/game.py
+++ b/code/game.py
@@ -432,7 +432,7 @@ class Game:
powerup.set_pos(pos)
self.actors_list["powerups"].add(powerup)
elif element.type == "background":
- self.background.nextTile(element.image)
+ self.background.nextTile(element.image, 1)
def loop(self):
"""
@@ -445,7 +445,7 @@ class Game:
self.counter = 0
# creates the background
- self.background = Background("tile.png")
+ self.background = Background("earth.jpg", "321.png")
#starts clock
clock = pygame.time.Clock()
diff --git a/data/graphic/321.png b/data/graphic/321.png
new file mode 100644
index 0000000..4afebfc
Binary files /dev/null and b/data/graphic/321.png differ
diff --git a/data/graphic/earth.jpg b/data/graphic/earth.jpg
new file mode 100644
index 0000000..21fe013
Binary files /dev/null and b/data/graphic/earth.jpg differ
diff --git a/data/graphic/tiles/stars1.jpg b/data/graphic/tiles/stars1.jpg
index 4676437..6e4545d 100644
Binary files a/data/graphic/tiles/stars1.jpg and b/data/graphic/tiles/stars1.jpg differ
diff --git a/data/graphic/tiles/stars2.jpg b/data/graphic/tiles/stars2.jpg
index fcf09fb..6e4545d 100644
Binary files a/data/graphic/tiles/stars2.jpg and b/data/graphic/tiles/stars2.jpg differ
diff --git a/data/graphic/tiles/stars3.jpg b/data/graphic/tiles/stars3.jpg
index 94f52f1..e86475a 100644
Binary files a/data/graphic/tiles/stars3.jpg and b/data/graphic/tiles/stars3.jpg differ
-----------------------------------------------------------------------
Summary of changes:
code/background.py | 149 ++++++++++++++++++++++------------------
code/game.py | 4 +-
data/graphic/321.png | Bin 0 -> 10581 bytes
data/graphic/earth.jpg | Bin 0 -> 355487 bytes
data/graphic/tiles/stars1.jpg | Bin 1172 -> 1393 bytes
data/graphic/tiles/stars2.jpg | Bin 1263 -> 1393 bytes
data/graphic/tiles/stars3.jpg | Bin 2336 -> 2768 bytes
7 files changed, 84 insertions(+), 69 deletions(-)
create mode 100644 data/graphic/321.png
create mode 100644 data/graphic/earth.jpg
--
Dead Channel is a sci-fi shoot 'em up game developed using pygame.
|