Added particle system to IntroState. Fixed image points in BaseActor.
This commit is contained in:
6
actor.py
6
actor.py
@@ -82,10 +82,8 @@ class BaseActor(pygame.sprite.Sprite):
|
|||||||
if point < 0 or point > len(self.image_points):
|
if point < 0 or point > len(self.image_points):
|
||||||
return (0, 0)
|
return (0, 0)
|
||||||
else:
|
else:
|
||||||
return self.image_points[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_tuple(self, point):
|
|
||||||
self.image_points.append(point)
|
|
||||||
|
|
||||||
def set_image_point_xy(self, point_x, point_y):
|
def set_image_point_xy(self, point_x, point_y):
|
||||||
self.image_points.append((point_x, point_y))
|
self.image_points.append((point_x, point_y))
|
||||||
|
12
intro.py
12
intro.py
@@ -13,6 +13,7 @@ except ImportError:
|
|||||||
from state import BaseState, VALID_STATES
|
from state import BaseState, VALID_STATES
|
||||||
import actor
|
import actor
|
||||||
import game
|
import game
|
||||||
|
import particle
|
||||||
|
|
||||||
class IntroState(BaseState):
|
class IntroState(BaseState):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -35,10 +36,14 @@ class IntroState(BaseState):
|
|||||||
|
|
||||||
image2 = pygame.image.load('gfx/submarino1.png')
|
image2 = pygame.image.load('gfx/submarino1.png')
|
||||||
self.submarine = actor.BaseActor(1, image2, "Submarine", True, True, False)
|
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.
|
# 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:
|
if game.DEBUG:
|
||||||
print "Velocity: " + str(self.sine_movement.get_velocity())
|
print "Velocity: " + str(self.sine_movement.get_velocity())
|
||||||
@@ -66,6 +71,8 @@ class IntroState(BaseState):
|
|||||||
self.sine_movement.update()
|
self.sine_movement.update()
|
||||||
sm_position = self.sine_movement.get_position()
|
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.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:
|
if game.DEBUG:
|
||||||
print
|
print
|
||||||
@@ -84,3 +91,4 @@ class IntroState(BaseState):
|
|||||||
def render(self, canvas):
|
def render(self, canvas):
|
||||||
canvas.fill(self.background_color)
|
canvas.fill(self.background_color)
|
||||||
self.submarine.draw(canvas)
|
self.submarine.draw(canvas)
|
||||||
|
self.particle_system.draw(canvas)
|
||||||
|
13
particle.py
13
particle.py
@@ -7,7 +7,6 @@ import random
|
|||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
import math_utils
|
import math_utils
|
||||||
import game
|
|
||||||
|
|
||||||
class Particle(pygame.sprite.Sprite):
|
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):
|
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.part_creation_accum = 0.0
|
||||||
self.then = pygame.time.get_ticks()
|
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.position = [pygame.display.Info().current_w / 2, pygame.display.Info().current_h / 2]
|
||||||
self.initial_velocity_max = 50 # Pixels per second.
|
self.initial_velocity_max = 50 # Pixels per second.
|
||||||
|
self.friction = 0.99
|
||||||
self.frame_rate = 60.0
|
self.frame_rate = 60.0
|
||||||
|
|
||||||
self.rectangle = pygame.Rect(0, 0, 25, 25)
|
self.rectangle = pygame.Rect(0, 0, 25, 25)
|
||||||
@@ -122,6 +122,9 @@ class ParticleSystem:
|
|||||||
def set_angle(self, angle):
|
def set_angle(self, angle):
|
||||||
self.angle = angle
|
self.angle = angle
|
||||||
|
|
||||||
|
def set_friction(self, friction):
|
||||||
|
self.friction = friction
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
# Calculate the time delta.
|
# Calculate the time delta.
|
||||||
now = pygame.time.get_ticks()
|
now = pygame.time.get_ticks()
|
||||||
@@ -146,12 +149,12 @@ class ParticleSystem:
|
|||||||
float(random.randrange(-self.initial_velocity_max, self.initial_velocity_max))]
|
float(random.randrange(-self.initial_velocity_max, self.initial_velocity_max))]
|
||||||
particle = Particle(
|
particle = Particle(
|
||||||
int(self.lifespan),
|
int(self.lifespan),
|
||||||
random.random(),
|
max(min(random.random(), 1.0), 0.5),
|
||||||
self.texture,
|
self.texture,
|
||||||
list(self.gravity),
|
list(self.gravity),
|
||||||
list(self.position),
|
list(self.position),
|
||||||
velocity,
|
velocity,
|
||||||
0.99,
|
self.friction,
|
||||||
int(self.frame_rate))
|
int(self.frame_rate))
|
||||||
self.particles.add(particle)
|
self.particles.add(particle)
|
||||||
self.part_creation_accum = 0.0
|
self.part_creation_accum = 0.0
|
||||||
@@ -164,7 +167,5 @@ class ParticleSystem:
|
|||||||
self.then = now
|
self.then = now
|
||||||
|
|
||||||
def draw(self, canvas):
|
def draw(self, canvas):
|
||||||
if game.DEBUG:
|
|
||||||
pygame.draw.rect(canvas, (255, 255, 0), self.rectangle)
|
|
||||||
for particle in self.particles:
|
for particle in self.particles:
|
||||||
particle.draw(canvas)
|
particle.draw(canvas)
|
||||||
|
Reference in New Issue
Block a user