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

Username:   Password: 
 RegisterRegister   
 Tetris
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
richcash




PostPosted: Mon Jul 10, 2006 10:53 pm   Post subject: Tetris

This is a basic tetris game.
It's more difficult to make than it seems.
The code is not 100% neat and proceduralized, but I tried to make it readable.

Code :
This code has been updated and new version can be found lower on the page.

I guess I should comment more! The shape array contains data for the 7 possible starting squares that can appear at the top of the screeen (obviously only four at a time to make a piece). The piece array contains data for the 7 possible pieces that can fall (and later uses the shape array to construct the tetris pieces).
Further explanation can be given if requested.
Sponsor
Sponsor
Sponsor
sponsor
richcash




PostPosted: Mon Jul 10, 2006 11:11 pm   Post subject: (No subject)

I almost forgot!
spacebar to rotate (clockwise)
arrow keys to move (left and right)
NikG




PostPosted: Tue Jul 11, 2006 12:33 am   Post subject: (No subject)

Very impressive!

Some suggestions:
-Different colors for the different types of blocks
-A block drop button (or a button to make the block drop faster)
-Perhaps a two-player mode?

I don't have the time to look at your code right now, but I'll comment further when I do.

+25 bits
richcash




PostPosted: Tue Jul 11, 2006 9:10 pm   Post subject: (No subject)

Thanks for the feedback!
I'll implement some of your suggestions soon once I finalize it (also adding a timer and levels, some day, lol)
Any comments or suggestions are appreciated!
Mr. T




PostPosted: Wed Jul 12, 2006 10:12 pm   Post subject: Alex's Opinion

It shouldn't be too difficult to implement levels because all this entails is an increase in the speed of the falling blocks.
MysticVegeta




PostPosted: Thu Jul 13, 2006 7:14 pm   Post subject: (No subject)

Nice game, I liked playing it. A couple of things though, perhaps making the motion smoother might help. Definitely a button to make them go down faster. (Hopefully Down arrow) so we dont have to wait for it to come down... else, its pretty good, I like it.
richcash




PostPosted: Tue Jul 25, 2006 8:44 pm   Post subject: (No subject)

I finally got around to updating the code, I have been busy doing other advanced topics of programming which I am still not finished.
This version is a LOT more fun to play. I implented most things that were advised.

code:
setscreen ("graphics:300;600,position:center;center,nobuttonbar,offscreenonly,title:..." + repeat (" ", 19) + "TETRIS" + repeat (" ", 20))

var piece : array 1 .. 7, 1 .. 4 of int := init (1, 2, 3, 5, 1, 2, 4, 7, 1, 2, 3, 4, 1, 2, 4, 6, 1, 2, 4, 5, 1, 2, 3, 6, 1, 3, 4, 5)
var shape : array 1 .. 7, 1 .. 3 of int := init (0, 0, 0, 90, 90, 15, 180, 180, 15, 270, 270, 15, 90, 180, 30, 270, 180, 30, 90, 90, 30)
var box, col : array - 3 .. 12, -3 .. 20 of int
var blockx, blocky, newblockx, newblocky, nextblockx, nextblocky : array 1 .. 4 of int
var chars : array char of boolean
var x, y, pos, p, np, inc, score := 0
var gameOver := false

colourback (7)
colour (0)
for m : 0 .. 9
    for n : 0 .. 17
        box (m, n) := 0
    end for
end for
for m : -3 .. 12
    for n : 1 .. 3
        box (m, 0 - n) := -1
        box (m, 17 + n) := -1
    end for
end for
for m : 1 .. 3
    for n : -3 .. 20
        box (0 - m, n) := -1
        box (9 + m, n) := -1
    end for
end for
randint (p, 1, 7)
randint (np, 1, 7)
x := 120
y := 510

fcn rsin (n : int) : int
    result round (sind (n))
end rsin
fcn rcos (n : int) : int
    result round (cosd (n))
end rcos

proc assign
    for m : 0 .. 9
        for n : 0 .. 17
            if box (m, n) > 0 then
                box (m, n) := 0
            end if
        end for
    end for
    for n : 1 .. 4
        blockx (n) := (rsin (pos + shape (piece (p, n), 1)) + rsin (pos + shape (piece (p, n), 2))) * shape (piece (p, n), 3) + x
        blocky (n) := (rcos (pos + shape (piece (p, n), 1)) + rcos (pos + shape (piece (p, n), 2))) * shape (piece (p, n), 3) + y
        if box (blockx (n) div 30, blocky (n) div 30) = -1 then
            gameOver := true
            exit
        else
            box (blockx (n) div 30, blocky (n) div 30) := 1
        end if
    end for
    for m : 0 .. 9
        for n : 0 .. 17
            if box (m, n) > 0 then
                col (m, n) := p
            end if
        end for
    end for
end assign

proc output
    cls
    for m : 0 .. 9
        for n : 0 .. 17
            if box (m, n) not= 0 then
                drawfillbox (m * 30, n * 30, m * 30 + 30, n * 30 + 30, col (m, n))
                drawbox (m * 30, n * 30, m * 30 + 30, n * 30 + 30, 0)
            end if
        end for
    end for
    drawline (0, 540, 300, 540, 0)
    put "Score : ", score
    locate (1, 14)
    put "Next"
    locate (2, 14)
    put "Piece"
    drawline (170, 540, 170, 600, 0)
    for n : 1 .. 4
        nextblockx (n) := (rsin (shape (piece (np, n), 1)) + rsin (shape (piece (np, n), 2))) * shape (piece (np, n), 3) + 200
        nextblocky (n) := (rcos (shape (piece (np, n), 1)) + rcos (shape (piece (np, n), 2))) * shape (piece (np, n), 3) + 570
        drawfillbox (nextblockx (n), nextblocky (n), nextblockx (n) + 30, nextblocky (n) + 30, np)
        drawbox (nextblockx (n), nextblocky (n), nextblockx (n) + 30, nextblocky (n) + 30, 0)
    end for
    View.Update
    delay (100)
end output

fcn moveOk (dirx, diry : int) : boolean
    var numOk := 0
    for n : 1 .. 4
        if box (blockx (n) div 30 + dirx, blocky (n) div 30 + diry) not= -1 then
            numOk += 1
        end if
    end for
    if numOk = 4 then
        result true
    else
        result false
    end if
end moveOk

fcn rotateOk : boolean
    var numOk := 0
    pos += 90
    for n : 1 .. 4
        newblockx (n) := (rsin (pos + shape (piece (p, n), 1)) + rsin (pos + shape (piece (p, n), 2))) * shape (piece (p, n), 3) + x
        newblocky (n) := (rcos (pos + shape (piece (p, n), 1)) + rcos (pos + shape (piece (p, n), 2))) * shape (piece (p, n), 3) + y
    end for
    pos -= 90
    for n : 1 .. 4
        if box (newblockx (n) div 30, newblocky (n) div 30) not= -1 then
            numOk += 1
        end if
    end for
    if numOk = 4 then
        result true
    else
        result false
    end if
end rotateOk

proc movement
    Input.KeyDown (chars)
    if chars (' ') and p not= 1 and rotateOk then
        pos += 90
        assign
    end if
    inc += 1
    if inc mod 5 = 1 and moveOk (0, -1) then
        y -= 30
        assign
    elsif inc mod 5 = 1 then
        for n : 1 .. 4
            box (blockx (n) div 30, blocky (n) div 30) := -1
            col (blockx (n) div 30, blocky (n) div 30) := p
        end for
        p := np
        randint (np, 1, 7)
        for n : 1 .. 4
            x := 120
            y := 510
            pos := 0
        end for
        assign
    end if
    if chars (chr (205)) and moveOk (1, 0) then
        x += 30
        assign
    elsif chars (chr (203)) and moveOk (-1, 0) then
        x -= 30
        assign
    end if
    if chars (chr (208)) and moveOk (0, -1) then
        y -= 30
        assign
    end if
end movement

proc comprow
    var linesum : array 0 .. 17 of int
    for N : 0 .. 17
        loop
            for n : 0 .. 17
                linesum (n) := 0
            end for
            for m : 0 .. 9
                for n : 0 .. 17
                    linesum (n) += box (m, n)
                end for
            end for
            if linesum (N) = -10 then
                score += 10
                for m : 0 .. 9
                    for n : N .. 16
                        box (m, n) := box (m, n + 1)
                    end for
                end for
                for n : 0 .. 9
                    box (n, 17) := 0
                end for
            else
                exit
            end if
        end loop
    end for
end comprow

loop
    assign
    output
    exit when gameOver
    movement
    comprow
end loop
cls
drawline (0, 540, 300, 540, 0)
put "Score : ", score
locate (1, 14)
put "Next"
locate (2, 14)
put "Piece"
drawline (170, 540, 170, 600, 0)
View.Update
delay (100)
Font.Draw ("Game Over", 25, 300, Font.New ("sanserif:36"), 0)


I didn't want to do multiple levels because then it gives the game a glitch that every Tetris game ever made has (although some claim it's not a glitch). Eventually, in any game of tetris, the pieces fall so fast that you can not move it over to the corner. Holding the arrow key left will only allow it to go 2 or 3 spaces left because it's moving so fast downward. This means eventually on a really fast level you can not make new lines becuase you can't fill in boxes on the side. The game becomes center-concentrated.

And, basically, that's Tetris, the only non-downloadable version on compsci.ca!
pj_ladd12




PostPosted: Wed Aug 02, 2006 11:55 am   Post subject: (No subject)

Awesome job man, love the graphics
Sponsor
Sponsor
Sponsor
sponsor
xtremehacker




PostPosted: Thu Sep 07, 2006 6:05 pm   Post subject: (No subject)

This game is awesome i really like it man good work.
Foundation




PostPosted: Mon Sep 18, 2006 8:16 pm   Post subject: (No subject)

Wow, that's great. I understand about the glitch you are talking about. That's a funny glitch. I think that all level games have glitches like that. ex. too many enemies too fast, not enough bullets shooting out even at the fastest rate and continually. Like Tank.
War_Caymore




PostPosted: Fri Sep 22, 2006 11:35 am   Post subject: (No subject)

Omigod it's tetris! i loved playing it. i've read the entire code and realized some of the mistakes i've been making in mine (not that i'm making a tetris game, jsut some other things taht jsut so happen to use parts of your code). Thanx a bunch for making a kewl game and i would give you buts but i got none to give out.
jr.ranger.33




PostPosted: Tue Oct 17, 2006 9:50 am   Post subject: Color Probs

Great game, I play during computer class all the time. But one problem i have with it is the black shapes. I can't tell if they are shapes or blank spaces. Maybe you can change the color of the black shape.
Silent Avenger




PostPosted: Tue Oct 17, 2006 6:05 pm   Post subject: (No subject)

Very nice game. I played it today in compsci when I finished my project early. Smile
Zacebdal




PostPosted: Tue Oct 17, 2006 9:20 pm   Post subject: Nice Work

Congrats, you fooled me into thinking it was the real game. yeh pretty good stuff but, as ive noticed other people aree with me, the black shapes can be confusing at times. thats all the 'contructive' criticism i can offer at the time.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: