# Category: Tutorials

## Simple text game with Python

In this article we will demonstrate how to create a simple guessing game.
The goal of the game is to guess the right number.

Example
An example run below:

Simple text game with Python

You may like

## Random number

The user will be asked to guess the random number. We first pick the random number:

The randint() function will pick a pseudo random number between 1 and 10. Then we have to continue until the correct number is found:

## Python Guessing Game

The code below starts the game:

An example run:

## Snake AI in Pygame

In this article we will show you how to create basic game AI (Artificial Intelligence). This article will describe an AI for the game snake.

In this game (snake) both the computer and you play a snake, and the computer snake tries to catch you. In short: the opponent AI tries to determine and go to the destination point based on your location on the board.

You may like

We extend the code with a new class called Computer which will be our computer player. This contains routines to draw and move the computer snake.

We then call the update and drawing method of the computer.

This will make the computer snake move and be drawn on the screen. It has the same properties as the human player.

Adding the intelligence to the computer player
Because this is a simple game, we do not need to create a complete thinking machine inside the game. We simply need some basic intelligence exhibited by our computer player. Intelligence in games is often quite limited because most of the time more complexity is not neccesary or there simply is not the time available to implement clever algorithms.

The algorithm we will add will simply go to the destination. It will neglect any obstacles (the human player).

Complete code
We end up with this complete code:

python snake

Conclusion
You learned how to create a basic computer player using an very simple AI algorithm.

## Game development with Pygame

Pygame Python

Welcome to the first tutorial of the series: Building games with Pygame. Games you create with Pygame can be run on any machine that supports Python, including Windows, Linux and Mac OS.

In this tutorial we will explain the fundamental of building a game with Pygame. We’ll start of with the basics and will teach you how to create the basic framework. In the next tutorials you will learn how to make certain types of games.

You may like

PyGame introduction
You’ll end up with a program similar to the one on the right:

A game always starts in an order similar to this (pseudo code):

The game starts with initialization. All graphics are loaded, sounds are loaded, levels are loaded and any data that needs to be loaded. The game continues running until it receives a quit event. In this game loop we update the game, get input and update the screen. Depending on the game the implementation widely varies, but this fundamental structure is common in all games.

In Pygame we define this as:

The Pygame program starts with the constructor init(). Once that is finished on_execute() is called. This method runs the game: it updates the events, updates the screen. Finally, the game is deinitialized using on_cleanup().

In the initialiasation phase we set the screen resolution and start the Pygame library: