Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Record/Bullet fire
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Flikerator




PostPosted: Sun Feb 20, 2005 2:55 pm   Post subject: Record/Bullet fire

code:

var ammo : int := 50
type Bullet :
    record
        x, y : int
    end record
var bullet : array 1 .. ammo of Bullet

colourback (black)
cls
colour (white)
View.Set ("offscreenonly")
Mouse.ButtonChoose ("multibutton")

var x1, y1 : int := 200
var x2, y2, mz : int
var L, M : real

loop
    cls
    Mouse.Where (x2, y2, mz)
    L := 50
    M := Math.Distance (x1, y1, x2, y2)
    Draw.FillOval (x1, y1, 10, 10, yellow)
    if mz = 1 and ammo > 0 then
        bullet (ammo).x := x1
        bullet (ammo).y := y1
        for i : 25 .. 300
            Draw.FillOval (bullet (ammo).x + i, bullet (ammo).y + i, 5, 5, yellow)
            View.Update
            Draw.FillOval (bullet (ammo).x + i, bullet (ammo).y + i, 5, 5, black)
            delay (1)
            locate (1, 1)
            put ammo
        end for
        ammo := ammo - 1
    end if
    locate (1, 1)
    put ammo
    delay (100)
    View.Update
end loop


When you click it fires. Simple. I used a record (Im new at records). I want it to fire towards the mouse. I have the distance from the circle to the mouse (Var L) if that helps. I just want it to fire towards the mouse. And I also want it to be able to move and have the bullets still moving, WITHOUT a process if possible.

Btw - I know how to do movement, I didn't add it in because I wanted to make the code smaller and easier to fix what I want fixed.

Quote:

When a new clip is added to the gun all bullet records will be cleared and replaced with the new "clip". It will then start at the max amount of the clip (In this case 50).
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Sun Feb 20, 2005 9:40 pm   Post subject: (No subject)

Don't use a for loop like that. You'll have a for loop, but it will move ALL your bullets. You need to add vx and vy (x velocity and y velocity) to your bullet record. And each time through the main loop, a for loop goes through all elements of the array bullets and updates their positions.
Also, I don't know why you're using an array. You should be using a flexible array, because you're not always going to have 50 bullets in the air at once (given the cooldown and how far they go, I doubt you'll EVER have anywhere near 50 bullets in the air at once.)
As for firing towards the mouse, you know deltaX and deltaY. Use those, in conjunction with the speed of the bullet (a value that you pick) to determine the x and y velocity of the bullet.
Sorry I can't give much more help than that at the moment. Best of luck, I'll help more tommorow if you still need it.
-Cervantes
Flikerator




PostPosted: Mon Feb 21, 2005 4:47 pm   Post subject: (No subject)

I have the xy, yx as velocity in the record now. I didn't use a Flexible array becuase I forgot how to use them, I will have to reread the tutorial on flexible arrays. It makes sense with what I can remember though.

As for DeltaX and DeltraY I am not sure what you are refering to.
Cervantes




PostPosted: Mon Feb 21, 2005 7:54 pm   Post subject: (No subject)

xy, yx as velocity? No offense, but those aren't very good variable names for velocity.
Delta (symbol is an equilatoral triangle) something just means the difference betweent two values of that something.
So, if we have a point at (5, 5) and a point at (-2, 5), the x2 value is -2 and the x1 value is 5. So, delta x = x2 - x1 = -2 - 5 = -7.

In the below code, I used deltaX and deltaY, in conjunction with the length of the hypotenuese (which I calculated by Pythagoras) to get a ratio of movement in the x plane to movement in the y plane. Then, I multipled that ratio by the speed of the bullet.

Turing:

var ammo : int := 50
var bullet :
    record
        x, y : real
        vx, vy : real
        speed : real
    end record

bullet.x := 200 % \
%  |--> bullet is inside the player.  :P
bullet.y := 200 % /
bullet.vx := 0
bullet.vy := 0  %really, this is just here so that the code won't crash because "variable has no value" the first time through
%ideally, you'll have a flexible array, so at the start, you won't even have any bullets.
bullet.speed := 4

colourback (black)
cls
colour (white)
View.Set ("offscreenonly")
Mouse.ButtonChoose ("multibutton")

var x1, y1 : int := 200
var x2, y2, mz : int
var L, M : real
L := 50
var deltaX, deltaY, hyp : real

loop

    Mouse.Where (x2, y2, mz)
    M := Math.Distance (x1, y1, x2, y2)
    if mz = 1 then
        bullet.x := 200 % \
        bullet.y := 200 % |--> Go back to inside the player
        deltaX := x2 - x1
        deltaY := y2 - y1
        hyp := (deltaX ** 2 + deltaY ** 2) ** 0.5
        bullet.vx := deltaX / hyp * bullet.speed
        bullet.vy := deltaY / hyp * bullet.speed
    end if
    bullet.x += bullet.vx
    bullet.y += bullet.vy
    cls
    Draw.FillOval (round (bullet.x), round (bullet.y), 5, 5, white)
    Draw.FillOval (x1, y1, 10, 10, yellow)
    View.Update
    delay (10)
end loop
Flikerator




PostPosted: Tue Feb 22, 2005 3:40 pm   Post subject: (No subject)

So if I wanted to make multiple bullets on the screen I would use a felxible array right? Using it in conjunction with my ammo to choose which array it is?

EDIT - You claim not to be a Turing Guro? At least a math genius Razz
Cervantes




PostPosted: Tue Feb 22, 2005 8:48 pm   Post subject: (No subject)

Yes, use a flexible array if you want to use multiple bullets.
Do not use it in conjunction with ammo to determine "which array it is" (by which I assume you mean which element it is).

When you fire a bullet, create a new element of the bullet record. Also, when you fire a bullet, subtract one from ammo. If you try to fire a bullet (ie. you click the mouse), make sure that ammo > 0.

So:
Turing:

if mb ~= 0 and ammo > 0 then
    new bullet, upper (bullet) + 1
    bullet (upper (bullet)).x := x
    bullet (upper (bullet)).y := y
    bullet (upper (bullet)).vx := vx
    bullet (upper (bullet)).vy := vy
    ammo -= 1
end if


Mind you, doing that upper (bullet) thing over and over again is not so nice-looking, plus turing has to re-evaluate the value of upper (bullet) each time. SIDE NOTE: Does anyone know if it's faster to re-evaluate upper (bullet) a bunch of times or to evaluate it once, store it to a variable, and then use that variable a bunch of times?
Anyways, it would be nice if we had a local variable in there. A local variable (not a global variable) implies this is inside a procedure. Yes, having a createBullet procedure would be a good idea. And don't make it always make the bullet at the location of the player -- have it make the bullet based on the parameters you pass INTO the procedure. It's better that way. Smile

Math genius? Heh, I don't mean to be condescending or anything, but this is not hard math. Fermat is tommorow. Some of that stuff is hard. Confused Rolling Eyes Dance
MihaiG




PostPosted: Wed Feb 23, 2005 5:08 pm   Post subject: (No subject)

pfft. fermat Rolling Eyes i tried the 2004 version i got like 98 Confused trhe pascal was really easy today i stole a copy.. Twisted Evil Twisted Evil Razz
MihaiG




PostPosted: Wed Feb 23, 2005 5:09 pm   Post subject: (No subject)

pfft. fermat Rolling Eyes i tried the 2004 version i got like 98 Confused trhe pascal was really easy today i stole a copy.. Twisted Evil Twisted Evil Razz
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: