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

Username:   Password: 
 RegisterRegister   
 Random Flashing
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Mr. T




PostPosted: Sun Oct 16, 2005 3:08 pm   Post subject: Random Flashing

Can anyone figure out why the screen flashes blue in these 3 scenarios:
1) Create a vertical bar (right-click) and make sure all the balls are to the left of the bar
2) Create a vertical bar and make sure that balls are on both sides of the bar
3) Create a horizontal bar (left-click) and make sure that all the balls are below the bar

code:

View.Set ("offscreenonly")
const RADIUS : int := 5
const NUM_BALLS : int := 3
var x, y, dx, dy, clr : array 1 .. NUM_BALLS of int
var box_x1, box_x2, box_y1, box_y2 : int := 0
var barX, barY, button, left, middle, right : int
var barCreator := true
var a, b : int := 0
var c1, c2 := false
var d1, d2 := false
var drawFill := true
var topCounterx, bottomCounterx,topCountery, bottomCountery := 0



for i : 1 .. NUM_BALLS
    x (i) := Rand.Int (RADIUS, maxx - RADIUS)
    y (i) := Rand.Int (RADIUS, maxy - RADIUS)
    dx (i) := Rand.Int (-5, 5)
    dy (i) := Rand.Int (-5, 5)
    clr (i) := Rand.Int (1, 15)
end for

loop
%Frame
Draw.Box (0,0,maxx,maxy,blue)

    %Outer Boundary Collision
    for i : 1 .. NUM_BALLS
        if x (i) + dx (i) < RADIUS or
                x (i) + dx (i) > maxx - RADIUS then
            dx (i) := -dx (i)
        end if
        if y (i) + dy (i) < RADIUS or
                y (i) + dy (i) > maxy - RADIUS then
            dy (i) := -dy (i)
        end if

        %Incompleted Wall Collision
        if whatdotcolour (x (i), y (i)) = red or whatdotcolour (x (i), y (i)) = green then
            return
        end if

        %Completed Wall Collision
        if x (i) + dx (i) >= box_x1 and x (i) + dx (i) <= box_x2 and whatdotcolour (x (i), y (i)) = blue then
            dx (i) *= -1
        end if

        if y (i) + dy (i) >= box_y1 and y (i) + dy (i) <= box_y2 and whatdotcolour (x (i), y (i)) = blue then
            dy (i) *= -1
        end if

        x (i) := x (i) + dx (i)
        y (i) := y (i) + dy (i)

        %Draw Ball
        Draw.FillOval (x (i), y (i), RADIUS, RADIUS, black)
    end for
    View.Update
    delay (10)
    cls
    %Wall Creation
    Mouse.ButtonChoose ("multibutton")
    Mouse.Where (barX, barY, button)
    left := button mod 10           % left = 0 or 1
    middle := (button - left) mod 100       % middle = 0 or 10
    right := button - middle - left         % right = 0 or 100

    if barCreator = true then

        if left = 1 and whatdotcolour (barX, barY) not= blue then
            a := 0
            b := 0
            button := 0
            box_x1 := barX
            box_x2 := barX
            box_y1 := barY
            box_y2 := box_y1 + 10
            if (box_x1 + a) <= maxx then
                c1 := true
            else
                c1 := false
            end if
            if (box_x1 - b) >= 0 then
                d1 := true
            else
                d1 := false
            end if
        end if
    end if

    if c1 = true then
        barCreator := false
        a += 3
    end if
    if d1 = true then
        barCreator := false
        b += 3
    end if
    if c1 = true then
        if box_x1 + a >= maxx then
            Draw.FillBox (box_x1 + a, box_y1, box_x2, box_y2, blue)
        else
            Draw.FillBox (box_x1 + a, box_y1, box_x2, box_y2, red)
        end if
    end if

    if d1 = true then
        if box_x1 - b <= 0 then
            Draw.FillBox (box_x1 - b, box_y1, box_x2, box_y2, blue)
        else
            Draw.FillBox (box_x1 - b, box_y1, box_x2, box_y2, green)
        end if
    end if

    %Which side to fill in upon completion of wall
    if box_x1 + a >= maxx and box_x1 - b <= 0 then

        for i : 1 .. NUM_BALLS
            if y (i) > box_y2 then
                topCountery += 1
            elsif y (i) < box_y1 then
                bottomCountery += 1
            end if
        end for

        if topCountery >= 1 and bottomCountery >= 1 then
            drawFill := false
            else
            drawFill:=true
        end if

        if drawFill = true then
            for i : 1 .. NUM_BALLS
                if y (i) >= box_y1 then
                   Draw.Fill (box_x1,box_y1-1,blue, blue)
                elsif drawFill = true and y (i) <= box_y1 then
                    Draw.Fill (box_x2,box_y2+1,blue, blue)
                end if

            end for

        end if
    end if

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    if barCreator = true then

        if right = 100 and whatdotcolour (barX, barY) not= blue then
            a := 0
            b := 0
            button := 0
            box_x1 := barX
            box_x2 := box_x1 + 10
            box_y1 := barY
            box_y2 := barY
            if (box_y1 + a) <= maxx then
                c2 := true
            else
                c2 := false
            end if
            if (box_y1 - b) >= 0 then
                d2 := true
            else
                d2 := false
            end if
        end if
    end if

    if c2 = true then
        barCreator := false
        a += 3
    end if
    if d2 = true then
        barCreator := false
        b += 3
    end if
    if c2 = true then
        if box_y1 + a >= maxy then
            Draw.FillBox (box_x1, box_y1 + a, box_x2, box_y2, blue)
        else
            Draw.FillBox (box_x1, box_y1 + a, box_x2, box_y2, red)
        end if
    end if

    if d2 = true then
        if box_y1 - b <= 0 then
            Draw.FillBox (box_x1, box_y1 - b, box_x2, box_y2, blue)
        else
            Draw.FillBox (box_x1, box_y1 - b, box_x2, box_y2, green)
        end if
    end if

    %Which side to fill in upon completion of wall
    if box_y1 + a >= maxx and box_y1 - b <= 0 then

        for i : 1 .. NUM_BALLS
            if x (i) > box_x2 then
                topCounterx += 1
            elsif x (i) < box_x1 then
                bottomCounterx += 1
            end if
        end for

        if topCounterx >= 1 and bottomCounterx >= 1 then
            drawFill := false
            else
            drawFill:=true
        end if

        if drawFill = true then
            for i : 1 .. NUM_BALLS
                if x (i) >= box_x1 then
                    Draw.Fill (box_x1-1,box_y1,blue, blue)
                elsif x (i) <= box_x1 then
                    Draw.Fill (box_x2+1,box_y2,blue, blue)
                end if

            end for

        end if
    end if



end loop
Sponsor
Sponsor
Sponsor
sponsor
GlobeTrotter




PostPosted: Sun Oct 16, 2005 6:22 pm   Post subject: (No subject)

I'd guess it has to do with your use of >= instead of >. Same thing with <= and <. If you notice, it only flashes when the ball hits the boundary.
Mr. T




PostPosted: Sun Oct 16, 2005 6:26 pm   Post subject: Alex's Opinion

Ok, I tried taking out the = 's but I still recieve the same results. Confused
[Gandalf]




PostPosted: Sun Oct 16, 2005 7:26 pm   Post subject: (No subject)

I think more people would be willing to help you if you organized your program a bit more. Do you know how to use functions/procedures? You should implement them into this program, makes for much better readability.

Since I am not sure of what the problem is, I will tell you that I think it has something to do with this code:
code:
    %Which side to fill in upon completion of wall
    if box_y1 + a >= maxx and box_y1 - b <= 0 then

        for i : 1 .. NUM_BALLS
            if x (i) > box_x2 then
                topCounterx += 1
            elsif x (i) < box_x1 then
                bottomCounterx += 1
            end if
        end for

        if topCounterx >= 1 and bottomCounterx >= 1 then
            drawFill := false
        else
            drawFill := true
        end if

        if drawFill = true then
            for i : 1 .. NUM_BALLS
                if x (i) >= box_x1 then
                    Draw.Fill (box_x1 - 1, box_y1, blue, blue)
                elsif x (i) <= box_x1 then
                    Draw.Fill (box_x2 + 1, box_y2, blue, blue)
                end if
            end for
        end if
    end if

If you want more specific help, be more specific in your questions.[/code]
Mr. T




PostPosted: Sun Oct 16, 2005 7:28 pm   Post subject: (No subject)

Ya, i know thats the troublesome part of the code...I shoulda been more specific Embarassed
Anyways, I think the issue lies with the Draw.Fill... am i doing something incorrectly?
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  [ 5 Posts ]
Jump to:   


Style:  
Search: