Computer Science Canada

Tetris!!

Author:  OmegaSyrus [ Sat Nov 18, 2006 10:38 pm ]
Post subject:  Tetris!!

This is a tetris remake i created for a final project for grade 11 last school year. Turned out beautifully. Unfortunately is a mute-program only because in order to upload the file i had to take out all the music to make it small enough. But the graphics are pretty. Unfortunately the program lags like CRAZY on my just-bought computer and i cant figure out why, so if it lags on you i beg your forgiveness, because it ran perfectly on every other computer i used it on. Anyway, give it a try and i hope u enjoy it! Smile

Author:  DemonZ [ Sun Nov 19, 2006 7:25 pm ]
Post subject: 

this is a very good and inviting tetris game! only thing is it did run a little slow on my computer, but I think the reason ur game runs so slow is because you have too many pictures being drawn at once, or if not that, then maybe its because of how turing compiled it all (turing v. 4.0.5 compiler is very slow, while turing v. 4.1 is faster and better) overall, this games graphics are very nice, and the menu is very thought out. one suggestion though, try commenting your program so that people will have an easier time understanding your logic and learning from it (I dare say stealing or cheating from it) but anyways, good game.

Author:  ericfourfour [ Sun Nov 19, 2006 8:45 pm ]
Post subject: 

Wow ~2400 lines for Tetris. When there is that many lines in a game like Tetris there has to be room for improvement.

Let's start here.
code:
if button = 1 & (x >= 542 & x <= 600 & y >= 20 & y <= 40) then
    exit
elsif button = 1 & (x >= 234 & x <= 252) & (y >= 180 & y <= 200) then
    levelchoice := 0
    levelchangechoice := -1
elsif button = 1 & (x >= 253 & x <= 271) & (y >= 180 & y <= 200) then
    levelchoice := 1
    levelchangechoice := 0
elsif button = 1 & (x >= 272 & x <= 290) & (y >= 180 & y <= 200) then
...

In the if statements you repeatedly check if the button equals 1. Make another if statement that checks if the button equals 1. If it is 1 it will go through the other if statements. It would look like this.
code:
if button = 1
    if (x >= 542 & x <= 600 & y >= 20 & y <= 40) then
        exit
    elsif (x >= 234 & x <= 252) & (y >= 180 & y <= 200) then
        levelchoice := 0
        levelchangechoice := -1
    elsif (x >= 253 & x <= 271) & (y >= 180 & y <= 200) then
        levelchoice := 1
        levelchangechoice := 0
    elsif (x >= 272 & x <= 290) & (y >= 180 & y <= 200) then
    ...
end if

Another thing I noticed was you repeatedly check 180 <= y <= 200. However it is different in the first check. Instead you check 20 <= y <= 40. Why not separate all of the 180 <= y <= 200 checks into a different if structure and have the 20 <= y <= 40 in an exit when statement. It would look like this.
code:
if button = 1
    exit when (x >= 542 & x <= 600) & (y >= 20 & y <= 40)
    if y >= 180 & y <= 200 then
        if x >= 234 & x <= 252 then
            levelchoice := 0
            levelchangechoice := -1
        elsif x >= 253 & x <= 271 then
            levelchoice := 1
            levelchangechoice := 0
        elsif x >= 272 & x <= 290 then
        ...
    end if
end if

Next, I noticed you had the level choice increase by 1 in every if and the level change choice was 1 less than the it. We will deal with this after one other thing.

This is where it can get a bit complicated. Each if checks if the x is between two certain values. These two values have a difference of 18 and the next if checks where the last one left off plus one. What you have to do is construct a good for loop to perform this check that increases by 19 every iteration. Then you would put the ifs inside the for loop so it would look like this.
code:
for i : 234 .. 481 by 19
    if x >= i & x <= i + 18 then

    end if
end for

Now it checks every place x can be in between. Next we have to convert i's value to a number that increments every iteration. The equation should look like this.
code:
value := (i - 234) div 19

Now lets add that to the for loop. Wait, there is one more thing. How do we get the value of the level change choice? We simply subtract one from whatever the level choice is. This is what we have so far.
code:
if button = 1
    exit when x >= 542 & x <= 600 & y >= 20 & y <= 40
    if y >= 180 & y <= 200 then
        for i : 234 .. 481 by 19
            if x >= i & x <= i + 18 then
                levelchoice := (i - 234) div 19
            end if
        end for
        levelchangechoice := levelchoice - 1
    end if
end if

You will realize that the for loop, loops through every if even if it was not supposed to. In this case we will just add an exit inside the if statement. Here is the new version.
code:
if button = 1
    exit when x >= 542 & x <= 600 & y >= 20 & y <= 40
    if y >= 180 & y <= 200 then
        for i : 234 .. 481 by 19
            if x >= i & x <= i + 18 then
                levelchoice := (i - 234) div 19
                exit
            end if
        end for
        levelchangechoice := levelchoice - 1

If you have been paying attention you will see that 44 lines have just been reduced to 10.

Now for the next part of the section.
code:
%part of the "if" statement that dictates the variable change of the music to be played in the game
elsif button = 1 & (x >= 248 & x <= 270) & (y >= 285 & y <= 305) then
    music := 1
elsif button = 1 & (x >= 278 & x <= 300) & (y >= 285 & y <= 305) then
    music := 2
elsif button = 1 & (x >= 308 & x <= 330) & (y >= 285 & y <= 305) then
    music := 3
elsif button = 1 & (x >= 338 & x <= 400) & (y >= 285 & y <= 305) then
    music := 4

Once, again the repetitions. Since they it is checking a different y position as the above ifs we will start a new elsif.
code:
(This is branching off the previous work done)
elsif y >= 285 & y < 305 then
    if x >= 248 & x <= 270 then
        music := 1
    elsif x >= 278 & x <= 300 then
        music := 2
    elsif x >= 308 & x <= 330 then
        music := 3
    elsif x >= 338 & x <= 400 then
        music := 4

This time it is checking if the x is between a space of 22 pixels and each if is separated by 30 pixels. This is going to work a lot like the previous for loop. Here is what it will look like.
code:
for i : 248 .. 338 by 30

For the checks we will need to do the same thing we did before and the formula for getting an incrementing value follows a pretty simple pattern.
code:
for i : 248 .. 338 by 30
    if x >= i & x <= i + 22 then
        music := (i - 248) div 30 + 1
        exit
    end if
end for

This is what the combined work looks like so far.
code:
if button = 1
    exit when x >= 542 & x <= 600 & y >= 20 & y <= 40
    if y >= 180 & y <= 200 then
        for i : 234 .. 481 by 19
            if x >= i & x <= i + 18 then
                levelchoice := (i - 234) div 19
                exit
            end if
        end for
        levelchangechoice := levelchoice - 1
    elsif y >= 285 & y < 305 then
        for i : 248 .. 338 by 30
            if x >= i & x <= i + 22 then
                music := (i - 248) div 30 + 1
                exit
            end if
        end for


Don't worry we're almost done. This is the second last part.
code:
%Part of "if" statement that dictates the variable change of the difficulty of the game
elsif button = 1 & (x >= 298 & x <= 318) & (y >= 250 & y <= 270) then
    difficulty := 1
elsif button = 1 & (x >= 328 & x <= 348) & (y >= 250 & y <= 270) then
    difficulty := 2
elsif button = 1 & (x >= 358 & x <= 378) & (y >= 250 & y <= 270) then
    difficulty := 3

If you've been following you know the drill.
code:
elsif y >= 250 & y < 270 then
    for i : 298 .. 358 by 30
        if x >= i & x <= i + 20 then
                diffuculty := (i - 298) div 30 + 1
                exit
            end if
    end for

The combined work looks like this.
code:
if button = 1
    exit when x >= 542 & x <= 600 & y >= 20 & y <= 40
    if y >= 180 & y <= 200 then
        for i : 234 .. 481 by 19
            if x >= i & x <= i + 18 then
                levelchoice := (i - 234) div 19
                exit
            end if
        end for
        levelchangechoice := levelchoice - 1
    elsif y >= 285 & y < 305 then
        for i : 248 .. 338 by 30
            if x >= i & x <= i + 22 then
                music := (i - 248) div 30 + 1
                exit
            end if
        end for
    elsif y >= 250 & y < 270 then
        for i : 298 .. 358 by 30
            if x >= i & x <= i + 20 then
                diffuculty := (i - 298) div 30 + 1
                exit
            end if
        end for


And now for the last part.
code:
elsif button = 1 & (x >= 248 & x <= 308) & (y >= 215 & y <= 235) then
    junk := false
elsif button = 1 & (x >= 338 & x <= 378) & (y >= 215 & y <= 235) then
    junk := true
end if

It can be reduced to.
code:
elsif y >= 215 & y <= 235 then
        if x >= 248 & x <= 308 then
            junk := false
        elsif x >= 338 & x <= 378 then
            junk := true
        end if

And we are done. The final product looks like this.
code:
if button = 1
    exit when x >= 542 & x <= 600 & y >= 20 & y <= 40
    if y >= 180 & y <= 200 then
        for i : 234 .. 481 by 19
            if x >= i & x <= i + 18 then
                levelchoice := (i - 234) div 19
                exit
            end if
        end for
        levelchangechoice := levelchoice - 1
    elsif y >= 285 & y < 305 then
        for i : 248 .. 338 by 30
            if x >= i & x <= i + 22 then
                music := (i - 248) div 30 + 1
                exit
            end if
        end for
    elsif y >= 250 & y < 270 then
        for i : 298 .. 358 by 30
            if x >= i & x <= i + 20 then
                diffuculty := (i - 298) div 30 + 1
                exit
            end if
        end for
    elsif y >= 215 & y <= 235 then
        if x >= 248 & x <= 308 then
            junk := false
        elsif x >= 338 & x <= 378 then
            junk := true
        end if
    end if
end if

65 lines reduced to 30 and I bet I saved more than 250 characters. It should run but I have not tested it. However, if there is an error and you were paying attention you should be able to spot it.

Author:  OmegaSyrus [ Mon Nov 20, 2006 7:45 pm ]
Post subject: 

The reason for the excessive lines is because for 2 player, i simply doubled up and modified my program to run 2 seperate 1 player games, this required doubling up all the tracking variables. Hence you end up with 3x the amount of lines, my actual tetris was i think roughly 750 lines or something, excluding my 'options' menu. And yes, i know i do some not well thought out programming methods for certain things, more knowingly the menu, in my defense the teacher taught us nothing and all that i came up with was what i could think of. Also i've always feared 'for' loops for checking. But good programming eric. Anyway, i designed it for flexibility, for instance to increase the amount of possible square pictures, one must simply change i think 3 lines' numbers. But ya, the original tetris came to around 750, it only runs the 750 if you are in 1 player, the last 1600 lines are ignored since they are for 2 player. And vice versa. If you can make a tetris using all my pictures in less code, go for it. And thank you demonz for the comment.


: