Initializing Pygame

When using Pygame, we first import it as a module:

import pygame

Then Pygame should set up a drawing area in a window. This can be done as follows:

screen = pygame.display.set mode((700, 1000))

This code specifically creates a window that is 700 x 1000 pixels and returns a handle to it as the variable screen. A handle is a variable that is used to access the drawing area. In other words, it is the connection to the drawing operations. To draw, we call a Pygame function And pass it the drawing area as a parameter, such as pygame.draw.line(screen, ….).

Now we can draw things onto the window display surface. They don’t show up on the screen right away, though. The system collects the changes to the draw­ing surface and draws them all at once when the programmer tells it to.

pygame.display.update()

Now everything that has been drawn to screen, it should be displayed. Noth­ing will be drawn until update() is called. The variable screen refers to what Pygame calls a Surface, and is a lot like an image in that it is a collection of rows and columns of pixels. The upper left pixel is the pair of coordinates (0,0), and the first coordinate represents the X or horizontal position.

1.  Colors

To start creating computer graphics, it is necessary to understand how colors and images are represented. When using a computer, everything must be repre­sented as numbers. A pixel is the color of a picture at a particular location, and so there must be a way to describe a color at that place. In physics, frequency is used: each color has a specific frequency of electromagnetic radiation. Unfortunately, this does not map very well onto a computer display, because monitors are based on television technology. On a TV, there are three colors, red, green, and blue, and these are used in various proportions to represent every color. There are red, green, and blue dots on the TV screen that are lit up to various degrees to create the colors that are seen. This is based on the way a human eye sees color; there are red, green, and blue sensors in the eye that in combination create our color perception. Another reason that frequency is not used is that there are colors that are not accurately represented as frequencies; they do not appear in the rainbow. The colors pink and brown are two examples.

Each color in the graphics system is represented as the degree of red, green, and blue that combine to create that color. In that sense, it is a bit like mixing paint. Yellow, on a computer, is a mixture of red and green. Each pixel has three components: a red, green, and blue component. These could be expressed as per­centages, but when using a computer, it is better to select numbers between 0 and 255 (8 bits or one byte) for each color. Each pixel requires 3 bytes of storage or 4 bytes in some cases, as will be seen shortly. If an image contains 100 rows of 100 pixels, then it has 10,000 pixels and is 10000*3=30000 bytes in size.

To humans, colors have names. Here’s a list of some named colors and their RGB equivalents:

There are, of course, a great many more named colors, and even more colors that can be represented with RGB values in this way (16,777,202 of them, in fact). Each pixel is a color value. All grey values have the special situation R=G=B, so there are 256 distinct values of grey ranging from black to white.

In summary, each pixel represents the color of the image or graphic at that point. A color is represented by the three color components (red, green, and blue), each having a value between 0 and 255. A color is a tuple. Thus, (0,0,0) is black, (255,255,255) is white, and (255, 0, 0) is red.

 

Source: Parker James R. (2021), Python: An Introduction to Programming, Mercury Learning and Information; Second edition.

Leave a Reply

Your email address will not be published. Required fields are marked *