Jump to content


Photo
* * * * * 1 votes

Tetris example


  • Please log in to reply
6 replies to this topic

#1 Davve

Davve

    GMC Member

  • GMC Member
  • 4091 posts
  • Version:GM:Studio

Posted 24 October 2010 - 03:35 PM

  • Title: Tetris
  • Description: A tutorial/example on a Tetris game in GML
  • GM Version: GM8
  • Registered: No
  • File Type: .gmk
  • File Size: 12.6 kb
  • File Link: Download

I noticed how the other Tetris examples were either dead or used objects for each Tetris piece and block. Here is another one which uses nothing but GML, executed in one single object. It uses an array of strings which holds the seven different pieces and their rotations, and another array which holds the blocks on the play field, rather than having sprites for every single piece and using objects filled with position_ and collision_ functions. The codes are also filled with comments so you know what's going on. smile.gif

Feel free to edit, modify, distribute or whatever.
 

2dh88pl.png


- Davve


Edited by Davve, 18 October 2015 - 02:14 PM.

  • 2

#2 xot

xot

    GMC Dismember

  • GMC Elder
  • 4785 posts
  • Version:GM:Studio

Posted 24 October 2010 - 05:13 PM

Pretty nice look at an alternative game loop. This is the sort of approach you might take with other languages. Piece distribution and rotation are different than standard Tetris, but those are minor things. It would have been nice to see wall kicks implemented in some fashion though. Still, informative and chock full of comments, which is always good.
  • 0
GMLscripts.com, rise from your grave!

If any of my posts contain broken images or links, I can probably supply them for you. PM with a link to the post.

#3 slayer 64

slayer 64

    Slayer of gingers

  • GMC Member
  • 4301 posts
  • Version:GM:Studio

Posted 24 October 2010 - 06:00 PM

interesting way to design a game.
why did you use strings to hold the game pieces? why not use 2d arrays? i don't think strings come into play when one does collision checking.

anyways. this is still impressive coding.
  • 0

5y5rs3d.pngfg0UQNL.png


#4 Davve

Davve

    GMC Member

  • GMC Member
  • 4091 posts
  • Version:GM:Studio

Posted 24 October 2010 - 06:45 PM

Pretty nice look at an alternative game loop. This is the sort of approach you might take with other languages. Piece distribution and rotation are different than standard Tetris, but those are minor things. It would have been nice to see wall kicks implemented in some fashion though. Still, informative and chock full of comments, which is always good.

I would probably cover every single Tetris mechanic if this was a game, and not a simple example made for learning purposes. But thanks for the links. :D

interesting way to design a game.
why did you use strings to hold the game pieces? why not use 2d arrays? i don't think strings come into play when one does collision checking.


game_piece[0,0]=0
game_piece[0,1]=1
game_piece[0,2]=0

game_piece[1,0]=1
game_piece[1,1]=1
game_piece[1,2]=1

game_piece[2,0]=0
game_piece[2,1]=0
game_piece[2,2]=0

game_piece[0,0]="010-111-000"
See the difference? :P Or was there another method you were thinking of?

anyways. this is still impressive coding.

Thanks, this means alot when coming from you :D
  • 0

#5 thatshelby

thatshelby

    GMC Member

  • GMC Member
  • 3823 posts
  • Version:GM8

Posted 24 October 2010 - 11:22 PM

This is a nice advanced tutorial! One error, I got though, while trying to rotate odd pieces (IE Z and T) as they approached the ground:

___________________________________________
ERROR in
action number 1
of  Step Event
for object obj_game:

Error in code at line 60:
           	if (game_field[game_current_piece_y + yy, game_current_piece_x + xx] > -1)  	//Check if there already is a block in the new cell.
               	^
at position 18: Unknown variable game_field or array index out of bounds

It can be ignored, but then the sprite goes into the ground.


  • 0

#6 Davve

Davve

    GMC Member

  • GMC Member
  • 4091 posts
  • Version:GM:Studio

Posted 25 October 2010 - 08:28 AM

One error, I got though, while trying to rotate odd pieces (IE Z and T) as they approached the ground:

Thanks, I probably forgot to check if the rotated piece is below the floor, I'll check into that in a bit. :)

Edit: Yup, fixed it and re-uploaded, thanks again. :D

Edited by Davve, 25 October 2010 - 09:25 AM.

  • 0

#7 slayer 64

slayer 64

    Slayer of gingers

  • GMC Member
  • 4301 posts
  • Version:GM:Studio

Posted 26 October 2010 - 01:06 AM

See the difference? :P Or was there another method you were thinking of?


yes, i was messing with the code a little and i see your point. it's a pain to make pieces using ASCII art or entering values uing code.

i was thinking you could define pieces and their rotations. there wouldn't be 6 pieces though, there would be 24 because of all the rotation definitions you would need.
to define a block using strings you did this
"1100-1100-0000-0000"
to define it using arrays you would have to do something like this:
a[0]=1 a[1]=1 a[2]=0 a[3]=0
a[4]=1 a[5]=1 a[6]=0 a[7]=0
a[8]=0 a[9]=0 a[10]=0 a[11]=0
a[12]=0 a[13]=0 a[14]=0 a[15]=0
using some math equations on "current_piece" and "current_piece_rotation" you can locate a piece in the "a" array to do look up piece definitions and do collision checking. 2d dimensional arrays are not really needed ever for anything, they are just easier to "visualize"

anyways, it's not too important how you implemented tetris. there's a million ways. the game doesn't lag, it's fine =)
  • 0

5y5rs3d.pngfg0UQNL.png