More results...

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
post
page
◑ Python IDE Dashboard

Falling/Scrolling Object Algorithms

rocketIn many arcade video games objects are flying across the screen (either vertically: falling objects, upcoming cars in car racing game) or horizontally (e.g. pipes in Super Mario or flappy bird, cars in Crossy Road).

In frame based games, motions is often implemented by positioning sprites on the screen using (x,y) coordinates and applying a velocity vector (Vx,Vy) to each sprite to indicate the direction and speed of the sprite.

Let’s review the algorithms used to implement three types of motions:

  • Vertical Scrolling: Falling objects (e.g. Catch the Fruit game)
  • Horizontal Scrolling (Right to left gliding pipes in Super Mario)
  • Combination of both Vertical and Horizontal Scrolling in the game of Space Invaders

Vertical Scrolling: Falling objects


scrolling-algorithm-falling-object

Horizontal Scrolling:


scrolling-algorithm-pipe

Space Invaders: Aliens Motion


scrolling-algorithm-space-invaders


–

Tagged with:

Computational Features of Video Games

spaceInvaderIf you are confident enough with your Python skills, you might feel ready to move on to the next stage and start creating your own arcade video game.

To do so you will have to research and investigate existing video games to identify some of the key computational features of these games and see how these can be implemented.

For arcade games we advise you to investigate the classic games including:

When investigating these video games try to identify their main features:

  • What are the main sprites of the game,
  • How does the player interact with the game / control the main sprite,
  • How does the scoring system work,
  • Is the game making use of a timer,
  • Can the player play against the computer,
  • Is the game using real physics,
  • Is the game in first-person view,
  • How does the computer control the movement of other sprites,
  • How does the computer decide what move to play next,
  • What information is stored in the game,
  • What extra features are included in the games (e.g. leaderboard?),
  • What settings can be changed in the game,
  • Are there multiple levels of difficulty,
  • What makes a level more difficult to complete,
  • How is the stage generated,
  • What are the main characteristics of the Graphical User Interface,
  • etc.

You will then be able to research these characteristics further and identify the computational features that you could use to build your own game.

To help you getting started we have listed here a few blog posts from this blog describing a range of computational features further:

Controlling Movement of Sprites:


Main Program Loop (Frame Based Game)

Main Program Loop (Frame Based Game)

Controlling a Sprite using the arrow keys

Controlling a Sprite using the arrow keys

Edge Detection & Bouncing Algorithm

Edge Detection & Bouncing Algorithm

Scrolling/Falling Object Algorithms

Scrolling/Falling Object Algorithms

Projectile-Motion

Projectile Motion Formula

Pacman Ghost Algorithm

Pacman Ghost Algorithm

Polar vs. Cartesian Coordinates

Polar vs. Cartesian Coordinates

Translations and Rotations in a 2D frame by frame animation

Translations and Rotations in a 2D frame based animation

2D Rotation Matrix

2D Rotation Matrix

Elastic Collision Formulas

Elastic Collision Formulas

Snake Game in Python

Stage Generation:


Random Background Generation Algorithm

Random Background Generation Algorithm

Parallax Scrolling Effect

Parallax Scrolling Effect

Battleship Grid Initialisation Algorithm

Battleship Grid Initialisation Algorithm

Level Generation using a 2D Array

Level Generation using a 2D Array

Pacman Maze using a 2D Array

Snakes & Ladders using a 2D array

Candy Crush – Level Generation

Collision Detection Algorithms:


Collision Detection Algorithm in a game of Curling

Collision Detection Algorithm in a game of Curling

Collision Detection between Sprites (PyGame)

Collision Detection between Sprites (PyGame)

Target Detection Algorithm

Target Detection Algorithm

Bouncing Algorithm in a platform game

Bouncing Algorithm (platform game)

Data Structures:


Connect4 Data Structure using a 2D-array

Connect4 Data Structure using a 2D-array

Othello Data Structure using a 2D-array

Othello Data Structure using a 2D-array

File Handling (CSV files) in a Monopoly Quiz

File Handling (CSV files) in a Monopoly Quiz

Using a 2D-array to implement a game of Tetris

Using a 2D-array to implement a game of Tetris

Using Lists/Arrays and Graphs to store/represent data

Using Lists/Arrays and Graphs to store/represent data

Using a Dictionary: Chemical Element Quiz

Using a Dictionary: Chemical Element Quiz

2D array - Minesweeper (Javascript)

2D array – Minesweeper (Javascript)

Football Results Tracker using a CSV file

Football Results Tracker using a CSV file

Frog Puzzle - Backtracking Algorithm using a Graph Data Structure

Frog Puzzle – Backtracking Algorithm using a Graph Data Structure

Noah's Ark - A backtracking algorithm based on a 2D array data structure.

Noah’s Ark – A backtracking algorithm based on a 2D array data structure.

 Laser Game - 2D array data structure.

Laser Game – 2D array data structure.

Scoring Systems:


Scoring System in a Quiz

Scoring System in a Quiz

Archery Scoring System

Archery Scoring System

Darts Scoring System

Darts Scoring System

Grading System

Grading System

Data Visualisation


Data Visualisation Algorithms

Data Visualisation Algorithms

Artificial Intelligence? When the player plays against the computer!


Tic-Tac-Toe Challenge

Using a Backtracking algorithm to solve a Sudoku Puzzle

Using a Backtracking algorithm to solve a Sudoku Puzzle

Using a backtracking algorithm to explore a maze.

Using a backtracking algorithm to explore a maze.

Heuristic approaches to problem solving

Heuristic approaches to problem solving

Machine learning top trumps game

Machine learning top trumps game

Rush Hour

Rush Hour: Backtracking Algorithm & Heuristics

Connect Flow - Backtracking Algorithm & Heuristics

Connect Flow – Backtracking Algorithm & Heuristics

3D User Interface:


Oblique Projection Formula

Oblique Projection Formula

3D Rotation Matrix

3D Rotation Matrix

Vanishing Point Perspective

Vanishing Point Perspective

Additional Features of video games:


Adding a timer in a video game

Sprite Animation using Pygame

Login Algorithm

Login Algorithm

Leaderboard (using a CSV file)

Leaderboard (using a CSV file)

Off-side Detection Algorithm

Off-side Detection Algorithm

Closest Player Detection Algorithm

Closest Player Detection Algorithm

Manhattan (taxicab) distance calculator

Manhattan (taxicab) distance calculator

Shuffling Algorithm (Shuffling a deck of cards)

Shuffling Algorithm (Shuffling a deck of cards)

Object-Oriented Programming:


OOP Concepts

OOP Concepts

Pong (Pygame Tutorial)

Pong (Pygame Tutorial)

Car Racing Game (Pygame Tutorial)

Car Racing Game (Pygame Tutorial)

Breakout Game (Pygame Tutorial)

Breakout Game (Pygame Tutorial)

UML Diagrams

UML Diagrams


Tagged with:

Pacman Ghost Algorithm

pacman
In a game of Pacman a specific algorithm is used to control the movement of the ghosts who are chasing (running towards) Pacman.

For this challenge we will assume that ghosts can walk through walls (as ghosts do!). So we will implement an algorithm that is slightly different to the algorithm used in the real game of Pacman where ghosts can only run alongside the corridors of the maze.

Our algorithm will be used in a frame based game where the sprites (e.g. Pacman, Ghosts) are positioned using (x,y) coordinates. The Pacman movement will be based on the position of the mouse cursor whereas the Ghosts will use a velocity vector (vx,vy) to move/translate between two frames.

velocity-vector

Trigonometric Ratios: SOH-CAH-TOA


Our algorithm will use the trigonometric ration to find the angle the Ghost needs to head towards to chase Pacman.
pacman-sohcahtoa-angle

The next step of our algorithm will use this angle to calculate the velocity vector (Vx,Vy) of the ghost:
pacman-sohcahtoa-velocity

The final step of our algorithm will update the (x,y) coordinates of our ghost to apply the velocity vector translation before drawing the ghost sprite on the screen.
pacman-sohcahtoa-translation

Python Code


Tagged with:

Bouncing Algorithm

pong-gameBouncing algorithms are often used in arcade games such as Pong or Breakout.

To understand how to implement a bouncing algorithm, it is essential to understand how the computer controls the trajectory of a sprite (e.g. ball) on the screen. Arcade games are based on a frame based animation where the screen is refreshed every x milliseconds. Moving sprites are positioned using (x,y) coordinates and have a velocity vector (Vx,Vy) which specifies the delta in pixels to apply to the (x,y) coordinates of a sprite between two frames:

  • frame n: Sprite Coordinates: (x,y)
  • frame n+1: Sprite Coordinates: (x+Vx,y+Vy)

velocity-vector

As the sprite moves across the screen, it may need to bounce against another sprite or against the edge of the screen.

Let’s investigate how the velocity vector is affected when the sprite bounces against vertical and horizontal walls/edges.

Right Wall/EdgeLeft Wall/EdgeTop Wall/EdgeBottom Wall/Edge
bouncing-algorithm-right
bouncing-algorithm-left
bouncing-algorithm-top
bouncing-algorithm-bottom

Python Code


The following Glowscript code implements a bouncing algorithm against all 4 edges of the canvas.

Tagged with:

Closest Player Detection Algorithm

basket-ballHave you ever noticed when playing a team sport video game such as a (football, basketball, rugby game, etc.) the computer often finds out who the closest player to the ball is so that you can run to the ball with the closest player.

To do so the computer uses a specific algorithm to identify who, of all the players in your team, is the closest to the ball. This algorithm can also be used when you already control a player who has the ball to find out who is your closest team player to pass the ball to.

In this blog post we will investigate how this algorithm works and will implement this algorithm using Python.

On a team sport video game, each player is a sprite that is given an x and a y coordinate to be positioned on the pitch/screen.

The ball is also positioned using (x,y) coordinates.

To find out who the closest player is to the ball, we need to calculate the distance between the ball and each player of the team. To do so we will use Pythagoras formula:
closest-player-detection-algorithm

We can then use an algorithm to find the shortest distance and highlight the corresponding player.

Python Code


Tagged with:

Random Background Generator

artillery-gameIn this challenge we will use a Python script (using Python Turtle) to generate a random background for a 2D video game.

artillery-game-background

To create a mountain range skyline we will use two different approaches:

  1. Using a polynomial function
  2. Using a composite sine wave function

Using a Polynomial Function

Polynomial Function of Degree 2

Polynomial Function of Degree 2

Polynomial Function of Degree 3

Polynomial Function of Degree 3

Polynomial Function of Degree 4

Polynomial Function of Degree 4

Polynomial Function of Degree 5

Polynomial Function of Degree 5

Polynomial Function of Degree 6

Polynomial Function of Degree 6

Polynomial Function of Degree 7

Polynomial Function of Degree 7

 

In our example (see trinket below) we will use a polynomial function of degree 3:

y = a*x3 + b*x2 + c*x + d

The coefficients a,b,c,d of our polynomial function will be randomly generated. This will hence create a different background/skyline every time the program is executed.

We will then use this function to place some trees on the skyline. The x coordinate of the tree will be randomly generated whereas the polynomial equation will be used to calculate the y coordinate of each tree.

Python Code


Your Challenge


Update this code to add another set of trees. The trees could be positioned randomly anywhere on the grass area of the landscape. (Anywhere between the bottom of the screen and the skyline)

Extension Task


Update this code to add some clouds. The clouds should be positioned randomly anywhere in the sky. (Anywhere between the top of the screen and the skyline)

Alternative Approach… Using a composite sine wave


Instead of using a polynomial function we could use a composite sine wave function. You may find this solution either to implement especially when generating several peaks and peats.

A composite sine wave function is a combination of two simple sine waves with different frequency, phase and amplitude.

Here is the equation of a simple sine wave function:


A sine wave is too predictable/repetitive to create an interesting background. We will therefore create a composite function to generate peaks and heaps of different amplitudes.

To create a composite sine wave function we will use two functions as follows:

Here is our demo:

Tagged with: ,

Oblique Projection Formulas

3D-coordinates-cubeThe aim of this challenge is to demonstrate how the oblique projection formulas are used to convert 3D coordinates (x,y,z) into 2D coordinates (x,y).

oblique-projection-formulae

The oblique projection formulas are essential to understand how 3D models are displayed on a 2D screen. They are heavily used in 3D video games, computer animations and virtual reality.

Let’s apply these formulas to represent a simple 3D cube on a 2D canvas using Python Turtle.

Your Task


Compete the code provided above to add some extra vertices and edges to create a 3D house as follows:house-3d-coordinates

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area
Tagged with: , ,

RISC and CISC Processors

cpu-risc-cisc
RISC and CISC are two computers architectures that are used by CPUs.
Complete this drag and drop activity to highlight the characteristics of RISC & CISC processors.

instructions
several
Complex
Reduced
Software
circuitry
1
Desktop
simpler
Embedded devices
Many
clock cycle

RISC Architecture

  • A RISC ( Instruction Set Computers) processor can only decode a small number of
  • A RISC processor has a design so is easier to design and manufacture.
  • A RISC processor processes one instruction per (= machine cycle).
  • In a RISC processor: 1 instruction = 1 FDE Cycle = Clock Cycle
  • Software is more complex to write with a RISC processor.
  • Typical Use: (e.g. Washing Machines, GPS SatNav, mobile phones etc…)

CISC Architecture

  • A CISC ( Instruction Set Computers) processor can decode a larger number of instructions.
  • A CISC processor has a complex design/.
  • A CISC processor processes one instruction using clock cycles (= machine cycles).
  • In a CISC processor: 1 instruction = 1 FDE Cycle = Clock Cycles
  • is easier to write with a CISC processor.
  • Typical Use: , Laptop

Parallax Scrolling Effect

parallax-scrolling-effect2D arcade video games often use a frame based approach. This means that the screen is refreshed every so often (for instance every 20 milliseconds). This enables to animate the Graphical User Interface of the game making the game more engaging.

When the main character is moving across the screen, a parallax scrolling effect can be used to animate the background according to the speed of the player.

A parallax effect is based on the idea that when your character is moving side to side, the objects closer to the camera appear to move faster, while the objects in the distance appear to move more slowly.

Parallax Effect Animation by Nathaniel Domek.

Parallax Effect Animation by Nathaniel Domek.

A parallax scrolling effect is sometimes used in video games to draw and animate the background. This technique is used for instance in the video games Angry Birds and Flappy Bird. It is based on the idea that the background landscape consists of different layers: the foreground closed to the camera viewpoint, short distance background layers (e.g. trees, buildings) and long distance background layers (e.g. mountains).

You can read more about Parallax Scrolling on this page: https://en.wikipedia.org/wiki/Parallax_scrolling

In this challenge we will draw a background consisting of three mountain ranges each having its own skyline. To create our parallax effect we will animate each skyline to translate horizontally at a different speed.

Parallax Effect in 3D games


Parallax effect algorithms can be a lot more complex especially when applied to 3D video games. Such Parallax effect algorithms will involved a lot of trigonometric calculations!

Parallax Effect in a 3D video game

Parallax Effect in a 3D video game

Parallax Scrolling Effect in Web Design


This technique is now used by web-designer to add a parallax animation effect to a web page when the user scroll down the page. It is based on the idea that different components of the web page (background pictures, pictures and video clips, header & footer sections, navigation bar, etc.) are positioned on different layers which will scroll at a different speed when the user scroll down the page.

Layer Cake using Python Turtle

layer-cakeIn this challenge we will use Python Turtle to create and draw a layer cake.

The code provided at the bottom of this blog post is using a few computing concepts such as:

  • A list to store the parameters of the layers of our cake,
  • A dictionary to match specific colour codes to different ingredients,
  • (x,y) coordinates to position the turtle on the screen.


List?


In Python, a list is used to save a collection of values. For instance, to save all the different days of the week we could declare a variable called “daysOfTheWeek” and assign a list as follows (Notice the use of square brackets):

daysOfTheWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

Based on this what do you think len(daysOfTheWeek) would return?
What’s about daysOfTheWeek[0]?
Or daysOfTheWeek[3]?

Looping through a list


It is possible to access each value of a list one at a time using a for loop. Look at the following code to print the seven days of the week.

daysOfTheWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

for day in daysOfTheWeek:
    print(day)

Dictionary?


By completing this challenge we are going to learn about Python dictionaries.
In Python, a dictionary consists of pairs (called items) of keys and their corresponding values.

Python dictionaries are also known as associative arrays or hash tables. The general syntax of a dictionary is as follows:
PythonDictionary

myDictionary = {"Red": "#FF0000", "Green": "#00FF00", "Blue": "#0000FF"}
print("The colour code for Green is:")
print(myDictionary["Green"])

Layer Cake Challenge


In this challenge we decided to create a list called layers to store the parameters (height and ingredient) of each layer.

Check the code below and add more layers from line 42. You can use any of the ingredients from the dictionary called ingredients.

layer-cakes

Tagged with: , ,
Privacy Overview

This website will store some information about your preferences on your own computer inside a tiny file called a cookie. A cookie is a small piece of data that a website asks your browser to store on your computer or mobile device. The cookie allows the website to remember your actions or preferences over time.

You can delete all cookies that are already on your computer, and you can set most browsers to prevent them from being placed. However, if you do this, you may have to manually adjust some preferences every time you visit a site, and some services and functionalities may not work.

Most browsers support cookies, but you can set your browser to decline them and can delete them whenever you like. You can find instructions here for how you can do that on various browsers.

This website uses cookies to:

  1. Identify you as a returning user and to count your visits in traffic statistics analysis
  2. Remember your custom display preferences (such as light/dark theme option)
  3. Provide other usability features, including tracking whether you have already given your consent to cookies

Enabling cookies is not strictly necessary for the website to work but it will provide you with a better browsing experience.

The cookie-related information is not used to identify you personally and is not used for any purpose other than those described here.

There may also be other types of cookies created after you have visited this website. This site uses Google Analytics, a popular web analytics service that uses cookies to help to analyse how users use the site. The information generated by the cookie about your use of this website (including your IP address) will be transmitted to and stored by Google on servers in the United States. Google will use this information for the purpose of evaluating your use of other website, compiling reports on website activity, and providing other services relating to website activity and internet usage. Google may also transfer this information to third parties where required to do so by law, or where such third parties process the information on Google’s behalf. Google undertakes not to associate your IP address with any other data held by Google.