Computer Science Canada

Control Problems via Input.KeyDown ()

Author:  Flashkicks [ Fri May 13, 2005 12:00 pm ]
Post subject:  Control Problems via Input.KeyDown ()

Hay hay ppl'z! This is Flashkicks here! I am just trying to make simple asteroids game and its been awhile. I was just seeing if anyone can remember or tell me how to have two things going at one withOUT using a process. Example, have my spaceship move, while shooting lasers at the same time. Here's the code.
code:
%%%%%%%%%%%%%%%%
%              %
%%%  JoyPad  %%%
%              %
%%%%%%%%%%%%%%%%
% Billy Boldt  %
%%%% % % % %%%%%


%% Button : ground (y) := orange
setscreen ("graphics:max;max")
View.Set ("offscreenonly")

drawfillbox (0, 0, maxx, maxy, 7)
var shipX, shipY, laserX, laserY : int
shipX := 10
shipY := maxy div 2
laserX := shipX
laserY := shipY
proc eraseShip
    drawfillbox (shipX - 10, shipY, shipX + 50, shipY + 25, 7)
end eraseShip

proc DrawShip
    drawfilloval (shipX + 25, shipY + 15, 25, 10, 28)
    drawfillbox (shipX - 10, shipY, shipX + 25, shipY + 10, 28)
    drawfilloval (shipX + 35, shipY + 20, 7, 3, 1)
    drawfillbox (shipX + 27, shipY + 2, shipX + 50, shipY + 4, 23)
    drawfillbox (shipX + 25, shipY, shipX + 27, shipY + 5, 23)
end DrawShip

proc eraseLaser
    drawfillbox (laserX + 51, laserY + 2, laserX + 75, laserY + 4, 7)
end eraseLaser

proc DrawLaser
    drawfillbox (laserX + 51, laserY + 2, laserX + 75, laserY + 4, 1)
end DrawLaser

proc Controls
    var input : array char of boolean
    var speed : int := 3
    loop
        Input.KeyDown (input)
        if input (KEY_UP_ARROW) then
            eraseShip
            shipY := shipY + speed
            DrawShip
            View.Update
        end if
        if input (KEY_DOWN_ARROW) then
            eraseShip
            shipY := shipY - speed
            DrawShip
            View.Update
        end if
        if input (KEY_LEFT_ARROW) then
            eraseShip
            shipX := shipX - speed
            DrawShip
            View.Update
        end if
        if input (KEY_RIGHT_ARROW) then
            eraseShip
            shipX := shipX + speed
            DrawShip
            View.Update
        end if
        if input (KEY_TAB) then
            laserX := shipX
            laserY := shipY
            loop
                eraseLaser
                laserX := laserX + 10
                DrawLaser
                View.Update
                exit when laserX >= maxx
            end loop
        end if
        if shipX <= 0 then
            shipX := 0
        end if
        if shipX >= maxx - 50 then
            shipX := maxx - 50
        end if
        if shipY <= 0 - 25 then
            shipY := maxy
        end if
        if shipY >= maxy + 25 then
            shipY := 0
        end if
    end loop
end Controls

DrawShip
Controls

So run it and you will see once you shoot- you can not move. And I've made a game like this before so i KNOW its possible. Good Luck-and have fun!
~Flashkicks

Author:  Dylan-182 [ Fri May 13, 2005 12:07 pm ]
Post subject: 

Surprised i tried it and its coolness i have no idea how 2 help u with it Rolling Eyes but so far its cool Razz i think im guna look at the code n figure out how 2 do this for myself Very Happy then maybe go on 2 make a cool game of my own... Thinking lol ne ways sry i culdnt help but at least i got 2 see that this sorta thing is possible with turing, gives me sumthin 2 aim for Very Happy

Author:  Delos [ Fri May 13, 2005 12:12 pm ]
Post subject: 

My general comment on this (after having looked through your code), is to set up your 'main' loop to be the one that controls all movement/firing etc. Break down these actions into procedures, somewhat like the way you have it.
This way, if one of the arrow keys are pressed, the corresponding movement is called via a procedure/function.
When the key to fire is pressed, the object be be fired is created, and then in subsequent checks by the main loop, its status is checked and changed (i.e., its x-/y- pos'ns are altered, etc etc). Of course, these changes are done in a seperate procedure altogether. Compartmentalization is good! Don't put additional loops within your main loop...
This is the general idea that I used when making ~Star Catcher~ [/shameless plug].

Author:  jamonathin [ Fri May 13, 2005 12:23 pm ]
Post subject: 

Now some of the variables can be changed to boolean, and I heard it a million tiem to do so, but it's my personal preference not to, say what you will, but anyways here's a code for what you need. I didn't edit your program, beacause you'll remember more you edit your program yourself.
Turing:

setscreen ("graphics:400;500,nobuttonbar,position:center;center,offscreenonly,title:SiMaCz Productionz")
colorback (black)
var dif : int := 3 %Difference of movement
var done, laserwait : int := 0 %Tells program when another laser is allowed to fire
var x, y : int := 20 %Player co-ordinates
var laserx, lasery, hold : array 1 .. 10 of int
var key : array char of boolean
drawfillbox (0, 0, 15, 20, 2)
var ship : int := Pic.New (0, 0, 15, 20)
cls
for i : 1 .. upper (hold) %Max amount of lasers
    laserx (i) := 0 %Just defaulting everything to zero
    lasery (i) := 0
    hold (i) := 0
end for

procedure shoot_lasers %Shoots the lasers
    for i : 1 .. upper (hold) %Checking for all lasers
        if hold (i) = 1 then %If the current laser(i) is being shot then . .
            Draw.ThickLine (laserx (i), lasery (i), laserx (i), lasery (i) + 15, 2, 13) %Draw laser
            lasery (i) += dif * 2 %Move laser twice the speed of the player
            if lasery (i) > maxy then %If its greater than the screen then reset it
                hold (i) := 0
            end if
        end if
    end for
end shoot_lasers

procedure draw
    cls
    shoot_lasers %Calling shoot_lasers
    Pic.Draw (ship, x, y, picMerge) %Space ship, just change the variable to an acutal picture
    View.Update
end draw

loop
    Input.KeyDown (key) %Just Checking teh boundries for movement
    if key (KEY_LEFT_ARROW) and x > dif then
        x -= dif
    elsif key (KEY_RIGHT_ARROW) and x + Pic.Width (ship) < maxx - dif then
        x += dif
    end if
    if key (KEY_UP_ARROW) and y + Pic.Height (ship) < maxy - dif then
        y += dif
    elsif key (KEY_DOWN_ARROW) and y > 30 then
        y -= dif
    end if
    if key (' ') and done = 0 then %If we're allowed to shoot another laser
        for i : 1 .. upper (hold)
            if hold (i) = 0 and done = 0 then %If the current laser is free to be shot
                laserx (i) := x + (Pic.Width (ship) div 2) %Centering the laser
                lasery (i) := y + Pic.Height (ship) %Making it drawn at the top of the ship
                hold (i) := 1 %Making it taken
                done := 1 %Telling the program to delay before shooting again
            end if
        end for
    end if
    if done = 1 and laserwait < 20 then %Waiting untill laserwait is 20
        laserwait += 1 %This will make a delay of [20 * (whatever delay is set)] before shooting again
    elsif done = 1 and laserwait >= 20 then %Now its ok to shoot, so reset laserwait
        laserwait := 0
        done := 0
    end if
    draw %Draw everything
    delay (5) %delay, drrrrrr...
end loop


Have fun. Smile

Edit: Once again, I should type faster Razz

Author:  Flashkicks [ Fri May 13, 2005 12:47 pm ]
Post subject: 

Hay- thanks very much for that demo. I will try to incorporate it into mine.. I will let you now how it goes!
~Flashkicks

Hold on- I have ONE question that I have been seeing a lot lately. Well, not the question, but .. yeah.Okay----what is this;

+=

??.. Anyways..

:: EDIT ::

(About 15 minutes later)
Okay, that demo proggy you left me is wonderful, however, I can NOT interpret it into my program :'( .. Im not sure what to do now. I tried the whole upper with hold and everything and it just wont shoot any lasers at all... I dont want to just copy all your code so do you think you can get me started as of what to do next to interpret it into my coding??.. I will keep trying but Im really just getting frustrated. Anyway, thanks, and hopefully you can help me out.. BTY-My teacher liked the sample you left Laughing lol.. Kay, c ya!
~Flashkicks

:: EDIT ::
Okay- I got it to shoot now, but once 10 lasers are shot-its DONE! You cant shoot no more Sad.. achoo Help!!.....

Author:  jamonathin [ Fri May 13, 2005 1:49 pm ]
Post subject: 

Ok, this is what += means.
Turing:

x -= 2
 % Is the same thing as
x := x - 2

% And . .

y += 7
 % Is the same thing as
y := y + 7

% And . .

z *= 5
 % Is the same thing as
z := z * 5

% And . .

a /= 9
 % Is the same thing as
a := a / 9


And for your shooting problem. . . when do you reset the shots? Do you ever make it so when the laser gets past the top of the screen that you reset it, making it available to shoot again?

Take a look at this again
Turing:

procedure shoot_lasers %Shoots the lasers
    for i : 1 .. upper (hold) %Checking for all lasers
        if hold (i) = 1 then %If the current laser(i) is being shot then . .
            Draw.ThickLine (laserx (i), lasery (i), laserx (i), lasery (i) + 15, 2, 13) %Draw laser
            lasery (i) += dif * 2 %Move laser twice the speed of the player
            if lasery (i) > maxy then %If its greater than the screen then reset it
                hold (i) := 0
            end if
        end if
    end for
end shoot_lasers


Once the lasery is > maxy, I reset that laser, making it available to shoot again, and I use a for loop to check through all 10 lasers to save up room.

Author:  Flashkicks [ Mon May 16, 2005 7:57 am ]
Post subject: 

haha-sorry.. I had a silly mistake and everything werks fine.. Embarassed My Baad.. Anyways.. I thank you very much.. Now comes the asteroids Laughing
~Flashkicks

:UPDATE:

OKAY-wtfrick... Evil or Very Mad My asteroids arent werking either.. I feel like I cant do anything.. Anyways.. Heres what I have for it- they draw but they never move..
code:
proc asteroids
    type asteroids :
        record
            x : int
            y : int
            size : int
            spd : int
            dx : int
        end record

    var rock : array 1 .. 100 of asteroids
    % Declare initial values
    for i : 1 .. 10
        rock (i).x := Rand.Int (maxx, maxx + 400)
        rock (i).y := Rand.Int (0, maxy)
        rock (i).size := 30
        % rock (i).spd := -1 - rock (i).size
        % rock (i).dx := Rand.Int (-2 - rock (i).size, 2 + rock (i).size)
    end for
    % Make move
    for i : 1 .. 10
        drawfilloval (rock (i).x, rock (i).y, rock (i).size, rock (i).size, 7)
        rock (i).x -= 5
        drawfilloval (rock (i).x, rock (i).y, rock (i).size, rock (i).size, 43)
        delay (5)
    end for
end asteroids

I call asteroids in my main Controls loop... Maybe I shouldnt be going through this using a record Confused But I thought it would be a good idea cuz then I could eventually keep track of different sizes of asteroids..

Author:  jamonathin [ Tue May 17, 2005 8:17 am ]
Post subject: 

Ok, there's nothing wrong with using a record, it just how you're using it.

Declare everything outside of the procedure. This is what your record should look like:
Turing:

var rock : array 1 .. 100 of
    record
        x, y, size, spd, dx : int
    end record

Now here's why your asteroids wont move. You "Declare initial values", and then you decide to "Make move". The when we run through the procedure again, you declare, then move. We run through the procedure again, you declare, them move. You only need to "declare" them once, not everytime in the procedure, because then they wont go anywhere. So add that to the bottom of your array.
Turing:

var rock : array 1 .. 100 of
    record
        x, y, size, spd, dx : int
    end record
for i : 1 .. upper (rock) %The amount in array (which is 100)
    rock (i).x := Rand.Int (maxx, maxx + 400)
    rock (i).y := Rand.Int (0, maxy)
    rock (i).size := 30
end for


Now, all thats left is to move, and that's what should be in your "asteroids" procedure.
I noticed that you're using the "draw over top of the old ball to get rid of it" technique, don't. There's no need to. Clear the last screen, and then Update.

Now, we need to change our setscreen/View.Set options to offscreenonly to use View.Update. But we only do this once, and not inside any loop or procedure. Just at the top of your program.
Turing:

setscreen ("graphics:640;480, position:center;center, nobuttonbar, offscreenonly, title: SiMaCz Productionz")
colorback(black)
cls


Now your loop and procedure is small, and this is what your entire program should look like.

Turing:

setscreen ("graphics:640;480, position:center;center, nobuttonbar, offscreenonly, title: SiMaCz Productionz")
colorback (black)
cls
var rock : array 1 .. 100 of
    record
        x, y, size, spd, dx : int
    end record
for i : 1 .. upper (rock) %The amount in array (which is 100)
    rock (i).x := Rand.Int (maxx, maxx + 400)
    rock (i).y := Rand.Int (0, maxy)
    rock (i).size := Rand.Int (10, 20)
    rock (i).spd := round (19 - (rock (i).size) / 1.007) %rock(i).spd must be whole/int so we use round()
    if rock (i).spd = 0 then %Checking if the speed is 0
        rock (i).spd := 1
    end if
    %rock (i).dx := not sure what this does
end for
proc asteroids
    % Make move
    for i : 1 .. upper (rock)
        rock (i).x -= rock (i).spd
        drawfilloval (rock (i).x, rock (i).y, rock (i).size, rock (i).size, 43)
        drawoval (rock (i).x, rock (i).y, rock (i).size, rock (i).size, 42)%For effect
    end for
end asteroids
loop
    cls
    asteroids
    View.Update
    exit when hasch %Exit when keyboard is touched
    delay (5)
end loop

Author:  Flashkicks [ Tue May 17, 2005 9:26 am ]
Post subject: 

Okay.. I tried all your stuff (which by the way was WOW, wut a cool program you made.. I love the "effect Razz ..lol.. Anyway") and it like.. Okay, the program RUNS- but not anything like yours.. First, the asteroids dont move quickly and swiftly. Also, my ship is lagging like a mad mother monkey Confused Im not sure what to do.. I will put my code in here so you can see how I have it ALL set up. If you comment out the "asteroids" in the 'DrawShipAndShoot' procedure, youll notice how nice and smooth it runs.. But oce I throw the 'asteroids' in there, it goes BOOM...lol
code:
%%%%%%%%%%%%%%%%
%              %
%%%  JoyPad  %%%
%              %
%%%%%%%%%%%%%%%%
% Billy Boldt  %
%%%% % % % %%%%%


%% Button : ground (y) := orange
setscreen ("graphics:max;max, offscreenonly")
colorback (7)
cls
%% Variables %%
drawfillbox (0, 0, maxx, maxy, 7)
var key : array char of boolean
var shipX, shipY : int
shipX := 10
shipY := maxy div 2
var laserX, laserY, hold : array 1 .. 10 of int
var done, laserwait : int := 0 % Tells program when another laser is allowed to fire
var dif : int := 3 % Difference of movement
var level : array 1 .. 10 of int

for i : 1 .. upper (hold) % Give the lasers initial values
    laserX (i) := 0     % Just defaulting everything to zero
    laserY (i) := 0
    hold (i) := 0
end for

proc eraseShip
    drawfillbox (shipX - 10, shipY, shipX + 50, shipY + 25, 7)
end eraseShip

proc DrawShip
    drawfilloval (shipX + 25, shipY + 15, 25, 10, 28)
    drawfillbox (shipX - 10, shipY, shipX + 25, shipY + 10, 28)
    drawfilloval (shipX + 35, shipY + 20, 7, 3, 1)
    drawfillbox (shipX + 27, shipY + 2, shipX + 50, shipY + 4, 23)
    drawfillbox (shipX + 25, shipY, shipX + 27, shipY + 5, 23)
end DrawShip

proc Shoot
    for i : 1 .. upper (hold)
        if hold (i) = 1 then
            drawfillbox (laserX (i) + 51, laserY (i) + 2, laserX (i) + 75, laserY (i) + 4, 7)
            laserX (i) += dif * 3
            drawfillbox (laserX (i) + 51, laserY (i) + 2, laserX (i) + 75, laserY (i) + 4, 1)
            if laserX (i) > maxx then   % If its greater than the screen then reset it
                hold (i) := 0
            end if
        end if
    end for
end Shoot

var rock : array 1 .. 100 of
    record
        x, y, size, spd, dx : int
    end record
for i : 1 .. upper (rock) %The amount in array (which is 100)
    rock (i).x := Rand.Int (maxx, maxx + 400)
    rock (i).y := Rand.Int (0, maxy)
    rock (i).size := Rand.Int (10, 20)
end for

proc asteroids
    % Make move
    for i : 1 .. upper (rock)
        drawfilloval (rock (i).x, rock (i).y, rock (i).size, rock (i).size, 7)
        rock (i).x -= 5
        drawfilloval (rock (i).x, rock (i).y, rock (i).size, rock (i).size, 43)
        drawoval (rock (i).x, rock (i).y, rock (i).size, rock (i).size, 42) %For effect
        delay (5)
        View.Update
        if rock (i).x <= 0 then
            rock (1).x := Rand.Int (maxx, maxx + 400)
            rock (1).y := Rand.Int (0, maxy)
        end if
    end for
end asteroids

proc DrawShipAndShoot
    Shoot
    DrawShip
    % asteroids
    View.Update
end DrawShipAndShoot

proc shoots
    for i : 1 .. upper (hold)
        if hold (i) = 0 and done = 0 then % If the current laser is free to be shot
            laserX (i) := shipX
            laserY (i) := shipY
            hold (i) := 1 % Making it taken
            done := 1     % Telling the program to delay before shooting again
        end if
    end for
end shoots

loop
    Input.KeyDown (key)
    if key (KEY_LEFT_ARROW) and shipX > dif then
        eraseShip
        shipX -= dif
    elsif key (KEY_RIGHT_ARROW) then
        eraseShip
        shipX += dif
    end if
    if key (KEY_UP_ARROW) then
        eraseShip
        shipY += dif
    elsif key (KEY_DOWN_ARROW) and shipY > 30 then
        eraseShip
        shipY -= dif
    end if
    if key (KEY_TAB) and done = 0 then % If we're allowed to shoot another laser
        shoots
    end if
    if done = 1 and laserwait < 20 then % Waiting untill laserwait is 20
        laserwait += 1 % This will make a delay of [20 * (whatever delay is set)] before shooting again
    elsif done = 1 and laserwait >= 20 then % Now its ok to shoot, so reset laserwait
        laserwait := 0
        done := 0
    end if
    if shipX <= 0 then % Checking for ship bounderies
        shipX := 0
    elsif shipX >= maxx - 40 then
        shipX := maxx - 40
    end if
    if shipY >= maxy - 30 then
        shipY := maxy - 30
    end if
    DrawShipAndShoot     % Draw everything
end loop

I think im missing something major(but is probably very small Mad ) .. If you can catch it than I must say, your Good...lol.. You've helped a lot alreay and Im still sitting here every stinkin day werking on it.. But once again-Im stuck:(..

Author:  jamonathin [ Tue May 17, 2005 1:04 pm ]
Post subject: 

Here's your code, I added a cool background for yas.

The reason it went incredibly slow was because you were delaying(5) and updating the screen (View.Update) 100 times in that for loop. You only need View.Update once, and thats before your delay. I changed things so that only one asteroid comes at a time, and as you shoot more, you can change the new variable i made (rocks) to a higher value, so more asteroids come.

I cleaned up a couple of little things that I forget, but I left the collision for you Wink. If you need help with it just ask, but your programs lookin good so far, keep goin. Smile

Turing:
%%%%%%%%%%%%%%%%
%              %
%%%  JoyPad  %%%
%              %
%%%%%%%%%%%%%%%%
% Billy Boldt  %
%%%% % % % %%%%%


%% Button : ground (y) := orange
setscreen ("graphics:max;max, offscreenonly,nobuttonbar")%nobuttonbar = no button bar :P
colorback (7)
cls

%COol Background
var stars : int := 100
var starx, stary, sdif : array 1 .. 100 of int
for i : 1 .. stars
    starx (i) := Rand.Int (1, maxx) %Making a default for the star somewhere on the screen
    stary (i) := Rand.Int (1, maxy)
    sdif (i) := Rand.Int (2, 7)
end for

proc move_stars
    for i : 1 .. stars %For all of the stars
        starx (i) -= sdif (i) %Move them to their corresponding speed
        drawdot (starx (i), stary (i), white) %Draw them
        if starx (i) < 0 then %If they're off the screen
            starx (i) := maxx %Reset at maxx
            stary (i) := Rand.Int (1, maxy) %Reset for a random y
            sdif (i) := Rand.Int (2, 7) %Reset a random speed
        end if
    end for
end move_stars
%% Variables %%
drawfillbox (0, 0, maxx, maxy, 7)
var key : array char of boolean
var shipX, shipY : int
shipX := 10
shipY := maxy div 2
var laserX, laserY, hold : array 1 .. 10 of int
var done, laserwait : int := 0 % Tells program when another laser is allowed to fire
var dif : int := 3 % Difference of movement
var level : array 1 .. 10 of int
var rocks : int := 2

for i : 1 .. upper (hold) % Give the lasers initial values
    laserX (i) := 0     % Just defaulting everything to zero
    laserY (i) := 0
    hold (i) := 0
end for

proc DrawShip
    drawfilloval (shipX + 25, shipY + 15, 25, 10, 28)
    drawfillbox (shipX - 10, shipY, shipX + 25, shipY + 10, 28)
    drawfilloval (shipX + 35, shipY + 20, 7, 3, 1)
    drawfillbox (shipX + 27, shipY + 2, shipX + 50, shipY + 4, 23)
    drawfillbox (shipX + 25, shipY, shipX + 27, shipY + 5, 23)
end DrawShip

proc Shoot
    for i : 1 .. upper (hold)
        if hold (i) = 1 then
            laserX (i) += dif * 3
            drawfillbox (laserX (i) + 51, laserY (i) + 2, laserX (i) + 75, laserY (i) + 4, 1)
            if laserX (i) > maxx then   % If its greater than the screen then reset it
                hold (i) := 0
            end if
        end if
    end for
end Shoot

var rock : array 1 .. 100 of
    record
        x, y, size, spd, dx : int
    end record
for i : 1 .. upper (rock) %The amount in array (which is 100)
    rock (i).x := Rand.Int (maxx, maxx + 400)
    rock (i).y := Rand.Int (0, maxy)
    rock (i).size := Rand.Int (10, 20)
    rock (i).spd := Rand.Int (1, 3)
end for

proc asteroids
    % Make move
    for i : 1 .. rocks div 2
        rock (i).x -= rock (i).spd
        drawfilloval (rock (i).x, rock (i).y, rock (i).size, rock (i).size, 43)
        drawoval (rock (i).x, rock (i).y, rock (i).size, rock (i).size, 42) %For effect
        if rock (i).x <= 0 then
            rock (1).x := Rand.Int (maxx, maxx + 200)
            rock (1).y := Rand.Int (50, maxy - 60)
        end if
    end for
end asteroids

proc DrawShipAndShoot
    cls
    move_stars
    asteroids
    Shoot
    DrawShip
    View.Update
end DrawShipAndShoot

proc shoots
    for i : 1 .. upper (hold)
        if hold (i) = 0 and done = 0 then % If the current laser is free to be shot
            laserX (i) := shipX
            laserY (i) := shipY
            hold (i) := 1 % Making it taken
            done := 1     % Telling the program to delay before shooting again
        end if
    end for
end shoots

loop
    Input.KeyDown (key)
    if key (KEY_LEFT_ARROW) and shipX > dif then
        shipX -= dif
    elsif key (KEY_RIGHT_ARROW) then
        shipX += dif
    end if
    if key (KEY_UP_ARROW) then
        shipY += dif
    elsif key (KEY_DOWN_ARROW) and shipY > 30 then
        shipY -= dif
    end if
    if key (KEY_TAB) and done = 0 then % If we're allowed to shoot another laser
        shoots
    end if
    if done = 1 and laserwait < 20 then % Waiting untill laserwait is 20
        laserwait += 1 % This will make a delay of [20 * (whatever delay is set)] before shooting again
    elsif done = 1 and laserwait >= 20 then % Now its ok to shoot, so reset laserwait
        laserwait := 0
        done := 0
    end if
    if shipX <= 0 then % Checking for ship bounderies
        shipX := 0
    elsif shipX >= maxx - 40 then
        shipX := maxx - 40
    end if
    if shipY >= maxy - 30 then
        shipY := maxy - 30
    end if
    DrawShipAndShoot     % Draw everything
end loop

Author:  Flashkicks [ Wed May 18, 2005 7:55 am ]
Post subject: 

Hay man!!.. I got the whole asteroids thing werking and I guess I shoudla told you that yesterday already bcuz I had it werking near the end of class- not sure if its as efficient as yours though..lol.. but anyways, I really like the background idea so I will throw that in mine.. Im werking on the collision detection right now and its going good, Im just making a life gauge and its giving me hassles..lol.. I'll let you know how it turns out.. TTY soon..
~Flashkicks

:: UPDATE ::
Okay.. The life gauge is all set and its kinda nifty.. It disappears when its not being used..lol.. But anyways.. Okay, not the asteroids. I made a lil procedure to check for collision with a laser and the rock. This is what I've got;
code:
proc RockHit
    for i : 1 .. upper (rock)
        if laserX (i) + 24 >= rock (i).x - 10 and laserY (i) > rock (i).y - 10 and laserY (i) < rock (i).y + 10 then
            drawfilloval (rock (i).x, rock (i).y, rock (i).size, rock (i).size, 14) % Eventually it'll explode
            drawfilloval (maxx div 2, maxy div 2, 200, 200, 12) % This is just to check if the rock is hit or not
        end if
    end for
end RockHit


It works maybe 40% of the time.. Any ideas Why it only werks so seldom??..
NOTE: the red oval in the middle is not part of the game, its just my way of checking if a rock is hit.. you know..

Author:  jamonathin [ Wed May 18, 2005 11:23 am ]
Post subject: 

This is the method that I use for collision with lasers.

- I Start off with a for loop running every rock
- Then I run a for loop inside that running for every laser shot
- Then I run a for loop inside That, checking the length of the laser

And what the third for loop does is it checks every pixel on that laser, and as a result, if any point on it touches anything you dont want it to, it'll know.

This method's very efficient. Actually last night I was playing some Intellevision games on my PS2 and I got addicted to Astrosmash! and decided to make it. I dont know when I'll be submitting it, I'm gonnan be busy in the next few weeks, but I'm actually almost done, I only need all of those special features that the actual game has . . .Thinking But I'm trying to make it exactly like the real game.

Anyways, here's your code that you should use.
code:

proc RockHit
    for i : 1 .. upper (rock) %Checking Every Rock
        for x : 1 .. lasers %Checking Every Laser / Not sure which variable you used (max lasers)
            for q : 0 .. 24 %Checking Ever Point In Every Laser
                if laserY (x) >= rock (i).y - 10 and laserY <= rock (i).y + 10 and laserX (x) + q >= rock (i).x - 10 and laserX (x) + q <= rock (i).x + 10 then
                    cls
                    delay (1577) %Just to see if it works. Do whatever.
                end if
            end for
        end for
    end for
end RockHit

Author:  Flashkicks [ Thu May 19, 2005 7:57 am ]
Post subject: 

Hmmm.. The procedure yopu gave is kinda confusing me.. I dont get how the "checking every point in laser" would werk.. Well- I guess I understand but I dont know if I ever woudla thought of that..lol.. Anyways- You said to use my lasers variable for
code:
        for x : 1 .. done %Checking Every Laser / Not sure which variable you used (max lasers)
but I tried all my variables, when I use laserX(i), or laserwait then my "array subscript is out of range". But if I get NO errors then the program runs fine but the lasers never detect once they've hit a rock.. (it doesnt werk) If you scroll up you can see all the variables I used and Im not sure if I have to change something.. Ill keep trying; hope for the best..lol..
~Flashkicks

::UPDATE::
Okay nevermind, .. I got it to werk now..lol.. Its pretty sweet man.. Now I get to design a nifty lil proc for them blowing up Laughing Thats always fun.. KAY- Ill tty soon, and let you know how things are going..
~Flashkicks

Author:  jamonathin [ Thu May 19, 2005 8:18 am ]
Post subject: 

Send your code over, I wanna see how its goin Smile

Author:  Flashkicks [ Thu May 19, 2005 9:05 am ]
Post subject: 

Okay, so I this right here is what i want it to look like.
code:
drawfillbox (0, 0, maxx, maxy, 7) % background
var boomX, boomY : int := 200 % to randomize 10 pixels wide in a square

drawfilloval (200, 200, 20, 20, 62) % draw original asteroid
drawoval (200, 200, 20, 20, 37) % effect
delay (500) % .. Here it comes!!....
for l : 1 .. 10
    randint (boomX, 189, 212)
    randint (boomY, 189, 212)
    for i : 1 .. 12
        drawfilloval (boomX, boomY, i, i, 12)
        delay (5)
    end for
    for i : 1 .. 12
        drawfilloval (boomX, boomY, i, i, 14)
        delay (5)
    end for
    for i : 1 .. 12
        drawfilloval (boomX, boomY, i, i, 7)
        delay (5)
    end for
end for

So run that and you will see what I mean.. Now when I transfered it into my actual program, it lagged like you wouldnt believe, yet didnt even do the explosion(effect).

::UPDATE::
hahahaha.. Okay.. Sorry. This was really dumb of me.. In my actual program that I sent you I forgot to tell the computer WHERE to draw all the explosions.. This is what it SHOULD look like..
code:
var boomX, boomY : int
proc Boom
    for i : 1 .. upper (rock)
        randint (boomX, rock (i).x - 10, rock (i).x + 10)
        randint (boomY, rock (i).y - 10, rock (i).y + 10)
    end for
    for i : 1 .. upper (rock) % <--Maybe thats the problem; should I NOT use that??
        drawfilloval (boomX, boomY, i, i, 12)
        delay (5)
    end for
    for i : 1 .. upper (rock) % <--Maybe thats the problem; should I NOT use that??
        drawfilloval (boomX, boomY, i, i, 14)
        delay (5)
    end for
    for i : 1 .. upper (rock) % <--Maybe thats the problem; should I NOT use that??
        drawfilloval (boomX, boomY, i, i, 7)
        delay (5)
    end for
end Boom

There we go.. But either way, it still lags like a biznitch.. And I dont know how to make it run swiftly and smooth like an Iced Cuppicinno...

Author:  jamonathin [ Sun May 22, 2005 10:04 am ]
Post subject: 

Flashkicks wrote:
Hay- in the new stuff you added, is there anywhere I can add something so that the laser will be depleted imiediately after it hits an asteroid?.. You know, so it doesnt keep shooting through killing all the asteroids behind it?.. I tried throwing some code in a few procedures and its just.. Okay, if it hits a rock, then another laser will immediatly shoot out even if Im NoT pressing/holding "Tab" .. Any ideas why that might be happeneding?? Ill keep playing around with it........
~Flashkicks


Yup, really simple. Look at his proc: proc shoots, and this line:
proc shoots wrote:
if hold (i) = 0 and done = 0 then

Now for a recap, 'hold (i)' tells the program that the current laser is free and can be reshot, and 'done' is just the delay inbetween the two lasers.

Now take a look at proc Shoot, and these lines:
proc Shoot wrote:

if hold (i) = 1 then
laserX (i) += dif * 3

The X-Value on the laser only moves when 'hold (i) = 1'. So if we look back at the other procedure (which is acutally after this procedure in the coding) It will reset any laser whos 'hold (i)' is equal to one.

Now, I'm not at home or school so I done have your current code, but where ever you check collision with the lasers, just make the 'hold (i)' equal to 0, and the program will/should take care of the rest.

Try it out and tell me how it goes.

Author:  Flashkicks [ Fri May 27, 2005 7:56 am ]
Post subject: 

Okay, in mycollision procedure where it checks if a rock has been hit, I added the one simple line of

hold (i) := 0

And as soon as the first asteroid is shot it gives the error

"Array subscript outta range"

.. And so I've tried SO many things and can not figure out what Im missing.. I've tried seperate for statements and changing hold(i) to different variables.. And still, nothing happens.. Any ideas??..
~Flashkicks

Author:  jamonathin [ Fri May 27, 2005 8:12 am ]
Post subject: 

Here's what you have to look at (since i dont know where your updated code is): what the array number on hold (var hold : array 1 .. ? of ***), then you need to check your for loop. Does the max number exceed the '?' in your hold variable? Does the for loop start at 0? Check those things out first, because that's the only reason you're getting that message.

Author:  Flashkicks [ Mon May 30, 2005 7:42 am ]
Post subject: 

woops, sorry-Here, Ill add the code in. Just comment out the lil intro part that requires the pic Wink..
code:
%%%%%%%%%%%%%%%%
%              %
%%%  JoyPad  %%%
%              %
%%%%%%%%%%%%%%%%
% Billy Boldt  %
%%%% % % % %%%%%


%% Button : ground (y) := orange
setscreen ("graphics:max;max, offscreenonly,nobuttonbar")
colorback (7)
cls
%% Variables %%
drawfillbox (0, 0, maxx, maxy, 7)
var key : array char of boolean
var shipX, shipY : int
shipX := 10
shipY := maxy div 2
var laserX, laserY, hold : array 1 .. 10 of int
var done, laserwait : int := 0 % Tells program when another laser is allowed to fire
var dif : int := 3 % Difference of movement
var level : array 1 .. 10 of int
var barX, barY, meterX, meterY : int
var track : int := 0
var track2 : int := 5
barX := 10
barY := maxy - 10
meterX := 10
meterY := maxy - 10
var stars : int := 100
var starx, stary, sdif : array 1 .. 100 of int
var death : boolean := false
var fig : int := Font.New ("Chiller:72")
var fig2 : int := Font.New ("Wide Latin:38")

for i : 1 .. stars
    starx (i) := Rand.Int (1, maxx) %Making a default for the star somewhere on the screen
    stary (i) := Rand.Int (1, maxy)
    sdif (i) := Rand.Int (2, 7)
end for

for i : 1 .. upper (hold) % Give the lasers initial values
    laserX (i) := 0     % Just defaulting everything to zero
    laserY (i) := 0
    hold (i) := 0
end for

proc moveStars
    for i : 1 .. stars
        starx (i) -= sdif (i) % Move them to their corresponding speed
        drawdot (starx (i), stary (i), white)
        if starx (i) < 0 then
            starx (i) := maxx
            stary (i) := Rand.Int (1, maxy)
            sdif (i) := Rand.Int (2, 7)
        end if
    end for
end moveStars

proc eraseShip
    drawfillbox (shipX - 10, shipY, shipX + 50, shipY + 25, 7)
end eraseShip

proc DrawShip
    drawfilloval (shipX + 25, shipY + 15, 25, 10, 28)
    drawfillbox (shipX - 10, shipY, shipX + 25, shipY + 10, 28)
    drawfilloval (shipX + 35, shipY + 20, 7, 3, 1)
    drawfillbox (shipX + 27, shipY + 2, shipX + 50, shipY + 4, 23)
    drawfillbox (shipX + 25, shipY, shipX + 27, shipY + 5, 23)
end DrawShip

proc Shoot
    for i : 1 .. upper (hold)
        if hold (i) = 1 then
            laserX (i) += dif * 3
            drawfillbox (laserX (i) + 51, laserY (i) + 2, laserX (i) + 75, laserY (i) + 4, 9)
            if laserX (i) > maxx then   % If its greater than the screen then reset it
                hold (i) := 0
            end if
        end if
    end for
end Shoot

var rock : array 1 .. 30 of
    record
        x, y, size, spd, dx : int
    end record

for i : 1 .. upper (rock) %The amount in array (which is 100)
    rock (i).x := Rand.Int (maxx, maxx + 400)
    rock (i).y := Rand.Int (50, maxy - 50)
    rock (i).size := Rand.Int (10, 20)
    rock (i).spd := round (19 - (rock (i).size) / 1.007) %rock(i).spd must be whole/int so we use round()
    if rock (i).spd = 0 then     %Checking if the speed is 0
        rock (i).spd := 1
    end if
end for

var grow, gx, gy, gend : array 1 .. upper (rock) of int %grow: size, gx: x-coord, gy: y-coord, gend: finishes explosion
for i : 1 .. upper (grow) %Making defaults for variables
    grow (i) := 0
    gend (i) := 0
end for

proc newRock (thing : int) %Created a new asteroid, same material from above
    rock (thing).x := Rand.Int (maxx, maxx + 400)
    rock (thing).y := Rand.Int (0, maxy)
    rock (thing).size := Rand.Int (10, 20)
    rock (thing).spd := round (19 - (rock (thing).size) / 1.007) %rock(i).spd must be whole/int so we use round()
    if rock (thing).spd = 0 then     %Checking if the speed is 0
        rock (thing).spd := 1
    end if
end newRock

proc asteroids
    % Make move
    for i : 1 .. upper (rock)
        rock (i).x -= rock (i).spd
        drawfilloval (rock (i).x, rock (i).y, rock (i).size, rock (i).size, 62) % 43 orange
        drawoval (rock (i).x, rock (i).y, rock (i).size, rock (i).size, 37) % 42 For effect
        if rock (i).x <= 0 - 25 then % Resets asteroids back when gone off screen
            newRock (i)
        end if
    end for
end asteroids

proc Lose % Game Over
    cls
    color (12)
    locatexy (10, maxy - 10)
    Font.Draw ("YOU DIED", maxx div 2 - 170, maxy div 2, fig, 12)
    death := true
end Lose

proc LifeMeter
    drawbox (barX, barY, barX + 200, barY + 10, 12)
    drawfillbox (meterX + 1, meterY + 1, meterX + track2, meterY + 9, 43)
    if meterX + track2 > 200 then
        Lose
    end if
    View.Update
    track2 += 5
end LifeMeter

proc CrashCheck
    for i : 1 .. upper (rock)
        if rock (i).x < shipX + 60 and rock (i).x > shipX - 20 and rock (i).y > shipY and rock (i).y < shipY + 25 then
            LifeMeter
        end if
    end for
end CrashCheck

proc Boom (boomx, boomy, current, thing : int)
    if thing <= rock (current).size and gend (current) not= -7 then %Red Circle
        drawfilloval (boomx, boomy, thing, thing, 12)
        if thing <= rock (current).size then
            gend (current) := -7 %Just a number, moving onto the yellow/orange
        end if
    elsif gend (current) = -7 then
        drawfilloval (boomx, boomy, rock (current).size, rock (current).size, 12) %red
        drawfilloval (boomx, boomy, (thing - rock (current).size) * 2, (thing - rock (current).size) * 2, 43) %orange
        drawfilloval (boomx, boomy, thing - rock (current).size, thing - rock (current).size, 14) %yellow
        if thing - rock (current).size >= round (rock (current).size / 2) then %Once its too big, its done.
            gend (current) := 0
        end if
    end if
end Boom
var count : int := 0
proc RockHit
    for i : 1 .. upper (rock)     % Checking Every Rock
        for x : 1 .. upper (hold)     % Checking Every Laser
            for q : 0 .. 24     % Checking Ever Point In Every Laser
                if laserY (x) >= rock (i).y - 15 and laserY (x) <= rock (i).y + 15 and laserX (x) + q >= rock (i).x - 10 and laserX (x) + q <= rock (i).x + 10 then
                    grow (i) := 1                     %Starting the growth of the explosion
                    gx (i) := rock (i).x %Making the x-coord equal to gx (so it can hold onto it)
                    gy (i) := rock (i).y %Making the y-coord equal to gx (so it can hold onto it)
                    newRock (i) %Creating a new rock, using 'i' which is the current one shot down
                    count := count + 1
                end if
            end for
        end for
    end for
end RockHit
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
proc ReAssignValues
    for j : 1 .. upper (rock)
        for i : 1 .. upper (rock)             %The amount in array (which is 100)
            rock (i).x := Rand.Int (maxx, maxx + 400)
            rock (i).y := Rand.Int (50, maxy - 50)
            rock (i).size := Rand.Int (10, 20)
            rock (i).spd := round (19 - (rock (i).size) / 1.007)     %rock(i).spd must be whole/int so we use round()
            if rock (i).spd = 0 then     %Checking if the speed is 0
                rock (i).spd := 1
            end if
            grow (i) := 0
            gend (i) := 0
        end for
    end for
end ReAssignValues
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
proc DrawShipAndShoot
    cls
    moveStars
    Shoot
    DrawShip
    asteroids
    CrashCheck
    RockHit
    for i : 1 .. upper (grow)     %Checking if any asteroids have exploded
        if grow (i) > 0 then     %Start exploding if they are
            grow (i) += 1     %Changing the radius of the explosion
            Boom (gx (i), gy (i), i, grow (i))     %Boom (x-coord, y-coord, which asteroid, radius of a'roid)
            if gend (i) = 1 then     %In the proc 'Boom' we make gend 1 when its finished
                grow (i) := 0     %Now we can reset that asteroids explosion for future use
            end if
        end if
    end for
    View.Update
end DrawShipAndShoot

proc shoots
    for i : 1 .. upper (hold)
        if hold (i) = 0 and done = 0 then     % If the current laser is free to be shot
            laserX (i) := shipX
            laserY (i) := shipY
            hold (i) := 1     % Making it taken
            done := 1     % Telling the program to delay before shooting again
        end if
    end for
end shoots
var Asteroids : int := Pic.FileNew ("Asteroids.bmp")

loop
    Pic.Draw (Asteroids, 0, 0, picMerge)
    % Font.Draw ("ASTEROIDS", 40, 400, fig2, 28)
    for decreasing i : 28 .. 21
        Font.Draw ("ASTEROIDS", 40, 400, fig2, i)
        delay (50)
        View.Update
    end for
    for i : 21 .. 28
        Font.Draw ("ASTEROIDS", 40, 400, fig2, i)
        delay (50)
        View.Update
    end for
    exit when hasch
end loop
Pic.Free (Asteroids)
delay (1000)
proc NextLevel
    cls
    drawfillbox (0, 0, maxx, maxy, 7)
    Font.Draw ("Next Level", 40, 400, fig, 28)
    delay (2000)
    count := 0
    View.Update
end NextLevel
loop
    loop
        Input.KeyDown (key)
        if key (KEY_LEFT_ARROW) and shipX > dif then
            eraseShip
            shipX -= dif
        elsif key (KEY_RIGHT_ARROW) then
            eraseShip
            shipX += dif
        end if
        if key (KEY_UP_ARROW) then
            eraseShip
            shipY += dif
        elsif key (KEY_DOWN_ARROW) and shipY > 30 then
            eraseShip
            shipY -= dif
        end if
        if key (KEY_TAB) and done = 0 then % If we're allowed to shoot another laser
            shoots
        end if
        if done = 1 and laserwait < 20 then % Waiting untill laserwait is 20
            laserwait += 1 % This will make a delay of [20 * (whatever delay is set)] before shooting again
        elsif done = 1 and laserwait >= 20 then % Now its ok to shoot, so reset laserwait
            laserwait := 0
            done := 0
        end if
        if shipX <= 0 then % Checking for ship bounderies
            shipX := 0
        elsif shipX >= maxx - 40 then
            shipX := maxx - 40
        end if
        if shipY >= maxy - 30 then
            shipY := maxy - 30
        end if
        DrawShipAndShoot         % Draw everything
        if death = true then % Once you die, the game is over.
            exit
        end if
        if count = 10 then
            ReAssignValues
            % delay (1000)
            NextLevel
            exit
        end if
    end loop
end loop


Also, you will notice my "Levels" isnt very good either.. It only shows on screen for like a split second. Sad I cant get that to werk nicely either.. Anyways.. Im sure you know where to look and you understand what does what.. Im gonna keep trying to get my levels to look a lil better here Rolling Eyes lol... TTYL
~Flashkicks

Author:  jamonathin [ Mon May 30, 2005 8:17 am ]
Post subject: 

Your View.Update was in the wrong spot. View.Update must go before an delay.
code:

proc NextLevel
    cls
    drawfillbox (0, 0, maxx, maxy, 7)
    Font.Draw ("Next Level", 40, 400, fig, 28)
    View.Update
    delay (2000)
    count := 0
end NextLevel


Also, use >= rather than =, it's more accurate, because you could be on 9 and hit 2 at the same time (juss happened to me)
code:

if count >= 10 then
            ReAssignValues
            % delay (1000)
            NextLevel
            exit
        end if


Other than that I dont see any other errors, just make your levels better, and reset the asteroids to maxx. There should be more asteroids per level. All you ahve to do is add 1 to a variable every time you use the nextlevel proc. Then change how many asteroids move accordingly. You sould also have to shoot down more than 10 asteroids.

Author:  Flashkicks [ Mon May 30, 2005 8:55 am ]
Post subject: 

Haha... Silly little errors.. I hate those.. Maybe Im too unfocused and Im not conentrating enough.. Im trying to make my final project at the same time and not just that- but the 'boss' for my Asteroids game as well..lol.. *phew*.. But thanks for that.. But now Im still stuck on the stupid non-depleting missiles...lol... *gRRRRrrr*
~Flashkicks

Author:  jamonathin [ Mon May 30, 2005 1:40 pm ]
Post subject: 

Your "hold" variable tells the program if that laser can be shot, so just add a "hold (x) := 0" into your proc, like this:

Turing:

roc RockHit
    for i : 1 .. upper (rock)     % Checking Every Rock
        for x : 1 .. upper (hold)     % Checking Every Laser
            for q : 0 .. 24     % Checking Ever Point In Every Laser
                if laserY (x) >= rock (i).y - 15 and laserY (x) <= rock (i).y + 15 and laserX (x) + q >= rock (i).x - 10 and laserX (x) + q <= rock (i).x + 10  then
                    grow (i) := 1                     %Starting the growth of the explosion
                    gx (i) := rock (i).x %Making the x-coord equal to gx (so it can hold onto it)
                    gy (i) := rock (i).y %Making the y-coord equal to gx (so it can hold onto it)
                    hold (x) := 0 %Laser no longer alive
                    newRock (i) %Creating a new rock, using 'i' which is the current one shot down
                    count := count + 1
                end if
            end for
        end for
    end for
end RockHit

Author:  Flashkicks [ Tue May 31, 2005 9:46 am ]
Post subject: 

Why is it that you make things seem so simple??..lol.. It werks great.. I think its bcuz I kept trying to change that variable in a different procedure.. Unless I tried it but used the wrong array Rolling Eyes Either way- the game is 99.9% DONE!! I made this really cool 'boss' thingy and it werks already. But the Asteroids game is supposed to be run with a joystick (which I got werking already as well Laughing ) But incorporating the joystick into my 'boss' program is giving me huge difficulties. I KNOW its possible but its just a matter of figuring out how it will werk... Ill figure it out.. Anyways-enuff rambling.. Thanks a bunch. Ill give you my whole proggy in a PM once I am COMPLETELY 100% done with it. (dont werry, you will be able to use the keyboard and not the joystick...)
~Flashkicks


: