Subscribe to MentalFloss - PyBlog        RSS Feed

PygPlane - Taking Stock

Icon Leave Comment
So, I figured since I'm not really implementing anything interesting that I haven't already covered in earlier posts to some degree, that I'd just take a minute to go over what's done.

I have implemented the following:

  • Ocean background that is animated and scrolls forward.
  • Get Ready wait-screen for a death (which might be off-putting and might be removed)
  • Game Over screen
  • Boat with a turret. The boat shoots forward but the turret does not shoot yet. The turret tracks player.
  • The olive, white, green, and blue plane are initially added. They spawn and fly and die but lack interesting flight paths or anything.
  • Planes explode when hit.
  • Four different bullet patterns. The others are a bit tricky and I figured I'd wait on them
  • Very sloppily added wingmen. They are just hard-coded in but they do shoot so that's something.
  • A score system that works but scoring is not actually being done. I'm currently using it as an update counter as it increments per update call.
  • A health bar that decreases when player takes damage (although all collision against player is not in game right now)
  • Power-up is barely added. Does not actually work yet in terms of collision and it meaning something.
  • Added bigger plane that's supposed to strafe left and right and shoot you. Currently it just goes to top of screen exits stage left.
  • Submarine is almost implemented. Worked on it tonight and so far it's going pretty good.

What still needs to get done is a huge list but I'll just say what I want to work on right now.

First, I want to get every enemy on the sprite sheet at least in the game and moving around. I'm almost there but I still have more to do. Next, I want to have some semblance of a level at least. I also want to get the collision in for everything.

I've only been at this game for 20 days or so and barely at that. I'm feeling really good about the progress but I know there's tons to do still.

I gotta say that I really enjoy pygame and I encourage anyone who's moderately interested to give it a shot.

That's all for now. Here's the sub currently:

import pygame
from pygame.locals import *
from utility import *
from particles import Explosion

X = 0
Y = 1
class Submarine(object):
    States: ACTIVE = able to attack and fully exposed.
            HIDDEN = Not running DIVING or RISING anim but is alive. No attack.
            DIVING = Vulnerable to attack but cannot attack. Run diving anim.
            RISING = Vulnerable to attack but cannot attack. Run rising anim.
            EXPLODING = Dead but has not exploded. Run explode anim.
            EXPLODED = Dead and has exploded. Remove from game.
    1. Submarine can explode when submerged. Not a problem to me.
    2. DIVING state can be re-entered when dived. Take care with AI
    3. RISING state can be re-entered when active. Take care with AI

    def __init__(self, screen, location):
        self.images = [ get_image('submarine', 0), # ACTIVE
                        get_image('submarine', 1), # 1/5 submerge
                        get_image('submarine', 2), # 2/5 submerge
                        get_image('submarine', 3), # 3/5 submerge
                        get_image('submarine', 4), # 4/5 submerge
                        get_image('submarine', 5)  # 5/5 submerge
        self.img_x, self.img_y = self.images[0].get_size()
        self.location = location
        self.screen = screen
        #  1-----SUBMERGING------------->--------------|5
        #  |-------------------<---------EMERGING-------
        self.frame  = 0
        self.delay  = 10
        self.image = self.images[self.frame]
        self.state = Submarine.ACTIVE
        self.explosion = None
    def get_center(self):
        return (self.location[X] + self.img_x/2, self.location[Y] + self.img_y/2)
    def _explode(self):
        self.explosion = Explosion(self.screen,
                              max_power = self.img_y/7,
                              max_radius = self.img_y/3)
    def _dive(self):
        Set the state to Submarine.DIVING and load the animation sequence
        accordingly if this was the first call.
        if self.state == Submarine.HIDDEN:
        if not self.state == Submarine.DIVING:
            self.state = Submarine.DIVING
            # the first frame of dive animation means do no more work yet.
            self.frame = 1
        # zero index and last frame for submerging.
        # HIDDEN is still alive but just invulnerable.
        if self.frame >= 4:
            self.state = Submarine.HIDDEN
            # we still have more diving to do.
            self.frame += 1
    def _rise(self):
        Set state to Submarine.RISING and load animation sequence accordingly
        if this was the first call.
        if self.state == Submarine.ACTIVE:
        if not self.state == Submarine.RISING:
            self.state = Submarine.RISING
            # the last frame of dive animation. Rising works backwards.
            self.frame = 4
        # An active sub is vulnerable and will attack.    
        if self.frame < 1:
            self.state = Submarine.ACTIVE
            self.frame -= 1
    def update(self):
        if self.state == Submarine.EXPLODING:
            if self.explosion is None:
        # animation delays...
        self.delay -= 1
        if self.delay == 0:
            self.delay = 10
            # here is where the sub will 'decide' to dive or rise.
            # for now, player controlled.
            if self.state == Submarine.RISING:
                print 'submarine rising'
            elif self.state == Submarine.DIVING:
                print 'submarine diving'
            elif self.state == Submarine.HIDDEN:
                print 'submarine is hidden'
            elif self.state == Submarine.ACTIVE:
                print 'submarine ready for combat'
            self.image = self.images[self.frame]
    def display(self):
        if self.state == Submarine.EXPLODING:
            if self.explosion.is_alive == False:
                self.state = Submarine.EXPLODED
        if self.state not in (Submarine.HIDDEN, 
            self.screen.blit(self.image, (self.location[X], self.location[Y]))
        # just for debugging...    
        if self.state == Submarine.HIDDEN:
            sub = pygame.Surface((self.img_x, self.img_y))
            sub.fill((2, 73, 120))
            self.screen.blit(sub, (self.location[X], self.location[Y]))
def main():
    screen = pygame.display.set_mode((400,400))
    init() # utility init
    submarine = Submarine(screen, (200,200))
    clock = pygame.time.Clock()
    in_game = True
    while in_game:
        for event in pygame.event.get():
            if event.type == QUIT:
                in_game = False
            if event.type == KEYDOWN:
                if event.key == K_d:
                    submarine.state = Submarine.DIVING
                if event.key == K_r:
                    submarine.state = Submarine.RISING
                if event.key == K_s:
                    submarine.state = Submarine.EXPLODING

if __name__ == '__main__':

0 Comments On This Entry


Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

May 2015

24 252627282930


    Recent Entries

    Recent Comments

    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)