Jump to content

Tetris (Last update: Jan 12, 2009)


Achilles
 Share

Recommended Posts

TETRIS

The music for Tetris is not attached here to due size.

Click here to download it. (Try this if the previous doesn't work)

Once you have it downloaded just place the three theme songs inside the same directory as Tetris - Pic.au3

Options menu is not supposed to be functional yet.

Controls

  • Move piece left - Left Arrow
  • Move piece right - Right Arrow
  • Move piece down - Down Arrow
  • Rotate Left - A
  • Rotate Right - S
  • Drop - Space
  • New Game - F2
  • Pause - F3
Updates

January 12, 2009]

  • Now uses pictures instead of labels
  • The bug with pieces getting stuck should be gone (hopefully)
  • High scores
  • Sound is now functional (Music is attached in a seperate file)
  • All pieces are redrawn in more efficient way that saves time and processing
  • Hotkeys get disabled when window loses focus
  • Preview is now shown for where a piece will drop
  • Preview for the next piece is now shown
January 2, 2009
  • Added drop (Space bar), drop gives points depending on how far it drops
  • Added menu (basic game operations and a currently non functional sound part)
  • New Game function (F2)
  • Pause function (F3)
  • High Scores with decent GUI
  • Ability to restart game
  • Levels are slightly easier (higher levels were going too fast)
To Do
  • Customizable hotkeys - almost there
  • Customizable colors
Tetris.zipPrevious Downloads: 113 Edited by Ichigo
My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

Cool but there are a few bugs, I once made it crash by holding the "down arrow" for a few secs, and I don't think that green block was supposed to stop in mid-air......

Posted Image

Link to comment
Share on other sites

@Ichigo

After few hours of game Ive only one beug : the same as AdmiralAlkex :)

It would be great that you add :

-pause button

-IsPressed instead of HotkeySet because with hotkeyset I can't tape 2keys in one time for example down and turn.

So with IsPressed you can do if IsPressed down and turn then etc...

-Store high score in a file :o

Cheers, FireFox.

Edited by FireFox
Link to comment
Share on other sites

@Ichigo

Added pause button and IsPressed functions : (Include Is_Pressed_UDF)

Edit : Added help button

Edit2 : Added restart button when lose

Edit3 : Added highscores and sounds

[autoit]#Region sound

$snd = "http://d3monautoit.free.fr/sound/"

If Not FileExists(@TempDir & "\move.mp3") Then

InetGet($snd & "move.mp3", @TempDir & "\move.mp3", 1, 0)

InetGet($snd & "down.mp3", @TempDir & "\down.mp3", 1, 0)

InetGet($snd & "start.mp3", @TempDir & "\start.mp3", 1, 0)

InetGet($snd & "gameover.mp3", @TempDir & "\gameover.mp3", 1, 0)

EndIf

#EndRegion sound

#Region Include

#include <Grid.au3>

#include <File.au3>

#include <Array.au3>

#include <IsPressed_UDF.au3>

#include <StaticConstants.au3>

#include <GUIConstantsEx.au3>

#include <WindowsConstants.au3>

#EndRegion Include

#Region Global

Global Const $SINGLE = 40, $DOUBLE = 100, $TRIPLE = 300, $TETRIS = 1200

Global Const $PIECE_FLAT = 0, $PIECE_SQUARE = 1, $PIECE_L = 2, $PIECE_BKL = 3, $PIECE_STEP = 4, $PIECE_BKSTEP = 5, $PIECE_MID = 6

Global Const $COLORS[7] = [0xFF0000, 0xFFFF00, 0xFF8000, 0x0000FF, 0xFF00FF, 0x00FF00, 0x00FFFF]

Global Const $WIDTH = 10, $HEIGHT = 20

Global Const $SIZE = 20

Global Const $BK_COLOR = 0x000000

Global Const $GUI_L_W = 70

Global $level = 0

Global $score = 0

Global $time = 800

Global $lines = 0

Global $idle = True ; set to false when redrawing or moving

Global $currentPieceType ; 0-6

Global $currentPieceOrien ; 0 = 12 o'clock, 1 = 3 0'clock...

Global $currentPiece[4] ; An array of four coords

#EndRegion Global

#Region GUI

$gui = GUICreate('Tetris', $SIZE * ($WIDTH + 1.5) + $GUI_L_W, $SIZE + 20 * ($HEIGHT + 1), -1, -1, $WS_SYSMENU, $WS_EX_CONTEXTHELP)

