Pythonista game faster V – Laser!

Let’s continue our game on Ipad with Pythonista. This time we are going to add the laser to our ship. We can fire, but we cannot destroy our enemies yet, because there is no collision detection among the lasers and the alien ships. This will be done in the next epidode.

The code:

We add an attribute to the setup method and the collision method of the class Game

# we add self.alive to the setup method
def setup(self):
    ...
    self.alive = 1

# we add the new attribute to the collisions method too

def collisions(self):
    ...
    ...
        ....
        self.alive = 0 # when there is a collision

then we add another method to create the lasers when we touche the ipad screen:

def touch_began(self, touch):
    if self.alive: # if the ship is destroyed it won't shoot
        self.sprite = SpriteNode('pzl:BallBlue',
            position = self.ship.position,
            z_position = -1,
            parent = self)
        self.laser.run_action(
            Action.sequence(
                Action.move_by(0, self.size.h),
                Action.remove()))
        sound.play_effect('arcade:Laser_1, 0.2)


 

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.