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

Username:   Password: 
 RegisterRegister   
 Tetris help!
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
tyuo9980




PostPosted: Thu Mar 24, 2011 10:12 pm   Post subject: Tetris help!

Here is what I have so far.

How would I be able to draw the boxes that hit the bottom?
How would I be able to rotate the shapes?
How would I be able to rotate the shapes without protruding the walls and the shapes?

code:
var x, y : int
var x2, y2 : int
var sx, sy : int

var input : string
var delay1 : int
var timenow, timelast : int
var piecenow, piecelast : int
var score : int
var timer : int
var key : string (1)
var chars : array char of boolean
var difficultyl : string
var name : string
var lines : int
var combo : int
var piece : int
var counter : int
var piecedir : int := 1

var p1, p2, p3, p4, p5, p6, p7 : int := 0

var x4, x5, x6, x7, y26, y27, y28, y29 : int

x4 := 4
x5 := 5
x6 := 6
x7 := 7
y26 := 26
y27 := 27
y28 := 28
y29 := 29

var grid : array 1 .. 10, 1 .. 26 of boolean
var coordx : array 1 .. 10 of int
var coordy : array 1 .. 29 of int

var oldpiecex : array 1 .. 520 of int
var oldpiecey : array 1 .. 520 of int

for awefx : 1 .. 520
    oldpiecex (awefx) := 0
end for
for awefy : 1 .. 520
    oldpiecey (awefy) := 0
end for

var GO : boolean
var back2menu : boolean
var retry : boolean
var tilestop : boolean

var prevtext : array 1 .. 20 of string
var fnum : int

back2menu := false                                                          %disabled by default

fnum := 1

x := 1                                                                      %800x600 resolution
y := 1
x2 := 800
y2 := 600
prevtext (1) := ""                                                          %filler

for assignx : 1 .. 10
    coordx (assignx) := 300 + ((assignx - 1) * 20)
end for

for assigny : 1 .. 29
    coordy (assigny) := 30 + ((assigny - 1) * 20)
end for

View.Set ("graphics:800;600, nocursor, offscreenonly, nobuttonbar")

process musicintro
    Music.PlayFile ("intro.wav")
end musicintro

process mainmusic
    Music.PlayFileLoop ("tetris.mp3")
end mainmusic

procedure setmap
    Draw.Box (x, y, x2, y2, black)
    Draw.FillBox (x, y, x2, y2, black)
    colourback (black)
    colour (white)
end setmap

procedure intro
    fork musicintro

    for a : 16 .. 31
        locatexy (230, 300)
        color (a)
        put "This pro tetris game is by..."
        View.Update
        delay (50)
    end for

    for decreasing b : 31 .. 16
        locatexy (230, 300)
        color (b)
        put "This pro tetris game is by..."
        View.Update
        delay (50)
    end for

    for c : 16 .. 31
        locatexy (435, 300)
        color (c)
        put "Peter Li"
        View.Update
        delay (100)
    end for

    delay (1250)

end intro

procedure leaderboard
    loop
        cls
        put "[0] Return"
        put ""
        View.Update

        open : fnum, "scoresurvival.txt", read
        for x : 1 .. 20
            exit when eof (fnum)
            read : fnum, prevtext (x)
        end for
        close : fnum
        put prevtext (x)

        View.Update

        getch (key)
        if key = "0" then
            exit
        end if

    end loop
end leaderboard

procedure leaderboardc
    loop
        cls
        put "[0] Return"
        put ""
        View.Update

        open : fnum, "score.txt", read
        for x : 1 .. 20
            exit when eof (fnum)
            read : fnum, prevtext (x)
        end for
        close : fnum
        put prevtext (x)

        View.Update

        getch (key)
        if key = "0" then
            exit
        end if

    end loop
end leaderboardc

procedure savefile
    View.Set ("graphics:800;600, nobuttonbar, nocursor, nooffscreenonly")
    locatexy (375, 500)
    put "Enter name"
    locatexy (375, 475)
    get name : *
    open : fnum, "score.txt", put, mod, seek
    seek : fnum, *
    put : fnum, difficultyl, "      ", name : 18, "  ", round (score)
    close : fnum
    View.Set ("graphics:800;600, nobuttonbar, nocursor, offscreenonly")
end savefile

procedure savefilec
    View.Set ("graphics:800;600, nobuttonbar, nocursor, nooffscreenonly")
    locatexy (375, 500)
    put "Enter name"
    locatexy (375, 475)
    get name : *
    open : fnum, "scorecampaign.txt", put, mod, seek
    seek : fnum, *
    put : fnum, difficultyl, "      ", name : 18, "  ", round (score)
    close : fnum
    View.Set ("graphics:800;600, nobuttonbar, nocursor, offscreenonly")
end savefilec

procedure reset                                                             %variable reset

    cls
    setmap

    GO := false
    retry := false
    back2menu := false
    tilestop := true
    sx := 400
    sy := 300
    score := 0
    timer := 0
    lines := 0
    combo := 0
    counter := 0

    for gridfalsex : 1 .. 10
        for gridfalsey : 1 .. 26
            grid (gridfalsex, gridfalsey) := false
        end for
    end for

end reset

procedure resetpiecevar
    for assignx : 1 .. 10
        coordx (assignx) := 300 + ((assignx - 1) * 20)
    end for

    for assigny : 1 .. 28
        coordy (assigny) := 30 + ((assigny - 1) * 20)
    end for

    x4 := 4
    x5 := 5
    x6 := 6
    x7 := 7
    y27 := 27
    y28 := 28
end resetpiecevar

procedure difficulty                                                        %Higher delay = slower
    loop                                                                    %Lower delay = faster
        cls
        put "Enter difficulty level"
        put "[1] Easy"
        put "[2] Normal"
        put "[3] Hard"
        put "[4] 1337"
        put "[5] Custom"
        put ""
        put "[0] Back"
        View.Update
        getch (key)
        if key = "1" then
            delay1 := 1200
            difficultyl := "Easy"
            exit
        elsif key = "2" then
            delay1 := 1000
            difficultyl := "Normal"
            exit
        elsif key = "3" then
            delay1 := 800
            difficultyl := "Hard"
            exit
        elsif key = "4" then
            delay1 := 600
            difficultyl := "1337"
            exit
        elsif key = "5" then
            View.Set ("graphics:800;600, nobuttonbar, nocursor, nooffscreenonly")
            put "Enter delay - 15 = 1337, 25 = Hard, 40 = Normal, 70 = Easy"
            get input
            if strintok (input) then
                delay1 := strint (input)
                difficultyl := input
            else
                put "Invalid input"
            end if
            View.Set ("graphics:800;600, nobuttonbar, nocursor, offscreenonly")
            exit
        elsif key = "0" then
            back2menu := true
            exit
        end if
    end loop
end difficulty

procedure instructions
    locatexy (330, 300)
    put "Use Arrow Keys to move"
    View.Update
    delay (1500)
end instructions

procedure collide

    if coordy (y27) = 30 or coordy (y28) = 30 then                              %hits bottom
        tilestop := true
        counter += 1
        oldpiecex (counter) := coordx (x4)
        oldpiecex (counter + 1) := coordx (x5)
        oldpiecex (counter + 2) := coordx (x6)
        oldpiecex (counter + 3) := coordx (x7)
        oldpiecey (counter + 4) := coordy (y27)
        oldpiecey (counter + 1) := coordy (y28)
        resetpiecevar
    end if

    %if tilestop = true then
    %   lines += 1
    %end if
end collide

procedure drawgrid
    for vertical : 0 .. 10
        Draw.Line (300 + (20 * vertical), 30, 300 + (20 * vertical), 550, white)
    end for

    for horizontal : 0 .. 26
        Draw.Line (300, 30 + (20 * horizontal), 500, 30 + (20 * horizontal), white)
    end for
end drawgrid

procedure createpiece
    if tilestop = true then
        randint (piece, 1, 7)
        tilestop := false
        case piece of
            label 1 :
                p1 += 1
            label 2 :
                p2 += 1
            label 3 :
                p3 += 1
            label 4 :
                p4 += 1
            label 5 :
                p5 += 1
            label 6 :
                p6 += 1
            label 7 :
                p7 += 1
        end case
    end if

end createpiece

procedure drawsquare
    if piecedir >= 1 then
        Draw.FillBox (coordx (x4), coordy (y27), coordx (x4) + 20, coordy (y27) + 20, brightred)
        Draw.FillBox (coordx (x4), coordy (y28), coordx (x4) + 20, coordy (y28) + 20, brightred)
        Draw.FillBox (coordx (x5), coordy (y27), coordx (x5) + 20, coordy (y27) + 20, brightred)
        Draw.FillBox (coordx (x5), coordy (y28), coordx (x5) + 20, coordy (y28) + 20, brightred)
        Draw.Box (coordx (x4), coordy (y27), coordx (x4) + 20, coordy (y27) + 20, white)
        Draw.Box (coordx (x4), coordy (y28), coordx (x4) + 20, coordy (y28) + 20, white)
        Draw.Box (coordx (x5), coordy (y27), coordx (x5) + 20, coordy (y27) + 20, white)
        Draw.Box (coordx (x5), coordy (y28), coordx (x5) + 20, coordy (y28) + 20, white)
    end if
end drawsquare

procedure drawbar
    if piecedir = 1 or piecedir = 3 then
        if coordx (x4) = 480 then
            coordx (x4) -= 20
            coordx (x5) -= 20
            coordx (x6) -= 20
            coordx (x7) -= 20
        elsif coordx (x4) = 300 then
            coordx (x4) += 40
            coordx (x5) += 40
            coordx (x6) += 40
            coordx (x7) += 40
        end if
        Draw.FillBox (coordx (x4), coordy (y27), coordx (x4) + 20, coordy (y27) + 20, brightblue)
        Draw.FillBox (coordx (x5), coordy (y27), coordx (x5) + 20, coordy (y27) + 20, brightblue)
        Draw.FillBox (coordx (x6), coordy (y27), coordx (x6) + 20, coordy (y27) + 20, brightblue)
        Draw.FillBox (coordx (x7), coordy (y27), coordx (x7) + 20, coordy (y27) + 20, brightblue)
        Draw.Box (coordx (x4), coordy (y27), coordx (x4) + 20, coordy (y27) + 20, white)
        Draw.Box (coordx (x5), coordy (y27), coordx (x5) + 20, coordy (y27) + 20, white)
        Draw.Box (coordx (x6), coordy (y27), coordx (x6) + 20, coordy (y27) + 20, white)
        Draw.Box (coordx (x7), coordy (y27), coordx (x7) + 20, coordy (y27) + 20, white)
    elsif piecedir = 2 or piecedir = 4 then
        Draw.FillBox (coordx (x6), coordy (y29), coordx (x6) + 20, coordy (y29) + 20, brightblue)
        Draw.FillBox (coordx (x6), coordy (y28), coordx (x6) + 20, coordy (y28) + 20, brightblue)
        Draw.FillBox (coordx (x6), coordy (y27), coordx (x6) + 20, coordy (y27) + 20, brightblue)
        Draw.FillBox (coordx (x6), coordy (y26), coordx (x6) + 20, coordy (y26) + 20, brightblue)
        Draw.Box (coordx (x6), coordy (y29), coordx (x6) + 20, coordy (y29) + 20, white)
        Draw.Box (coordx (x6), coordy (y28), coordx (x6) + 20, coordy (y28) + 20, white)
        Draw.Box (coordx (x6), coordy (y27), coordx (x6) + 20, coordy (y27) + 20, white)
        Draw.Box (coordx (x6), coordy (y26), coordx (x6) + 20, coordy (y26) + 20, white)
    end if
end drawbar

procedure drawt
    if piecedir = 1 then
        Draw.FillBox (coordx (x4), coordy (y27), coordx (x4) + 20, coordy (y27) + 20, brightpurple)
        Draw.FillBox (coordx (x5), coordy (y27), coordx (x5) + 20, coordy (y27) + 20, brightpurple)
        Draw.FillBox (coordx (x6), coordy (y27), coordx (x6) + 20, coordy (y27) + 20, brightpurple)
        Draw.FillBox (coordx (x5), coordy (y28), coordx (x5) + 20, coordy (y28) + 20, brightpurple)
        Draw.Box (coordx (x4), coordy (y27), coordx (x4) + 20, coordy (y27) + 20, white)
        Draw.Box (coordx (x5), coordy (y27), coordx (x5) + 20, coordy (y27) + 20, white)
        Draw.Box (coordx (x6), coordy (y27), coordx (x6) + 20, coordy (y27) + 20, white)
        Draw.Box (coordx (x5), coordy (y28), coordx (x5) + 20, coordy (y28) + 20, white)
    elsif piecedir = 2 then

    elsif piecedir = 3 then

    elsif piecedir = 4 then

    end if
end drawt

procedure drawz
    if piecedir = 1 or piecedir = 3 then
        Draw.FillBox (coordx (x4), coordy (y28), coordx (x4) + 20, coordy (y28) + 20, yellow)
        Draw.FillBox (coordx (x5), coordy (y28), coordx (x5) + 20, coordy (y28) + 20, yellow)
        Draw.FillBox (coordx (x5), coordy (y27), coordx (x5) + 20, coordy (y27) + 20, yellow)
        Draw.FillBox (coordx (x6), coordy (y27), coordx (x6) + 20, coordy (y27) + 20, yellow)
        Draw.Box (coordx (x4), coordy (y28), coordx (x4) + 20, coordy (y28) + 20, white)
        Draw.Box (coordx (x5), coordy (y28), coordx (x5) + 20, coordy (y28) + 20, white)
        Draw.Box (coordx (x5), coordy (y27), coordx (x5) + 20, coordy (y27) + 20, white)
        Draw.Box (coordx (x6), coordy (y27), coordx (x6) + 20, coordy (y27) + 20, white)
    elsif piecedir = 2 or piecedir = 4 then

    end if
end drawz

procedure drawrz
    if piecedir = 1 or piecedir = 3 then
        Draw.FillBox (coordx (x4), coordy (y27), coordx (x4) + 20, coordy (y27) + 20, 42)
        Draw.FillBox (coordx (x5), coordy (y27), coordx (x5) + 20, coordy (y27) + 20, 42)
        Draw.FillBox (coordx (x5), coordy (y28), coordx (x5) + 20, coordy (y28) + 20, 42)
        Draw.FillBox (coordx (x6), coordy (y28), coordx (x6) + 20, coordy (y28) + 20, 42)
        Draw.Box (coordx (x4), coordy (y27), coordx (x4) + 20, coordy (y27) + 20, white)
        Draw.Box (coordx (x5), coordy (y27), coordx (x5) + 20, coordy (y27) + 20, white)
        Draw.Box (coordx (x5), coordy (y28), coordx (x5) + 20, coordy (y28) + 20, white)
        Draw.Box (coordx (x6), coordy (y28), coordx (x6) + 20, coordy (y28) + 20, white)
    elsif piecedir = 2 or piecedir = 4 then

    end if
end drawrz

procedure drawl
    if piecedir = 1 then
        Draw.FillBox (coordx (x4), coordy (y27), coordx (x4) + 20, coordy (y27) + 20, brightgreen)
        Draw.FillBox (coordx (x5), coordy (y27), coordx (x5) + 20, coordy (y27) + 20, brightgreen)
        Draw.FillBox (coordx (x6), coordy (y27), coordx (x6) + 20, coordy (y27) + 20, brightgreen)
        Draw.FillBox (coordx (x6), coordy (y28), coordx (x6) + 20, coordy (y28) + 20, brightgreen)
        Draw.Box (coordx (x4), coordy (y27), coordx (x4) + 20, coordy (y27) + 20, white)
        Draw.Box (coordx (x5), coordy (y27), coordx (x5) + 20, coordy (y27) + 20, white)
        Draw.Box (coordx (x6), coordy (y27), coordx (x6) + 20, coordy (y27) + 20, white)
        Draw.Box (coordx (x6), coordy (y28), coordx (x6) + 20, coordy (y28) + 20, white)
    elsif piecedir = 2 then

    elsif piecedir = 3 then

    elsif piecedir = 4 then

    end if
end drawl

procedure drawrl
    if piecedir = 1 then
        Draw.FillBox (coordx (x4), coordy (y28), coordx (x4) + 20, coordy (y28) + 20, gray)
        Draw.FillBox (coordx (x4), coordy (y27), coordx (x4) + 20, coordy (y27) + 20, gray)
        Draw.FillBox (coordx (x5), coordy (y27), coordx (x5) + 20, coordy (y27) + 20, gray)
        Draw.FillBox (coordx (x6), coordy (y27), coordx (x6) + 20, coordy (y27) + 20, gray)
        Draw.Box (coordx (x4), coordy (y28), coordx (x4) + 20, coordy (y28) + 20, white)
        Draw.Box (coordx (x4), coordy (y27), coordx (x4) + 20, coordy (y27) + 20, white)
        Draw.Box (coordx (x5), coordy (y27), coordx (x5) + 20, coordy (y27) + 20, white)
        Draw.Box (coordx (x6), coordy (y27), coordx (x6) + 20, coordy (y27) + 20, white)
    elsif piecedir = 2 then

    elsif piecedir = 3 then

    elsif piecedir = 4 then

    end if
end drawrl

procedure drawpiece
    piecenow := Time.Elapsed
    if piecenow - piecelast >= delay1 then
        y26 -= 1
        y27 -= 1
        y28 -= 1
        y29 -= 1
        piecelast := piecenow
    end if
    if piece = 1 then                                                                           %square
        drawsquare
    elsif piece = 2 then                                                                        %bar
        drawbar
    elsif piece = 3 then                                                                        %T shape
        drawt
    elsif piece = 4 then                                                                        %Z shape
        drawz
    elsif piece = 5 then                                                                        %reverse Z shape
        drawrz
    elsif piece = 6 then                                                                        %L shape
        drawl
    elsif piece = 7 then                                                                        %reverse L shape
        drawrl
    end if
end drawpiece

procedure drawoldpiece
    for x : 1 .. counter + 4
        for y : 1 .. counter + 1
            Draw.FillBox (oldpiecex (x), oldpiecey (y), oldpiecex (x) + 20, oldpiecey (y) + 20, gray)
            Draw.Box (oldpiecex (x), oldpiecey (y), oldpiecex (x) + 20, oldpiecey (y) + 20, white)
        end for
    end for
end drawoldpiece

procedure drawstats
    locatexy (0, 500)
    put "Square: ", p1
    put "Bar: ", p2
    put "T: ", p3
    put "Z: ", p4
    put "rZ: ", p5
    put "L: ", p6
    put "rL: ", p7
end drawstats

procedure shownext

end shownext

procedure showhold

end showhold

procedure survival
    fork mainmusic

    loop
        if back2menu = true then
            exit
        end if

        reset
        difficulty

        if back2menu = true then
            exit
        end if

        instructions

        timelast := Time.Elapsed
        piecelast := Time.Elapsed

        loop

            cls
            collide

            if GO = true then
                savefile
                loop
                    cls
                    setmap
                    Input.KeyDown (chars)
                    put "Do you want to retry? (Y/N)"
                    if chars ('y') or chars ('Y') then
                        retry := true
                        exit
                    elsif chars ('n') or chars ('N') then
                        quit
                    end if
                    locatexy (340, 400)
                    put "Your score: ", round (score)
                    locatexy (375, 300)
                    put "GAME OVER"

                    View.Update

                end loop
            end if

            if retry = true then
                exit
            end if

            drawstats
            createpiece
            drawgrid
            drawpiece
            drawoldpiece

            shownext
            showhold

            timenow := Time.Elapsed

            if timenow - timelast >= 1000 then
                timer += 1
                timelast := timenow
            end if

            locatexy (20, 585)                                              %scoreboard
            put "Press 0 to return to menu"
            locatexy (350, 585)
            put "Time: ", timer
            locatexy (650, 585)
            put "Score: ", round (score)

            Input.KeyDown (chars)
            if chars (KEY_UP_ARROW) then
                piecedir += 1
                if piecedir = 5 then
                    piecedir := 1
                end if
            elsif chars (KEY_LEFT_ARROW) then
                if coordx (x4) = 300 or coordx (x5) = 300 or coordx (x6) = 300 or coordx (x7) = 300 then                         %hits left side
                    x4 += 1
                    x5 += 1
                    x6 += 1
                    x7 += 1
                end if


                x4 -= 1
                x5 -= 1
                x6 -= 1
                x7 -= 1
            elsif chars (KEY_RIGHT_ARROW) then
                if coordx (x4) = 480 or coordx (x5) = 480 or coordx (x6) = 480 or coordx (x7) = 480 then                         %hits right side
                    x4 -= 1
                    x5 -= 1
                    x6 -= 1
                    x7 -= 1
                end if

                x4 += 1
                x5 += 1
                x6 += 1
                x7 += 1
            elsif chars (KEY_DOWN_ARROW) then
                if y27 = 1 or y28 = 1 then
                    y26 += 1
                    y27 += 1
                    y28 += 1
                    y29 += 1
                end if
                y26 -= 1
                y27 -= 1
                y28 -= 1
                y29 -= 1
            elsif chars (' ') then

            elsif chars ('0') then
                loop
                    cls
                    put "Are you sure you want to quit? (Y/N)"
                    Input.KeyDown (chars)
                    if chars ('y') or chars ('Y') then
                        back2menu := true
                        exit
                    elsif chars ('n') or chars ('N') then
                        exit
                    end if

                    View.Update

                end loop

                if back2menu = true then
                    exit
                end if

            end if

            View.Update
            delay (75)

        end loop
    end loop
end survival

procedure campaign

end campaign

procedure menu
    loop
        reset
        cls
        put "[1] Survival"
        put "[2] Campaign"
        put "[3] Survival Scoreboard"
        put "[4] Campaign Scoreboard"
        put ""
        put "[0] Exit"

        View.Update

        getch (key)
        if key = "1" then
            survival
        elsif key = "2" then
            campaign
        elsif key = "3" then
            cls
            setmap
            leaderboard
        elsif key = "4" then
            cls
            setmap
            leaderboardc
        elsif key = "0" then
            loop
                cls
                put "Are you sure you want to quit? (Y/N)"
                Input.KeyDown (chars)
                if chars ('y') or chars ('Y') then
                    quit
                elsif chars ('n') or chars ('N') then
                    exit
                end if

                View.Update

            end loop
        end if
    end loop
end menu

setmap
intro
menu
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Fri Mar 25, 2011 10:36 am   Post subject: RE:Tetris help!

There's probs a way easier way to turn them, but you could turn them according to what shape you have. Unfortunately, this way you would need to have a turning statement for any possible combination.
If you don't want them to protrude the walls, you compare the x value of the shape to the value of the wall. If any of them are greater than the wall, you could set them to the value of the wall.
Lastly, I would look into a flexible array for the shapes. You would use an element for each shape and continue its path until it hits the bottom.
Tony




PostPosted: Fri Mar 25, 2011 12:38 pm   Post subject: Re: RE:Tetris help!

Raknarg @ Fri Mar 25, 2011 10:36 am wrote:
you could turn them according to what shape you have. Unfortunately, this way you would need to have a turning statement for any possible combination.

Typing out repetitive code often suggests a problem with the design.

So -- http://en.wikipedia.org/wiki/Rotation_matrix which simplify quite considerably, since the only valid rotations are at 90 degree angles.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Raknarg




PostPosted: Fri Mar 25, 2011 5:46 pm   Post subject: RE:Tetris help!

Yeah, i know that. I was just throwing out random ideas. Obvs it's better if you find the short way around.
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  [ 4 Posts ]
Jump to:   


Style:  
Search: