Computer Science Canada

Help with multiple bullets

Author:  ha1o [ Thu May 10, 2007 9:48 pm ]
Post subject:  Help with multiple bullets

I'm trying to make a space shooter and I need help getting multiple bullets on the screen at once. This is what I have so far:

code:

View.Set ("graphics: 500, 600, offscreenonly")

colorback (black)
cls

%Draws the spaceship
Draw.Line (1, 1, 26, 52, white)
Draw.Line (52, 1, 26, 52, white)
Draw.Line (1, 1, 52, 1, white)
Draw.Fill (26, 2, white, white)
var spaceship : int := Pic.New (0, 0, 52, 52)
Pic.SetTransparentColor (spaceship, black)

cls

var shot : int := 0
const SHIP_SPEED := 2
const BULLET_SPEED := 3

%Spaceship
var x : int := 330
var y : int := 0
var chars : array char of boolean

%Laser
var bullet_x : int
var bullet_y : int
var bullet_y2 : int
var chars2 : array char of boolean

forward procedure movement

procedure shoot
    loop
        shot := 1
        bullet_y += BULLET_SPEED
        bullet_y2 += BULLET_SPEED
        exit when bullet_y > 700
        movement
    end loop
    shot := 0
end shoot

body procedure movement
    Input.KeyDown (chars)
    Input.KeyDown (chars2)

    %Spaceship Movement
    if chars (KEY_RIGHT_ARROW) and x <= 445 then
        x += SHIP_SPEED
    end if
    if chars (KEY_LEFT_ARROW) and x >= 1 then
        x -= SHIP_SPEED
    end if
    if chars (KEY_UP_ARROW) and y <= 545 then
        y += SHIP_SPEED
    end if
    if chars (KEY_DOWN_ARROW) and y >= 1 then
        y -= SHIP_SPEED
    end if

    if chars2 (' ') and shot = 0 then
        bullet_x := x
        bullet_y := y
        bullet_y2 := y + 20
        shoot
    end if

    if shot = 1 then
        Draw.ThickLine (bullet_x + 26, bullet_y + 55, bullet_x + 26, bullet_y2 + 55, 5, brightred)
    end if

    Pic.Draw (spaceship, x, y, picMerge)

    View.Update
    delay (1)
    cls
end movement

loop
    movement
end loop

Author:  DIIST [ Fri May 11, 2007 6:24 am ]
Post subject:  Re: Help with multiple bullets

Why Do you Have 2 Input.KeyDowns? Far as I know you only need to use it once in the loop. As for the bullets and multiple of them consider either this: Arrays(probably the easiest) and/or a link list. For arrays you can check here http://www.compsci.ca/v3/viewtopic.php?t=14333 for a tut on how it works and when it should be used. For Linklist i don't know if a tutorial is up for that but you can try Googling it. Smile

Author:  LaZ3R [ Fri May 11, 2007 2:39 pm ]
Post subject:  Re: Help with multiple bullets

1) You only need one input keydown... No point in 2
2) Flexible arrays would be your best bet when it comes to creating and removing objects because it A) Won't lag Turing as much, B) Allows you to create as many bullets as you want as opposed to regular arrays which would only allow you to create a set amount of bullets right from start and any time you create more than that, Turing will crash.

Flexible Arrays is what you should learn for this as it's most effective for this type of idea you want. If you don't already know them, try to learn regular arrays first considering flexible arrays aren't all that much more difficult! Good luck!


: