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

Username:   Password: 
 RegisterRegister   
 Shooting multiple times
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ficca951




PostPosted: Thu Jan 19, 2012 3:46 pm   Post subject: Shooting multiple times

What is it you are trying to achieve?
I am trying to create a shooting game.


What is the problem you are having?
I am having an issue with having multiple bullets on screen at once.


Describe what you have tried to solve this problem
I have tried searching around on compsci but nothing appears to be exactly what I am looking for.

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

I figured that using a process for shooting would allow me to shoot multiple times without stopping any other bullets, unfortunately I keep receiving the error: "Process "shoot": Variable has no value." each time I press the button to shoot. Any advice would be appreciated. Right now the only thing you can do is press space to shoot, but I intend to have mobility later on. I just really want to get over this hiccup before I continue.

Turing:


var bullet_x, bullet_y : flexible array 0 .. 1 of int
var i : int := 0
var input : array char of boolean


process shoot
    loop
        drawfilloval (bullet_x (upper (bullet_x)), bullet_y (upper (bullet_y)), 1, 1, 7)
        delay (10)
        drawfilloval (bullet_x (upper (bullet_x)), bullet_y (upper (bullet_y)), 1, 1, 0)
        bullet_y (upper (bullet_y)) += 5
        exit when bullet_y (upper (bullet_y)) > maxy + 50
    end loop
end shoot

loop
    Input.KeyDown (input)
    if input (' ') then
        new bullet_x, upper (bullet_x) + 1
        new bullet_y, upper (bullet_y) + 1
        bullet_x (upper (bullet_x)) := 10
        bullet_y (upper (bullet_y)) := 10
        fork shoot
    end if
end loop



Please specify what version of Turing you are using
4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Alex C.




PostPosted: Thu Jan 19, 2012 4:08 pm   Post subject: RE:Shooting multiple times

well its not "shoot" that has no value, its the variables that are IN that process such as "bulletx & bullety".

therefore you have to give that varaible array a value somehow
ficca951




PostPosted: Thu Jan 19, 2012 4:12 pm   Post subject: Re: Shooting multiple times

I assumed I was doing so with

Turing:

bullet_x (upper (bullet_x)) := 10
bullet_y (upper (bullet_y)) := 10
Alex C.




PostPosted: Thu Jan 19, 2012 4:16 pm   Post subject: RE:Shooting multiple times

oh oops, im sorry. maybe if you don't make it a process... the bullet doesn't move though :/
copthesaint




PostPosted: Thu Jan 19, 2012 4:17 pm   Post subject: Re: Shooting multiple times

First issue is you are using a process, Which is BAD! unless its for music or internet dont use it.

Second you need to start your flexible array 0..-1 of int because the way you are doing declairing your array you have two null values, bullet_x (0) and (1)

third thing is that in your process you are always ONLY manipulating the upper () value of bullet_x which means if there are 5 bullets you are only drawing the 5th. You need to manipulate them all by putting it in a for loop from the first value (0) to the last value (upper (bullet_x))

forth thing is you should really put the values of the bullet in a type. Meaning instead of two variables make it
Turing:
type bullet :
record
x : int
y : int
end record

var bullets : flexible array 0..-1 of bullet
bullets(upper(bullets)).x
bullets(upper(bullets)).y


Thats all I have to complain about Smile
copthesaint




PostPosted: Thu Jan 19, 2012 4:18 pm   Post subject: Re: Shooting multiple times

Here is an example for using a flexible array.


Turing:
View.Set ("Graphics, Offscreenonly, nobuttonbar")
type Dot :
    record
        x : int
        y : int
        c : int
    end record

var x : flexible array 0 .. -1 of Dot
var mX, mY, mB : int
loop
    mousewhere (mX, mY, mB)
    if mB = 1 then
        new x, upper (x) + 1
        x (upper (x)).x := mX
        x (upper (x)).y := mY
        x (upper (x)).c := Rand.Int (0, 255)

    end if
    for i : 0 .. upper (x)
        drawdot (x (i).x, x (i).y, x (i).c)
    end for
    View.Update
    cls
end loop
Dreadnought




PostPosted: Thu Jan 19, 2012 4:29 pm   Post subject: Re: Shooting multiple times

First off, you don't need processes and, in my opinion, they are usually evil.

You're main code is itself a process. When you fork a process you tell Turing to run both processes simultaneously. That sounds nice, and you figure that with most computers being dual or quad core that this would be achievable. But Turing does not run concurrent processes side by side. Instead, it switches between the processes in a quick and mostly random fashion.

