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

Username:   Password: 
 RegisterRegister   
 Remove Flickering (it came back) and program simple AI
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Velocity




PostPosted: Tue Dec 13, 2011 6:42 pm   Post subject: Remove Flickering (it came back) and program simple AI

What is it you are trying to achieve?
Remove the god damn flickering! once and for all... before i got rid of it but now i came back and its not gonig away.

I also want to program the AI where i indicated it... i want to know how to go about doing that.

What is the problem you are having?
The flickering is really pissing me off. And the A.I is confusing :O

I want the A.I to follow the direction of the ball once i make it move ( which i can do ) but i want to know how to make the paddle follow the ball and make it miss sometimes but not alot

Describe what you have tried to solve this problem
Read all the documentations for flickering ... got nada from it.

A.I - I have not really looked that much into it, im just looking for someone to share some words of wisdom or a tutorial on it.

Turing:


import GUI
View.Set ("graphics:638;690")
%_______The "Bank"_______%
type DataOfBlock :
    record
        x, y, width, height, center : int
        defeated : boolean
    end record
type DataOfShell :
    record
        right, top, left, bottom, c : int
    end record
type BallData :
    record
        x, y, r, xv, yv, speed, c : int
    end record
%_______Global Variables_______%
var PicID : int
var x : int := 700
var y : int := 500
var replay : string
var fontS : int
fontS := Font.New ("Verdana:20")
PicID := Pic.FileNew ("ball02.jpg")
Pic.Draw (PicID, maxx div 2 - (638 div 2), maxy div 2 - (690 div 2), picCopy)
%_______Procedures_______%
procedure TheGame
    setscreen ("graphics:1400;700,title:The   AI   BrickBreaker   Game")
    View.Set ("offscreen")
    %------------------------------
    %Block variables              %
    var xSpacing, ySpacing : int  %
    var blockH, blockW : int      %
    var shell : DataOfShell       %
    var ball : BallData           %%%
    var blockXDimension : int := 20 %
    var blockYDimension : int := 10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    var block : array 1 .. blockXDimension, 1 .. blockYDimension of DataOfBlock %
    shell.right := 710            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    shell.top := 680              %
    shell.left := 60              %
    shell.bottom := 50            %
    shell.c := 35                 %
    %------------------------------
    %---------------------------
    %Player Co-ords            %
    var x1, y1, x2, y2 : int   %
    x1 := 250                  %
    y1 := 25                   %
    x2 := 350                  %
    y2 := 50                   %
    %---------------------------
    %------------------------------------------------
    %Ball Co-ords                                   %
    var ball_x1, ball_x2, ball_y1, ball_y2 : int    %
    var ball_directionx : int := 1                  %
    var ball_directiony : int := 1                  %
    var ball_speed : int                            %
    ball_speed := 10                                %
    ball_x1 := 512                                  %
    ball_y1 := 200                                  %
    ball_x2 := 1024                                 %
    ball_y2 := 200                                  %
    %
    %------------------------------------------------
    %----------------------------------------
    %Game Basics                            %
    var continueGame : string               %
    var lives : int := 2                    %
    var score : int := 0                    %
    var timeElapsed : int := Time.Elapsed   %
    %----------------------------------------
    %----------------------------------------------------
    %Boundaries (paddle + ball)                         %
    var boundary_x1 : int := ((maxx div maxx) - 1) + 20 %
    var boundary_x2 : int := maxx - 20                  %
    var boundary_y1 : int := maxy - 20                  %
    var boundary_y2 : int := ((maxy div maxy) - 1) + 20 %
    %----------------------------------------------------
    var arrowKeys : array char of boolean
    lives += 1
    loop
        locate (1, 10)
        put "Lives: ", lives
        locate (1, 30)
        put "Score: ", score
        locate (1, 50)
        put "Game Time: ", Time.Elapsed / 1000
        var block_x1, block_y1, block_x2, block_y2 : int
        block_x1 := 50
        block_y1 := 300
        block_x2 := 100
        block_y2 := 350

        %%          Player Screen           %%________________________________________________

        drawfillbox (block_x1 + 40, block_y1 + 75, block_x2 + 40, block_y2 + 75, blue)
        drawfillbox (block_x1 + 120, block_y1 + 75, block_x2 + 120, block_y2 + 75, blue)
        drawfillbox (block_x1 + 200, block_y1 + 75, block_x2 + 200, block_y2 + 75, blue)
        drawfillbox (block_x1 + 280, block_y1 + 75, block_x2 + 280, block_y2 + 75, blue)
        drawfillbox (block_x1 + 360, block_y1 + 75, block_x2 + 360, block_y2 + 75, blue)

        drawfillbox (block_x1 + 40, block_y1 + 150, block_x2 + 40, block_y2 + 150, blue)
        drawfillbox (block_x1 + 120, block_y1 + 150, block_x2 + 120, block_y2 + 150, blue)
        drawfillbox (block_x1 + 200, block_y1 + 150, block_x2 + 200, block_y2 + 150, blue)
        drawfillbox (block_x1 + 280, block_y1 + 150, block_x2 + 280, block_y2 + 150, blue)
        drawfillbox (block_x1 + 360, block_y1 + 150, block_x2 + 360, block_y2 + 150, blue)

        drawfillbox (block_x1 + 40, block_y1 + 225, block_x2 + 40, block_y2 + 225, blue)
        drawfillbox (block_x1 + 120, block_y1 + 225, block_x2 + 120, block_y2 + 225, blue)
        drawfillbox (block_x1 + 200, block_y1 + 225, block_x2 + 200, block_y2 + 225, blue)
        drawfillbox (block_x1 + 280, block_y1 + 225, block_x2 + 280, block_y2 + 225, blue)
        drawfillbox (block_x1 + 360, block_y1 + 225, block_x2 + 360, block_y2 + 225, blue)

        drawfillbox (block_x1 + 40, block_y1 + 300, block_x2 + 40, block_y2 + 300, blue)
        drawfillbox (block_x1 + 120, block_y1 + 300, block_x2 + 120, block_y2 + 300, blue)
        drawfillbox (block_x1 + 200, block_y1 + 300, block_x2 + 200, block_y2 + 300, blue)
        drawfillbox (block_x1 + 280, block_y1 + 300, block_x2 + 280, block_y2 + 300, blue)
        drawfillbox (block_x1 + 360, block_y1 + 300, block_x2 + 360, block_y2 + 300, blue)

        drawfillbox (block_x1 + 440, block_y1 + 75, block_x2 + 440, block_y2 + 75, blue)
        drawfillbox (block_x1 + 440, block_y1 + 150, block_x2 + 440, block_y2 + 150, blue)
        drawfillbox (block_x1 + 440, block_y1 + 225, block_x2 + 440, block_y2 + 225, blue)
        drawfillbox (block_x1 + 440, block_y1 + 300, block_x2 + 440, block_y2 + 300, blue)

        %%          AI Screen           %%________________________________________________

        drawfillbox (block_x1 + 760, block_y1 + 75, block_x2 + 760, block_y2 + 75, blue)
        drawfillbox (block_x1 + 840, block_y1 + 75, block_x2 + 840, block_y2 + 75, blue)
        drawfillbox (block_x1 + 920, block_y1 + 75, block_x2 + 920, block_y2 + 75, blue)
        drawfillbox (block_x1 + 1000, block_y1 + 75, block_x2 + 1000, block_y2 + 75, blue)
        drawfillbox (block_x1 + 1080, block_y1 + 75, block_x2 + 1080, block_y2 + 75, blue)

        drawfillbox (block_x1 + 760, block_y1 + 150, block_x2 + 760, block_y2 + 150, blue)
        drawfillbox (block_x1 + 840, block_y1 + 150, block_x2 + 840, block_y2 + 150, blue)
        drawfillbox (block_x1 + 920, block_y1 + 150, block_x2 + 920, block_y2 + 150, blue)
        drawfillbox (block_x1 + 1000, block_y1 + 150, block_x2 + 1000, block_y2 + 150, blue)
        drawfillbox (block_x1 + 1080, block_y1 + 150, block_x2 + 1080, block_y2 + 150, blue)

        drawfillbox (block_x1 + 760, block_y1 + 225, block_x2 + 760, block_y2 + 225, blue)
        drawfillbox (block_x1 + 840, block_y1 + 225, block_x2 + 840, block_y2 + 225, blue)
        drawfillbox (block_x1 + 920, block_y1 + 225, block_x2 + 920, block_y2 + 225, blue)
        drawfillbox (block_x1 + 1000, block_y1 + 225, block_x2 + 1000, block_y2 + 225, blue)
        drawfillbox (block_x1 + 1080, block_y1 + 225, block_x2 + 1080, block_y2 + 225, blue)

        drawfillbox (block_x1 + 760, block_y1 + 300, block_x2 + 760, block_y2 + 300, blue)
        drawfillbox (block_x1 + 840, block_y1 + 300, block_x2 + 840, block_y2 + 300, blue)
        drawfillbox (block_x1 + 920, block_y1 + 300, block_x2 + 920, block_y2 + 300, blue)
        drawfillbox (block_x1 + 1000, block_y1 + 300, block_x2 + 1000, block_y2 + 300, blue)
        drawfillbox (block_x1 + 1080, block_y1 + 300, block_x2 + 1080, block_y2 + 300, blue)

        drawfillbox (block_x1 + 1160, block_y1 + 75, block_x2 + 1160, block_y2 + 75, blue)
        drawfillbox (block_x1 + 1160, block_y1 + 150, block_x2 + 1160, block_y2 + 150, blue)
        drawfillbox (block_x1 + 1160, block_y1 + 225, block_x2 + 1160, block_y2 + 225, blue)
        drawfillbox (block_x1 + 1160, block_y1 + 300, block_x2 + 1160, block_y2 + 300, blue)

        %%_______________________________________________________________________________%%

        View.UpdateArea (0, 0, maxx, maxy)
        delay (5)
       
        Input.KeyDown (arrowKeys)
        if arrowKeys (KEY_RIGHT_ARROW) then
            x1 += 10
            x2 += 10
        end if
        if arrowKeys (KEY_LEFT_ARROW) then
            x2 -= 10
            x1 -= 10
        end if

        if x1 > 250 and x2 < 350 then
            x1 := 100
        elsif x1 < 250 and x2 > 350 then
            x2 := 100
        elsif y1 > 25 and y2 < 50 then
            y1 := 25
        elsif y1 < 25 and y2 > 50 then
            y2 := 25
        end if
        if arrowKeys (KEY_ESC) then
            cls
            break
        end if
        var colorchanger1 : int
        randint (colorchanger1, 1, 101)
        delay (1)
        drawfillbox (x1, y1, x2, y2, colorchanger1)
        delay (5)
        cls
        drawbox (20, 20, maxx - 20, maxy - 20, 51)
        drawbox (5, 5, maxx - 5, maxy - 5, 51)
        drawfillbox (250, 900, 350, 925, 101)
        drawfillbox (maxx div 2 - 40, 20, maxx div 2 - 35, maxy - 20, 51)
        drawfilloval (ball_x1, ball_y1, 15, 15, 48)
        drawfill (maxx - 15, maxy - 15, 71, 51)
        View.UpdateArea (0, 0, maxx, maxy)
        if x1 >= maxx - 125 or x2 >= maxx - 25 then
            x1 -= 10
            x2 -= 10
        end if
        if x1 <= 0 or x2 <= 25 + 100 then
            x1 += 10
            x2 += 10
        end if
        if ball_y1 >= maxy - 40 then
            ball_directiony *= -1
        end if
        if ball_x1 >= maxx div 2 - 55 then
            ball_directionx *= -1
        end if
        if ball_x1 <= 45 then
            ball_directionx *= -1
        end if
        var direction : int
        ball_x1 += (ball_directionx * ball_speed)
        ball_y1 += (ball_directiony * ball_speed)
        if ball_x1 <= x2 and ball_y1 <= y2 + 15 and ball_x1 >= x1 and ball_y1 >= y1 + 15 then
            ball_directiony *= -1
        end if
        %% ______________ (AI + Paddle + ball)_______________%%

        var x1_1, y1_1, x2_2, y2_2 : int
        x1_1 := 1050
        y1_1 := 25
        x2_2 := 1150
        y2_2 := 50
        drawfilloval (ball_x2, ball_y2, 15, 15, 61)
        drawfillbox (x1_1, y1_1, x2_2, y2_2, 141)
        if x1_1 > 1 then
            x1_1 += 10
            x2_2 += 10
        end if



        %%_________________________________________________%%
        if ball_y1 < 25 then
            lives -= 1
            if lives <= 0 then
                cls
                Font.Draw ("You Have Lost :(", 50, 50, fontS, green)
                put "You have acquired a total score of: ", score
                loop
                    put "Would you like to play again?"
                    get replay
                    if replay = "Yes" or replay = "yes" or replay = "y" then
                        put "Your game will replay shortly..."
                        put "Get READY!"
                        Time.Delay (3000)
                        timeElapsed := 0
                        TheGame
                    elsif replay = "No" or replay = "no" or replay = "n" then
                        put "Good bye, and thanks for playing!"
                        Time.Delay (2500)
                        cls
                        break
                    else
                        put "Invalid answer"
                        cls
                    end if
                end loop
            end if
        end if
        loop
            if ball_y1 <= 20 then
                cls
                put "Would you like to continue? y/n: " ..
                get continueGame
            else
                exit
            end if
            if continueGame = "y" or continueGame = "yes" then
                cls
                ball_x1 := 512
                ball_y1 := 200
                ball_directionx := 1
                ball_directiony := 1
                exit
            elsif continueGame = "n" or continueGame = "No" then
                put "Have A Nice Day!"
                delay (2500)
                break
            else
                put "Invalid answer."
                delay (2500)
                cls
            end if
        end loop
        delay (10)
    end loop
end TheGame
procedure Instructions
    colorback (16)
    color (48)
    cls
    locate (4, 1)
    put "The objective of the game is..."
    locate (7, 1)
    color (58)
    put "to destroy all of the blocks on your side of the screen (bottom)"
    locate (10, 1)
    color (73)
    put "Before the computer destroys all of his blocks on his side of the screen (top)"
    locate (13, 1)
    color (81)
    put "You will use the 'Left' and 'Right' Arrow keys to move left and right"
    locate (16, 1)
    color (67)
    put "If you press the 'ESC' key on your keyboard the game will exit."
    locate (19, 1)
    color (96)
    put "Good Luck and go smash some blocks. HINT : You only have 3 Lives!"

end Instructions
procedure TheTitleScreen
    var font1 : int
    font1 := Font.New ("Arial:13")
    assert font1 > 0
    Pic.Draw (PicID, maxx div 2 - (638 div 2), maxy div 2 - (690 div 2), picCopy)
    var game_Button : int := GUI.CreateButton (265, 275, 0, "Click To Play!", TheGame)
    var instruction : string
    locate (37, 4)
    Font.Draw ("Please hold 'Click to Play' while you type your answer below!", 50, 150, font1, yellow)
    locate (38, 4)
    Font.Draw ("You will be redirected to the instructions so keep holding your mouse.", 50, 125, font1, green)
    locate (39, 4)
    Font.Draw ("Or drag away from 'Click to Play' then read and Click it afterwards.", 50, 75, font1, yellow)
    locate (40, 4)
    Font.Draw ("Do you want to see the game Instructions? ", 50, 40, font1, green)
    locate (50, 47)
    get instruction
    if instruction = "yes" then
        Instructions
    else
        Instructions
    end if
end TheTitleScreen
procedure returnBack
    var go_back : char := "n"
    loop
        get go_back
        if go_back = "y" then
            TheTitleScreen
        end if
    end loop
end returnBack
TheTitleScreen
loop
    exit when GUI.ProcessEvent
end loop



Please specify what version of Turing you are using
4.1.1a

Gladly giving karma for help Smile
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Tue Dec 13, 2011 6:51 pm   Post subject: RE:Remove Flickering (it came back) and program simple AI

code:
View.UpdateArea (0, 0, maxx, maxy)


How many times does this line occur in your code?
Dreadnought




PostPosted: Tue Dec 13, 2011 7:08 pm   Post subject: Re: Remove Flickering (it came back) and program simple AI

Velocity wrote:

Read all the documentations for flickering ... got nada from it.

Did the documentation actually tell you that this line (the exact way it is written here and in your code) was valid?
Turing:
View.Set ("offscreen")
crossley7




PostPosted: Tue Dec 13, 2011 8:17 pm   Post subject: RE:Remove Flickering (it came back) and program simple AI

first, try
Turing:
View.Set ("offscreenonly")
and for the AI, a possible way you can have it move is that if the paddle is not even with the ball's location, you can have it follow by a random integer such as
Turing:
randint (move, 1, min (abs(ballx-paddlex), 10)

or whatever you wan the upper range to be. You can adjust these bounds through testing to find an optimal way of having it work. If it has a constant movement then you can give it a certain chance that the paddle will move or not. I have implemented basic AI such as this in a few places and it works quite well. Since it should read through the code many times a second, it will appear to move slowly as opposed to stop/start type of motion that would come with a boolean move or don't statement. Thee are a few ideas to get you started, but there are countless ways of designing an AI and it all depends on how strong you want it and what you want it to do.
Velocity




PostPosted: Tue Dec 13, 2011 10:00 pm   Post subject: RE:Remove Flickering (it came back) and program simple AI

crossley thanks, i know alot about A.I in C++ as i do quite alot of work in C++, i also tutor it. Turing is like a whole new story for me. Thanks alot for the advice and @ insectoid and dreadnought i have already tried all the possible things that are inside the documentation and none of them work? can you guys please run my program and implement something that could possibly run it smoothly? i was thinking of how it take a picture of every single line or procedure that way it has a saved knowledge of how it may run.

P.S To all i will gladly give you all karma and bits once i get the commands implemented, and since you tried Smile thanks.
Tony




PostPosted: Tue Dec 13, 2011 10:17 pm   Post subject: Re: RE:Remove Flickering (it came back) and program simple AI

Velocity @ Tue Dec 13, 2011 10:00 pm wrote:
i do quite alot of work in C++, i also tutor it.

I highly doubt that you are able to write anything meaningful in something as complex as C++, and then have trouble figuring out Turing's documentation while people are telling you exactly what you are doing wrong.

With a bit of paraphrasing:
Dreadnought @ Tue Dec 13, 2011 7:08 pm wrote:

[hey Velocity, you made up an invalid mode instead of using one from documentation; your error is at:]
Turing:
View.Set ("offscreen")



crossley7 @ Tue Dec 13, 2011 8:17 pm wrote:

[yeah, it should be]
Turing:
View.Set ("offscreenonly")

Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Aange10




PostPosted: Wed Dec 14, 2011 12:10 am   Post subject: Re: Remove Flickering (it came back) and program simple AI

I agree. I'm sorry to flame, but 90% of your posts fail to show that you actually try anything. You usually fail to do anything on your own, or use tools provided to you. You seem to waver from question to question, never figuring out how to think for your self.

velocity wrote:

i have already tried all the possible things that are inside the documentation and none of them work?


... Further more ...
velocity wrote:

can you guys please run my program and implement something


134 posts and you still fail to think for your self. You deem the tools everybody else uses as useless, and look to everybody else to solve your problems. Here's some people to look to;

http://wiki.compsci.ca/index.php?title=Main_Page
www.wikipedia.org
www.google.com
Velocity




PostPosted: Wed Dec 14, 2011 7:14 am   Post subject: RE:Remove Flickering (it came back) and program simple AI

okay i tried to use this as a help forums, never mind.
Sponsor
Sponsor
Sponsor
sponsor
Aange10




PostPosted: Wed Dec 14, 2011 8:34 am   Post subject: Re: RE:Remove Flickering (it came back) and program simple AI

Velocity @ 14/12/2011, 6:14 am wrote:
okay i tried to use this as a help forums, never mind.


into

Quote:
can you guys please run my program and implement something


If you read the rules

Welcome to Turing Help; Important Info & Tutorial Links wrote:

... Also plz don't post asking people to do your schoolwork for you, we will help but we will not do your projects for you. ...
... Please use these sources, as well as the Search, before posting your question. ...


You must try so hard.
mirhagk




PostPosted: Wed Dec 14, 2011 8:45 am   Post subject: RE:Remove Flickering (it came back) and program simple AI

Velocity, I ran your code, and fixed it (along with other various mistakes you had which prevented it from even running). Here is your game logic:

draw players screen
View.Update
delay
draw paddle
View.Update
delay
cls
draw other stuff
View.Update
delay

As everyone has said, you need to draw everything all at once and do any of the following:

draw everything
View.Update
delay
cls


or

cls
draw everything
VIew.Update
delay

Do one of those. Also in this particular instance I fixed it by first of all using offscreenonly not offscreen, and then removing every View.Update or View.UpdateArea and only having 1 in your code. You should only ever have 1 per loop.
Insectoid




PostPosted: Wed Dec 14, 2011 9:06 am   Post subject: RE:Remove Flickering (it came back) and program simple AI

Velocity, do you know what causes flickering? It happens because Turing draws things to the video buffer at a different speed than the video card draws to the screen. When you tell Turing to draw lots of things, it only manages to draw a few of them before the video card draws to the screen. So the video card draws half an image, then Turing draws a little more, the video card draws a little more, Turing draws a little more, the video card draws a little more, etc. This causes flickering.

View.Set ("offscreenonly") tells turing to double-buffer graphics. This means when you tell it to draw things, it will draw them to its own internal 'screen' (you can't see it). The video card never gets to see this. Then, when you call View.Update, Turing shows the whole picture to the video card and says "Draw all of these things".

If you call View.Update more than once per loop, it's just like drawing things individually. You'll draw half an image, show it to the video card, then draw a bit more, show it to the video card, and then draw some more, and show it to the video card.

Hopefully this illustrates why you should be using View.Update, how you are using it wrong, and how to fix it.
crossley7




PostPosted: Wed Dec 14, 2011 12:07 pm   Post subject: RE:Remove Flickering (it came back) and program simple AI

Also, if you say you have made AI in C++ then you should be fine in turing. Changing language doesn't change the logic, just the words that are used to implement it. I use C++ mostly now and I find it tends to be a step up from Turing rather than a step down. It is faster and has more functionality and the only thing turing has on it is if you need really basic graphics for a simple program.

I somehow doubt that you struggle with Turing if you have any understanding of any language, nevermind C++.
Velocity




PostPosted: Wed Dec 14, 2011 6:37 pm   Post subject: RE:Remove Flickering (it came back) and program simple AI

Thank you very much for mirhagk for that help, since now i have completely removed my flickering.
Also thank you very much to Insectoid for clearly explaining in detail the purpose of those commands (that cleared up alot of thing, thanks alot) !
mirhagk




PostPosted: Wed Dec 14, 2011 8:23 pm   Post subject: RE:Remove Flickering (it came back) and program simple AI

For future reference don't post and say "My program flickers and I can't fix it, I've read everything, you guys fix it". Try and understand and fix it on your own, and failing that, ask us to explain it. We will gladly explain things in detail if you ask, it's just a matter of asking for help in understanding.
Velocity




PostPosted: Wed Dec 14, 2011 8:36 pm   Post subject: RE:Remove Flickering (it came back) and program simple AI

That being said, may you please explain to me my next post that i made about clearing the brick after it is hit.
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 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: