|
From: João <lv...@us...> - 2009-12-14 05:06:02
|
This is an automated email from the git, the tree hashes are:
via c6138743a73a2e531aa9347b5e4e4139d488154b (commit)
from b844710174d54bf78f72e45bf017350c0aa3c525 (commit)
- Log -----------------------------------------------------------------
commit c6138743a73a2e531aa9347b5e4e4139d488154b
Author: João <jcorrea@marvim.(none)>
Date: Mon Dec 14 03:05:18 2009 -0200
Forgot to add foreground.py =)
diff --git a/code/foreground.py b/code/foreground.py
new file mode 100644
index 0000000..e3b88ff
--- /dev/null
+++ b/code/foreground.py
@@ -0,0 +1,125 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+#----------------------------------------------------------------------
+# Authors:
+# Joao Correa <jo...@li...>
+#
+# Copyright (C) 2009 João Correa
+#
+# Released under GNU GPL, read the file 'COPYING' for more information
+# ----------------------------------------------------------------------
+
+# Suggestions to be discussed:
+# Blitting directly to the screen, instead of blitting twice
+# We are not moving layers here, so there is no need to blit twice
+# Should we really not use moving layers here?
+
+import os
+import pygame
+from pygame.locals import *
+from collections import deque
+
+class Image:
+ """
+ A simple image that goes on the Layer
+ """
+ def __init__(self, image, x, y):
+ self.image = image
+ self.size = self.image.get_size()
+ self.pos = [x, int(y)]
+
+class Layer:
+ """
+ It is the foreground Layer
+ """
+ def __init__(self, w, h, speed):
+ self.L = deque()
+ self.w = w
+ self.h = h
+ self.speed = speed
+
+ def move(self):
+ """
+ Moves the layer
+ """
+ for i in range(0, len(self.L)):
+ self.L[i].pos[0] -= self.speed
+ # if the image reaches the end of the screen, pop it!
+ if self.L[i].pos[0] == -self.L[i].size[0]:
+ self.L.popleft()
+ self.build()
+
+ def build(self):
+ """
+ This function builds the foreground layer based on the queue of images L
+ """
+ foreg = pygame.Surface((self.w, self.h), SRCALPHA)
+ # Put all foreground images on screen
+ for i in range(0, len(self.L)):
+ foreg.blit(self.L[i].image, (self.L[i].pos[0], self.L[i].pos[1]))
+
+ self.screen = foreg
+
+ def addImage(self, image, y):
+ """
+ Just adds a new image to the queue
+ """
+ self.L.append(Image(image, self.w, y))
+
+class Foreground:
+ """
+ It's the animated game foreground.
+ Images on this object goes above the game chars.
+ """
+ loaded_imgs = {}
+ ms = 0
+ size = [0, 0]
+
+ def __init__(self):
+ """
+ Creates the layer.
+ """
+ screen = pygame.display.get_surface()
+ screen_size = screen.get_size()
+ self.layer = Layer(screen_size[0], screen_size[1], 10)
+ self.layer.build()
+
+ def addForeg(self, image, y):
+ """
+ Includes a new image to the layer.
+ """
+ self.layer.addImage(self.loadImage(image), y)
+
+ def update(self, ms):
+ """
+ Moves the images on the layer.
+ The layer is not moved, but the objects inside.
+ """
+ self.ms += ms
+ if self.ms > 10:
+ self.ms = 0
+ self.layer.move()
+
+ def draw(self,screen):
+ """
+ Draws the foreground
+ """
+ screen.blit(self.layer.screen, (0, 0))
+
+ def loadImage(self, image_path):
+ """
+ Loads the image that is going to be used
+ """
+ # tries to load from memory for better performance
+ # need to check if it is really better!
+ if image_path in self.loaded_imgs:
+ image = self.loaded_imgs[image_path]
+ else:
+ image = os.path.join('graphic', image_path)
+ # loads the image
+ image = pygame.image.load(image).convert_alpha()
+ # image is saved in memory, for no file reading
+ self.loaded_imgs[image_path] = image
+ return image
+
-----------------------------------------------------------------------
Summary of changes:
code/foreground.py | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 125 insertions(+), 0 deletions(-)
create mode 100644 code/foreground.py
--
Dead Channel is a sci-fi shoot 'em up game developed using pygame.
|