Computer Science Canada

Problem with using (i)'s in For loops

Author:  Ethan3210 [ Wed Jan 21, 2009 4:46 pm ]
Post subject:  Problem with using (i)'s in For loops

Ok, so I'm trying to make a game for my ISU in computers (and I'm trying to make a breakout rip-off. So I start working, everything's going good, but then I get to a problem. Getting all the boxes on the screen using arrays.

Here's what I had before I tried using arrays:
Turing:
var x1, x2, y1, y2, c : int

    x1 := 25
    x2 := x1 + 50
    y1 := 500
    y2 := 500 + 10

loop
        randint (c, 9, maxcolor)
        drawfillbox (x1, y1, x2, y2, c)
        boxcount += 1
        x1 := x2 + 5
        x2 := x1 + 50
        if x2 = 570 then
            drawfillbox (x1, y1, x2, y2, c)
            boxcount += 1
            x1 := 25
            x2 := x1 + 50
            y1 := y1 - 15
            y2 := y1 + 10
        end if
        exit when boxcount = 100
    end loop


That's just for the boxes, don't worry about the rest. So that works fine, and then my friend tells me that collision detection would be very hard do to for all 99 of those boxes. 'Cause when you hit the box with the ball, the box disappears, right? It would work fine, but I noticed that if when my ball hits a box, and I try to draw a box over it, it would be too hard to do(?).

So I change my program to a for loop, using the same variables. And then there's a few problems...

Turing:
var x1, x2, y1, y2, c : array 1..99 of int

for i:1..99
    x1(i) := 25
    x2(i) := 75
    y1(i) := 500
    y2(i) := 500 + 10
end for

for i : 1 .. 99
        delay (50)
        randint (c (i), 0, maxcolor)
        drawfillbox (x1 (i), y1 (i), x2 (i), y2 (i), c (i))
        x1 (i) := x2 (i) + 5
        x2 (i) := x1 (i) + 50
        if x2 (i) = 570 then
            drawfillbox (x1 (i), y1 (i), x2 (i), y2 (i), c (i))
            x1 (i) := 25
            x2 (i) := x1 (i) + 50
            y1 (i) := y1 (i) - 10
            y2 (i) := y1 (i) + 10
        end if
    end for

And all that does is draw 99 white boxes on a white screen, which isn't what my coding says it should do, right?
So yeah, I need to have this fixed by TOMMORROW or else I get a 0 on my project. Sad

So someone help please? D:


Mod Edit: Syntax tags are for code not quote tags:)
code:
[syntax="turing"]Code Here[/syntax]

Author:  saltpro15 [ Wed Jan 21, 2009 5:14 pm ]
Post subject:  RE:Problem with using (i)\'s in For loops

instead of using c(i) just make c a Rand.Int (1,99) and put c in the place of c(i), should work fine

Author:  isaiahk9 [ Wed Jan 21, 2009 5:22 pm ]
Post subject:  RE:Problem with using (i)\'s in For loops

Wow, 0% if the program doesn't work? In my class, you can get up to an 85% even if it doesn't work.

Although I'm not sure about the white color yet, but here is what I've come up with so far :

In your first for loop, initializing (giving a value to) x1, x2, y1, y2 : you are giving all those variables the same value. So all the boxes are being drawn on top of each other.

To fix this, you could go a something like this (although this code only works for about 20 boxes, so you would need to make the boxes smaller) (once you've fixed the colors) :
Turing:
x1(i) := 5 * i
x2(i) := (5 * i) + 5
y1(i) := 500
y2(i) := 510


. . . inside a for loop.
That should draw many boxes in a row on top of the screen. Now you need help fixing colors.
Is there anything else missing in your game?

PS : Welcome to compsci



Mod Edit: Remember to use syntax tags! Thanks Smile
code:
[syntax="turing"]Code Here[/syntax]

Author:  Ethan3210 [ Wed Jan 21, 2009 5:53 pm ]
Post subject:  Re: RE:Problem with using (i)\'s in For loops

isaiahk9 @ Wed Jan 21, 2009 5:22 pm wrote:
Wow, 0% if the program doesn't work? In my class, you can get up to an 85% even if it doesn't work.

Although I'm not sure about the white color yet, but here is what I've come up with so far :

In your first for loop, initializing (giving a value to) x1, x2, y1, y2 : you are giving all those variables the same value. So all the boxes are being drawn on top of each other.

To fix this, you could go a something like this (although this code only works for about 20 boxes, so you would need to make the boxes smaller) (once you've fixed the colors) :
x1(i) := 5 * i
x2(i) := (5 * i) + 5
y1(i) := 500
y2(i) := 510

. . . inside a for loop.
That should draw many boxes in a row on top of the screen. Now you need help fixing colors.
Is there anything else missing in your game?

PS : Welcome to compsci


I fixed the white box stuff. I forgot to resize the screen, it was making the boxes way above the program. xD

Now I have...

Turing:
var x1, x2, y1, y2, c : array 1..99 of int

for i : 1 .. 99
        x1 (i) := 25 * i
        x2 (i) := (25 * i) + 5
        y1 (i) := 500
        y2 (i) := 510
    end for

for i : 1 .. 99
        randint (c (i), 9, maxcolor)
        drawfillbox (x1 (i), y1 (i), x2 (i), y2 (i), c (i))
        boxcount += 1
        x1 (i) := x2 (i) + 5
        x2 (i) := x1 (i) + 50
        if x2 (i) >= 570 then
            drawfillbox (x1 (i), y1 (i), x2 (i), y2 (i), c (i))
            boxcount += 1
            x1 (i) := 25
            x2 (i) := x1 (i) + 50
            y1 (i) := y1 (i) - 15
            y2 (i) := y1 (i) + 10
        end if
        exit when boxcount = 100
    end for


It works... sorta. The boxes are too small, and it doesn't relocate to a new y coordinate once it gets greater than 570.
So, I'll look into that. Thanks though. Smile

Author:  Ethan3210 [ Wed Jan 21, 2009 5:56 pm ]
Post subject:  Re: RE:Problem with using (i)\'s in For loops

saltpro15 @ Wed Jan 21, 2009 5:14 pm wrote:
instead of using c(i) just make c a Rand.Int (1,99) and put c in the place of c(i), should work fine

'c' is the colour of the boxes, which is unimportant to me right now.(?)

I don't understand what you're getting at. xD

Author:  saltpro15 [ Wed Jan 21, 2009 5:58 pm ]
Post subject:  RE:Problem with using (i)\'s in For loops

lol my bad, I misunderstood what you were asking help with

Author:  Ethan3210 [ Wed Jan 21, 2009 5:59 pm ]
Post subject:  Re: RE:Problem with using (i)\'s in For loops

saltpro15 @ Wed Jan 21, 2009 5:58 pm wrote:
lol my bad, I misunderstood what you were asking help with

Ok, E for effort! Smile
xD
Thanks. Very Happy

Author:  saltpro15 [ Wed Jan 21, 2009 6:06 pm ]
Post subject:  Re: Problem with using (i)'s in For loops

Quote:

And all that does is draw 99 white boxes on a white screen, which isn't what my coding says it should do, right?


that was what I read, probably should have gone through the whole thing Embarassed

Author:  isaiahk9 [ Wed Jan 21, 2009 6:08 pm ]
Post subject:  RE:Problem with using (i)\'s in For loops

If the boxes are too small, instead of going like this in your first for loop :
Turing:
        x1 (i) := 25 * i
        x2 (i) := (25 * i) + 5
        y1 (i) := 500
        y2 (i) := 510

. . . go like this :
Turing:
        x1 (i) := 25 * i
        x2 (i) := (25 * i) + 25
        y1 (i) := 500
        y2 (i) := 510


And I think it would be perfectly fine (and a lot easier) if you just had fewer than 99 boxes.

Mod Edit: Syntax tags

Author:  Ethan3210 [ Wed Jan 21, 2009 6:13 pm ]
Post subject:  Re: RE:Problem with using (i)\'s in For loops

isaiahk9 @ Wed Jan 21, 2009 6:08 pm wrote:
If the boxes are too small, instead of going like this in your first for loop :
x1 (i) := 25 * i
x2 (i) := (25 * i) + 5
y1 (i) := 500
y2 (i) := 510
. . . go like this :
x1 (i) := 25 * i
x2 (i) := (25 * i) + 25
y1 (i) := 500
y2 (i) := 510

And I think it would be perfectly fine (and a lot easier) if you just had fewer than 99 boxes.


Ok, I'll make it less than 99 boxes, but it has to be at least 2 rows, which brings us to the row problem again.

How about I just give you the whole game code? (well, the level 1 code)?

Author:  Ethan3210 [ Wed Jan 21, 2009 6:35 pm ]
Post subject:  Re: Problem with using (i)'s in For loops

I got it! Very Happy

Turing:
var x1, x2, y1, y2, c : array 1 .. 99 of int
var a:int
    a:= 15
    var b:int
    b:= 1
    var d:int
    d:= 0

for i : 1 .. 99
        d:= d + 1
        x1 (i) := 25 * d
        x2 (i) := (25 * d) + 25
        y1 (i) := 500 - a
        y2 (i) := 510 - a
        if i = 10 * b then
        b:= b + 1
        a:= a + 15
        d:= 0
        y1 (i) := 500 - a
        y2 (i) := 510 - a
        end if
    end for

for i : 1 .. 99
        randint (c (i), 9, maxcolor)
        drawfillbox (x1 (i), y1 (i), x2 (i), y2 (i), c (i))
        boxcount += 1
        x1 (i) := x2 (i) + 5
        x2 (i) := x1 (i) + 50
        if x2 (i) >= 575 then
            drawfillbox (x1 (i), y1 (i), x2 (i), y2 (i), c (i))
            boxcount += 1
            x1 (i) := 25
            x2 (i) := x1 (i) + 50
            y1 (i) := y1 (i) - 15
            y2 (i) := y1 (i) + 10
        end if
        exit when boxcount = 100
    end for


Only thing left is to make the boxes wide enough to fit across the screen...


Mod Edit: Typo in the syntax tag fixed Smile

Author:  Ethan3210 [ Wed Jan 21, 2009 7:04 pm ]
Post subject:  Re: Problem with using (i)'s in For loops

Here's my Breakout rip-off so far. I need to get collision detection working, and the tutorial needs to return to the menu when it ends. Other than that, not much needs to be fixed.

Author:  isaiahk9 [ Wed Jan 21, 2009 8:10 pm ]
Post subject:  Re: Problem with using (i)'s in For loops

Huzzah! Now at least you will get some marks.
When you want the program to just go back to the menu, when the tutorial button is clicked/key is pressed, just go :
Turing:

include "menu.t"
%runs the menu procedure
menu

It seems kinda stupid that if your teacher can't help solve your problem, you get docked marks. Oh well.
As for collision detection, in your master loop, you might want to go something a-like this (master loop refers to your gameloop) :
Turing:

%Note : Do after drawing the boxes but before drawing the ball.  # is replaced by the number of boxes you have.  Should draw is a boolean array for the number of boxes you have.  They are all set to true in the beginning.
    For i : 1 , #
        If ballx > boxX(i) And ballx < boxX(i) + 25 And bally > boxY and bally < boxY(I) + 10 Then
            shoulddraw(i) = False
        End If
    End For

And when you are drawing the boxes, go a-like this in your for loop :
Turing:

If shoulddraw(i) = True
   %Draws the box
End If

Author:  Ethan3210 [ Wed Jan 21, 2009 10:53 pm ]
Post subject:  Re: Problem with using (i)'s in For loops

isaiahk9 @ Wed Jan 21, 2009 8:10 pm wrote:
Huzzah! Now at least you will get some marks.
When you want the program to just go back to the menu, when the tutorial button is clicked/key is pressed, just go :
Turing:

include "menu.t"
%runs the menu procedure
menu

It seems kinda stupid that if your teacher can't help solve your problem, you get docked marks. Oh well.
As for collision detection, in your master loop, you might want to go something a-like this (master loop refers to your gameloop) :
Turing:

%Note : Do after drawing the boxes but before drawing the ball.  # is replaced by the number of boxes you have.  Should draw is a boolean array for the number of boxes you have.  They are all set to true in the beginning.
    For i : 1 , #
        If ballx > boxX(i) And ballx < boxX(i) + 25 And bally > boxY and bally < boxY(I) + 10 Then
            shoulddraw(i) = False
        End If
    End For

And when you are drawing the boxes, go a-like this in your for loop :
Turing:

If shoulddraw(i) = True
   %Draws the box
End If


For the include part, I've tried that, I always get an error that says "Recursive include of "menu.t"...

As for the collision detection, it sorta worked, but now the ball keeps hitting invisible walls... so umm yeah. >.<
I hope he still passes me for this...

Author:  isaiahk9 [ Thu Jan 22, 2009 7:08 am ]
Post subject:  RE:Problem with using (i)\'s in For loops

You could've mentioned you have recurrsion. It means that fileA.t is calling fileB.t which is calling fileA.t It means it is just repetiitively calling itself. If you wanted to get rid of that, then you could search "recurrsion" in the Turing help forum.
As for hitting inbisible walls, it means that it is hitting boxes that are no longer being drawn.
It just means that in this piece of code :
Turing:

%Note : Do after drawing the boxes but before drawing the ball.  # is replaced by the number of boxes you have.  Should draw is a boolean array for the number of boxes you have.  They are all set to true in the beginning.
    For i : 1 , #
        If ballx > boxX(i) And ballx < boxX(i) + 25 And bally > boxY and bally < boxY(I) + 10 Then
            shoulddraw(i) = False
        End If
    End For

You should go like this :
Turing:

%Note : Do after drawing the boxes but before drawing the ball.  # is replaced by the number of boxes you have.  Should draw is a boolean array for the number of boxes you have.  They are all set to true in the beginning.
    For i : 1 , #
        If ballx > boxX(i) And ballx < boxX(i) + 25 And bally > boxY and bally < boxY(I) + 10 And shoulddraw(i) = True Then
            shoulddraw(i) = False
        End If
    End For

That should work.
He should still pass you if he is anything fair.
Good luck on your exam today by the way, should be easy if it is anything like last year's.

Author:  Ethan3210 [ Thu Jan 22, 2009 8:52 am ]
Post subject:  Re: RE:Problem with using (i)\'s in For loops

isaiahk9 @ Thu Jan 22, 2009 7:08 am wrote:
You could've mentioned you have recurrsion. It means that fileA.t is calling fileB.t which is calling fileA.t It means it is just repetiitively calling itself. If you wanted to get rid of that, then you could search "recurrsion" in the Turing help forum.
As for hitting inbisible walls, it means that it is hitting boxes that are no longer being drawn.
It just means that in this piece of code :
Turing:

%Note : Do after drawing the boxes but before drawing the ball.  # is replaced by the number of boxes you have.  Should draw is a boolean array for the number of boxes you have.  They are all set to true in the beginning.
    For i : 1 , #
        If ballx > boxX(i) And ballx < boxX(i) + 25 And bally > boxY and bally < boxY(I) + 10 Then
            shoulddraw(i) = False
        End If
    End For

You should go like this :
Turing:

%Note : Do after drawing the boxes but before drawing the ball.  # is replaced by the number of boxes you have.  Should draw is a boolean array for the number of boxes you have.  They are all set to true in the beginning.
    For i : 1 , #
        If ballx > boxX(i) And ballx < boxX(i) + 25 And bally > boxY and bally < boxY(I) + 10 And shoulddraw(i) = True Then
            shoulddraw(i) = False
        End If
    End For

That should work.
He should still pass you if he is anything fair.
Good luck on your exam today by the way, should be easy if it is anything like last year's.

Yeah, that worked. Thanks. Very Happy
But now, the ball is still going through some of the boxes. Oh well. x)


: