Archive for August, 2007

Gaming part two

Sunday, August 12th, 2007

Alrighty, here’s some of the code for the start of a car game:

from livewires import *SCREEN_WIDTH = 640

SCREEN_HEIGHT = 480

def collide(x1, y1, x2, y2):

    if abs(x1 - x2) < 10 and abs(y1 - y2) < 10:

        return True

    else:

        return False

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)

class Enemy:

    pass

enemies = []

for i in range(10):

    e = Enemy()

    e.x = random_between(0, 640)

    e.y = random_between(40, 480)

    enemies.append(e)

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)

player_x = 40

player_y = 40

for e in enemies:

    e.image = drawenemy(e.x, e.y)

    e.direction = ‘right’

# Begin the game.

while True:

    keys = keys_pressed()

    if ‘\x1b’ in keys:

        break

    if ‘4′ in keys:

        move_by(player, -10, 0)

        player_x -= 10

    elif ‘6′ in keys:

        move_by(player, 10, 0)

        player_x += 10

    elif ‘8′ in keys:

        move_by(player, 0, 10)

        player_y += 10

    elif ‘2′ in keys:

        move_by(player, 0, -10)

        player_y -= 10

for e in enemies:

        if e.x < SCREEN_WIDTH-10 and e.direction == ‘right’:

            move_by(e.image, 5, 0)

            e.x += 5

        elif e.x > 10 and e.direction == ‘left’:

            move_by(e.image, -5, 0)

            e.x -= 5

        else:

            if e.direction == ‘right’:

                e.direction = ‘left’

            else:

                e.direction = ‘right’

    sleep(0.1)

for e1 in enemies:

        if collide(e1.x, e1.y, player_x, player_y):

            print “PLAYER COLLISION!”

        for e2 in enemies:

            if e1 is not e2:

                if collide(e1.x, e1.y, e2.x, e2.y):

                    print “Collision!”

                    enemies.remove(e1)

                    enemies.remove(e2)

end_graphics()

Here is some more code for a space invaders like game:

from livewires import *SCREEN_WIDTH = 640

SCREEN_HEIGHT = 480

def collide(x1, y1, x2, y2):

    if abs(x1 - x2) < 5 and abs(y1 - y2) < 5:

        return True

    else:

        return False

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)

class Enemy:

    pass

enemies = []

for i in range(10):

    e = Enemy()

    e.x = random_between(0, 640)

    e.y = random_between(40, 480)

    enemies.append(e)

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)

for e in enemies:

    e.image = drawenemy(e.x, e.y)

    e.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)

for e in enemies:

        if e.x < SCREEN_WIDTH-10 and e.direction == ‘right’:

            move_by(e.image, 5, 0)

            e.x += 5

        elif e.x > 10 and e.direction == ‘left’:

            move_by(e.image, -5, 0)

            e.x -= 5

        else:

            if e.direction == ‘right’:

                e.direction = ‘left’

            else:

                e.direction = ‘right’

    sleep(0.1)

for e1 in enemies:

        for e2 in enemies:

            if e1 is not e2:

                if collide(e1.x, e1.y, e2.x, e2.y):

                    print “Collision!”

                    enemies.remove(e1)

                    enemies.remove(e2)

end_graphics()