Advertisement

Tetris Source-Code

Started by May 15, 2001 04:38 PM
5 comments, last by Silent Player 23 years, 8 months ago
Would anyone happen to know where I could find the source code for some Tetris games? I''m getting into game programming and Tetris is usually one of the first places to start when making games to get the basics down so I thought I should take a look at some source code from some other Tetris-like games. If you have any links or anything please reply :-) -Silent Player
-SP
Course most newbies decide to make tetris, rather than just get the code for it, but I guess it''s just not worth the time : )

G''luck,
-Alamar
Advertisement
you should not even think of making tetris. You didnt state which OS you want to develop for, which API you would use, or anything of the sort. You didnt even state the language you are using. You should get the basics down of the language, most likely (c/c++) then learn to code under windows, then learn direct x , then think about how you would make a tetris game. You should always develop your own code, not use others. think about how you would code the game.
bad!
I agree with the posts above me, source code is not the way to go when learning. Especially since you''ve just started. I have written Tetris before (www.lizardsoft.com/blockcraze) and while it is nothing difficult for a programmer past the alphabet blocks stage of learning, i saw a lot of things in it that are guarenteed to knock a newbie''s teeth out and then stomp on his/her head to boot. I know cuz the first time i tried to write Tetris (at that point i knew basic and some advanced Pascal stuff, but nothing regarding timing, fast input and other game related things) it became a mess real quick.

I highly recommened writing Tic Tac Toe, i did this in several languages and it is a great excercise both in terms of learning how to program properly (none of those giant if statement horrors) and quickly getting the hang of all the basic syntax of a language. Plus, and once again i speak from footsteps i have taken, it is INCREDIBLY satisfying to turn your original "has someone won?" 200 line code to three nested and neat loops 20 lines in length (approx). Hell, I''ll even admit that my very first "has someone won" code actually did every single condition as an if, for EACH player. In the second one i got it down to be a bit more efficient. In the third tic tac toe game a lightbulb suddenly dinged in my head (yes a lightbulb can ding, now hush ) and i figured out how to get it as efficeient as possible (afaik). I spent an hour on that algorithm shrinking it down to perfection.

If you copy all the algorithms from someone else''s code, you will NEVER build up the ability to write a good algorithm. You might understand the stuff u copy perfectly, but without using any thinking skills yourself, you will never be able to write your own algorithm (meaning your unemployable anywhere except Microsoft). Programming is about being able to solve problems using programming language elements. Learning only the programming language and ignoring actual computer science is a waste of your time.

Knowing how to use a tool doesn''t mean knowing how to do something useful with it.

(END RANT)

Good luck

Resist Windows XP''s Invasive Production Activation Technology!
BetaShare - Run Your Beta Right!
I disagree with the post above; if you can find source code for a Tetris-like game that someone has been benevolent to give away, it’d give you a jump-start on how to make a game. Instead of writing another Tetris clone you could do something else and reference the ripped code for implementation methodologies.

You could browse www.SourceForge.com

There's no sense in struggling with trivial problems that were solved decades ago. You can learn faster by reviewing other people’s mistakes (& successes) than making them yourself.

If you want to be analytical about it, download several Tetris games and compare their implementations & designs. Decide which part of which one is the best way.

Magmai Kai Holmlor
- The disgruntled & disillusioned



Edited by - Magmai Kai Holmlor on May 15, 2001 8:13:58 PM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
See I agree with the post above this one, but I don''t see why the post above this one disagrees with my post above that one. You have hit the nail on the head on why open source is great. But this guy is *learning* programming. He would not be capable of making a much better game from the source of an existing one. He would either be able to simply copy/paste the code and make a few minor changes, if even that, or he would become lost. If he did understand it, and grab the algorithms, he is missing out on valuable learning experience. In order to use existing code in a way that is beneficial to you, your programming skill must already be at a level where you would be capable of coding this stuff yourself (not counting any extra APIs you would have to learn or specificiations to go over, since those just require textbook style memorization if ur already familiar with the programming concepts they use). Else your just copying a mess you don''t understand, and hoping for pot luck.

Resist Windows XP''s Invasive Production Activation Technology!
BetaShare - Run Your Beta Right!
Advertisement
Game programming is a science and an art at the same time. The part that deals with science has to do with setting forth the rules that govern the goings on of your game. Much like organisms in nature, games all seem to derive characteristics from each other and games could probably be divided up into a neat tree of various genre of games that branch out into everything from tic-tac-toe to the most advanced 3D strategy games.

It is best to understand the rules that govern the game of Tetris if you would like to advance into making tile based scrolling games such as Super Mario Brothers. The concepts of dynamic objects --the falling piece-- colliding with static objects --the blocks already in place in the well-- is vital to creating a decent Tetris clone.

The well in Tetris is composed of a two dimensional array of 10 blocks wide, by 20 blocks high. Each of the seven varieties of pieces that can fall into the well are composed of exactly four blocks, hence the name Tetris. (Tetra = four) It is important to keep track of the ''state'' of the game. This is done simply by storing certain pieces of data. The array will provide the state of the well which the pieces fall into. You also need the ''state'' of the falling piece itself which is given by the position of the four blocks which make up the piece as it exists within the well as given by its rotation. (a sort of sub state)

When a piece falls into the well, it is super imposed into the two dimensional array. The rule is that a piece may continue to fall from the top of the array into the bottom as long as the place in the well that the piece is falling into is not already occupied by another block. How does one detect if a block is already occupying a place in the well? Imagine the game as it begins, with an empty well. The array representing the well will be filled with zeros indicating an empty well. When a falling piece lands on the bottom of the well, the form of the falling piece is ''burnt'' into the array that represents the well, simply by writing a non zero value. This value may be used by the graphics portion of the game to choose the style in which a block is drawn, either by color or graphical tile. As an example, let''s take a single ''stick'' block (the one EVERYONE waits for) falling vertically to the lower left hand corner... in such a case, the well would look like this. (using a 10 x 10 well due to size)

0000000000000000000000000000000000000000000000000000000000001000000000100000000010000000001000000000


As other pieces fall in, the current ''state'' of the well is used to determine where a piece may or may not go... if you were to get a T shaped piece and place it atop the ''stick'' you''d get.

0000000000000000000000000000002000000000220000000020000000001000000000100000000010000000001000000000


How you choose the handle rotating the pieces is limited only by your imagination. If you are familiar with simple 2D rotation, you will see that the rules of rotating points using triginometry isn''t such a bad way of applying rules to rotation. That can be done using a VERY simple sin and cos table using only 4 angles: 0, 90, 180 and 270.

I hope this helps anyone who is interested in doing a Tetris game. If you have any questions e-mail me at NitroSR@att.net I''ll be happy to answer any questions about this posting.

Daniel Piron

This topic is closed to new replies.

Advertisement