Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: New To SFML, Creating a simple Tetris Game  (Read 10496 times)

0 Members and 1 Guest are viewing this topic.

Aeox

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
New To SFML, Creating a simple Tetris Game
« on: November 25, 2015, 11:43:19 am »
Hello!

I am completely new to SFML as a program and i've been studying C++ at Uni for about 2-3 months now. We have just been introduced to SFML which will be included in a Game project we have just started working on. I have decided to create a simple Tetris game as the project since it's a bit challenging for a beginner but not completely impossible. As previously stated i am extremely new to all of this and if you happened to stumble on to this thread and feel helpful i would very much appreciate any tips, helpful comments or just friendly remarks considering creating a Tetris game and how to learn SFML.

Thanks for taking the time to read this and i hope you will leave a comment :)
All the best / Joel

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
New To SFML, Creating a simple Tetris Game
« Reply #1 on: November 25, 2015, 06:03:23 pm »
Hello :)

Out of curiosity, at which uni are you taught SFML?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: New To SFML, Creating a simple Tetris Game
« Reply #2 on: November 25, 2015, 07:44:56 pm »
I might be wrong but I'd say - try to decouple your Tetris logic and SFML input/graphic code.

Have one class that does all the logic (falling blocks, walls, collisions, creating random new block from the list of tetrominos, game start/end, ect.) and has very nice and simple interface to send to it the input (left, right, up, down), make it go forward a frame/step, query the points count and how large the play field is and what each block is (what color, full or not, part of the falling block or part of already fallen one or wall, ect.).
Then your SFML code will become as simple as just querying that logic class a little to pass it the input or advance and receive game state and draw it, no game logic mixed in it.

That's what I did in my PacMan clone in old uni and I'd say it helped me finish that project (on time :P).
It almost felt as if my logic code is a library and I'm just gluing it to SFML for input and display in the end.
The split is not super clear (makeWallsMap function) and the code is a mess, but I think it illustrates well what I mean:
https://github.com/FRex/Man/blob/master/src/GameState.cpp
https://github.com/FRex/Man/blob/master/src/PacEngine.h
https://github.com/FRex/Man/blob/master/src/PacEngine.cpp
Back to C++ gamedev with SFML in May 2023

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: New To SFML, Creating a simple Tetris Game
« Reply #3 on: December 01, 2015, 10:19:25 pm »
Hi Joel

I think perhaps you would like to read some SFML starting tutorials in order to get familiar with the common classes, methods, and syntax

Unfortunately i don't use C++ but C# .Net. Anyway i'll try to write some Tetris basic sample to help you to start, and i hope some member, Lady or Guy, may do us the favor to translate from C# to C++. I'll post it when it's quite Ok.

In advance, i imagine we could create the game pieces or figures as below:

    int[][][] pieces = new int[][][] { { { 0, 0 }, { 1, 0 }, { 0, 1}, { 1, 1 } },
                                         { { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 } },
                                         { { 0, 0 }, { 0, 1 }, { 0, 2 }, { 1, 2 } },
                                         { { 0, 0 }, { 0, 1 }, { 0, 2 }, { 1, 1 } },
                                         { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 1, 2 } } };
                                         // couples are each figure's squares' (x,y) pos. stepping 40 pixels  

    RectangleShape square = new RectangleShape(new Vector2f(40, 40)); // figure's square, 40 pixels side

    private void DrawTetrisPiece(int x, int y, int index) // (x, y) is the position in the playing Box
    {
        int a;
        for (a = 0; a < 4; a++)
        {
            square.Position = new Vector2f(x + pieces[index][a][0] * 40, y + pieces[index][a][1] * 40);
            window.Draw(square);
        }
    }
 

Good luck in your SFML learning
Pablo

Galeswift

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: New To SFML, Creating a simple Tetris Game
« Reply #4 on: December 02, 2015, 02:40:37 am »
Hey Joel, I just happened to have finished up a simple version of tetris in C++.  You can see the source and the post here:

http://en.sfml-dev.org/forums/index.php?topic=19440.msg140077#msg140077

Grundkurs

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: New To SFML, Creating a simple Tetris Game
« Reply #5 on: September 21, 2018, 12:23:00 pm »
You can find an in-depth Tutorial about creating Tetris with SFML here: http://zaunfish.blogspot.com/2018/07/tetris-made-with-sfml-part-i.html