Step by step guide to writing Tetris in Python with PyGame

In this tutorial, we will write a simple Tetris using the PyGame library in Python. The algorithms inside are pretty simple but can be a little challenging for the beginners. We will not concentrate on PyGame mechanics too much, but rather focus on the game logic. If you are too lazy to read all the stuff, you may simply copy and paste the code in the end.

Image for post
Image for post
Tetris Game
  1. Python3. This may be downloaded from the official website.
  2. PyGame. Go to your command prompt or terminal, depending on the OS you are using, and type pip install pygame or pip3 install pygame. …


You will be surprised by how simple it is!

In this article, you will learn:

  • PyGame basics
  • Gravity physics implemented in Python
  • Keyboard controls in Pygame
Image for post
Image for post
  1. Python3. This may be downloaded from the official website.
  2. PyGame. Go to your command prompt or terminal, depending on the OS you are using, and type pip install pygame or pip3 install pygame.
  3. A png file of a helicopter. Any png file will do.
  4. Basic knowledge of Python. See my other articles for that if needed.

You may experience issues with installing PyGame or Python itself, but this is out of scope here. …


Intro to Programming for beginners

The Problem

I am teaching programming for more than 20 years already. And normally, when people start learning to program, they face courses or books that give syntax, data types, and then immediately switch to data structures and so on.

Image for post
Image for post

The biggest problems are:

  • Beginners do not understand the program flow
  • Beginners do not understand variables
  • Beginners think they know, but they have no idea

Solution

  • Learn arithmetical operations, print, input, and the IF statement (covered in this article)
  • Based on that, write at least 50 programs
  • Learn Loops and Arrays (or lists)
  • Start solving tasks on the Internet, based on…


A step by step guide

Image for post
Image for post

Because we installed our Christmas tree next to the heating battery, it died pretty soon. So I decided to draw a Christmas Tree in Matplotlib. You won’t need to protect it from the Sun or heat, it also won’t need any water. On Planet Arrakis, water is a limited supply. On Earth, by the way, too.

Initial code

To draw in matplotlib, we need to include it.

Also, we prepare all the staff for 3D.

import math
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")

Let’s draw a 3D circle first to be sure everything works. …


A common mistake that leads to a bug

I have recently written an article with a bug investigation that shows a surprising scope of variables in classes.

Today, I want to show another bug that is frequently made not only by beginners but also by some “senior” developers — mostly because of laziness.

We will try to filter a list by some criteria, what can be simpler, and how can someone generate a bug here?

Image for post
Image for post

The Buggy Code

We create a list of 10 random integer numbers and then filter out the odd numbers. …


Especially, if you switch from another programming language

Introduction

In my bigger project, I had a bug that was hard to find. It was absolutely not obvious, and I have lost quite some time investigating it.

So I thought that can be interesting to investigate this bug together.

We will build a simple tree, and there will already be a small bug harming it.

Image for post
Image for post
Source: https://www.warrenphotographic.co.uk/00725-green-tree-ants-with-mimic-bug

Let’s do it!

Building a Tree

Building a tree is simple and straightforward. We only need to define a class Node, and then we connect them using a list.

Here is an implementation:

class Node:
children = []

def __init__(self, name, parent=None):
self.name = name
self.parent = parent
if parent is not None:
parent.children.append(self) …


When people say Python is slow, show them this article

Image for post
Image for post

I want to say in advance: Yes, when you write the same code, C++ is way more performant than most of the modern languages. And Python would be somewhere at the end of the list.

But when you implement real projects, nobody cares about your code or language, the result matters, and the faster you write your code the better.

Say, you need to create a new website. And there are two options: C++ and Python. In the end, the C++ project will be 10 times faster. So, your HTML pages will be produced in 0.01 sec instead of 0.1 sec. But who cares? In C++ you will be writing the same project for several months vs. …


An introduction to Django mechanics

Introduction to our Website

In this article, I try to explain what Django is, how it composes and serves HTML files, and how you control the URL.

Image for post
Image for post

What we do here is the following:

  1. Create two simple HTML files for our new website. We start with HTML because this is simple :)
  2. Create a Django project, set it up, and run.
  3. Make two URLs navigate to our two HTML files, so you understand how your program flow depends on what a user enters in the URL bar.
  4. See how you communicate with the user — we will create a simple form where we ask what type of Pizza a user would like to order. …


Implementation of Substitution Cipher and some statistical analysis

Motivation

Are you here because you are worried that all your traffic is sniffed and analyzed by the government? You are at the right place! Here you will learn both encrypting your text and read someone’s secret chat that was encrypted with a substitution cipher.

The bad thing is that in the end, you will be sure that all your chats in WhatsApp can be easily decoded (sure this is not true, Facebook decided to not use the substitution cipher for WhatsApp around 40 years ago).

We will implement a substitution cipher in Python, and then we will try to break the code using statistics. …


With Linear Regression

Motivation

It is surprising how much fun one can have with linear regression.

This is the work of my master’s student. She has implemented this simple prediction algorithm to learn the basics of Machine Learning and Linear (Poly) Regression.

This article describes an algorithm for geographical coordinates processing using polynomial regression to calculate the common behavior of a movement. The primary advantage of this algorithm is having a map of trajectories in any region. We have used Python programming language together with scikit-learn and matplotlib libraries for machine learning and visualization. …

About

Timur Bakibayev

Professor, Software Developer, Ph.D. in Computer Science

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store