Skip to content

Tutorial: Space Shooter

Hallucino edited this page May 15, 2017 · 3 revisions

Space Shooter

You could find the original tutorial here.

I converted everything to the Python support and I added new chapters.

Draw ship on screen

Source

class Ship(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

ship = None

def _init():
    global ship
    ship = Ship(x=60,y=60)

def _update():
    pass

def _draw():
    cls()
    spr(1,ship.x,ship.y)

Animate propulsion

Source

class Ship(object):
    def __init__(self, x, y, sp):
        self.x = x
        self.y = y
        self.sp = sp

T=0
ship = None

def _init():
 global ship
 ship = Ship(sp=1,x=60,y=60)


def _update():
 global T, ship

 T=T+1
 if(T%6<3):
  ship.sp=1
 else:
  ship.sp=2


def _draw():
 global ship
 cls()
 spr(ship.sp,ship.x,ship.y)

Basic ship movement

Source

class Ship(object):
    def __init__(self, x, y, sp):
        self.x = x
        self.y = y
        self.sp = sp

T=0
ship = None

def _init():
    global ship
    ship = Ship(sp=1,x=60,y=60)


def _update():
    global T, ship

    T=T+1
    if(T%6<3):
        ship.sp=1
    else:
        ship.sp=2

    if btn(0):
        ship.x-=1
    if btn(1):
        ship.x+=1
    if btn(2):
        ship.y-=1
    if btn(3):
        ship.y+=1

def _draw():
    global ship
    cls()
    spr(ship.sp,ship.x,ship.y)

Fire laser bullets

Source

class Ship(object):
    def __init__(self, x, y, sp):
        self.x = x
        self.y = y
        self.sp = sp

class Bullet(object):
    def __init__(self, sp, x, y, dx, dy):
        self.sp = sp
        self.x = x
        self.y = y
        self.dx = dx
        self.dy = dy

    def update(self):
        self.x+=self.dx
        self.y+=self.dy

    def draw(self):
        spr(self.sp, self.x, self.y)


class Bullets(object):
    def __init__(self):
        self.bullets = []

    def add(self, sp, x, y, dx, dy):
        self.bullets.append(Bullet(sp, x, y, dx, dy))

    def update(self):
        for b in self.bullets:
            b.update()

    def draw(self):
        for b in self.bullets:
            b.draw()

T=0
ship = None
B = Bullets()

def _init():
    global ship
    ship = Ship(sp=1,x=60,y=60)

def fire():
    global ship, B
    B.add(3, ship.x, ship.y, 0, -3)

def _update():
    global T, ship, B

    T=T+1

    B.update()

    if(T%6<3):
        ship.sp=1
    else:
        ship.sp=2

    if btn(0):
        ship.x-=1
    if btn(1):
        ship.x+=1
    if btn(2):
        ship.y-=1
    if btn(3):
        ship.y+=1
    if btnp(4):
        fire()

def _draw():
    global ship, B

    cls()
    spr(ship.sp,ship.x,ship.y)
    B.draw()

Remove bullets, if they leave screen

Source

class Ship(object):
    def __init__(self, x, y, sp):
        self.x = x
        self.y = y
        self.sp = sp

class Bullet(object):
    def __init__(self, sp, x, y, dx, dy):
        self.sp = sp
        self.x = x
        self.y = y
        self.dx = dx
        self.dy = dy

    def update(self):
        self.x+=self.dx
        self.y+=self.dy

    def draw(self):
        spr(self.sp, self.x, self.y)


class Bullets(object):
    def __init__(self):
        self.bullets = []

    def add(self, sp, x, y, dx, dy):
        self.bullets.append(Bullet(sp, x, y, dx, dy))

    def update(self):
        to_del = []
        for k, b in enumerate(self.bullets):
            b.update()
            if b.x < 0 or b.x > 128 or b.y < 0 or b.y > 128:
               to_del.append(b)

        # delete bullets outside the screen
        for remove_element in to_del:
            self.bullets.pop(self.bullets.index(remove_element))

    def draw(self):
        for b in self.bullets:
            b.draw()

T=0
ship = None
B = Bullets()

def _init():
    global ship
    ship = Ship(sp=1,x=60,y=60)

def fire():
    global ship, B
    B.add(3, ship.x, ship.y, 0, -3)

def _update():
    global T, ship, B

    T=T+1

    B.update()

    if(T%6<3):
        ship.sp=1
    else:
        ship.sp=2

    if btn(0):
        ship.x-=1
    if btn(1):
        ship.x+=1
    if btn(2):
        ship.y-=1
    if btn(3):
        ship.y+=1
    if btnp(4):
        fire()

def _draw():
    global ship, B

    cls()
    px8_print(str(len(B.bullets)), 0, 0, 9)

    spr(ship.sp,ship.x,ship.y)
    B.draw()

Draw enemies

Source

class Ship(object):
    def __init__(self, x, y, sp):
        self.x = x
        self.y = y
        self.sp = sp

class Bullet(object):
    def __init__(self, sp, x, y, dx, dy):
        self.sp = sp
        self.x = x
        self.y = y
        self.dx = dx
        self.dy = dy

    def update(self):
        self.x+=self.dx
        self.y+=self.dy

    def draw(self):
        spr(self.sp, self.x, self.y)


class Bullets(object):
    def __init__(self):
        self.bullets = []

    def add(self, sp, x, y, dx, dy):
        self.bullets.append(Bullet(sp, x, y, dx, dy))

    def update(self):
        to_del = []
        for k, b in enumerate(self.bullets):
            b.update()
            if b.x < 0 or b.x > 128 or b.y < 0 or b.y > 128:
               to_del.append(b)

        # delete bullets outside the screen
        for remove_element in to_del:
            self.bullets.pop(self.bullets.index(remove_element))

    def draw(self):
        for b in self.bullets:
            b.draw()

class Enemy(object):
    def __init__(self, sp, x, y):
        self.sp = sp
        self.x = x
        self.y = y

    def draw(self):
        spr(self.sp, self.x, self.y)


class Enemies(object):
    def __init__(self, nb):
        self.enemies = []
        for i in range(0, nb):
            self.enemies.append(Enemy(sp=17, x=i*16, y=60-i*8))

    def draw(self):
        for e in self.enemies:
            e.draw()


T=0
ship = None
B = Bullets()
E = Enemies(10)

def _init():
    global ship
    ship = Ship(sp=1,x=60,y=60)

def fire():
    global ship, B
    B.add(3, ship.x, ship.y, 0, -3)

def _update():
    global T, ship, B

    T=T+1

    B.update()

    if(T%6<3):
        ship.sp=1
    else:
        ship.sp=2

    if btn(0):
        ship.x-=1
    if btn(1):
        ship.x+=1
    if btn(2):
        ship.y-=1
    if btn(3):
        ship.y+=1
    if btnp(4):
        fire()

def _draw():
    global ship, B, E

    cls()
    px8_print(str(len(B.bullets)), 0, 0, 9)

    spr(ship.sp,ship.x,ship.y)
    B.draw()
    E.draw()

Move enemies

Source

class Ship(object):
    def __init__(self, x, y, sp):
        self.x = x
        self.y = y
        self.sp = sp

class Bullet(object):
    def __init__(self, sp, x, y, dx, dy):
        self.sp = sp
        self.x = x
        self.y = y
        self.dx = dx
        self.dy = dy

    def update(self):
        self.x+=self.dx
        self.y+=self.dy

    def draw(self):
        spr(self.sp, self.x, self.y)


class Bullets(object):
    def __init__(self):
        self.bullets = []

    def add(self, sp, x, y, dx, dy):
        self.bullets.append(Bullet(sp, x, y, dx, dy))

    def update(self):
        to_del = []
        for k, b in enumerate(self.bullets):
            b.update()
            if b.x < 0 or b.x > 128 or b.y < 0 or b.y > 128:
               to_del.append(b)

        # delete bullets outside the screen
        for remove_element in to_del:
            self.bullets.pop(self.bullets.index(remove_element))

    def draw(self):
        for b in self.bullets:
            b.draw()

class Enemy(object):
    def __init__(self, sp, m_x, m_y, x, y, r):
        self.sp = sp
        self.m_x = m_x
        self.m_y = m_y
        self.x = x
        self.y = y
        self.r = r

    def update(self, t):
        self.x = self.r*sin(t/50) + self.m_x
        self.y = self.r*cos(t/50) + self.m_y

    def draw(self):
        spr(self.sp, self.x, self.y)


class Enemies(object):
    def __init__(self, nb):
        self.enemies = []
        for i in range(0, nb):
            self.enemies.append(Enemy(sp=17, m_x=i*16, m_y=60-i*8, x=-32, y=-32, r=12))

    def update(self, t):
        for e in self.enemies:
            e.update(t)

    def draw(self):
        for e in self.enemies:
            e.draw()

T=0
ship = None
B = Bullets()
E = Enemies(10)

def _init():
    global ship
    ship = Ship(sp=1,x=60,y=60)

def fire():
    global ship, B
    B.add(3, ship.x, ship.y, 0, -3)

def _update():
    global T, ship, B, E

    T=T+1

    E.update(T)
    B.update()

    if(T%6<3):
        ship.sp=1
    else:
        ship.sp=2

    if btn(0):
        ship.x-=1
    if btn(1):
        ship.x+=1
    if btn(2):
        ship.y-=1
    if btn(3):
        ship.y+=1
    if btnp(4):
        fire()

def _draw():
    global ship, B, E

    cls()
    px8_print(str(len(B.bullets)), 0, 0, 9)

    spr(ship.sp,ship.x,ship.y)
    B.draw()
    E.draw()

Points and health

Source

class Ship(object):
    def __init__(self, x, y, sp, h):
        self.x = x
        self.y = y
        self.sp = sp
        self.h = h

    def draw(self):
        spr(self.sp,self.x,self.y)

        for i in range(1, 5):
            if i <= self.h:
                spr(33,80+6*i,3)
            else:
                spr(34,80+6*i,3)

class Bullet(object):
    def __init__(self, sp, x, y, dx, dy):
        self.sp = sp
        self.x = x
        self.y = y
        self.dx = dx
        self.dy = dy

    def update(self):
        self.x+=self.dx
        self.y+=self.dy

    def draw(self):
        spr(self.sp, self.x, self.y)


class Bullets(object):
    def __init__(self):
        self.bullets = []

    def add(self, sp, x, y, dx, dy):
        self.bullets.append(Bullet(sp, x, y, dx, dy))

    def update(self):
        to_del = []
        for k, b in enumerate(self.bullets):
            b.update()
            if b.x < 0 or b.x > 128 or b.y < 0 or b.y > 128:
               to_del.append(b)

        # delete bullets outside the screen
        for remove_element in to_del:
            self.bullets.pop(self.bullets.index(remove_element))

    def draw(self):
        for b in self.bullets:
            b.draw()

class Enemy(object):
    def __init__(self, sp, m_x, m_y, x, y, r):
        self.sp = sp
        self.m_x = m_x
        self.m_y = m_y
        self.x = x
        self.y = y
        self.r = r

    def update(self, t):
        self.x = self.r*sin(t/50) + self.m_x
        self.y = self.r*cos(t/50) + self.m_y

    def draw(self):
        spr(self.sp, self.x, self.y)


class Enemies(object):
    def __init__(self, nb):
        self.enemies = []
        for i in range(0, nb):
            self.enemies.append(Enemy(sp=17, m_x=i*16, m_y=60-i*8, x=-32, y=-32, r=12))

    def update(self, t):
        for e in self.enemies:
            e.update(t)

    def draw(self):
        for e in self.enemies:
            e.draw()

T=0
ship = None
B = Bullets()
E = Enemies(10)

def _init():
    global ship
    ship = Ship(sp=1, x=60, y=60, h=3)

def fire():
    global ship, B
    B.add(3, ship.x, ship.y, 0, -3)

def _update():
    global T, ship, B, E

    T=T+1

    E.update(T)
    B.update()

    if(T%6<3):
        ship.sp=1
    else:
        ship.sp=2

    if btn(0):
        ship.x-=1
    if btn(1):
        ship.x+=1
    if btn(2):
        ship.y-=1
    if btn(3):
        ship.y+=1
    if btnp(4):
        fire()

def _draw():
    global ship, B, E

    cls()
    px8_print(str(len(B.bullets)), 0, 0, 9)

    ship.draw()
    B.draw()
    E.draw()

Collision 1: Clean up and preparation

Collision 2: Define boxes

Collision 3: Collide boxes

Change states

Health, game over, immortality

Explosions

Stars

Enemy movement and respawn