Computer Science Canada

How to take a "snapshot" of the XY coordinates to fire a bullet from an oval

Author:  vapour99 [ Sun Jun 01, 2008 6:44 pm ]
Post subject:  How to take a "snapshot" of the XY coordinates to fire a bullet from an oval

I am currently coding a SHUMP (spaceship shooter) game for my Grade 10 coding class and have encountered a simple problem:
When I move the ship, the bullets shot from the ship move as well.
The game is in its very early stages with simple titles and animations, but I need to solve this problem before I can continue programming the logic part of the game.

here is the code:

code:
var win, win2 : int
var message : string
var keyup : real
var count : real
var chars : array char of boolean
var x1, x2, y1, y2, xb1, yb1 : int
var x3, x4, y3, y4 : int
var clr : int
var shot : int
var mx, my, button : int
var boomy1, boomx1, boomy2, boomx2 : int
var continue : string

win := Window.Open ("position:middle,center, graphics: 640;800, Title: pauls moveoval")

View.Set ("offscreenonly")

x1 := 320
y1 := 20
x2 := 20
y2 := 20
clr := 2
boomx1 := 1
boomy1 := 1

process oval1
    Draw.FillOval (x1, y1, x2, y2, 1)
end oval1

process bullet %need to take a snapshot of the x/y cordinates of the mouse and fire a bullet straight up without (so mouse wont move bullet) ALSO NEEDS TO STOP FLASHING (viewupdate)
    xb1 := mx
    yb1 := my
    shot := 1
    for a : 1 .. 1000

        drawfilloval (xb1, yb1 + a, 10, 10, brightred)
        delay (1)
        drawfilloval (yb1, yb1 + a, 10, 10, 0)

    end for

end bullet

put "Welcome to Paul's Space shooter"
put " "
put " Mouse Rclick = shoot"
put " Mouse = movment"
put " "
put "type y to play, n to quit"
View.Update
get continue

if continue = "n"
        then
    Window.Close (win)
end if


loop
    shot := 0
    if continue = "n"
            then
        exit
    end if

    mousewhere (mx, my, button)
    Draw.FillOval (mx, my, x2, y2, 1)

    if button = 1 then
        fork bullet
    end if
    if y1 >= 800
            then
        y1 := 800
    end if

    if x1 >= 640
            then
        x1 := 640
    end if

    if x1 <= 0
            then
        x1 := 0
    end if

    if y1 <= 0
            then
        y1 := 0
    end if

    View.Update
    cls
    Draw.FillOval (mx, my, x2, y2, 1)
    if shot = 1 then
        drawfilloval (xb1, yb1, 10, 10, brightred)
    end if

end loop
win2 := Window.Open ("position:middle,center, graphics: 200;10, Title: bye!")
put "thx for playing!"
delay (1000)
Window.Close (win2)


I also have a minor problem with View.Update on the "bullets" (flashing)

Any help given to a "newbe" programmer is much appreciated. I hope to contribute to the community as I learn.
-Regards; paul

Author:  TheWanderer [ Sun Jun 01, 2008 10:35 pm ]
Post subject:  Re: How to take a "snapshot" of the XY coordinates to fire a bullet from an oval

very good start (' ')b

looks like you're only keeping track of the bullets' x location with a single variable

solution contains many things to look into:
- you'll have to set a limit of bullets to be able to be shot from the ship
- you'll have to set a delay between firing a bullet, otherwise you'll start lagging for the next idea

- set up an array up to the limit of bullets that will keep track of bullet locations
- add to this array when the ship fires

its a little difficult for gr 10, but if you're determined, its not that hard to figure out in the end.
i think the major problem you'll encounter is going beyond array limits

another solution for you is to create just one bullet that moves very fast
keep track of its location separate from your ship position. but it'd be very boring right? XD

yes.. easy takes away from cool XD

Author:  vapour99 [ Mon Jun 02, 2008 7:43 am ]
Post subject:  RE:How to take a "snapshot" of the XY coordinates to fire a bullet from an oval

I am going to have to use the array option as a delay in the process halts the entire program and a delay in the if statements with the forks will create delays in the entire program is the key is pressed

Ill look into Arrays then.

You mention keeping track of a bullets position seperate from the ships position; how are you doing that without moving the ship as well?

Author:  Insectoid [ Mon Jun 02, 2008 7:57 am ]
Post subject:  RE:How to take a "snapshot" of the XY coordinates to fire a bullet from an oval

INstead of a delay, use a counter. Every time the game loops, the counter goes down by one. when it reches zero, reset it and fire another bullet!

Author:  vapour99 [ Mon Jun 02, 2008 8:32 am ]
Post subject:  RE:How to take a "snapshot" of the XY coordinates to fire a bullet from an oval

it should work, but now I cannot see the bullets;

here is the code after today's class (i am trying to fixc the bullets before I try to fix the enemies)

code:
var win, win2 : int
var message : string
var keyup : real
var count : real
var chars : array char of boolean
var x1, x2, y1, y2, xb1, yb1 : int
var x3, x4, y3, y4 : int
var clr : int
var shot : int
var mx, my, button : int
var boomy1, boomx1, boomy2, boomx2 : int
var continue : string
var random, random1 : int
var level : string
var scnt, acnt : int

win := Window.Open ("position:middle,center, graphics: 500;640, Title: Space shooter v.1")

View.Set ("offscreenonly")

x1 := 320
y1 := 20
x2 := 20
y2 := 20
clr := 2
boomx1 := 1
boomy1 := 1

process bullet %need to take a snapshot of the x/y cordinates of the mouse and fire a bullet straight up without (so mouse wont move bullet) ALSO NEEDS TO STOP FLASHING (viewupdate)
    xb1 := mx
    yb1 := my
    for a : 1 .. 660
        drawfilloval (xb1, yb1 + a, 10, 10, yellow)
        delay (1)
        drawfilloval (yb1, yb1 + a, 10, 10, 0)
    end for
end bullet

%process randships
%random1 := Rand.Int (0,50)
%if random1 > 25 then
%    random := Rand.Int (0, 500)

%    Draw.FillOval (random, 500, 10, 10, purple)
%    for a : 1 .. 801
%        drawfilloval (random, 500 - a, 10, 10, green)
%        delay (1)
%        drawfilloval (random, 500 - a, 10, 10, 0)
%    end for
%end if
%end randships

put "Welcome to Paul's Space shooter"
put " "
put " Mouse Rclick = shoot"
put " Mouse = movment"
put " "
put "type y to play, n to quit"
View.Update
get continue

if continue = "n"
        then
    Window.Close (win)
end if

scnt := 0


loop
    loop
        scnt := scnt + 1

        if scnt = 10 then
    if button = 1 then
                fork bullet
                View.Update
            end if
        end if
        scnt := 0
        exit
    end loop
    level := "y"
    if continue = "n"
            then
        exit
    end if

    mousewhere (mx, my, button)
    Draw.FillOval (mx, my, x2, y2, 1)

    if my >= 640
            then
        my := 640
    end if

    if mx >= 500
            then
        mx := 500
    end if

    if mx <= 0
            then
        mx := 0
    end if

    if my <= 0
            then
        my := 0
    end if

    View.Update
    cls

    Draw.FillOval (mx, my, x2, y2, 1)

    %    if button = 1 then
    %        fork bullet

    %    end if

    %    if level = "y" then
    %        fork randships

    %    end if

end loop

win2 := Window.Open ("position:middle,center, graphics: 200;10, Title: bye!")
put "thx for playing!"
delay (1000)
Window.Close (win2)

Author:  vapour99 [ Mon Jun 02, 2008 3:57 pm ]
Post subject:  RE:How to take a "snapshot" of the XY coordinates to fire a bullet from an oval

I have implemented the correct array code; thanks for the recommendation TheWanderer


: