🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

I want to learn how to code Tetris :).

Started by
5 comments, last by Anri 5 years, 7 months ago

Hi, i would like to learn how to code Tetris, or, well, at least something that looks like Tetris.

I would like to start my project in C++ Builder ( Embarcadero ). 

As a beginner, i did what i could. I made the windows with everything that passed through my mind: Buttons, images, sound, collect data from player like name, etc etc.

I did my researches and i studied Tetris algorithm but i see no connection on how to apply it in C++ Builder.

Where should i start with the graphics? What should blocks be made of? What should the game "stage" be made of? Some people told me to use a StringGrid and to color the cells, other told me to use a matrix, but i don't really know where and how to start.

I honestly want to create my own classes and work with them. But i don't even know how to create a class in C++ Builder. I know what a class is, i use them in C++ with the console, but i don't understand the concept of C++ Builder and how it works with classes.

There is so much auto generated code and it is so different from what i'm used to ( for 4 years i have only programmed in console, i'm a student, by the way ).

For example, are buttons and labels and etc classes? If yes, where i can see their code to get inspired? How can i create my own class? I'm thinking about making a class for each piece, or a class for all of them, but i think that is better to make a class for each individual piece because they are different, right? 

But first i would really like find out how does classes work in C++ Builder and how to create my own. 

Thank you for your time and i wish you all good !

P.S: Sorry for my bad English.

Hi ! Wish you a good day !

Advertisement
1 hour ago, g0dafk said:

There is so much auto generated code and it is so different from what i'm used to ( for 4 years i have only programmed in console, i'm a student, by the way ).

Sounds like auto generated code gets in your way and is just distracting. I don't know C++ Builder, but likely the code comes from windows, buttons, forms etc. that you edit with a extra GUI tool.

One option here is to make your own UI, eventually using the same graphics system that the game uses. For games this is the common way anyways. And even for tools we often do this (e.g. using a small library like ImGui). This way your code is also more portable between OS and IDE.

So for now i would ignore the confusion here, start with the game itself and decide this later. (Personally i have stopped using OS GUI elements pretty early and i do not miss that stuff.)

 

1 hour ago, g0dafk said:

Where should i start with the graphics? What should blocks be made of? What should the game "stage" be made of?

For the graphics there are two main options: 2D bitmaps (which you can blit to the screen using OS functions), or a 3D rendering API like OpenGL or DirectX (the way to go for games - but not necessary for Tetris).

There are many libs that make the latter easy. I remember SFML (graphics, networking and sound. I wrote a game in only one file using this), others are Allegro, SDL, .... (some are lightweight and initialize just a graphics window and give you the context.)

You can also do it without such a library, which is a bit more boring work to set things up.

 

Then you learn for example how to draw a colored triangle, and you draw one quad of a tetris block using two triangles. (again, using library makes this a lot easier. SFML would do very well here. You can draw sprites without learning how to texture triangles.)

For the stage you can use strings (allows to 'edit' small levels within code) but i would use a 2D array of integer numbers like this:

int stage[12][24];

For a block you could use:

int blockShape[4][4] = {

0,0,0,0,

0,0,0,0,

0,1,0,0,

1,1,1,0

};

So you can still 'edit' the shape of the blocks within code. (Under the hood 'string', 'matrix' or '2D array'... its all the same.)

You then can copy this block to the stage, move it on player input, scroll the stage downwards... all simple copy operations. You might need two versions of the stage, one with the static blocks, and another where the active block can be added to after movement for rendering and game logic.

 

I assume setting up libraries and learning their API is initially frustrating, but after taking that burden programming the game should be fun and Tetris is a very good choice. :)

I generate my classes by creating the files manually and adding them to the project. It's not necessary to use the IDE or any templates here.

 

 

It sounds like you're fighting C++ builder rather than the C++ language  or tetris itself.

The best place to find answers for C++ builder is in the C++ builder manual, or in a forum dedicated to that software.

 

This forum is about game development in general. While we know a lot about games (and the c++ language) in general. However there is much less knowledge about any particular software package, unless you accidentally bump into the right person. (Most people here use something else for editing code, so they just don't know the answer.)

Forums dedicated to your c++ builder package are filled to the top with people knowing about and using c++ builder, so you'll get better answers much faster.

 

Personally, if you are already comfortable with command line( and make files? ), I'd stick to it and use SDL for this project.

That said, you seem to have your heart set on C++ Builder.  I assume you have a reason for that?

Languages; C, Java. Platforms: Android, Oculus Go, ZX Spectrum, Megadrive.

Website: Mega-Gen Garage

Yes, it s a project at school and my teacher wants to be in C++ Builder.

And thank you to everybody for the informations given.

Hi ! Wish you a good day !

In that case definitely get on the C++ Builder forum.

Oh, don't forget to check out stackoverflow to see if your issue has been solved previously.

Best of luck with the project!

Languages; C, Java. Platforms: Android, Oculus Go, ZX Spectrum, Megadrive.

Website: Mega-Gen Garage

This topic is closed to new replies.

Advertisement