More results...

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

Sampling Sound – Revision

sampling-interval-15
Before attempting this task make sure you understand what is meant by sampling sound.
Complete this drag and drop activity to highlight the key concepts of sound sampling.

digital
large
sampling rate
analogue
quality
Hz
milliseconds
bps
measurements
lower
bits
intervals

Sampling Intervals and Sample Rate

  • Sampling sound is the process of converting an sound wave into a signal.
  • When sampling sound, the computer takes of the sound wave at regular .
  • The sampling interval is the number of between two samples.
  • The larger the sampling interval, the the quality and file size of the sound file.
  • The is the number of samples in one second. It is measured in .
  • A sample interval leads to a low sample rate.

Bit Depth and Bit Rate

  • The Bit Depth is the number of used to store one sample into binary.
  • A high bit depth leads to a better sound file.
  • The Bit Rate is the number of bits used per seconds, measured in .
  • A high bit rate leads to a better quality sound file.

Sampling Sound

microphone-sampling-soundSampling is a method of converting an analogue audio signal into a digital signal. While sampling a sound wave, the computer takes measurements of this sound wave at a regular interval called sampling interval. Each measurement is then saved as a number in binary format.

Sampling Interval


The quality and file size of the sound files are linked to the size of the sampling interval (in milliseconds).

Sampling Interval: 15 ms

Sampling Interval: 15 ms

Sampling Interval: 40ms

Sampling Interval: 40ms

A small sampling interval results in a better quality sound file and increases the size of the audio file.

Sample Rate


To describe the quality of a sound file we often use the term sample rate as opposed to the sampling interval.

The sample rate (in Hz) is the number of samples in one seconds.

Sample Rate = 1 / Sampling Interval
Sampling Interval: 15 ms

Sample Rate: 66Hz

Sampling Interval: 40ms

Sample Rate: 25Hz

A small sampling interval results in a high sample rate which results in a better quality sound file and increases the size of the audio file.

Sampling Sound Visualisation using Python


Bit Depth


Bit depth is the number of bits available for each sample. The higher the bit depth, the higher the quality of the audio. Bit depth is usually 16 bits on a CD and 24 bits on a DVD.

Bit Rate


The bit rate of a file tells us how many bits of data are processed every second. Bit rates are usually measured in kilobits per second (kbps). The higher the bit depth, the higher the quality of the audio.

Take the Quiz


Take the Quiz on Sampling Sound

Vanishing Point Perspective

In graphical perspective, a vanishing point is an abstract point on the image plane where 2D projections (or drawings) of a set of parallel lines in 3D space appear to converge. The vanishing point may also be referred to as the “direction point”.

perspective-vanishing-point-1

Vanishing Point Perspective is used in Graphic editing and 3D video games. It can be used to render 3D shapes (3D Buildings and objects), add perspective to a background scene (road, train track) or add shadow effects.

The following slideshow, demonstrates the use of vanishing points in computer graphics:

You can read more about Vanishing Point Perspective on Wikipedia.

Vanishing Point Perspective using python Turtle


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 Python 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: , ,