Tested the BulletActor class.
This commit is contained in:
33
actor.py
33
actor.py
@@ -6,12 +6,13 @@ import math
|
|||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
import math_utils
|
import math_utils
|
||||||
|
import game
|
||||||
|
|
||||||
ACTOR_STATES = { 'IDLE': 0, 'MOVING': 1 }
|
ACTOR_STATES = { 'IDLE': 0, 'MOVING': 1 }
|
||||||
|
|
||||||
class BaseActor(pygame.sprite.Sprite):
|
class BaseActor(pygame.sprite.Sprite):
|
||||||
def __init__(self, id, image, name = "Default", animated = False, visible = True, solid = True):
|
def __init__(self, id, image, name = "Default", animated = False, visible = True, solid = True):
|
||||||
super(Sprite, self).__init__()
|
pygame.sprite.Sprite.__init__(self)
|
||||||
|
|
||||||
self.id = id
|
self.id = id
|
||||||
self.name = name
|
self.name = name
|
||||||
@@ -51,7 +52,7 @@ class BaseActor(pygame.sprite.Sprite):
|
|||||||
self.position = list(new_pos)
|
self.position = list(new_pos)
|
||||||
|
|
||||||
def get_velocity(self):
|
def get_velocity(self):
|
||||||
return self.get_velocity
|
return self.velocity
|
||||||
|
|
||||||
def set_velocity(self, new_vel):
|
def set_velocity(self, new_vel):
|
||||||
self.velocity = list(new_vel)
|
self.velocity = list(new_vel)
|
||||||
@@ -76,10 +77,10 @@ class BaseActor(pygame.sprite.Sprite):
|
|||||||
|
|
||||||
class BulletActor(BaseActor):
|
class BulletActor(BaseActor):
|
||||||
""" Actor class with fixed velocity bullet behavior. """
|
""" Actor class with fixed velocity bullet behavior. """
|
||||||
def __init__(self, id, image, name = "Default", animated = False, visible = True, solid = True, frame_rate = 60):
|
def __init__(self, id, image, name = "Default", animated = False, visible = True, solid = True, frame_rate = 60.0):
|
||||||
super(BaseActor, self).__init__(id, image, name, animated, visible, solid)
|
BaseActor.__init__(self, id, image, name, animated, visible, solid)
|
||||||
self.then = 0
|
self.then = pygame.time.get_ticks()
|
||||||
self.now = 0
|
self.now = pygame.time.get_ticks()
|
||||||
self.frame_rate = frame_rate
|
self.frame_rate = frame_rate
|
||||||
self.moving = False
|
self.moving = False
|
||||||
|
|
||||||
@@ -99,13 +100,21 @@ class BulletActor(BaseActor):
|
|||||||
if delta_t < 0:
|
if delta_t < 0:
|
||||||
delta_t = 0 # Compensatefor overflow of self.now
|
delta_t = 0 # Compensatefor overflow of self.now
|
||||||
|
|
||||||
|
if game.DEBUG:
|
||||||
|
print
|
||||||
|
print "Bullet actor: " + self.name
|
||||||
|
print "THEN: " + str(self.then) + " :: NOW: " + str(self.now) + " :: DELTA: " + str(delta_t)
|
||||||
|
|
||||||
if self.moving:
|
if self.moving:
|
||||||
# Then we update it's velocity components compensating for time.
|
if game.DEBUG:
|
||||||
self.velocity[0] += (self.velocity[0] * delta_t) * (self.frame_rate / 1000)
|
print "NEW VEL X: " + str((self.velocity[0] * delta_t) * (self.frame_rate / 1000))
|
||||||
self.velocity[1] += (self.velocity[1] * delta_t) * (self.frame_rate / 1000)
|
print "NEW VEL Y: " + str((self.velocity[1] * delta_t) * (self.frame_rate / 1000))
|
||||||
# Finally we take friction into account.
|
self.position[0] += (self.velocity[0] * delta_t) * (self.frame_rate / 1000)
|
||||||
self.velocity[0] *= self.friction
|
self.position[1] += (self.velocity[1] * delta_t) * (self.frame_rate / 1000)
|
||||||
self.velocity[1] *= self.friction
|
self.rect.center = (self.position[0], self.position[1])
|
||||||
|
|
||||||
|
if game.DEBUG:
|
||||||
|
print "NEW POSITION: " + str(self.position)
|
||||||
|
|
||||||
# TODO: Update animation frame if any.
|
# TODO: Update animation frame if any.
|
||||||
|
|
||||||
|
4
game.py
4
game.py
@@ -14,8 +14,8 @@ from ingame import InGameState
|
|||||||
from score import ScoreState
|
from score import ScoreState
|
||||||
from notvalidstate import NotValidState
|
from notvalidstate import NotValidState
|
||||||
|
|
||||||
# Debug constant. Comment to turn off debugging messages.
|
# Debug constant. Set to False to turn off debugging messages.
|
||||||
DEBUG = True
|
DEBUG = False
|
||||||
|
|
||||||
# The Game class implements the state machine of the game and
|
# The Game class implements the state machine of the game and
|
||||||
# runs the main game loop.
|
# runs the main game loop.
|
||||||
|
35
intro.py
35
intro.py
@@ -9,6 +9,8 @@ except ImportError:
|
|||||||
android = None
|
android = None
|
||||||
|
|
||||||
from state import BaseState, VALID_STATES
|
from state import BaseState, VALID_STATES
|
||||||
|
from actor import BulletActor
|
||||||
|
import game
|
||||||
|
|
||||||
class IntroState(BaseState):
|
class IntroState(BaseState):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -18,6 +20,21 @@ class IntroState(BaseState):
|
|||||||
self.next_transition = VALID_STATES['STAY']
|
self.next_transition = VALID_STATES['STAY']
|
||||||
self.background_color = (139, 210, 228)
|
self.background_color = (139, 210, 228)
|
||||||
|
|
||||||
|
image = pygame.image.load('gfx/burbuja.png')
|
||||||
|
self.sine_movement = BulletActor(0, image, "SineMovement", False, True, False)
|
||||||
|
self.sine_movement.set_position([-20, pygame.display.Info().current_h / 2])
|
||||||
|
# The next line calculates the horizontal velocity of sine_movement.
|
||||||
|
# We want it to cover the width of the screen in 20 seconds. We divide
|
||||||
|
# by 60 to obtain the speed in pixels per frame.
|
||||||
|
x_velocity = (float(pygame.display.Info().current_w) / 20.0) / 60.0
|
||||||
|
self.sine_movement.set_velocity([0.5, 0])
|
||||||
|
self.sine_movement.move()
|
||||||
|
|
||||||
|
if game.DEBUG:
|
||||||
|
print "Velocity: " + str(self.sine_movement.get_velocity())
|
||||||
|
print "Position: " + str(self.sine_movement.get_position())
|
||||||
|
print self.sine_movement.is_moving()
|
||||||
|
|
||||||
def input(self):
|
def input(self):
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if android:
|
if android:
|
||||||
@@ -31,15 +48,21 @@ class IntroState(BaseState):
|
|||||||
self.next_transition = VALID_STATES['QUIT']
|
self.next_transition = VALID_STATES['QUIT']
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
|
self.sine_movement.update()
|
||||||
|
if game.DEBUG:
|
||||||
|
print
|
||||||
|
print "NEXT UPDATE"
|
||||||
|
print "Velocity: " + str(self.sine_movement.get_velocity())
|
||||||
|
print "Position: " + str(self.sine_movement.get_position())
|
||||||
|
print self.sine_movement.is_moving()
|
||||||
if self.next_transition != VALID_STATES['QUIT']:
|
if self.next_transition != VALID_STATES['QUIT']:
|
||||||
if self.count < 120:
|
sm_position = self.sine_movement.get_position()
|
||||||
self.count += 1
|
if sm_position[0] > pygame.display.Info().current_w + 30:
|
||||||
self.next_transition = VALID_STATES['STAY']
|
|
||||||
else:
|
|
||||||
self.count = 0
|
|
||||||
self.next_transition = VALID_STATES['MENU']
|
self.next_transition = VALID_STATES['MENU']
|
||||||
|
else:
|
||||||
|
self.next_transition = VALID_STATES['STAY']
|
||||||
return self.next_transition
|
return self.next_transition
|
||||||
|
|
||||||
def render(self, canvas):
|
def render(self, canvas):
|
||||||
canvas.fill(self.background_color)
|
canvas.fill(self.background_color)
|
||||||
pygame.draw.rect(canvas, (255, 255, 255), self.rectangle)
|
canvas.blit(self.sine_movement.image, self.sine_movement.rect)
|
||||||
|
Reference in New Issue
Block a user