PyTris: a Tetris clone using PyCap

"PyCap? What the hell is that?" you would ask. Here the answer from Farbs:

"... PyCap is the ultimate framework for the super slack programmer. Pycap wraps the sweet sweet PopCap Game Framework in a super friendly high level python interface. With Pycap you can start building pretty 2D games without installing a compiler or learning C++."

To continue Farbs´ explanation of PyCap:

"The popcap framework's great for building robust 2D games with pretty antialiased sprites and sub-pixel scrolling. Unfortunately it's written in C++, which is labor intensive, damaging to creativity, and eats babies when nobody's looking. That's why I wrote Pycap. It's a simple bridge between the Popcap framework and the Python scripting language. With Pycap you can write an entire game in Python script, never once having to compile or worry about infanticide. Development is super fast, allowing your game ideas to leap out into reality and flit about like sugar-fuelled butterflies."

Please read the whole story on Farbs Website where you also can download PyCap.

Ok, programming with the only focus on game logic? No compiling necessary, just save the Python-file and run the .exe? No worries about shooting myself into the foot (or even lose my whole leg) like with C++? Sounds interesting, I think, I should take a look...

My thoughts: "But... Python? Isn´t that a scripting language like Perl or LUA? I know a few programming languages like C, C++, Java, Delphi, Visual Basic, SQL and so on, but after 2 weeks of shell-scripting in Unix many years ago, I hate script-languages. Nevertheless, perhaps there´s a online book about Python somewhere in the web..."

And yes, there are a lot of informations about Python, I picked the online-book "A Byte of Python", read it in a few hours and wrote my first little Python programms from the examples of the book. I was surprised: this Python language is cool!

Next step: use Pycap to make a little game. Perhaps a Tetris clone, because the preliminary considerations about implementing the Tetris gameplay were done long ago. PyTris was born! This is how it goes:

  • use a simple boolean-array for the playfield (true means: there is a stone)
  • define the possible stone-combos with the origin of the coordinates in the center of rotation
  • add a easy collision detection, based on the playfield dimensions and values
  • when collided, add a new random combo to the playfield
  • check and delete full rows from the playfield
  • add the possibility to rotate a combo, this is easy because we set the origin in the center of rotation and just have to swap the x- and y-values of each stone with the right sign (see below)
  • take care, that a combo does match the playfield borders after rotating
  • choose a random combo to drop
     

So, there will be a very easy mainloop of the game in pseudocode:

advance the falling combo
if test combo of collision = true
        undo last advance of the combo
        add combo to the playfield
        check for full rows to delete
        drop a new combo

 Pytris Shapes

possible combos

Now imagine, we have a object "point" for a pair of coordinates and a object "block" witch accepts a list of points. We can easily construct a combo:

block([point( 0, 0), point(-1, 0), point( 1, 0), point( 0,-1)])

The rotation of this combo can be done, by simply swapping the coordinates like this

   clockwise: newX = -Y       counterclockwise: newX = Y
              newY = X                          newY = -X

 

so we have: 0,0 -> 0,0   -1,0 -> 0,1   1,0 -> 0,-1   0,-1 -> 1,0 as you can see on the next sketch for a clockwise rotation:

Pytris Rotate

Enough pre-stuff - with PyCap, it was fun developing the game. Now check out the game and the source - and have fun!

 Pytris In Action

Pytris in action

Thoughts of enhancement (up to you): 

  • add a pause-function when the game lost focus
  • add functionality to drop a combo down with one keyhit
  • advance the falling speed of a combo regarding of the number of rows cleared
     

Just a last hint: tab stops are very important in Python, I used a tab size of 2 so take care that you set the tab size of your editor to 2 when viewing the source.

Downloads: 

Bitte bewerten Sie den Beitrag, vielen Dank für Ihr feedback!: 

Noch keine Bewertungen vorhanden