Computer Science Canada

mousewhere in a target shooting game

Author:  nightfeng2 [ Mon Jan 10, 2011 9:49 pm ]
Post subject:  mousewhere in a target shooting game

Turing:


%By: Nightfeng2
setscreen ("graphics:640;480,offscreenonly")
var targetx, targety, Mx, My, button, CC : int
var score := 0
score := 0
CC := 7 %crosshair color
for count : 1 .. 50
    randint (targetx, 50, 590) %%%targets x coor
    randint (targety, 50, 430) %targets y coor
    loop

        put "Your score: ", score
        %%%%the target
        drawfilloval (targetx, targety, 50, 50, 12)
        drawfilloval (targetx, targety, 35, 35, 31)
        drawfilloval (targetx, targety, 20, 20, 12)
        drawfilloval (targetx, targety, 5, 5, 31)
        delay (10)
        %%%crosshair
        mousewhere (Mx, My, button)
        drawoval (Mx, My, 100, 100, CC)
        drawline (Mx, My, Mx, My + 100, CC)
        drawline (Mx, My, Mx + 100, My, CC)
        drawline (Mx, My, Mx - 100, My, CC)
        drawline (Mx, My, Mx, My - 100, CC)
        drawoval (Mx, My, 50, 50, CC)
        drawfilloval (Mx, My, 3, 3, 4)
        View.Update
        cls
        if Mx > targetx + 50 and button = 1 then
            score -= 1
        elsif Mx < targetx - 50 and button = 1 then
            score -= 1
        elsif My > targety + 50 and button = 1 then
            score -= 1
        elsif My < targety - 50 and button = 1 then
            score -= 1
        end if
        exit when Mx <= targetx + 50 and Mx >= targetx - 50 and My <= targety + 50 and My >= targety - 50 and button = 1 %end the loop if the cc hits inside the target

    end loop
    if Mx <= targetx + 50 and Mx >= targetx - 50 and My <= targety + 50 and My >= targety - 50 and button = 1 then %if the cc hit the target, score+1
        score += 1

    end if
end for
put score
 




if you tried the game, you will notice that when the mouse is clicked, the score will go crazy because of the button=1, which when the mouse is down the score will be changed. How can i fix this so that the mouse can only be checked once or something like that. thanks.

Author:  Tony [ Mon Jan 10, 2011 9:53 pm ]
Post subject:  RE:mousewhere in a target shooting game

A mouse can have 4 states: held up, held down, clicked down, let go. Think about how you could figure out when the latter 2 events happen (as one can't hold transient states).

Author:  nightfeng2 [ Mon Jan 10, 2011 10:18 pm ]
Post subject:  Re: mousewhere in a target shooting game

hi Tony,
i dont get what you mean by four states, how do you change the states? is it like 0,1,2,3 - held down , , etc

Author:  Tony [ Mon Jan 10, 2011 11:19 pm ]
Post subject:  RE:mousewhere in a target shooting game

You change the states by clicking on the mouse. Mouse.Where will tell you if the button is currently up or down (2 states). If you think about it, you can figure out how to make it into 4 states, to include events such as clicks.

That is, if you can use only "up" and "down" for words, how would you describe that a click happened?

Author:  nightfeng2 [ Tue Jan 11, 2011 9:04 am ]
Post subject:  Re: mousewhere in a target shooting game

i think u need like button=1 and then equal to 0 or something like that? i just cant program it out =[

Author:  DemonWasp [ Tue Jan 11, 2011 9:58 am ]
Post subject:  RE:mousewhere in a target shooting game

I have discussed what you're after, which is called "polling input", in Part 3 of my basic game programming tutorial. Read through that carefully, replacing the minesweeper bits with your program.

Author:  nightfeng2 [ Tue Jan 11, 2011 9:30 pm ]
Post subject:  RE:mousewhere in a target shooting game

hi demonwasp, i read your tutorial, however, i really dont understand how i should do it,i know what is the polling input but i dont know the code or the logic for it

Author:  Tony [ Tue Jan 11, 2011 9:38 pm ]
Post subject:  Re: RE:mousewhere in a target shooting game

nightfeng2 @ Tue Jan 11, 2011 9:30 pm wrote:
i dont know the code or the logic for it

You have all the needed tools to figure out and implement that, available in this thread.

Author:  nightfeng2 [ Tue Jan 11, 2011 10:26 pm ]
Post subject:  RE:mousewhere in a target shooting game

ahhhhh, my brains stuck, i really cant do it. can you explain???

Author:  Tony [ Tue Jan 11, 2011 10:53 pm ]
Post subject:  RE:mousewhere in a target shooting game

You should be able to do it. I've looked at your code, you've used all the right elements before.

If you are still having problems, try asking very specific questions and post the relevant (not all) bits of code that you are working on.

Author:  nightfeng2 [ Wed Jan 12, 2011 9:50 pm ]
Post subject:  RE:mousewhere in a target shooting game

The problem is that i dont know how to make the mouse into 4 states, i only have 2 states which is held down and held up.

Author:  TerranceN [ Wed Jan 12, 2011 10:12 pm ]
Post subject:  RE:mousewhere in a target shooting game

Think about the previous state of the mouse button compared to the state it is currently in.

Author:  nightfeng2 [ Wed Jan 12, 2011 10:28 pm ]
Post subject:  Re: mousewhere in a target shooting game

i tried buttonwait but then the crosshair will not be moving as the mouse move

Turing:

%By: Nightfeng2
setscreen ("graphics:800;600,offscreenonly")
var targetx, targety, Mx, My, button, CC, score, bullets, timer, button2 : int
score := 0
bullets := 20
CC := 7 %crosshair color



    for count : 1 .. 50
        randint (targetx, 50, 750)     %%%targets x coor
        randint (targety, 50, 550)     %targets y coor
        loop

            put "Your score: ", score, " Bullets: ", bullets
            %%%%the target
            drawfilloval (targetx, targety, 50, 50, 12)
            drawfilloval (targetx, targety, 35, 35, 31)
            drawfilloval (targetx, targety, 20, 20, 12)
            drawfilloval (targetx, targety, 5, 5, 31)

            %%%crosshair
            buttonwait ("down", Mx, My, button, button2)
            drawoval (Mx, My, 100, 100, CC)
            drawline (Mx, My, Mx, My + 100, CC)
            drawline (Mx, My, Mx + 100, My, CC)
            drawline (Mx, My, Mx - 100, My, CC)
            drawline (Mx, My, Mx, My - 100, CC)
            drawoval (Mx, My, 50, 50, CC)
            drawfilloval (Mx, My, 3, 3, 4)
            View.Update
            cls
            if Mx > targetx + 50 and button = 1 then %this part: if the crosshair is out of the target and the button is clicked, bullet -1
                bullets -= 1
            elsif Mx < targetx - 50 and button = 1 then
                bullets -= 1
            elsif My > targety + 50 and button = 1 then
                bullets -= 1
            elsif My < targety - 50 and button = 1 then
                bullets -= 1
            end if
            exit when Mx <= targetx + 50 and Mx >= targetx - 50 and My <= targety + 50 and My >= targety - 50 and button = 1     %end the loop if the cc hits inside the target

            exit when bullets <= 0
        end loop
        if Mx <= targetx + 50 and Mx >= targetx - 50 and My <= targety + 50 and My >= targety - 50 and button = 1 then     %if the cc hit the target, score+1
            score += 1
            bullets -= 1
            exit when bullets <= 0
        end if
        exit when bullets <= 0
    end for
    put "Your final score is: ", score



it really doesnt work that well, i still want mousewhere to work. and the projects due very soon =S really need help.

Author:  Tony [ Wed Jan 12, 2011 10:38 pm ]
Post subject:  RE:mousewhere in a target shooting game

try harder. Mouse.ButtonWait
Quote:

If there isn't such an event, Mouse.ButtonWait waits until there is one and then returns (much like getch handles keystrokes).

It is doing _exactly_ what it says it should be doing.

Author:  nightfeng2 [ Wed Jan 12, 2011 11:24 pm ]
Post subject:  Re: mousewhere in a target shooting game

Turing:

%By: Nightfeng2
setscreen ("graphics:800;600,offscreenonly")
var targetx, targety, Mx, My, button, CC, score, bullets, timer, button2 : int
score := 0
bullets := 20
CC := 7 %crosshair color


    for count : 1 .. 50
        randint (targetx, 50, 750)     %%%targets x coor
        randint (targety, 50, 550)     %targets y coor
        loop

            put "Your score: ", score, " Bullets: ", bullets
            %%%%the target
            drawfilloval (targetx, targety, 50, 50, 12)
            drawfilloval (targetx, targety, 35, 35, 31)
            drawfilloval (targetx, targety, 20, 20, 12)
            drawfilloval (targetx, targety, 5, 5, 31)

            %%%crosshair
            Mouse.ButtonWait ("down", Mx, My, button, button2)
            drawoval (Mx, My, 100, 100, CC)
            drawline (Mx, My, Mx, My + 100, CC)
            drawline (Mx, My, Mx + 100, My, CC)
            drawline (Mx, My, Mx - 100, My, CC)
            drawline (Mx, My, Mx, My - 100, CC)
            drawoval (Mx, My, 50, 50, CC)
            drawfilloval (Mx, My, 3, 3, 4)
            View.Update
            cls
            if Mx > targetx + 50 and button = 1 then
                bullets -= 1
            elsif Mx < targetx - 50 and button = 1 then
                bullets -= 1
            elsif My > targety + 50 and button = 1 then
                bullets -= 1
            elsif My < targety - 50 and button = 1 then
                bullets -= 1
            end if
            exit when Mx <= targetx + 50 and Mx >= targetx - 50 and My <= targety + 50 and My >= targety - 50 and button = 1     %end the loop if the cc hits inside the target

            exit when bullets <= 0
        end loop
        if Mx <= targetx + 50 and Mx >= targetx - 50 and My <= targety + 50 and My >= targety - 50 and button = 1 then     %if the cc hit the target, score+1
            score += 1
            bullets -= 1
            exit when bullets <= 0
        end if
        exit when bullets <= 0
    end for
    put "Your final score is: ", score


this does the same thing, ill try something tomorrow and see if it works

Author:  TheGuardian001 [ Thu Jan 13, 2011 8:06 am ]
Post subject:  Re: mousewhere in a target shooting game

Mouse.ButtonWait and buttonwait are the same command.
The point Tony was making is that that command waits until there is input before continuing. When your program gets to that line, it waits until it sees the "down" event before continuing. You need a different command.

Author:  nightfeng2 [ Thu Jan 13, 2011 8:47 am ]
Post subject:  RE:mousewhere in a target shooting game

ive tried mousewhere but its not working properly

Author:  nightfeng2 [ Thu Jan 13, 2011 2:59 pm ]
Post subject:  RE:mousewhere in a target shooting game

Maybe i'll delete this function in the game if it still doesn't work

Author:  Tony [ Thu Jan 13, 2011 3:06 pm ]
Post subject:  Re: RE:mousewhere in a target shooting game

nightfeng2 @ Thu Jan 13, 2011 8:47 am wrote:
ive tried mousewhere but its not working properly

I am fairly certain that the command is working exactly as it's expected to work Smile

Author:  nightfeng2 [ Thu Jan 13, 2011 3:59 pm ]
Post subject:  RE:mousewhere in a target shooting game

ok maybe the command itself is working properly but its not wat i want it to be.
The one i want is like down-buttonwait, but then the whole program will wait until mouse is clicked, so i really dont know wat to do.

Author:  TerranceN [ Thu Jan 13, 2011 4:30 pm ]
Post subject:  Re: mousewhere in a target shooting game

Look at the following table,

code:

mouse button now        previous mouse button
       0                         1
       1                         1
       0                         0
       1                         0



Now try to match these descriptions to each row
-mouse is being held down
-mouse has just been released
-mouse has just been pressed
-mouse is not being pressed

Author:  nightfeng2 [ Thu Jan 13, 2011 5:10 pm ]
Post subject:  RE:mousewhere in a target shooting game

-mouse is being held down 1 1
-mouse has just been released 0 1
-mouse has just been pressed 1 0
-mouse is not being pressed 0 0

Author:  TerranceN [ Thu Jan 13, 2011 5:19 pm ]
Post subject:  RE:mousewhere in a target shooting game

Ok, since you want to register a click when the mouse is just pressed, before you call Mouse.Where, store the previous mouse button value. Then just change your if statement to make sure the mouse button is down now, but was not last frame.

Author:  nightfeng2 [ Thu Jan 13, 2011 10:04 pm ]
Post subject:  RE:mousewhere in a target shooting game

Sounds so complicated, i want to try what you are saying, but what do you mean by store the previous mouse button value before calling mousewhere. Can you gimme an example of that ?

Author:  Tony [ Thu Jan 13, 2011 10:09 pm ]
Post subject:  Re: RE:mousewhere in a target shooting game

nightfeng2 @ Thu Jan 13, 2011 10:04 pm wrote:
store ... value

usually suggests that you might want to use some variable.

Author:  nightfeng2 [ Thu Jan 13, 2011 10:16 pm ]
Post subject:  RE:mousewhere in a target shooting game

The previous button value should be 0 since the mouse is not pressed, that means I store 0 into a variable. If this part is right, the next part is giving me a headache cause i really dont understand it.

Author:  Tony [ Thu Jan 13, 2011 10:25 pm ]
Post subject:  RE:mousewhere in a target shooting game

Not quite. You can only know what the value of the mouse is _right now_. For it to become a previous value, you would have to wait some time.

Author:  nightfeng2 [ Thu Jan 13, 2011 10:33 pm ]
Post subject:  RE:mousewhere in a target shooting game

ah, here comes the complicated stuff xD

Author:  nightfeng2 [ Thu Jan 13, 2011 11:13 pm ]
Post subject:  RE:mousewhere in a target shooting game

instead of the bullet system and -1 score, ill change it to a timer instead.

Author:  Tony [ Thu Jan 13, 2011 11:23 pm ]
Post subject:  RE:mousewhere in a target shooting game

You could do better than that, come on:

You have a loop, it looks something like this
code:

loop
   read_the_mouse
   ...
   do_some_stuff
end loop

And it has two variables we've already discussed: mouse_now and mouse_previous. They had some values. Going forward, you know that you want mouse_now to be... what it is now. And you want mouse_previous to remember what happened in the previous loop.

How do you make this happen?

Author:  nightfeng2 [ Fri Jan 14, 2011 9:25 am ]
Post subject:  Re: mousewhere in a target shooting game

Turing:

%By: Nightfeng2
setscreen ("graphics:800;600,offscreenonly")
var targetx, targety, Mx, My, button, CC, score, bullets, timer, button2 : int
score := 0
bullets := 20
CC := 7 %crosshair color




    loop
        randint (targetx, 50, 750)     %%%targets x coor
        randint (targety, 50, 550)     %targets y coor
        loop

            put "Your score: ", score, " Bullets: ", bullets
            %%%%the target
            drawfilloval (targetx, targety, 50, 50, 12)
            drawfilloval (targetx, targety, 35, 35, 31)
            drawfilloval (targetx, targety, 20, 20, 12)
            drawfilloval (targetx, targety, 5, 5, 31)

            %%%crosshair
            mousewhere (Mx, My, button)
            drawoval (Mx, My, 100, 100, CC)
            drawline (Mx, My, Mx, My + 100, CC)
            drawline (Mx, My, Mx + 100, My, CC)
            drawline (Mx, My, Mx - 100, My, CC)
            drawline (Mx, My, Mx, My - 100, CC)
            drawoval (Mx, My, 50, 50, CC)
            drawfilloval (Mx, My, 3, 3, 4)
            View.Update
            cls

            if button = 1 then
                button2 := button

                exit
            end if


        end loop



        if Mx <= targetx + 50 and Mx >= targetx - 50 and My <= targety + 50 and My >= targety - 50 and button2 = 1 then     %if the cc hit the target, score+1
            score += 1
            bullets -= 1
        elsif button2 = 1 then
            bullets -= 1
            exit when bullets <= 0
        end if
        exit when bullets <= 0
    end loop
    put "Your final score is: ", score






this is what i can do right now, im sure u what you mean is different from me. giving an example of code would really help. thx

Author:  Tony [ Fri Jan 14, 2011 1:08 pm ]
Post subject:  Re: mousewhere in a target shooting game

nightfeng2 @ Fri Jan 14, 2011 9:25 am wrote:

if button = 1 then
button2 := button

This part will guarantees that both button and button2 will always be the same when you exit the loop. Read your if statements out-loud to make sure they make sense to you: "if button is 1 then assign value of button (1) to button2".

Author:  nightfeng2 [ Fri Jan 14, 2011 7:15 pm ]
Post subject:  RE:mousewhere in a target shooting game

i changed the game to timer system, its not bad actually, ill try the bullet system again after the assignment cuz its due very soon


: