Added particle system to IntroState. Fixed image points in BaseActor.

This commit is contained in:
2013-01-10 14:31:49 -04:30
parent 5482a508ff
commit 3fa5f21a9d
3 changed files with 19 additions and 12 deletions

View File

@@ -82,10 +82,8 @@ class BaseActor(pygame.sprite.Sprite):
if point < 0 or point > len(self.image_points):
return (0, 0)
else:
return self.image_points[point]
def set_image_point_tuple(self, point):
self.image_points.append(point)
im_point = [self.image_points[point][0] + self.rect.left, self.image_points[point][1] + self.rect.top]
return im_point
def set_image_point_xy(self, point_x, point_y):
self.image_points.append((point_x, point_y))

View File

@@ -13,6 +13,7 @@ except ImportError:
from state import BaseState, VALID_STATES
import actor
import game
import particle
class IntroState(BaseState):
def __init__(self):
@@ -35,10 +36,14 @@ class IntroState(BaseState):
image2 = pygame.image.load('gfx/submarino1.png')
self.submarine = actor.BaseActor(1, image2, "Submarine", True, True, False)
self.submarine.set_image_point_tuple((117, 284))
self.submarine.set_image_point_xy(117, 284)
# Instert second animation frame of the subamrine.
# Create the particle system.
self.particle_system = particle.ParticleSystem(0, "Bubbles", 'gfx/burbuja.png', 1000, 1000, 1, -130.0)
self.particle_system.set_friction(1.0)
self.particle_system.set_gravity([0.0, -60.0])
self.particle_system.set_max_velocity(5.0)
self.particle_system.start()
if game.DEBUG:
print "Velocity: " + str(self.sine_movement.get_velocity())
@@ -66,6 +71,8 @@ class IntroState(BaseState):
self.sine_movement.update()
sm_position = self.sine_movement.get_position()
self.submarine.set_position([sm_position[0], self.screen_vertical_half + math.sin(0.05 * float(sm_position[0])) * 42.0])
self.particle_system.set_position(self.submarine.get_image_point(0))
self.particle_system.update()
if game.DEBUG:
print
@@ -84,3 +91,4 @@ class IntroState(BaseState):
def render(self, canvas):
canvas.fill(self.background_color)
self.submarine.draw(canvas)
self.particle_system.draw(canvas)

View File

@@ -7,7 +7,6 @@ import random
import pygame
import math_utils
import game
class Particle(pygame.sprite.Sprite):
def __init__(self, lifespan, scale, texture, gravity = [0.0, 9.8], position = [0,0], initial_vel = [100.0, 100.0], friction = 1.0, frame_rate = 60.0):
@@ -91,9 +90,10 @@ class ParticleSystem:
self.part_creation_accum = 0.0
self.then = pygame.time.get_ticks()
self.gravity = [0.0, -60.0]
self.gravity = [0.0, 9.8]
self.position = [pygame.display.Info().current_w / 2, pygame.display.Info().current_h / 2]
self.initial_velocity_max = 50 # Pixels per second.
self.friction = 0.99
self.frame_rate = 60.0
self.rectangle = pygame.Rect(0, 0, 25, 25)
@@ -122,6 +122,9 @@ class ParticleSystem:
def set_angle(self, angle):
self.angle = angle
def set_friction(self, friction):
self.friction = friction
def update(self):
# Calculate the time delta.
now = pygame.time.get_ticks()
@@ -146,12 +149,12 @@ class ParticleSystem:
float(random.randrange(-self.initial_velocity_max, self.initial_velocity_max))]
particle = Particle(
int(self.lifespan),
random.random(),
max(min(random.random(), 1.0), 0.5),
self.texture,
list(self.gravity),
list(self.position),
velocity,
0.99,
self.friction,
int(self.frame_rate))
self.particles.add(particle)
self.part_creation_accum = 0.0
@@ -164,7 +167,5 @@ class ParticleSystem:
self.then = now
def draw(self, canvas):
if game.DEBUG:
pygame.draw.rect(canvas, (255, 255, 0), self.rectangle)
for particle in self.particles:
particle.draw(canvas)