GUIRegisterMsg($WM_SYSCOMMAND, "_help")

;~ GUICtrlCreateLabel('-- Preview --', 220, 200, 70, 20, $SS_CENTER)

;~ _Grid_Create(4, 5, 18, $BK_COLOR, 220, 220, $gui)

_Grid_Create($WIDTH, $HEIGHT, $SIZE, $BK_COLOR, $SIZE / 2, $SIZE / 2, $gui)

GUICtrlCreateLabel('-- Score --', $SIZE * ($WIDTH + 1), $SIZE / 2, $GUI_L_W, 20, $SS_CENTER)

$lblScore = GUICtrlCreateLabel('', $SIZE * ($WIDTH + 1), $SIZE / 2 + 25, $GUI_L_W, 21, BitOR($SS_RIGHT, $SS_SUNKEN))

GUICtrlSetData(-1, $score)

GUICtrlCreateLabel('-- Level --', $SIZE * ($WIDTH + 1), $SIZE / 2 + 60, $GUI_L_W, 20, $SS_CENTER)

$lblLevel = GUICtrlCreateLabel('', $SIZE * ($WIDTH + 1), $SIZE / 2 + 85, $GUI_L_W, 21, BitOR($SS_RIGHT, $SS_SUNKEN))

GUICtrlSetData(-1, $level)

GUICtrlCreateLabel('-- Lines --', $SIZE * ($WIDTH + 1), $SIZE / 2 + 120, $GUI_L_W, 20, $SS_CENTER)

$lblLines = GUICtrlCreateLabel('', $SIZE * ($WIDTH + 1), $SIZE / 2 + 145, $GUI_L_W, 21, BitOR($SS_RIGHT, $SS_SUNKEN))

GUICtrlSetData(-1, $level)

GUISetFont(11)

GUISetState(@SW_SHOW, $gui)

#EndRegion GUI

#Region Start

SoundPlay(@TempDir & "\start.mp3")

MsgBox(0, "Tetris", "Welcome to Tetris !")

_GeneratePiece()

AdlibEnable('_Down', $time)

_EnableHotkeys()

#EndRegion Start

While 1

Sleep(75)

If _IsAndKeyPressed("25|27") Then

_Left()

_Right()

ElseIf _IsAndKeyPressed("25|28") Then

_Left()

_Down()

ElseIf _IsAndKeyPressed("25|41") Then

_Left()

_RotateLeft()

ElseIf _IsAndKeyPressed("25|53") Then

_Left()

_RotateRight()

EndIf

If _IsAndKeyPressed("27|28") Then

_Right()

_Down()

ElseIf _IsAndKeyPressed("27|41") Then

_Right()

_RotateLeft()

ElseIf _IsAndKeyPressed("27|53") Then

_Right()

_RotateRight()

ElseIf _IsAndKeyPressed("41|53") Then

_RotateLeft()

_RotateRight()

EndIf

If _IsPressed("25") Then

_Left()

ElseIf _IsPressed("27") Then

_Right()

ElseIf _IsPressed("28") Then

_Down()

ElseIf _IsPressed("41") Then

_RotateLeft()

ElseIf _IsPressed("53") Then

_RotateRight()

ElseIf _IsPressed("26") Then

_Bottom()

EndIf

WEnd

#Region *** GAME ***

Func _GeneratePiece()

$currentPieceType = Random(0, 6, 1)

$currentPieceOrien = 0

$posCurrentPiece = $currentPiece

Switch $currentPieceType

Case $PIECE_FLAT

For $i = 0 To 3

Dim $temp[2] = [5, $i]

$posCurrentPiece[$i] = $temp

Next

Case $PIECE_SQUARE

For $i = 0 To 3

If $i < 2 Then

Dim $temp[2] = [4 + $i, 0]

Else

Dim $temp[2] = [4 + ($i - 2), 1]

EndIf

$posCurrentPiece[$i] = $temp

Next

Case $PIECE_L

For $i = 0 To 2

Dim $temp[2] = [4, $i] ; 1 is the rotation point

$posCurrentPiece[$i] = $temp

Next

Dim $temp[2] = [5, 2]

$posCurrentPiece[3] = $temp

Case $PIECE_BKL ; 1 is the rotation point

For $i = 0 To 2

Dim $temp[2] = [5, $i]

$posCurrentPiece[$i] = $temp

Next

Dim $temp[2] = [4, 2]

$posCurrentPiece[3] = $temp

Case $PIECE_STEP

Dim $temp[2] = [5, 0]

$posCurrentPiece[0] = $temp

Dim $temp[2] = [4, 1]

$posCurrentPiece[1] = $temp

Dim $temp[2] = [5, 1]

$posCurrentPiece[2] = $temp ; 2 is the rotation point

Dim $temp[2] = [4, 2]

$posCurrentPiece[3] = $temp

Case $PIECE_BKSTEP

Dim $temp[2] = [4, 0]

$posCurrentPiece[0] = $temp

Dim $temp[2] = [4, 1]

$posCurrentPiece[1] = $temp ; 2 is the rotation point

Dim $temp[2] = [5, 1]

$posCurrentPiece[2] = $temp

Dim $temp[2] = [5, 2]

$posCurrentPiece[3] = $temp

Case $PIECE_MID

For $i = 0 To 2

Dim $temp[2] = [4 + $i, 0] ; 1 is the rotation point

$posCurrentPiece[$i] = $temp

Next

Dim $temp[2] = [5, 1]

$posCurrentPiece[3] = $temp

EndSwitch

If Not _AreaIsClear($posCurrentPiece) Then

_GameOver()

Else

$currentPiece = $posCurrentPiece

_DrawPiece()

EndIf

EndFunc ;==>_GeneratePiece

Func _Land()

_CheckGrid()

_GeneratePiece()

EndFunc ;==>_Land

Func _CheckGrid()

Local $tempErase[4] = [-1, -1, -1, -1]

$rowDrop = 0

For $i = $HEIGHT - 1 To 0 Step -1

$possible = True

For $j = 0 To $WIDTH - 1

;~ Msgbox(0, '', _Grid_GetColor($j, $i))

If _Grid_GetColor($j, $i) = 0 Then

$possible = False

ExitLoop

EndIf

Next

If $possible Then

$tempErase[$rowDrop] = $i

$rowDrop += 1

EndIf

Next

If $tempErase[0] = -1 Then Return

AdlibDisable()

_DisableHotkeys()

For $i = 0 To $rowDrop - 1

_Grid_SetRowColor($tempErase[$i], 0xFFFFFF)

Next

Sleep(500)

$tempGrid = $colorGrid ; $colorGrid is taken from Grid.au3

$passed = 0 ; How many rows to bring down the everything (varies as the loop progresses)

$row = $tempErase[0]

$consec = 0 ; Number of consecutive

Do

While _ArraySearch($tempErase, $row - $passed) <> -1

$passed += 1

$consec += 1

WEnd

If $consec > 0 Then

Switch $consec

Case 1

$score += ($level * $SINGLE) + $SINGLE

$lines += 1

Case 2

$score += ($level * $DOUBLE) + $DOUBLE

$lines += 2

Case 3

$score += ($level * $TRIPLE) + $TRIPLE

$lines += 3

Case 4

$score += ($level * $TETRIS) + $TETRIS

$lines += 4

EndSwitch

GUICtrlSetData($lblScore, $score)

GUICtrlSetData($lblLines, $lines)

If $lines >= (($level + 1) * 10) Then

$level += 1

GUICtrlSetData($lblLevel, $level)

$time *= .8

EndIf

$consec = 0

EndIf

;~ ConsoleWrite('> $row = ' & $row & @TAB & '$passed = ' & $passed & @CRLF)

For $j = 0 To $WIDTH - 1

If $row - $passed < 0 Then

$tempGrid[$row][$j] = $BK_COLOR

Else

$tempGrid[$row][$j] = $tempGrid[$row - $passed][$j]

EndIf

Next

$row -= 1

Until $row - $passed < -1

; Redraw the grid

For $i = 0 To $HEIGHT - 1

For $j = 0 To $WIDTH - 1

_Grid_SetColor($tempGrid[$i][$j], $j, $i)

Next

Next

AdlibEnable('_Down', $time)

_EnableHotkeys()

;~ Msgbox(0, '', 'Any good?')

EndFunc ;==>_CheckGrid

#Region Modified

Func _DisableHotkeys()

;None

HotKeySet("{PAUSE}", "_Pause")

HotKeySet("a", "_None")

HotKeySet("s", "_None")

EndFunc ;==>_DisableHotkeys

Func _EnableHotkeys()

;None

HotKeySet("{PAUSE}", "_Pause")

HotKeySet("a", "_none")

HotKeySet("s", "_none")

EndFunc ;==>_EnableHotkeys

Func _None()

;Function for disable sound

; when enter a or s

EndFunc ;==>_None

Func _Pause()

TrayTip("Tetris", "Game paused !", 0, 1)

HotKeySet("{PAUSE}", "_Unpause")

Global $idle = False

While 1

Sleep(100)

If $idle = True Then ExitLoop

WEnd

EndFunc ;==>_Pause

Func _Unpause()

TrayTip('Tetris', 'Game restarted !', 0, 1)

HotKeySet("{PAUSE}", "_Pause")

Global $idle = True

EndFunc ;==>_Unpause

Func _Restart()

_Grid_Destroy()

_Grid_Create($WIDTH, $HEIGHT, $SIZE, $BK_COLOR, $SIZE / 2, $SIZE / 2, $gui)

GUICtrlSetData($lblLevel, 0)

GUICtrlSetData($lblScore, 0)

GUICtrlSetData($lblLines, 0)

AdlibEnable('_Down', $time)

EndFunc ;==>_Restart

Func _help($hWnd, $Msg, $wParam, $lParam)

If $wParam = 0x0000F180 Then ;HELP BUTTON

MsgBox(64, "Tetris help/About...", "Author(s) :

Edited by FireFox
Link to comment
Share on other sites

@Firefox

I get the following error as soon as the first piece hits the bottom:

" Line 220==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

$tempErase[$rowDrop] = $i

^ ERROR"

Link to comment
Share on other sites

Cool but there are a few bugs, I once made it crash by holding the "down arrow" for a few secs, and I don't think that green block was supposed to stop in mid-air......
I've had that happen a few times. I'll try adding a bit more checks for making sure it's idle when you press down. It use to be way worse, but I added an $idle variable that stops it from redrawing in the middle of it's drawing, but I guess it isn't covering everything yet.

Wow nicely done :o

I like it :)

AlmarM

Thanks!

Thanks for all your input. Most of those suggestions were what I was thinking of doing. I'll probably use some of what you added if you don't mind. I personally don't like the sound effects much, but I'll try to find something that works to my liking more. I'll probably add a GUI instead of using a message box at the end though...

I'm not sure if the _IsPressed udf is an improvement. It seems to make the pieces move/rotate too fast. I know you said that you could do both at once, but when you try it seems kind of buggy, like it only works sometime.

@fmen: Does that happen with my version?

The error you described happens when an item is trying to be drawn outside the grid (causes an array error, as I have experienced many times in testing). I thought I got all possibilities of that happening blocked...

@everyone: I'll be adding some of firefox's updates and some of my own soon...

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

Thanks... Those other ones you listed don't have too much to more than mine. As soon as I get back to my desktop I'll be making some decent graphics and adding a few other things. I'm getting sick of programming on my laptop without a real mouse...
My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

Link to comment
Share on other sites

@Ichigo

Menu for sound but no sound... Edit : I saw that you say its normal...

First time I play when I click on highscore there is array variable error (you should write in highscore file like me with 0 score)

Do you preffer to get more score only when you erase line ? (in my version +20 when you put a piece)

Cheers, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Pause allows you to still move pieces. And I believe _IsPressed is better... :o

But I like the concept. Thank you for sharing it.

Cheers,

Brett

Ok, I'll try changing it to _IsPressed with a slightly longer sleep time. Lol, I forgot to fix pause all the way.. I'll change that too.

@Ichigo

Menu for sound but no sound... Edit : I saw that you say its normal...

First time I play when I click on highscore there is array variable error (you should write in highscore file like me with 0 score)

Do you preffer to get more score only when you erase line ? (in my version +20 when you put a piece)

Cheers, FireFox.

Yeah, I was just getting sound ready to add. I'll check the high score error.

Ichigo

Cool! :) 4 stars! :D

Thanks!
My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

@Ichigo

Can you add my function "bottom" when you press up arrow then piece go to bottom :)

Cheers, FireFox.

I didn't realize you had added it... However, I did add it... Push space bar. It seems to unlogical to have a piece drop when you press up so I think I'm going to leave it with space bar..

I'll be adding customizable hotkeys soon so you'll be able to pick whatever keys you want.

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...