Computer Science Canada

help with game?

Author:  programmer1337 [ Fri Dec 02, 2011 7:39 pm ]
Post subject:  help with game?

What is it you are trying to achieve?
I want to make a fully functional shooting game with humans that pop up on the screen and you shoot them and the game calculates how many times you succesfully shoot them, what i really want out of this is how to get it so that when you have the circle on the human and press the spacebar it calculates one more point, this is the only thing i am having trouble with at the moment.




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

Turing:


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Author: Daniel ...
% Date: November 29, 2011
% Filename: TuringGraphics.t
% Description: graphics
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

View.Set ("graphics:1200;800,offscreenonly")

x := 400
y := 400
var chars : array char of boolean
loop
    cls ()
    Input.KeyDown (chars)

    if chars (KEY_UP_ARROW) then
        y := y + 3
    end if
    if chars (KEY_RIGHT_ARROW) then
        x := x + 3
    end if
    if chars (KEY_LEFT_ARROW) then
        x := x - 3
    end if
    if chars (KEY_DOWN_ARROW) then
        y := y - 3
    end if
    if chars (chr (ORD_SPACE)) then
 
    end if
    Draw.FillBox (0, 450, 1200, 800, 79) %keeps updating the sky, and drawing sky
    Draw.FillBox (0, 0, 1200, 450, green) %keeps updating the grass, and drawing grass
    Draw.FillOval (x, y, 10, 10, red) %Draws oval
    View.Update
end loop


Author:  RandomLetters [ Fri Dec 02, 2011 9:36 pm ]
Post subject:  RE:help with game?

What do you mean by "calculates one more point". What is the "point"? Where the bullet hits? The other corner of the crosshair?

Author:  programmer1337 [ Sat Dec 03, 2011 11:59 am ]
Post subject:  Re: help with game?

well what i mean is that with each hit on the human or whatever it will add one point to your total score, i would know how to calculate the score, but i need to know how to hit the humans so that when you press the spacebar and the dot is on them at the same time, it interprets it as a hit

Author:  RandomLetters [ Sat Dec 03, 2011 3:20 pm ]
Post subject:  Re: help with game?

one way is to use whatdotcolor, which works if your targets are all the same color, and unique from the background. Before you draw the cursor, check the color of the new coordinate, and if it is the correct color, then you know it hit.
code:

    var targetcolor : int
    targetcolor := 1
    Draw.FillBox (200, 100, 250, 150, targetcolor) %a target
    if whatdotcolor(x, y) = targetcolor then
         %Add score
    end if
    Draw.FillOval (x, y, 10, 10, red) %Draws oval


Alternatively, you can represent targets using shapes or hitboxes, such as a rectangle, and use math to check if the coordinates of the cursor is within a hitbox. http://compsci.ca/v3/viewtopic.php?t=75&highlight=collision
[/code]

Author:  programmer1337 [ Sat Dec 03, 2011 3:36 pm ]
Post subject:  Re: help with game?

this is my current code for mous control, it keeps lagging, can you guys help me?
code:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Author: Daniel ...
% Date: November 29, 2011
% Filename: TuringGraphics.t
% Description: graphics
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

View.Set ("graphics:1200;800,offscreenonly")

var count, count2, count1, x, y, rndInt, rndInt1, rndInt3 : int
var mx, my, mb : int %vars for the mouse where code

x := 400
y := 400

var chars : array char of boolean
loop

    cls ()

    Input.KeyDown (chars)

    mousewhere (mx, my, mb)

    if my > 790 then
        my := 790
    elsif my < 10 then
        my := 10
    end if
    if mx > 1175 then
        mx := 1175
    elsif mx < 10 then
        mx := 10
    end if

    Draw.FillBox (0, 450, 1200, 800, 79) %keeps updating the sky, and drawing sky
    Draw.FillBox (0, 0, 1200, 450, green) %keeps updating the grass, and drawing grass
    Draw.FillOval (mx, my, 5, 5, black) %Draws oval

    rndInt3 := Rand.Int (10, 100)
    rndInt := Rand.Int (10, 1150)
    rndInt1 := Rand.Int (10, 550)
    delay (rndInt3)
    Draw.FillOval (rndInt, rndInt1, 40, 40, brightred)
    Draw.FillOval (rndInt, rndInt1, 30, 30, white)
    Draw.FillOval (rndInt, rndInt1, 10, 10, brightred)


    delay (500)
    View.Update

end loop

Author:  DemonWasp [ Sat Dec 03, 2011 3:44 pm ]
Post subject:  Re: help with game?

programmer1337 @ Sat Dec 03, 2011 3:36 pm wrote:
code:

    rndInt3 := Rand.Int (10, 100)
    %...
    delay (rndInt3)

    % ...
    delay (500)


Just a guess, but delaying for 1/2 second after a random delay of 1/100th to 1/10th of a second is probably going to cause some "lag".

Author:  programmer1337 [ Sat Dec 03, 2011 3:49 pm ]
Post subject:  Re: help with game?

ok all i want to do is randomly make the ovals pop up and then with the mouse i click on them before they disappear, and that adds a point, so what should i do?

Author:  RandomLetters [ Sat Dec 03, 2011 3:55 pm ]
Post subject:  RE:help with game?

You should have only one delay in your loop. This delay should be the one which sets the (max) length per frame.

To have targets pop out randomly, instead of another delay, use a counter to count the number of frames that have passed.

Author:  programmer1337 [ Sat Dec 03, 2011 3:57 pm ]
Post subject:  Re: help with game?

can you please just start me off, with the counter?

Author:  RandomLetters [ Sat Dec 03, 2011 7:48 pm ]
Post subject:  RE:help with game?

code:
var c : int
var duration: int
var frame: int
frame := 17 %60 fps

loop
     c += 1            %counts the # of frames

     if c * frame > duration then     %multiplying the # of frames by the time per frame and check if that time is greater
          duration := randomnumber(); %you can change the duration each time
          c := 0
     end if

     delay(frame)
end loop


to make sure the duration doesnt change for slow computers, you can (in fact should) also use things like time.elapsed and time.delaysincelast instead of a counter and delay. However, the concept is still the same.

Maybe I'm editing too much

Author:  programmer1337 [ Sat Dec 03, 2011 9:30 pm ]
Post subject:  Re: help with game?

this is my current code and it isnt working what should i do?
code:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Author: Daniel ..
% Date: November 29, 2011
% Filename: TuringGraphics.t
% Description: graphics
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

View.Set ("graphics:1200;800,offscreenonly")

var count, count2, count1, x, y, rndInt, rndInt1, rndInt3 : int
var mx, my, mb : int %vars for the mouse where code

x := 400
y := 400
var c : int
var duration: int
var frame: int
frame := 17 %60 fps
rndInt := Rand.Int(100, 1000)

var chars : array char of boolean
loop

    cls ()

    Input.KeyDown (chars)

    mousewhere (mx, my, mb)

    if my > 790 then
        my := 790
    elsif my < 10 then
        my := 10
    end if
    if mx > 1175 then
        mx := 1175
    elsif mx < 10 then
        mx := 10
    end if

    Draw.FillBox (0, 450, 1200, 800, 79) %keeps updating the sky, and drawing sky
    Draw.FillBox (0, 0, 1200, 450, green) %keeps updating the grass, and drawing grass
    Draw.FillOval (mx, my, 5, 5, black) %Draws oval

    rndInt3 := Rand.Int (100, 1000)
    rndInt := Rand.Int (10, 1150)
    rndInt1 := Rand.Int (10, 550)
   

loop
     c := c + 1            %counts the # of frames

     if c * frame > duration then     %multiplying the # of frames by the time per frame and check if that time is greater
          duration := rndInt3 %you can change the duration each time
          c := 0
     end if
Draw.FillOval (rndInt, rndInt1, 40, 40, brightred)
    Draw.FillOval (rndInt, rndInt1, 30, 30, white)
    Draw.FillOval (rndInt, rndInt1, 10, 10, brightred)
     delay(frame)
end loop
    View.Update

end loop


Author:  Aange10 [ Sat Dec 03, 2011 9:52 pm ]
Post subject:  RE:help with game?

Problem #1 : Don't copy an paste somebody's code into your own. Why don't you try seeing what your telling the program to do, and see where you messed up?


EDIT: You need to remove
Turing:

Author: Daniel ..


Because you didn't write all of that code. Citations are necessary, or credit should be void. You can't just copy and paste something and call it yours.

Author:  programmer1337 [ Sat Dec 03, 2011 9:58 pm ]
Post subject:  Re: help with game?

dude i wouldnt copy it >.>, and maybe i forgot to take it out sorrryyyy, all i want is help not criticism

Author:  ProgrammingFun [ Sat Dec 03, 2011 10:01 pm ]
Post subject:  Re: help with game?

programmer1337 @ Sat Dec 03, 2011 9:58 pm wrote:
dude i wouldnt copy it >.>, and maybe i forgot to take it out sorrryyyy, all i want is help not criticism

Copying won't help you...and helping you learn that is the best help you can ever get.
Understanding of coding does not come from copying (nor does anything else), it comes from understanding the code then implementing your own.

And what you have done by claiming authorship is commonly known as plagiarism and this is not a good thing in any industry, or life in general.
(and what do you mean by "i wouldnt copy it"? did someone forcefully make you do it?)

Author:  programmer1337 [ Sat Dec 03, 2011 10:06 pm ]
Post subject:  Re: help with game?

this is what i wanted, i copied his code into my own, then i wanted to make it so it worked, then when it works, i would tweak it and personalize it, then i would know how it works, because now it doesnt work, and im trying to understand it lol

Author:  Aange10 [ Sat Dec 03, 2011 10:12 pm ]
Post subject:  RE:help with game?

I like the reasoning, but copying the code into yours program, and pasting the result here and asking us what's next isn't 'tweaking and personalizing' it.

If you need help you tell us the problem, what YOU'VE TRIED, and your results.

Saying 'It doesn't work what do i do', copying the code, and then saying it again doesn't do much good.

Author:  RandomLetters [ Sun Dec 04, 2011 12:58 am ]
Post subject:  RE:help with game?

Take a look at some of the simpler game programs that people have submitted in the submissions forum. They give you a good idea of how a typical game works.

Author:  programmer1337 [ Mon Dec 05, 2011 1:03 pm ]
Post subject:  Re: help with game?

this is my code now, but the problem is it just spams all of the targets im supposed to shoot at, at a rate no one can click it, how can i slow down the rate they pop-up without altering the mouse speed, or oval speed.
code:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Author: Daniel ..
% Date: November 29, 2011
% Filename: TuringGraphics.t
% Description: graphics
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
View.Set ("graphics:1200;800,offscreenonly")

var count, count2, count1, x, y, rndInt, rndInt1, rndInt3 : int
var mx, my, mb : int %vars for the mouse where code

x := 400
y := 400
var c : int
var duration: int
var frame: int
frame := 17 %60 fps
rndInt := Rand.Int(100, 1000)
c := 0
duration := 0
var chars : array char of boolean
loop

    cls ()

    Input.KeyDown (chars)

    mousewhere (mx, my, mb)

    if my > 790 then
        my := 790
    elsif my < 10 then
        my := 10
    end if
    if mx > 1175 then
        mx := 1175
    elsif mx < 10 then
        mx := 10
    end if

    Draw.FillBox (0, 450, 1200, 800, 79) %keeps updating the sky, and drawing sky
    Draw.FillBox (0, 0, 1200, 450, green) %keeps updating the grass, and drawing grass
    Draw.FillOval (mx, my, 5, 5, black) %Draws oval

    rndInt3 := Rand.Int (1000, 10000)
    rndInt := Rand.Int (10, 1150)
    rndInt1 := Rand.Int (10, 550)
   

     c := c + 1            %counts the # of frames

     if c * frame > duration then     %multiplying the # of frames by the time per frame and check if that time is greater
          duration := rndInt3 %you can change the duration each time
          c := 0
     end if
Draw.FillOval (rndInt, rndInt1, 40, 40, brightred)
    Draw.FillOval (rndInt, rndInt1, 30, 30, white)
    Draw.FillOval (rndInt, rndInt1, 10, 10, brightred)
     delay(frame)
    View.Update

end loop

Author:  Aange10 [ Mon Dec 05, 2011 6:31 pm ]
Post subject:  Re: help with game?

Make a timer. Here's a basic timer.


(Btw, this is plenty of information to solve your problem. If not, this solves your problem for you, so don't ask me for help on 'wur do i pt it'.)

Author:  programmer1337 [ Mon Dec 05, 2011 6:54 pm ]
Post subject:  Re: help with game?

thank you very much, i wont bother you lol, just one last thingy im sorry Sad but in this code
code:
if buttonmoved("down") and mousewhere(mx,my,mb) then
c := c + 1
end if
put c

it doesnt put c and it doesnt do anything lol

Author:  Aange10 [ Mon Dec 05, 2011 7:01 pm ]
Post subject:  RE:help with game?

If there are commands your not too familiar with, read the documentation. Obviously you are unfamiliar with mousewhere. The example in the documentation will show you its correct use, and fix your problem.

Author:  programmer1337 [ Mon Dec 05, 2011 7:14 pm ]
Post subject:  Re: help with game?

i tried doing this
code:


if buttonmoved("down") and my := rndInt1 and mx := rndInt then
c := c + 1
end if
put c

it still doesnt work?

Author:  Aange10 [ Mon Dec 05, 2011 7:38 pm ]
Post subject:  RE:help with game?

Read the error.

The things being compared have to be true or false. my := rndInt1 isn't true or false. my = rndInt1 is true or false

Author:  programmer1337 [ Mon Dec 05, 2011 7:40 pm ]
Post subject:  Re: help with game?

yea that is what i put now sorry lol it works, i knew that my teacher told me Wink sorry

Author:  programmer1337 [ Mon Dec 05, 2011 7:52 pm ]
Post subject:  Re: help with game?

Im sorry, I finished everything that i possibly could think of, one more thing all i want is to slow down the rates of which the target boards disappear instead of 1 millisecond to like 500 milliseconds
code:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Author: Daniel ..                                 %
% Date: November 29, 2011                           %
% Filename: TuringGraphics.t                        %
% Description: graphics                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
View.Set ("graphics:1200;800,offscreenonly")

var count, count2, count1, x, y, rndInt, rndInt1, rndInt3 : int
var mx, my, mb : int %vars for the mouse where code
count2 := 0
x := 400
y := 400
var c : int
var duration : int
var frame : int
frame := 17 %60 fps
rndInt := Rand.Int (100, 1000)
c := 0
duration := 0
count1 := Time.Elapsed()

var chars : array char of boolean
loop

    cls ()

    Input.KeyDown (chars)

    mousewhere (mx, my, mb)

    if my > 790 then
        my := 790
    elsif my < 10 then
        my := 10
    end if
    if mx > 1175 then
        mx := 1175
    elsif mx < 10 then
        mx := 10
    end if

    Draw.FillBox (0, 450, 1200, 800, 79) %keeps updating the sky, and drawing sky
    Draw.FillBox (0, 0, 1200, 450, green) %keeps updating the grass, and drawing grass
    Draw.FillOval (mx, my, 2, 2, black) %Draws oval
   
put Time.Elapsed()
    rndInt3 := Rand.Int (1000, 10000)
    rndInt := Rand.Int (10, 1150)
    rndInt1 := Rand.Int (10, 550)
if mb = 1 and mx = rndInt and my = rndInt1 then
c := c + 1
delay(200)
end if
put "Score: "..
put c

if Time.Elapsed() = 5000 then
    Draw.FillOval (rndInt, rndInt1, 40, 40, brightred)
    Draw.FillOval (rndInt, rndInt1, 30, 30, white)
    Draw.FillOval (rndInt, rndInt1, 10, 10, brightred)
    delay(100)
    elsif Time.Elapsed() = 7950 then
    Draw.FillOval (rndInt, rndInt1, 40, 40, brightred)
    Draw.FillOval (rndInt, rndInt1, 30, 30, white)
    Draw.FillOval (rndInt, rndInt1, 10, 10, brightred)
    delay(100)
    elsif Time.Elapsed() = 8500 then
    Draw.FillOval (rndInt, rndInt1, 40, 40, brightred)
    Draw.FillOval (rndInt, rndInt1, 30, 30, white)
    Draw.FillOval (rndInt, rndInt1, 10, 10, brightred)
    delay(100)
    elsif Time.Elapsed() = 10000 then
    Draw.FillOval (rndInt, rndInt1, 40, 40, brightred)
    Draw.FillOval (rndInt, rndInt1, 30, 30, white)
    Draw.FillOval (rndInt, rndInt1, 10, 10, brightred)
    delay(100)
    elsif Time.Elapsed() = 13000 then
    Draw.FillOval (rndInt, rndInt1, 40, 40, brightred)
    Draw.FillOval (rndInt, rndInt1, 30, 30, white)
    Draw.FillOval (rndInt, rndInt1, 10, 10, brightred)
    elsif Time.Elapsed() = 17000 then
    Draw.FillOval (rndInt, rndInt1, 40, 40, brightred)
    Draw.FillOval (rndInt, rndInt1, 30, 30, white)
    Draw.FillOval (rndInt, rndInt1, 10, 10, brightred)
    elsif Time.Elapsed() = 19200 then
    Draw.FillOval (rndInt, rndInt1, 40, 40, brightred)
    Draw.FillOval (rndInt, rndInt1, 30, 30, white)
    Draw.FillOval (rndInt, rndInt1, 10, 10, brightred)
    elsif Time.Elapsed() = 22000 then
    Draw.FillOval (rndInt, rndInt1, 40, 40, brightred)
    Draw.FillOval (rndInt, rndInt1, 30, 30, white)
    Draw.FillOval (rndInt, rndInt1, 10, 10, brightred)
    elsif Time.Elapsed() = 24500 then
    Draw.FillOval (rndInt, rndInt1, 40, 40, brightred)
    Draw.FillOval (rndInt, rndInt1, 30, 30, white)
    Draw.FillOval (rndInt, rndInt1, 10, 10, brightred)
    elsif Time.Elapsed() = 27000 then
    Draw.FillOval (rndInt, rndInt1, 40, 40, brightred)
    Draw.FillOval (rndInt, rndInt1, 30, 30, white)
    Draw.FillOval (rndInt, rndInt1, 10, 10, brightred)
    end if
    View.Update
if count1 = 27000 then
count1 := 0
c := 0
end if

    exit when chars (KEY_ESC)
end loop


Author:  Aange10 [ Mon Dec 05, 2011 8:04 pm ]
Post subject:  RE:help with game?

The attachment above shows you how to make a timer.

Author:  programmer1337 [ Mon Dec 05, 2011 8:10 pm ]
Post subject:  Re: help with game?

i already made a timer, that isnt the problem, the problem is to make the targets time before disappearing prolonged

Author:  Dreadnought [ Mon Dec 05, 2011 9:33 pm ]
Post subject:  Re: help with game?

The code you posted doesn't even reliably make the targets appear. Not to mention that after 27 seconds targets will never appear. You should probably have a look at these issues.

To keep targets on the screen for longer periods you should use timers of some sort like Aange10 said. I know you have one in your code (sorta) but it does absolutely nothing except change a variable that you never use anywhere else. Its not enough to throw a few lines that can be used as a timer and hope it works. You have to actually make use of your variable. For example, check if Time.Elapsed is 500 greater than your timer variable to see if 0.5 seconds have passed.

Also, you can have more than one timer.

Author:  programmer1337 [ Tue Dec 06, 2011 7:48 am ]
Post subject:  Re: help with game?

ohh thank you i actually find out how to do it


: