
Many tutorials on how to create a game in Scratch will be based on using the arrow keys to control the main sprite. This is a great approach indeed but is not suitable if you are designing the game toe be used on a tablet or smartphone. In this case, your input device to control the main sprite is not the keyboard but the touchscreen.
The following short tutorial will show you how you can control the movement of your sprite (in this case the cat) using a touchscreen. The idea will be to create two other sprites/buttons called Arrow_Left and Arrow_Right that, when clicked using a mouse or tapped using a touchscreen will tell the cat sprite to move in the relevant direction.
You can recreate this code online using the scratch website.
You should rename this sprite “Arrow_Right”.
You will then need to add the following code to this sprite to detect when the user clicks or taps on this sprite:
You will also need to change the costume of this sprite to show an arrow pointing to the left:
During the coronavirus pandemic, the rainbow has become a symbol of gratitude for the medical staff working in hospitals as well as all the other key workers who carried on working to provide essential services to their community.
All over the UK, children have drawn or painted colourful rainbows and have displayed them on their windows or front doors.
In this challenge, we will use some python code to draw a rainbow and a “Thank You” message to all medical staff and key workers.
This is what the output of our code should look like:
Could you:
The output of your code should be the same as the above picture.
The luminance of a colour is a measure used to describe the perceived brightness of a colour. It is possible to calculate the luminance of a colour based on its RGB colour code.
So let’s consider an algorithm that collects an RGB colour code as an input, using the (255,255,255) format. The algorithm will then convert these R, G, B values to a 0 to 1 scale as follows:
New R1,G1 B1 values are then calculated using the following rules:
Finally, the luminance value of the colour can be calculated using the following formula:
The reason such complex formulas are necessary to calculate the luminance (“perceived brightness”) of a colour is partly because our eyes’ perception of the brightness of a colour is not linear (it is not directly proportionate to the number of photons being emitted by a computer screen). Effectively our eyes perceive more levels of light in dim conditions, and less in brighter conditions.
You can use the trinket at the bottom of this page to write the Python code for this fucntion.
To calculate the contrast ratio between two colours (e.g. font/text colour and background colour) you must first calculate the luminance of both colours and identify which colour is the lightest colour and which colour is the darkest colour. You can then use these luminance values (labelled Llight and Ldark) to calculate the contrast ratio as follows:
Test # | Input Values | Expected Output | Pass/Fail? |
#1 | (255,255,255) (0,0,0) |
Luminance: 100% Luminance: 0% Contrast Ratio: 21 |
|
#2 | (148, 57, 173) (174, 84, 199) |
Luminance: 12.24% Luminance: 19.46% Contrast Ratio: 1.42 |
|
#3 | (148, 57, 173) (209, 157, 223) |
Luminance: 12.24% Luminance: 43% Contrast Ratio: 2.78 |
|
#4 | (148, 57, 173) (245, 232, 248) |
Luminance: 12.24% Luminance: 83.9% Contrast Ratio: 5.16 |
In this blog post we are investigating whether we can teach a computer how to perform a magic trick.
One of the key characteristic of an algorithm is that the instructions are given in a specific order, forming a sequence of instructions. When the computer will complete your algorithm, it will complete each instructions one instruction at a time in the order they appear on the algorithm. We call this sequencing, a key concept of computer algorithms.
When designing an algorithm, we can either write our instructions as a list (Pseudocode) or we can use a more visual representation of our algorithm using a flowchart.
Let’s see if the computer can perform this magic trick successfully:
Assembly language is a low-level programming language. Each assembly language is specific to a particular computer architecture. Assembly language uses mnemonics to represent low-level machine instructions or opcodes. Many operations require one or more operands in order to form a complete instruction. Most assembly languages let you use different modes of addressing to specify the value (immediate addressing) or location/memory address of an operand. Assembly languages also let you use labels in the code to point to specific memory locations or registers.
Because assembly depends on the machine code instructions, every assembler has its own assembly language which is designed for exactly one specific computer architecture.
Assembly language usually has one statement per machine instruction so there is the 1:1 relationship between a low-level instruction and a machine instruction. Basic computer architectures will result in a reduced number of instructions. More complex architectures will result in a wider range of instructions making it easier to write assembly code. (Find our more about RISC – Reduced Instruction Set – and CISC – Complex instruction Set – processors)
Unlike assembly language, most high-level programming languages are generally portable across multiple architectures but require interpreting or compiling, a much more complicated task than assembling.
In the early days of computer science (1950s, 1960s) computer programs could be “printed” using hole punched cards or tape. Our LMC assembler let you preview the resulting hole punched tape for your programs.
The radian is a unit of measure for angles used mainly in trigonometry. It is used as an alternative to degrees.
Whereas, in degrees a full circle is 360 degrees, in radians a full circle is 2π radians:
It is easy to convert an angle from degrees to radians and vice-versa using the following formulas:
To structure this program we will make use of 3 subroutines as follows:
We will then write an algorithm to complete this challenge, making use of our three subroutines.
The pygame library contains several modules and classes that you will use when creating video retro arcade games.
You will need to familiarise yourself with the most useful modules and classes as listed below: (Click on a module/class to access a description of the module/class)
Zoom in / Open pygame framework in new window
You can access the Pygame online documentation to gain a better understanding of the main functions/methods and attributes from this framework and access relevant code examples.
Modules/Classes | Description | Pygame Documentation |
![]() |
pygame.init() Initialise all imported pygame modules. pygame.quit() |
Access online documentation |
![]() |
Sprite() The base class for all the sprites of your game. Group() spritecollide() groupcollide() collide_mask() |
Access online documentation |
![]() |
alive Does this sprite belong to any group? (Boolean value) groups update() add() remove() kill() |
Access online documentation |
![]() |
sprites A list of all the sprites contained by this group add() remove() has() update() draw() clear() empty() |
Access online documentation |
![]() |
init() Initialise the display module. quit() set_mode() get_surface() flip() update() set_icon() set_caption() get_caption() get_window_size() |
Access online documentation |
![]() |
line() Draw a line on the specified surface. rect() polygon() circle() ellipse() |
Access online documentation |
![]() |
flip() Flip the specified horizontally and/or vertically. scale() rotate() |
Access online documentation |
![]() |
blit() Draw a specified image/surface on this surface. convert() convert_alpha() fill() set_colorkey() set_alpha() get_width() get_height() get_rect() get_at() |
Access online documentation |
![]() |
x,y Used to set or retrieve the x and y coordinates of the rectangle object. Other properties can also be used such as top, left, bottom, right, topleft, bottomleft, topright, bottomright, midtop, midleft, midbottom, midright, center, centerx, centery. width height copy() move() inflate() collidepoint() colliderect() collidelist() |
Access online documentation |
![]() |
render() Add text on a new surface. set_underline() set_bold() set_italic() |
Access online documentation |
![]() |
wait() Pause the program for a specified amount of milliseconds. delay() get_ticks() set_timer() Clock() |
Access online documentation |
![]() |
tick() Update the clock. Should be used once per frame. get_time() get_fps() |
Access online documentation |
![]() |
Event() Instantiate a new event object. get() wait() clear() |
Access online documentation |
![]() |
type The type of event such as QUIT, KEYDOWN, KEYUP, MOUSEMOTION, MOUSEBUTTONDOWN, MOUSEBUTTONUP, etc. key pos |
Access online documentation |
![]() |
get_pressed() Returns a list of Boolean values representing the state of all the mouse buttons. get_pos() set_pos() set_visible() set_cursor() |
Access online documentation |
![]() |
get_pressed() Returns a list of Boolean values representing the state of all the keyboard keys. get_mods() name() |
Access online documentation |
![]() |
load() Load a music file for playback. unload() play() rewind() pause() unpause() fadeout() stop() get_volume() set_volume() queue() |
Access online documentation |
![]() |
pre_init() Preset the mixer init arguments init() pause() unpause() fadeout() stop() quit() Sound() |
Access online documentation |
![]() |
play() Begin sound playback. stop() fadeout() set_volume() get_volume() get_length() |
Access online documentation |
In computer science, an abstract syntax tree (AST), or just syntax tree, is a tree representation of the high level source code.
The compilation process consists of translating the high level source code (e.g. Java, Python, C++, FORTRAN, etc.) into machine code. This process consists of 4 steps:
A list of tokens is generated during the lexical analysis of the compilation process.
An Abstract Syntax Tree (AST) is generated during the syntax analysis of the compilation process.
The following tool enables you to type/import some high level code (Javascript) in the first tab below and generate the list of tokens and the Abstract Syntax Tree corresponding to your code. You can see what the tree looks like using the JSON format (third tab) or as a visual representation (fourth tab).
Rear Admiral Grace Hopper, 1984
Grace Hopper had a key role in the development of computer science. She invented the concept of sub-routines to help programmers save time when writing code. Her work on developing high-level languages was also motivated by the need to be able to save time when writing programs, as writing low-level code can be extremely time consuming.
The first computer bug!
In this challenge we will use the Arduino board to control a RGB LED to create a gradient light effect where the LED will fade from red to purple, to blue, to purple and back to red.
You do not need to have access to an Arduino board. Instead you can use the online Arduino simulator from Tinkercad to recreate the electronic circuit and add the code provided below.
Open Tinkercad in new windowFor instance the RGB code for:
Check the following RGB Color picker to see how RGB codes work:
The purpose of the resistors is to limit the amperage in the circuit and hence protect the LED from receiving a too strong current (which could damage the LED). The resistors we use here are 100 ohms (Brown Black Brown rings).
// Arduino - RGB Gradient - www.101computing.net/arduino-rgb-gradient int redOut = 13; int greenOut = 11; int blueOut = 12; void setup() { pinMode(redOut, OUTPUT); pinMode(greenOut, OUTPUT); pinMode(blueOut, OUTPUT); } void setRGB(int red, int green, int blue) { analogWrite(redOut, red); analogWrite(greenOut, green); analogWrite(blueOut, blue); } void loop() { int red = 255; int green = 0; int blue = 0; for (int i = 0; i <= 255; i++) { blue = i; setRGB(red,green,blue); delay(20); } for (int i = 0; i <= 255; i++) { blue = 255 - i; setRGB(red,green,blue); delay(20); } }