Computer Science Canada

Loops cancel out Mouse.Where - See inside for explanation

Author:  Flawedspirit [ Thu Jan 15, 2009 1:03 pm ]
Post subject:  Loops cancel out Mouse.Where - See inside for explanation

Okay, so, for a project I am working on, I'm making a game that involves red and blue balls bouncing about a play area being deflected by the player's ball, that follows the mouse. On opposite corners are red and blue goals. Obviously, the red and blue balls go in their respective goals to increase your score. But I digress. What I need is for the playing field to draw and update itself, yet still have the player's ball appear and update: Here is the code, see for yourself:

code:

View.Set ("offscreenonly")
%VARIABLES
var mouseX, mouseY, button : int
var ballRad : int
var goalRad : int
var doomHoleX : int
var doomHoleY : int
var doomHoleRad : int

ballRad := 20
goalRad := 20
doomHoleRad := 20
doomHoleX := maxx div 2
doomHoleY := maxy div 2

%DRAW OBJECTS
procedure drawObjects
    loop
        cls
        %doomhole
        drawfilloval (doomHoleX, doomHoleY, doomHoleRad, doomHoleRad, black)
        %red goal
        drawfilloval (1 + goalRad, maxy - goalRad, goalRad, goalRad, red)
        %blue goal
        drawfilloval (maxx - goalRad, 1 + goalRad, doomHoleRad, doomHoleRad, blue)
        View.Update
    end loop
end drawObjects

%PLAYER BALL
procedure movePlayer
    loop
        Mouse.Where (mouseX, mouseY, button)
        if button = 0 then
            cls
            drawfilloval (mouseX, mouseY, ballRad, ballRad, grey)
            drawoval (mouseX, mouseY, ballRad, ballRad, black)
            View.Update
        else
            cls
            drawfilloval (mouseX, mouseY, ballRad, ballRad, grey)
            drawoval (mouseX, mouseY, ballRad, ballRad, black)
            View.Update
        end if
    end loop
end movePlayer

%HOLE OF DOOM COLLISION
%if you let balls touch the doomhole, you fail!
procedure doomHoleCol
    loop
        if mouseX + ballRad = doomHoleX + doomHoleRad then
            if mouseY + ballRad = doomHoleY + doomHoleRad then
                put "collision"
            end if
        end if
    end loop
end doomHoleCol

%RUNTIME!
drawObjects
movePlayer
doomHoleCol

Author:  The_Bean [ Thu Jan 15, 2009 1:23 pm ]
Post subject:  Re: Loops cancel out Mouse.Where - See inside for explanation

Follow the path the computer will take through your code (hint: how do you get out of a loop)

Author:  CodeMonkey2000 [ Thu Jan 15, 2009 8:05 pm ]
Post subject:  RE:Loops cancel out Mouse.Where - See inside for explanation

To be more blunt: you never leave drawObjects. You are stuck in an infinite loop, and so movePlayer never gets executed. Try getting rid of the loops within the procedures and call the procedures inside a main loop.


: