Computer Science Canada

Fixing the flicker in my program

Author:  Darkmantis [ Sat Jun 03, 2006 7:04 pm ]
Post subject:  Fixing the flicker in my program

hey I have this pong that i made but the program seems to slow down alot when the paddles move and the ball constantly flickers. Some people suggest to put "offscreenonly" and View.Updates in my program but the problem is I dont know where to put them, here is my code.

code:
setscreen ("graphics:610;400,nocursor")
var ballx, bally, paddleleftx, paddlelefty, paddlerightx, paddlerighty, direction1, direction2, c1, c2, c3, c4, o, q, t, r : int
var score, score2, scorel : int
var kp : array char of boolean
var ch : string (1)
var font1, font2, font3, font4, font5 : int
randint (ballx, 40, 360)
randint (bally, 40, 350)
paddleleftx := 30
paddlelefty := 180
paddlerightx := 580
paddlerighty := 180
score := 0
score2 := 0
direction1 := 1
direction2 := 1
font1 := Font.New ("serif:28")
font2 := Font.New ("serif:20")
font3 := Font.New ("serif:40")
font4 := Font.New ("serif:33")
font5 := Font.New ("serif:75")
procedure pong
    Draw.ThickLine (paddleleftx, paddlelefty, paddleleftx, paddlelefty + 30, 10, black)
    Draw.ThickLine (paddlerightx, paddlerighty, paddlerightx, paddlerighty + 30, 10, black)
    Draw.ThickLine (0, 365, 610, 365, 5, black)
    Draw.ThickLine (0, 40, 610, 40, 5, black)
    loop
        drawfilloval (ballx, bally, 3, 3, brightblue)
        delay (3)
        drawfilloval (ballx, bally, 3, 3, white)
        if ballx = 0 then
            score := score + 1
        end if
        if ballx = 610 then
            score2 := score2 + 1
        end if
        locate (1, 30)
        put "Paddle Right: ", score
        locate (2, 30)
        put "Paddle Left: ", score2
        locate (20, 30)
        c1 := whatdotcolor (ballx - 4, bally)
        c2 := whatdotcolor (ballx + 4, bally)
        c3 := whatdotcolor (ballx, bally + 4)
        c4 := whatdotcolor (ballx, bally - 4)
        if ballx > maxx and direction1 > 0 then
            direction1 := -direction1
        elsif ballx < 1 and direction1 < 0 then
            direction1 := -direction1
        elsif bally > maxy and direction2 > 0 then
            direction2 := -direction2
        elsif bally < 1 and direction2 < 0 then
            direction2 := -direction2
        end if
        if c1 = black then
            direction1 := -direction1
        end if
        if c2 = black then
            direction1 := -direction1
        end if
        if c3 = black then
            direction2 := -direction2
        end if
        if c4 = black then
            direction2 := -direction2
        end if
        ballx := ballx + direction1
        bally := bally + direction2
        Input.KeyDown (kp)
        if kp ('w') then
            Draw.ThickLine (paddleleftx, paddlelefty, paddleleftx, paddlelefty + 30, 12, white)
            Draw.ThickLine (paddleleftx, paddlelefty, paddleleftx, paddlelefty + 30, 10, black)
            delay (1)
            paddlelefty := paddlelefty + 1
            if paddlelefty > 325 then
                paddlelefty := paddlelefty - 1
            end if
        end if
        if kp ('s') then
            Draw.ThickLine (paddleleftx, paddlelefty, paddleleftx, paddlelefty + 30, 12, white)
            Draw.ThickLine (paddleleftx, paddlelefty, paddleleftx, paddlelefty + 30, 10, black)
            delay (1)
            paddlelefty := paddlelefty - 1
            if paddlelefty < 50 then
                paddlelefty := paddlelefty + 1
            end if
        end if
        if kp (KEY_UP_ARROW) then
            Draw.ThickLine (paddlerightx, paddlerighty, paddlerightx, paddlerighty + 30, 12, white)
            Draw.ThickLine (paddlerightx, paddlerighty, paddlerightx, paddlerighty + 30, 10, black)
            delay (1)
            paddlerighty := paddlerighty + 1
            if paddlerighty > 325 then
                paddlerighty := paddlerighty - 1
            end if
        end if
        if kp (KEY_DOWN_ARROW) then
            Draw.ThickLine (paddlerightx, paddlerighty, paddlerightx, paddlerighty + 30, 12, white)
            Draw.ThickLine (paddlerightx, paddlerighty, paddlerightx, paddlerighty + 30, 10, black)
            delay (1)
            paddlerighty := paddlerighty - 1
            if paddlerighty < 50 then
                paddlerighty := paddlerighty + 1
            end if
        end if
        if score = scorel then
            cls
            Draw.ThickLine (paddlerightx, paddlerighty, paddlerightx, paddlerighty + 30, 10, black)
            delay (1)
            Draw.Text ("Right Paddle Wins!!!", 150, 200, font3, green)
            return
        elsif score2 = scorel then
            cls
            Draw.ThickLine (paddleleftx, paddlelefty, paddleleftx, paddlelefty + 30, 10, black)
            Draw.Text ("Left Paddle Wins!!!", 150, 200, font3, green)
            return
        end if
    end loop
end pong

procedure mainmenu
    Draw.Text ("Space Pong!", 155, 300, font3, green)
    Draw.Text ("PRESS ANY KEY TO CONTINUE", 125, 50, font2, green)
    getch (ch)
    cls
    put "Choose the score limit"
    get scorel
    Draw.Text ("PRESS ANY KEY TO CONTINUE", 125, 50, font2, green)
    getch (ch)
    cls
    put "How many obstacle?"
    get o
    Draw.Text ("PRESS ANY KEY TO CONTINUE", 125, 50, font2, green)
    getch (ch)
    cls
    Draw.Text ("5", 280, 190, font5, green)
    delay (1000)
    cls
    Draw.Text ("4", 280, 190, font5, green)
    delay (1000)
    cls
    Draw.Text ("3", 280, 190, font5, green)
    delay (1000)
    cls
    Draw.Text ("2", 280, 190, font5, green)
    delay (1000)
    cls
    Draw.Text ("1", 280, 190, font5, green)
    delay (1000)
    cls
    Draw.ThickLine (paddleleftx, paddlelefty, paddleleftx, paddlelefty + 30, 10, black)
    Draw.ThickLine (paddlerightx, paddlerighty, paddlerightx, paddlerighty + 30, 10, black)
    Draw.ThickLine (0, 365, 610, 365, 5, black)
    Draw.ThickLine (0, 40, 610, 40, 5, black)
    Draw.Text ("GO!", 270, 200, font3, green)
    delay (1000)
    Draw.Text ("GO!", 270, 200, font3, white)

    for p : 1 .. o
        randint (q, 1, 25)
        randint (t, 40, 590)
        randint (r, 40, 350)
        drawfilloval (t, r, q, q, black)
    end for
    pong
end mainmenu

setscreen ("position:truemiddle,centre")
var clr : int := 218
var text1 : int := Font.New ("Times New Roman:38:Bold")
drawfillbox (0, 0, 610, 400, black)
for n : 0 .. 39
    RGB.SetColor (clr, 0, n / 39, 0)
    Font.Draw ("DarkMantis Productions", (maxx div 2) - (Font.Width ("DarkMantis Productions", text1) div 2), (maxy div 1.5) - 9, text1, clr)
    RGB.SetColor (clr, 0, n / 39, 0)
    Font.Draw ("Presents...", (maxx div 2) - (Font.Width ("Presents...", text1) div 2), (maxy div 2.5) - 9, text1, clr)
    View.Update
    delay (50)
end for
delay (1000)
for decreasing n : 39 .. 0
    RGB.SetColor (clr, 0, n / 39, 0)
    Font.Draw ("DarkMantis Productions", (maxx div 2) - (Font.Width ("DarkMantis Productions", text1) div 2), (maxy div 1.5) - 9, text1, clr)
    RGB.SetColor (clr, 0, n / 39, 0)
    Font.Draw ("Presents...", (maxx div 2) - (Font.Width ("Presents...", text1) div 2), (maxy div 2.5) - 9, text1, clr)
    View.Update
    delay (50)
end for

mainmenu

Author:  MysticVegeta [ Sat Jun 03, 2006 7:09 pm ]
Post subject: 

the setscreen ("offscreenonly") goes above the drawing loop and your View.Update seems to be in the right place...

Author:  Cervantes [ Sat Jun 03, 2006 7:10 pm ]
Post subject: 

View.Set("offscreenonly") goes wherever you want to induce double buffering. The start of your program is a good place. You can inlcude offscreenonly with the other commands in your View.Set string.

View.Update goes wherever you want to flip the back buffer into view. This is probably after you've drawn everything in your main loop.

code:

loop
    cls
    %draw everything
    View.Update
    delay (5)
end loop

Author:  Darkmantis [ Sat Jun 03, 2006 9:32 pm ]
Post subject: 

Thx guys the flickering is now gone but now I have to fix a bunch of bugs but I have no Idea where to start, my ball now eats the obstacles more then before and now my paddles arent erasing themselves, heres the code.

code:
setscreen ("graphics:610;400,nocursor")
setscreen ("offscreenonly")
var ballx, bally, paddleleftx, paddlelefty, paddlerightx, paddlerighty, direction1, direction2, c1, c2, c3, c4, o, q, t, r : int
var score, score2, scorel : int
var kp : array char of boolean
var ch : string (1)
var font1, font2, font3, font4, font5 : int
randint (ballx, 40, 360)
randint (bally, 40, 350)
paddleleftx := 30
paddlelefty := 180
paddlerightx := 580
paddlerighty := 180
score := 0
score2 := 0
direction1 := 5
direction2 := 5
font1 := Font.New ("serif:28")
font2 := Font.New ("serif:20")
font3 := Font.New ("serif:40")
font4 := Font.New ("serif:33")
font5 := Font.New ("serif:75")
procedure pong
    Draw.ThickLine (paddleleftx, paddlelefty, paddleleftx, paddlelefty + 30, 10, black)
    Draw.ThickLine (paddlerightx, paddlerighty, paddlerightx, paddlerighty + 30, 10, black)
    Draw.ThickLine (0, 365, 610, 365, 5, black)
    Draw.ThickLine (0, 40, 610, 40, 5, black)
    loop
        drawfilloval (ballx, bally, 3, 3, brightblue)
        View.Update
        delay (1)
        drawfilloval (ballx, bally, 3, 3, white)
        if ballx = 0 then
            score := score + 1
        end if
        if ballx = 610 then
            score2 := score2 + 1
        end if
        locate (1, 30)
        put "Paddle Right: ", score
        locate (2, 30)
        put "Paddle Left: ", score2
        locate (20, 30)
        c1 := whatdotcolor (ballx - 4, bally)
        c2 := whatdotcolor (ballx + 4, bally)
        c3 := whatdotcolor (ballx, bally + 4)
        c4 := whatdotcolor (ballx, bally - 4)
        if ballx > maxx and direction1 > 0 then
            direction1 := -direction1
        elsif ballx < 1 and direction1 < 0 then
            direction1 := -direction1
        elsif bally > maxy and direction2 > 0 then
            direction2 := -direction2
        elsif bally < 1 and direction2 < 0 then
            direction2 := -direction2
        end if
        if c1 = black then
            direction1 := -direction1
        end if
        if c2 = black then
            direction1 := -direction1
        end if
        if c3 = black then
            direction2 := -direction2
        end if
        if c4 = black then
            direction2 := -direction2
        end if
        ballx := ballx + direction1
        bally := bally + direction2
        Input.KeyDown (kp)
        if kp ('w') then
            Draw.ThickLine (paddleleftx, paddlelefty, paddleleftx, paddlelefty + 30, 12, white)
            Draw.ThickLine (paddleleftx, paddlelefty, paddleleftx, paddlelefty + 30, 10, black)
            View.Update
            delay (1)
            paddlelefty := paddlelefty + 10
            if paddlelefty > 325 then
                paddlelefty := paddlelefty - 10
            end if
        end if
        if kp ('s') then
            Draw.ThickLine (paddleleftx, paddlelefty, paddleleftx, paddlelefty + 30, 12, white)
            Draw.ThickLine (paddleleftx, paddlelefty, paddleleftx, paddlelefty + 30, 10, black)
            View.Update
            delay (1)
            paddlelefty := paddlelefty - 10
            if paddlelefty < 50 then
                paddlelefty := paddlelefty + 10
            end if
        end if
        if kp (KEY_UP_ARROW) then
            Draw.ThickLine (paddlerightx, paddlerighty, paddlerightx, paddlerighty + 30, 12, white)
            Draw.ThickLine (paddlerightx, paddlerighty, paddlerightx, paddlerighty + 30, 10, black)
            View.Update
            delay (1)
            paddlerighty := paddlerighty + 10
            if paddlerighty > 325 then
                paddlerighty := paddlerighty - 10
            end if
        end if
        if kp (KEY_DOWN_ARROW) then
            Draw.ThickLine (paddlerightx, paddlerighty, paddlerightx, paddlerighty + 30, 12, white)
            Draw.ThickLine (paddlerightx, paddlerighty, paddlerightx, paddlerighty + 30, 10, black)
            View.Update
            delay (1)
            paddlerighty := paddlerighty - 10
            if paddlerighty < 50 then
                paddlerighty := paddlerighty + 10
            end if
        end if
        if score = scorel then
            cls
            Draw.ThickLine (paddlerightx, paddlerighty, paddlerightx, paddlerighty + 30, 10, black)
            delay (1)
            Draw.Text ("Right Paddle Wins!!!", 150, 200, font3, green)
            return
        elsif score2 = scorel then
            cls
            Draw.ThickLine (paddleleftx, paddlelefty, paddleleftx, paddlelefty + 30, 10, black)
            Draw.Text ("Left Paddle Wins!!!", 150, 200, font3, green)
            return
        end if
    end loop
end pong

procedure mainmenu
    Draw.Text ("Space Pong!", 155, 300, font3, green)
    Draw.Text ("PRESS ANY KEY TO CONTINUE", 125, 50, font2, green)
    View.Update
    getch (ch)
    cls
    put "Choose the score limit"
    View.Update
    get scorel
    Draw.Text ("PRESS ANY KEY TO CONTINUE", 125, 50, font2, green)
    View.Update
    getch (ch)
    cls
    put "How many obstacle?"
    View.Update
    get o
    Draw.Text ("PRESS ANY KEY TO CONTINUE", 125, 50, font2, green)
    View.Update
    getch (ch)
    cls
    Draw.Text ("5", 280, 190, font5, green)
    View.Update
    delay (1000)
    cls
    Draw.Text ("4", 280, 190, font5, green)
    View.Update
    delay (1000)
    cls
    Draw.Text ("3", 280, 190, font5, green)
    View.Update
    delay (1000)
    cls
    Draw.Text ("2", 280, 190, font5, green)
    View.Update
    delay (1000)
    cls
    Draw.Text ("1", 280, 190, font5, green)
    View.Update
    delay (1000)
    cls
    Draw.ThickLine (paddleleftx, paddlelefty, paddleleftx, paddlelefty + 30, 10, black)
    Draw.ThickLine (paddlerightx, paddlerighty, paddlerightx, paddlerighty + 30, 10, black)
    Draw.ThickLine (0, 365, 610, 365, 5, black)
    Draw.ThickLine (0, 40, 610, 40, 5, black)
    Draw.Text ("GO!", 270, 200, font3, green)
    View.Update
    delay (1000)
    Draw.Text ("GO!", 270, 200, font3, white)
    View.Update
    for p : 1 .. o
        randint (q, 1, 25)
        randint (t, 40, 590)
        randint (r, 40, 350)
        drawfilloval (t, r, q, q, black)
        View.Update
    end for
    pong
end mainmenu
setscreen ("position:truemiddle,centre")
var clr : int := 218
var text1 : int := Font.New ("Times New Roman:38:Bold")
drawfillbox (0, 0, 610, 400, black)
View.Update
for n : 0 .. 39
    RGB.SetColor (clr, 0, n / 39, 0)
    Font.Draw ("DarkMantis Productions", (maxx div 2) - (Font.Width ("DarkMantis Productions", text1) div 2), (maxy div 1.5) - 9, text1, clr)
    RGB.SetColor (clr, 0, n / 39, 0)
    Font.Draw ("Presents...", (maxx div 2) - (Font.Width ("Presents...", text1) div 2), (maxy div 2.5) - 9, text1, clr)
    View.Update
    delay (50)
end for
delay (1000)
for decreasing n : 39 .. 0
    RGB.SetColor (clr, 0, n / 39, 0)
    Font.Draw ("DarkMantis Productions", (maxx div 2) - (Font.Width ("DarkMantis Productions", text1) div 2), (maxy div 1.5) - 9, text1, clr)
    RGB.SetColor (clr, 0, n / 39, 0)
    Font.Draw ("Presents...", (maxx div 2) - (Font.Width ("Presents...", text1) div 2), (maxy div 2.5) - 9, text1, clr)
    View.Update
    delay (50)
end for


mainmenu

Author:  Darkmantis [ Wed Jun 07, 2006 10:42 am ]
Post subject: 

Is there anyone who will help me out?

Author:  Remm [ Wed Jun 07, 2006 2:27 pm ]
Post subject: 

take off nocursor so it is possible to see what is typed. If you dont want it visible for your 'click to continue' stuff, you could always change it to a mouse click anywhere on screen to continue, or put nocruisor on then and take it off (though i tried that and it didint work for me Confused but thats not saying much)

anyways, nice startup with the fade in/out, Although it leaves a bit of residue in the end. In game everythign seems to be going uber fast, so you might wannna add some delays in the ball and movement keys.

Author:  Remm [ Wed Jun 07, 2006 2:31 pm ]
Post subject: 

Uh, sorry to double post but i just tried out the first one you posted, and it works fine - on my computer anyways... how badly is it flickering for you? i only see minor flickers in the paddle and ball.

Author:  Clayton [ Wed Jun 07, 2006 2:36 pm ]
Post subject: 

actually for you to be able to see what you have typed you have to either take off 'offscreenonly' using 'nooffscreenonly', or you can View.Update after every letter you have typed (i wouldnt suggest doing that) check out my View.Set tutorial at : http://compsci.ca/v2/viewtopic.php?t=12533 to read all you need on View.Update Very Happy

Author:  Darkmantis [ Wed Jun 07, 2006 3:03 pm ]
Post subject: 

I still dont see how to fix my program Sad. In the second set of code in this thread my paddles dont erase themselves, why? I dont see whats wrong. If I can fix this then the flickering is gone and my paddles wont lag the program plz help and thx to everyone for helping me Very Happy

Author:  Clayton [ Wed Jun 07, 2006 3:59 pm ]
Post subject: 

did you actually read my tut? you have to use Draw.Cls to clear the screen, not just draw over the items you want to move, also, you arent even getting rid of the paddles in question, resulting in the "smearing" of the paddles

Author:  Darkmantis [ Wed Jun 07, 2006 9:00 pm ]
Post subject: 

The problem is I dont want to clear the entire screen

Author:  HellblazerX [ Wed Jun 07, 2006 9:02 pm ]
Post subject: 

Then use Draw.FillBox. Just draw a white box over whatever portion of your picture you need to erase.

Author:  Darkmantis [ Thu Jun 08, 2006 6:31 am ]
Post subject: 

Thats what I'm doing but it doesnt work when useing View.Update.

Author:  HellblazerX [ Thu Jun 08, 2006 6:44 am ]
Post subject: 

Use View.UpdateArea. It only updates the one area you specify.

Author:  Clayton [ Thu Jun 08, 2006 7:39 am ]
Post subject: 

the reason drawing over the objects you want to move doesnt work for you is..... you arent doing it! nowhere in your loop are you drawing over the paddles then redrawing them

Author:  Darkmantis [ Thu Jun 08, 2006 6:24 pm ]
Post subject: 

Yes I am drawing over my paddle and then redrawing, superfreak! Its right here in part of my code :
code:
   if kp ('w') then
            Draw.ThickLine (paddleleftx, paddlelefty, paddleleftx, paddlelefty + 30, 12, white)
            Draw.ThickLine (paddleleftx, paddlelefty, paddleleftx, paddlelefty + 30, 10, black)
            delay (1)
            paddlelefty := paddlelefty + 1
            if paddlelefty > 325 then
                paddlelefty := paddlelefty - 1
            end if
        end if


and thanks hellblazer Ill try the View.Update Area Smile

Author:  TheOneTrueGod [ Thu Jun 08, 2006 6:59 pm ]
Post subject: 

Trace through your code linearly

Darkmantis wrote:

code:
   if kp ('w') then
            Draw.ThickLine (paddleleftx, paddlelefty, paddleleftx, paddlelefty + 30, 12, white)
            Draw.ThickLine (paddleleftx, paddlelefty, paddleleftx, paddlelefty + 30, 10, black)
            delay (1)
            paddlelefty := paddlelefty + 1
            if paddlelefty > 325 then
                paddlelefty := paddlelefty - 1
            end if
        end if 



Allright, first its going to erase at the paddles location, then it draws at the paddles current location, then it moves the paddle then it erases at the current location... Notice anything funny there? Its going to erase at the new position

Author:  Darkmantis [ Fri Jun 09, 2006 10:33 am ]
Post subject: 

well it worked perfectly until I added View.Updates

Author:  TheOneTrueGod [ Fri Jun 09, 2006 2:22 pm ]
Post subject: 

It shouldn't have...

Author:  Hack.saw [ Fri Jun 09, 2006 7:47 pm ]
Post subject: 

in alot less technical terms to use setscreen ('offscreenonly") you put a View.Update after a group of things u want to see happen. If you dont hte computer stores the inforamtion and u dont get to see it.
EX:
code:
setscreen ("offscreenonly")

for c:1..200
drawfilloval (20+c,20,30,30,red)
%View.Update
delay (10)
end for
%this wont work

and this will:
code:
setscreen ("offscreenonly")

for c:1..200
drawfilloval (20+c,20,30,30,red)
View.Update
delay (10)
end for


View.Updates usually go before delays also.


: