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

Username:   Password: 
 RegisterRegister   
 Turing slows down
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
adrianr2z




PostPosted: Tue Jun 04, 2013 2:34 pm   Post subject: Turing slows down

What is it you are trying to achieve?
Keep turing at the same speed. Basically to do so, I Have the overwrite on the arrays or get rid of them as the "bricks" roll off the screen.

What is the problem you are having?
Turing slows down over time when you play my game on the hard and medium level. The reason that this is caused by is because i have made an array of 1..999999.


Describe what you have tried to solve this problem
Nothing. Unsure of what to do. Read about flexible arrays and removing elements but don't quite understand. I have read the arrays tutorial many times and specifically about removing elements but I just can't seem to understand how to imply it in my code.


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, buttonnumber, buttonupdown : int
var x, y, rand1, value2, xvaluerand, speedofball, blockdivision : int
var xvalue, width, score, scorex, scorey, speed : int := 0
var score1 : string
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 := 300
value2 := 20
xvaluerand := Rand.Int (0,250)
speedofball := 0
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
boxWidth := 200
boxHeight := 100
numBlocks := 1
var chars : array char of boolean
var blocks : array 1 .. 999999, 1 .. 2 of int
var newblock : boolean := false


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

procedure game



    loop

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


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


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





        for i : 1 .. numBlocks
            Draw.FillBox (blocks (i, 1), blocks (i, 2), blocks (i, 1) + 100, blocks (i, 2) + 20, black)
        end for



        for i : 1 .. numBlocks

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

            if blocks (i, 2) = blockdivision then

                newblock := true

            end if
        end for

        if newblock = true then

            xvalue := Rand.Int (0, xvaluerand)

            loop
                numBlocks := numBlocks + 1

                randint (width, ballradius * 2 + 1, rand1) % sets the x location

                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

        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 (score1, scorex + 120, scorey, font, brightblue)

end game


procedure reset
    ballx := maxx div 2
    bally := maxy div 2
    score := 0
    xvalue := Rand.Int (0, xvaluerand)
    numBlocks := 0

    loop
        numBlocks := numBlocks + 1

        randint (width, ballradius * 2 + 1, rand1)         % sets the x location

        blocks (numBlocks, 1) := xvalue

        xvalue := xvalue + value2 + width

        blocks (numBlocks, 2) := 0

        blocks (numBlocks + 1, 1) := xvalue

        exit when xvalue >= maxx

    end loop
end reset



loop

    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)
    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, 21)
    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
            speedofball := 1
            blockdivision := maxx div 20
            speed := 1
            ballspeed := 2
            game
            reset
        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
            speedofball := 2
            blockdivision := maxx div 15
            speed := 2
            ballspeed := 4
            game
            reset
        end if

        if x > box3X and x < box3X + boxWidth and y > box3Y and y < box3Y + boxHeight then
            speedofball := 3
            blockdivision := maxx div 10
            speed := 3
            ballspeed := 6
            game
            reset
        end if
    end if

    View.Update

end loop



Please specify what version of Turing you are using
Latest
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Tue Jun 04, 2013 3:00 pm   Post subject: RE:Turing slows down

Why do you need an array that size? Why don't you have a certain amount of blocks and just reuse them? I remember when I made a sidescrolling shooter, I had a maximum enemy count of 30 enemies. There would never be that many on the screen, so I wouldn't have to worry about an upper limit.

I don't know what a good number is for yours, but maybe you could have a maximum of 100 blocks or something, and just kill off blocks once they reach the top of the screen. That way you don't have to have rediculous arrays. here's an example:

Turing:

setscreen ("offscreenonly")

type blockData :
    record
        x, y : int
        live : boolean
    end record

var block : array 1 .. 10 of blockData
var blockNum : int := 0
var spawnDelay : int := 100

for i : 1 .. 10
    block (i).live := false
end for

loop
    for i : 1 .. 10
        if block (i).live = true then
            block (i).x -= 2
            if block (i).x <= 0 then
                block (i).live := false
            end if
        end if
    end for

    for i : 1 .. 10
        if block (i).live = true then
            Draw.FillBox (block (i).x - 5, block (i).y - 5, block (i).x + 5, block (i).y + 5, 12)
        end if
    end for

    spawnDelay -= 1
    if spawnDelay <= 0 then
        spawnDelay := 100
       
        blockNum += 1
        if blockNum > 10 then
            blockNum := 1
        end if
       
        block (blockNum).live := true
        block (blockNum).x := maxx
        block (blockNum).y := Rand.Int (0, maxy)
    end if

    View.Update
    cls
end loop

adrianr2z




PostPosted: Thu Jun 06, 2013 3:25 pm   Post subject: Re: Turing slows down

Trying to follow the above suggestion got a little tricky in my code so instead of doing that, I kind of "cheated" and just said that when the brick reaches maxy, reset its Y value to be 0 which worked but now my code is all problematic. I spent time trying to figure where its going wrong but I'm stumped. Could you please look at it and see if you can help out at all? Thanks

The problem is Medium and Hard . Level "Easy" works flawlessly but when I choose medium and hard, thats where it gets problematic.



Falldown 3 Unfix V3.t
 Description:

Download
 Filename:  Falldown 3 Unfix V3.t
 Filesize:  5.88 KB
 Downloaded:  68 Time(s)

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  [ 3 Posts ]
Jump to:   


Style:  
Search: