Archive for July 29th, 2007

Space Invaders!

Sunday, July 29th, 2007

Here’s the space invaders type game, like I promised :)

Ryan

from livewires import *

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480

def drawplayer(x,y):
    """This function draws the player at the position x y, modify this to
    change how the player looks."""
    return circle(x,y,r=10,filled=1)

def drawenemy(x,y):
    """This function draws enemies at the position x y, modify this to change
    you the enemies look."""
    return box(x-5,y-5,x+5,y+5,colour=Colour.red,filled=1)

enemy_x = 40
enemy_y = 400

begin_graphics(width=SCREEN_WIDTH,height=SCREEN_HEIGHT,title="Game")
allow_moveables()

# This is all the stuff that happens before we begin the game.
player = drawplayer(40, 40)
enemy = drawenemy(enemy_x, enemy_y)
direction = 'right'

# Begin the game.
while True:
    keys = keys_pressed()
    if 'x1b' in keys:
        break
    if '4' in keys:
        move_by(player, -10, 0)
    elif '6' in keys:
        move_by(player, 10, 0)

    if enemy_x < SCREEN_WIDTH-10 and direction == 'right':
        move_by(enemy, 5, 0)
        enemy_x += 5
    elif enemy_x > 10 and direction == 'left':
        move_by(enemy, -5, 0)
        enemy_x -= 5
    else:
        if direction == 'right':
            direction = 'left'
        else:
            direction = 'right'

    sleep(0.1)

end_graphics()