Now, keeping this idea in mind consider what's going when you run you program. Your "shoot" process updates a variable in a flexible array. In your case it checks what the highest index of the array is and updates that value. That's already a behavior that I assume is undesired, since you could have 10 processes and they would all only affect the upper index of the array.

Also, recall that the execution in random. Say you hit space, you create a new index in the array, but now Turing switches to one of your "shoot" processes and tries to update the highest index of the array. But we just created that index and haven't had time to assign it an index. This is the cause of your errors.

There are ways of making this work with processes but I strongly suggest you don't make use of processes for this. Instead, I recommend that you view things differently. You have two arrays representing the coordinates of your bullets. You want to update these coordinates. You could have a process for each but is each process doing that is unique to itself? The only thing that is unique might be the index of the array that the process is updating. But if you could somehow use a loop to go through all the indices and update each one then you wouldn't need multiple processes.

Also, once you get your bullets working I suggest you try to make sure that the arrays don't become full of old bullet coordinates that you don't need.

Hope this helps.

EDIT: looks like copthesaint beat me to this.
ficca951




PostPosted: Thu Jan 19, 2012 4:46 pm   Post subject: Re: Shooting multiple times

Thank you for the input Smile

I have listened to what you guys have said and it is definitely understood that PROCESSES ARE BAD. Also I just realized my error about only using the the latest array Razz. So I tried without processes, and I just can't seem to figure out how to fire a bullet and fire another quickly after whilst keeping track of both. In my mind I'm thinking that there must be a way to accomplish this but I can't see how to interrupt the current bullet with another and not stop the initial bullet.

Here's what I have:

Turing:

var bullet_x, bullet_y : flexible array 0 .. 0 of int
var i : int := 0
var input : array char of boolean

proc shoot
    loop
        for a : 1 .. upper (bullet_x)
            drawfilloval (bullet_x (a), bullet_y (a), 2, 2, 7)
            delay (10)
            drawfilloval (bullet_x (a), bullet_y (a), 2, 2, 0)
            bullet_y (a) += 5
        end for
    end loop
end shoot

loop
    Input.KeyDown (input)
    if input (' ') then
        new bullet_x, upper (bullet_x) + 1
        new bullet_y, upper (bullet_y) + 1
        bullet_x (upper (bullet_x)) := 10
        bullet_y (upper (bullet_y)) := 10
        shoot
    end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
ficca951




PostPosted: Thu Jan 19, 2012 5:03 pm   Post subject: Re: Shooting multiple times

Okay, this seems logical to me but it has a weird glitchiness to it. :/

Turing:

var bullet_x, bullet_y : flexible array 0 .. 0 of int
var i : int := 0
var input : array char of boolean

loop
    Input.KeyDown (input)
    if input (' ') then
        new bullet_x, upper (bullet_x) + 1
        new bullet_y, upper (bullet_y) + 1
        bullet_x (upper (bullet_x)) := 10
        bullet_y (upper (bullet_y)) := 10
    end if
    for a : 1 .. upper (bullet_x)
        drawfilloval (bullet_x (a), bullet_y (a), 2, 2, 7)
        delay (10)
        drawfilloval (bullet_x (a), bullet_y (a), 2, 2, 0)
        bullet_y (a) += 5
    end for
end loop
Alex C.




PostPosted: Thu Jan 19, 2012 5:25 pm   Post subject: RE:Shooting multiple times

actually you just need to use

setscreen ("offscreenonly") and View.Update...
there is a tutorial for that
copthesaint




PostPosted: Thu Jan 19, 2012 5:31 pm   Post subject: RE:Shooting multiple times

IF you cannot figure out why its drawing the way
it is, then I suggest not even using flexible arrays and learn the language more since you likly just copied n pasted your code.
if someone else wrote it for you, ask them for help.
Raknarg




PostPosted: Thu Jan 19, 2012 6:43 pm   Post subject: RE:Shooting multiple times

I found that using a flexible array for bullets was mroe trouble than it was worth. I'm assuming the idea behind that was "Well, Idk how many bullets are going to be on the screen at any time, so I'm just going to change the array to the bullets!" That is unecessary. Instead, I would recommend you use a regular array with an upper bound that you know won't ever be attained. (If you think there will be about 15 bullets, make the array size 20).

The problem you'll run into with this is that eventually the amount of bullets will become so large that the program starts to lag.
Alex C.




