Computer Science Canada

Flashing colours ????????

Author:  adrianr2z [ Sun Jun 16, 2013 1:24 pm ]
Post subject:  Flashing colours ????????

What is it you are trying to achieve?
Change Colour Bricks


What is the problem you are having?
Bricks flash colours and aren't staying the colour assigned.
I want them to stay that colour but they start changing colour non stop and I don't know why its doing that.

Describe what you have tried to solve this problem
Nothing

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


Turing:



View.Set ("graphics:max;max,nobuttonbar, offscreenonly")

%Introducing variables
% Add powerups
% add red bricks
% speed up as game goes on

var ballx, bally, font, font1, ballradius, numBlocks, ballspeed, box1X, box1Y, boxWidth, boxHeight, box2X, box2Y, box3X, box3Y, box4X, box4Y, buttonnumber, buttonupdown : int
var x, y, rand1, value2, xvaluerand, blockdivision, replace : int
var xvalue, width, score, scorex, scorey, speed : int := 0
replace := 0
ballradius := 20
ballx := maxx div 2
bally := maxy div 2
scorex := -60
scorey := maxy - 30
font1 := Font.New ("serif:15:bold")
font := Font.New ("serif:30:bold")
ballspeed := speed * 2
rand1 := 0
value2 := 0
xvaluerand := Rand.Int (0, 230)
blockdivision := 0
box1X := 100
box1Y := maxy div 2
box2X := maxx div 2 - 130
box2Y := maxy div 2
box3X := maxx div 2 + 300
box3Y := maxy div 2
box4X := maxx div 2 - 130
box4Y := 0
boxWidth := 200
boxHeight := 100
numBlocks := 1
var chars : array char of boolean
var blocks : array 1 .. 200, 1 .. 2 of int
var newblock, newblock2 : boolean := false
var score1, score2, score3 : array 1 .. 10 of int
var names, names2, names3 : array 1 .. 10 of string
var newscore : int := score % whatever the score is
var streamin, streamout, streamin2, streamout2, streamin3, streamout3, yval : int
var colour1 : int
colour1 := black
yval := maxy - 100



procedure game




    newblock2 := false
    loop


        Draw.FillOval (ballx, bally, ballradius, ballradius, brightgreen)
        bally := bally - speed


        if (bally - ballradius) - speed <= 0 then
            bally := bally + speed
        end if



        score := score + 1

        Font.Draw ("Score: ", scorex + 70, scorey, font, brightgreen)
        Font.Draw (intstr (score, 10), scorex + 120, scorey, font, brightblue)

        if Rand.Int (1, 5) = 1 then
            colour1 := red
        elsif Rand.Int (1, 5) = 2 then
            colour1 := black
        elsif Rand.Int (1, 5) = 3 then
            colour1 := green
        elsif Rand.Int (1, 5) = 4 then
            colour1 := blue
        elsif Rand.Int (1, 5) = 5 then
            colour1 := yellow
        end if


        for i : 1 .. numBlocks

            Draw.FillBox (blocks (i, 1), blocks (i, 2), blocks (i, 1) + 100, blocks (i, 2) + 20, Rand.Int (1,5))
       
        end for


        for i : 1 .. numBlocks

            blocks (i, 2) := blocks (i, 2) + speed

            if blocks (i, 2) = blockdivision then

                newblock := true

            end if

            if blocks (i, 2) >= maxy then
                newblock2 := true
                blocks (i, 2) := 0
                blocks (
                    i, 1) := blocks (i, 1) + 60 * Rand.Int (-1, 1)

            end if


        end for



        if newblock2 = false then
            if newblock = true then

                xvalue := Rand.Int (0, xvaluerand)

                loop
                    numBlocks := numBlocks + 1

                    randint (width, ballradius * 2 + 1, rand1)

                    blocks (numBlocks, 1) := xvalue

                    xvalue := xvalue + value2 + width

                    blocks (numBlocks, 2) := 0

                    blocks (numBlocks + 1, 1) := xvalue

                    exit when xvalue >= maxx
                end loop
                newblock := false
            end if
        end if

        for i : 1 .. numBlocks

            if bally - ballradius >= blocks (i, 2) and bally - ballradius <= blocks (i, 2) + 20 and ballx - ballradius <= blocks (i, 1) + 100 and ballx + ballradius >= blocks (i, 1) then

                bally := bally + ballspeed

            end if
        end for




        Input.KeyDown (chars)
        if chars (KEY_RIGHT_ARROW) and ballx + ballradius <= maxx then
            ballx := ballx + speed
        elsif chars (KEY_LEFT_ARROW) and ballx - ballradius >= 0 then
            ballx := ballx - speed
        end if



        View.Update

        cls


        exit when bally - ballradius > maxy


    end loop

    Font.Draw ("Score: ", scorex + 70, scorey, font, brightgreen)

    Font.Draw (intstr (score, 10), scorex + 120, scorey, font, brightblue)

end game


procedure reset
    score := 0
    ballx := maxx div 2
    bally := maxy div 2
    speed := 0
    blockdivision := 0
    speed := 0
    ballspeed := 0
    numBlocks := 1
    newblock2 := false
    newblock := false
    xvalue := Rand.Int (0, xvaluerand)
    blocks (numBlocks, 1) := Rand.Int (0, xvaluerand)

    loop

        width := Rand.Int (ballradius * 2 + 1, rand1)     % sets the x location
        xvalue := xvalue + value2 + width
        blocks (numBlocks, 2) := 0
        blocks (numBlocks + 1, 1) := xvalue

        exit when xvalue >= maxx
        numBlocks := numBlocks + 1
    end loop

end reset


loop


    cls
    Draw.FillBox (0, 0, maxx, maxy, black)
    Draw.FillBox (box1X, box1Y, (box1X + boxWidth), (box1Y + boxHeight), brightred)
    % this is the box to change the color to red
    Draw.FillBox (box2X, box2Y, box2X + boxWidth, box1Y + boxHeight, blue)
    Draw.FillBox (box3X, box3Y, box3X + boxWidth, box1Y + boxHeight, brightgreen)
    Draw.FillBox (box4X, box4Y, box4X + boxWidth, box4Y + boxHeight, yellow)
    Font.Draw ("Highscores!", box4X + 50, box4Y + 40, font1, purple)
    Font.Draw ("Easy", box1X + 50, box1Y + 40, font, brightblue)
    Font.Draw ("Medium", box2X + 40, box2Y + 40, font, yellow)
    Font.Draw ("Hard", box3X + 50, box3Y + 40, font, brightred)
    Font.Draw ("Instructions:", maxx div 10, maxy - 100, font, purple)
    Font.Draw ("Welcome to my game young padawan and let me (Hossein) teach you the ways of mastering the game controls.", maxx div 8, maxy - 130, font1, brightblue)
    Font.Draw ("It is pretty simple to master them, simply use the left (<-) and right (->) arrow keys to move the ball side to side.", maxx div 8, maxy - 150, font1, brightgreen)
    Font.Draw ("The objective of the game is to keep the ball from going off the top of the screen", maxx div 8, maxy - 170, font1, white)
    Font.Draw ("Good luck young padawan on your journey.", maxx div 8, maxy - 190, font1, 33)

    if Mouse.ButtonMoved ("down") then

        Mouse.ButtonWait ("down", x, y, buttonnumber, buttonupdown)


        cls

        % check if they are clicking inside the grey box
        if x > box1X and x < box1X + boxWidth and y > box1Y and y < box1Y + boxHeight then

            rand1 := 400
            value2 := 15
            reset
            blockdivision := maxx div 17
            speed := 1
            ballspeed := 2
            game
            newscore := score
            %highscore

        end if

        % check if they are clicking inside the red box
        if x > box2X and x < box2X + boxWidth and y > box2Y and y < box2Y + boxHeight then
            rand1 := 350
            value2 := 15
            reset
            blockdivision := maxx div 14 - 10
            speed := 2
            ballspeed := 4
            game
            newscore := score
            %highscore2

        end if

        if x > box3X and x < box3X + boxWidth and y > box3Y and y < box3Y + boxHeight then
            rand1 := 220
            value2 := 25
            reset
            blockdivision := maxx div 14 + 30
            speed := 3
            ballspeed := 6
            game
            newscore := score
            %highscore3

        end if
        /*
         if x > box4X and x < box4X + boxWidth and y > box4Y and y < box4Y + boxHeight then

         for i : 0 .. 1
         x := 0
         y := 0
         cls
         highdis
         highdis2
         highdis3
         Draw.FillBox (box4X - 200, box4Y, box4X - 200 + boxWidth, box4Y + boxHeight, yellow)

         Font.Draw ("Hard:", maxx - 150, maxy - 40, font, 89)
         Font.Draw ("Medium:", maxx div 2 - 130, maxy - 40, font, 33)
         Font.Draw ("Easy:", 40, maxy - 40, font, 240)

         Font.Draw ("Back", box4X - 200 + 50, box4Y + 40, font1, purple)
         View.Update
         end for

         loop

         if Mouse.ButtonMoved ("down") then
         Mouse.ButtonWait ("down", x, y, buttonnumber, buttonupdown)
         exit when x > box4X - 200 and x < box4X - 200 + boxWidth and y > box4Y and y < box4Y + boxHeight
         end if
         end loop

         end if
         */

    end if

    View.Update

end loop




Please specify what version of Turing you are using
Latest

Author:  Insectoid [ Sun Jun 16, 2013 2:13 pm ]
Post subject:  RE:Flashing colours ????????

You go to all sorts of trouble to randomly assign color1, and then you never use it. I think that might be your problem.

Author:  adrianr2z [ Sun Jun 16, 2013 2:20 pm ]
Post subject:  Re: Flashing colours ????????

Ooops. I was just playing around with colours. Should have looked like this:

Turing:

 if Rand.Int (1, 5) = 1 then
            colour1 := red
        elsif Rand.Int (1, 5) = 2 then
            colour1 := black
        elsif Rand.Int (1, 5) = 3 then
            colour1 := green
        elsif Rand.Int (1, 5) = 4 then
            colour1 := blue
        elsif Rand.Int (1, 5) = 5 then
            colour1 := yellow
        end if


        for i : 1 .. numBlocks

            Draw.FillBox (blocks (i, 1), blocks (i, 2), blocks (i, 1) + 100, blocks (i, 2) + 20,colour1)
       
        end for


However, this still doesn't fix the problem of flashing colours. It doesn't stay the colour assigned.

Author:  Tony [ Sun Jun 16, 2013 4:18 pm ]
Post subject:  RE:Flashing colours ????????

how about
code:

if Rand.Int (1, 5) = 1 then
            colour1 := red
        elsif Rand.Int (1, 5) = 2 then
            colour1 := black
        elsif Rand.Int (1, 5) = 3 then
            colour1 := green
        elsif Rand.Int (1, 5) = 4 then
            colour1 := blue
        elsif Rand.Int (1, 5) = 5 then
            colour1 := yellow
        else
           put "missed rolling the die 5 times in the row"
        end if

Author:  adrianr2z [ Sun Jun 16, 2013 7:25 pm ]
Post subject:  Re: Flashing colours ????????

No that didn't work either. Still changing colours and all.

Author:  Insectoid [ Sun Jun 16, 2013 8:24 pm ]
Post subject:  RE:Flashing colours ????????

What happens if Rand.Int(1,5) returns 2,1,1,1,1?

Also, where in your code is that block? Do you think there might be a better spot for it?

Author:  Tony [ Sun Jun 16, 2013 8:37 pm ]
Post subject:  Re: Flashing colours ????????

adrianr2z @ Sun Jun 16, 2013 7:25 pm wrote:
No that didn't work either. Still changing colours and all.

you just pasted that code in, hoping to take someone else's solution as your own, without even thinking why
code:

else
           put "missed rolling the die 5 times in the row"

might print that message.

Author:  nullptr [ Sun Jun 16, 2013 9:20 pm ]
Post subject:  Re: Flashing colours ????????

It looks like you're changing the blocks' colours every time you go through the loop. If you want them to stay the same from frame to frame, you should set them once when the game starts and then keep those values forever.


: