Informatics 9. Билингвальный учебник - [16]
fruit = [“b”, “n”, “n”, “”]
print “a”.join(fruit)
a) bnn
b) ba na na
c) banana
d) abanana
20. What is the value of num after this code runs?
shapes = [“triangle”, “square”, “hexagon”, “circle”, “pentagon”]
num = len(shapes)
a) 0
b) 4
c) 5
d) 35
CHAPTER 4. PROGRAMMING 2D GAMES
4.1 PYGAME LIBRARY
You will:
Install PyGame library;
use the PyGame library to create a window for the game.
What does ‘library’ mean in the case of programming languages?
What is PyGame?
PyGame (the library) is a Free and Open Source python programming language library for making multimedia applications. Pygame is highly portable and runs on nearly every platform and operating system.
PyGame makes it simple to:
- Draw graphic shapes
- Display bitmapped images
- Animate
- Interact with keyboard, mouse, and gamepad
- Play sound
- Detect when objects collide
How to install Pygame library
The best way to install pygame is with the pip tool (which is what python uses to install packages). Note, this comes with python in recent versions. We use the --user fl ag to tell it to install into the home directory, rather than globally.
py -m pip install -U pygame --user
To see if it works, run one of the included examples
py -m pygame.examples.aliens
If it works, you are ready to go!
The first code a Pygame program needs to do is load and initialize the Pygame library. Every program that uses Pygame should start with Importing and initializing Pygame:
# Import a library of functions called ‘pygame’
import pygame
# Initialize all imported pygame modules
pygame.init()
Simple Pygame Window
pygame.display.set_mode(resolution=(width, height))
This function will create a display Surface. The resolution argument is a pair of numbers representing the width and height.
Keep in mind
Important:
Don’t name any file “pygame.py”
The import pygame looks for a library file named pygame. If a programmer creates a new program named pygame.py, the computer will import that file instead! This will prevent any pygame programs from working until that pygame.py file is deleted
Practice 1
Create a simple PyGame window of 600 pixels in height and 400 pixels in width and run. What did you notice?
If you run the code above, a 600x400 pixel window will appear and close immediately.
Why does the window close immediately?
Because the program ends after the execution of these expressions. Neither init () nor set_mode () suggest cyclic event. That’s why you need to create a loop, causing the program to hang. And now, create a loop:
Example 1
run = True
while run:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
run = False # Close clicked so exit this loop
To set the title of the window (which is shown in the title bar) use the following line of code:
pygame.display.set_caption(“Title”)
Ending the Program
Right now, clicking the “close” button of a window while running this Pygame program in IDLE will still cause the program to crash. The problem is, even though the loop has exited, the program hasn’t told the computer to close the window. By calling the command below, the program will close any open windows and exit as desired.
pygame.quit() # Uninitialize all pygame modules
Practice 2
1. Create a simple PyGame window(495x120 pixels).
2. Set title “Cool game in PyGame“.
Literacy
1. What is the PyGame library?
2. What functions does PyGame provide?
3. What can you get if you use it?
Terminology
library - кітапхана - библиотека
package - пакет - пакет
collide – соқтығысу - сталкиваться
import – импорттау - импортировать
surface – беті - поверхность
initialize – инициализациялау - инициализировать
event – жағдай - событие
4.2 BACKGROUND IMAGE IN PYGAME
You will:
Set a background image.
What makes you want to play a 2d game?
How to create background
First of all, you need to add variables that define colors that you use in program. Colors are defined in a list of three colors: red, green, and blue.
As you remember, lists in Python are surrounded by either square brackets or parentheses. Individual numbers in the list are separated by commas. Below is an example that creates variables and sets them equal to lists of three numbers. These lists will be used later to specify colors.
# Define some colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
BLUE = ( 0, 0, 255)
Clearing the Screen
The following code clears whatever might be in the window with a white background. Remember that the variable WHITE was defined earlier as a list of 3 RGB values.
# Clear the screen and set the screen background
screen.fill(WHITE)
Keep in mind
To find RGB code of color you need you can use online color picker at
www.colorpicker.com
Flipping the Screen
Very important!
You must flip the display after you draw. The computer will not display the graphics as you draw them because it would cause the screen to flicker. This waits to display the screen until the program has finished drawing. The command below “flips” the graphics to the screen.
# Go ahead and update the screen with what we’ve drawn