PostPosted: Thu Jan 19, 2012 8:33 pm   Post subject: RE:Shooting multiple times

would you like to see how i did shooting?
just replace all the Pics with like dots or something

Turing:


View.Set ("nobuttonbar,offscreenonly")
colorback (30)
var shipx, shipy : int := 0

var font1 : int := Font.New ("bank_gothic:12")
var ship : int := Pic.FileNew ("Cargo_Ship.gif")
var BULLET : int := Pic.FileNew ("Bullets.gif")
var chars : array char of boolean

var bullets : array 1 .. 3 of boolean := init (false, false, false)
var bulletx : array 1 .. 3 of int:= init (0, 0, 0)
var bullety : array 1 .. 3 of int:= init (0, 0, 0)
var Reloading : int := 20
var x, y : array 1 .. 10 of int
for i : 1 .. 10
    x (i) := maxx div 2
end for
for i : 1 .. 10
    y (i) := 15
end for

loop
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) then
        shipy := shipy + 3
    end if
    if chars (KEY_RIGHT_ARROW) then
        shipx := shipx + 3
    end if
    if chars (KEY_LEFT_ARROW) then
        shipx := shipx - 3
    end if
    if chars (KEY_DOWN_ARROW) then
        shipy := shipy - 3
    end if
    if chars (chr (27)) then
        Window.Hide (defWinID)
    end if
    if chars (' ') and Reloading >= 20 then
        if bullets (1) = false then
            Reloading := 0
            bullets (1) := true
            bulletx (1) := shipx
            bullety (1) := shipy
        elsif bullets (2) = false then
            Reloading := 0
            bullets (2) := true
            bulletx (2) := shipx
            bullety (2) := shipy
        elsif bullets (3) = false then
            Reloading := 0
            bullets (3) := true
            bulletx (3) := shipx
            bullety (3) := shipy
        end if
    end if

    % Moves your bullet forward
    for i : 1 .. 3
        if bullets (i) = true then
            bullety (i) += 5
        end if
    end for

    % Slight collison detection to make sure the ship is within boundaries
    if shipx <= 0 then
        shipx += 3
    end if
    if shipx >= maxx - 100 then
        shipx -= 3
    end if
    if shipy <= 0 then
        shipy += 3
    end if
    if shipy >= maxy - 100 then
        shipy -= 3
    end if

    % Destroys the bullet if it is off screen
    for d : 1 .. 3
        if bullety (d) >= maxy then
            bullets (d) := false
        end if
    end for

    % LOLZ  :P
    Font.Draw ("Use the arrow keys to move the ship,", 5, maxy - 30, font1, brightblue)
    Font.Draw ("and press ESC to exit", 5, maxy - 48, font1, brightblue)
    Pic.Draw (ship, shipx, shipy, 2)

    % Draws the bullet in relation to the ship
    for b : 1 .. 3
        if bullets (b) = true then
            Pic.Draw (BULLET, bulletx (b) + 40, bullety (b) + 70, 2)
        end if
    end for

    % The reload counter with delay...
    Reloading += 2
    delay (10)

    % Updates the scren then refreshes it...
    View.Update
    cls

end loop

evildaddy911




PostPosted: Thu Jan 19, 2012 11:53 pm   Post subject: Re: Shooting multiple times

If you dont know how many bullets you will need, then do that first.
One way is, if the wondow borders are the boundaries for your game, then the upper should be round( Math.Distance(0,0,maxx,maxy) ). then, in order to reuse bullets, you need a boolean "live" varable.
Turing:
for i:
 if live(i) = true then
Drawbullet
Check_collision
% etcetara
end if

Then when you fire, you need to find an unused bullet to fire,
Turing:
loop
Bullet_counter+=1
if live (bullet_counter)= false then
Bullet_to_use:= bullet_counter
end if
end loop
Live(bullet_counter) := true
Bullet_counter:=0
ficca951




PostPosted: Fri Jan 20, 2012 10:46 pm   Post subject: Re: Shooting multiple times

Thanks for the help guys, I appreciate it. I have managed to find a solution and I'd like your opinions. Obviously this isn't complete, but its the main function.


stuff.rar
 Description:
And by the way, this IS my code... The reason it might look weird is because I learned flexible arrays like, 10 minute before I used them. But whatever if you don't believe me I don't really care. :P

Download
 Filename:  stuff.rar
 Filesize:  4.36 KB
 Downloaded:  79 Time(s)

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  [ 15 Posts ]
Jump to:   


Style:  
Search: