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.
pip install pygame
or pip3 install pygame
. …You will be surprised by how simple it is!
In this article, you will learn:
pip install pygame
or pip3 install pygame
.You may experience issues with installing PyGame or Python itself, but this is out of scope here. …
Intro to Programming for beginners
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.
The biggest problems are:
A step by step guide
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.
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?
We create a list of 10 random integer numbers and then filter out the odd numbers. …
Especially, if you switch from another programming language
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.
Let’s do it!
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
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. …
In this article, I try to explain what Django is, how it composes and serves HTML files, and how you control the URL.
What we do here is the following:
Implementation of Substitution Cipher and some statistical analysis
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
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