Опубликован: 06.08.2013 | Доступ: свободный | Студентов: 783 / 11 | Длительность: 27:51:00
Лекция 2:

Pygame basics

< Лекция 1 || Лекция 2: 1234 || Лекция 3 >
Аннотация: GUI vs. CLI. Source Code for Hello World with Pygame. Setting Up a Pygame Program.
Ключевые слова: LIKE, python, with, additional, drawing, Graphics, handling, mouse, input, AND, this, chapter, cover, Basic, programming, if, trouble, CAN, read, computer game, book, online, FROM, online documentation, Write, built-up, text, screen, let, USER, keyboard, First, part, CLI, GET, function, even, enter, respond, waiting, GUI, OPEN, new, editor, clicking, idle, Menu, save, AS, Top, NOT, Line, black, MOST, video game, blank, step, graphical, Button, corner, end, keyboard input, output, look, lines of code, begin, statement, ALL, sound, MODULE, mixer, format, call, skip, portion, reason, form, constant variable, front, regular, information, function call, SEE, error message, CHECK, surface, Object, DESCRIBE, pass, tuple, Width, Height, correct, cause, error, short, caption, string, CONDITION, DUE, ITS, execution, exit, comment, WHERE, set, VALUES, State, player, health, Position, board, damage, point, prevent, response, mouse click, TIME, second, ONE, Actions, library, record, event, EXISTS, find, list, Last, ITERATE, iteration, NEXT, item, return, equal, constant, used, quit, terminate, hang, program termination, create, change, example, explain, entire, unchangeable, learning, color, composability, square, pixel, off, Grid, axis, cartesian coordinate system, column, row, integer, Locate, while, math, class, cartesian, programming language, cartesian coordinates, coordinate, computer, execute, difference, method, permanent, argument passing, method call, EXCEPT, constructor, capital, letter, lowercase letter, imported, parentheses, CASE, guarantee, safe, spamming, 2d image, border, title bar, draw, memory, still image, DVD, very, PER, FPS, frame rate, primitive, primary colors, light, RED, Green, blue, paint, maximum, amount, combination, try, negative number, store, result, Full, MIX, RGB, glass, tint, shade, mimic, measure, transparent, alpha, Add, half, transparency, invisible, blit, section, important, typing, interactive, shell, BIT, PARAMETER, Data, evaluate, Left, rectangle, right, EDGE, attribute, Calculated, reassign, recalculation, top side, individualization, source coding, Shape, size, DESCRIPTION, thickness, scene, testing, still, PNG, JPG, accessing, DELETE, error surface, Copy, animated, ROCK, animated image, split, continue, CAT, animation, picture, fraction, demonstration, require, image file, folder, download, available, go, common name, hertz, low, video, issue, OLD, help, game programming, fast, speed, length, pause, long, previous, Instruction, setting, able, load, GIF, BMP, separate, bottom, Font, font file, word, point size, info, render, aliasing, background, leave, technique, blur, computation, zoom in, smooth, filename, WAV, MP3, audio file, play, sample code, finish, slash, background music, argument, MIDI, looping, FLOAT, example code, course, focus, IDEA, free

Just like how Python comes with several modules like random, math , or time that provide additional functions for your programs, the Pygame framework includes several modules with functions for drawing graphics, playing sounds, handling mouse input, and other things.

This chapter will cover the basic modules and functions that Pygame provides and assumes you already know basic Python programming. If you have trouble with some of the programming concepts, you can read through the "Invent Your Own Computer Games with Python" book online at http://invpy.com/book . This book is aimed at complete beginners to programming.

The "Invent with Python" book also has a few chapters covering Pygame. You can read them online at http://invpy.com/chap17.

Once you learn more about Pygame, you can view the other modules that Pygame provides from the online documentation at http://pygame.org/docs.

GUI vs. CLI

The Python programs that you can write with Python's built-in functions only deal with text through the print() and input() functions. Your program can display text on the screen and let the user type in text from the keyboard. This type of program has a command line interface, or CLI (which is pronounced like the first part of "climb" and rhymes with "sky"). These programs are somewhat limited because they can't display graphics, have colors, or use the mouse. These CLI programs only get input from the keyboard with the input() function and even then user must press Enter before the program can respond to the input. This means real- time (that is, continuing to run code without waiting for the user) action games are impossible to make.

Pygame provides functions for creating programs with a graphical user interface, or GUI (pronounced, "gooey"). Instead of a text-based CLI, programs with a graphics-based GUI can show a window with images and colors.

Source Code for Hello World with Pygame

Our first program made with Pygame is a small program that makes a window that says "Hello World!" appear on the screen. Open a new file editor window by clicking on IDLE's File menu, then New Window. Type in the following code into IDLE’s file editor and save it as blankpygame.py. Then run the program by pressing F5 or selecting Run > Run Module from the menu at the top of the file editor.

Remember, do not type the numbers or the periods at the beginning of each line (that’s just for reference in this book).

1. import pygame, sys
2. from pygame.locals import *
3.
4. pygame.init()
5. DISPLAYSURF = pygame.display.set_mode((400, 300))
6. pygame.display.set_caption('Hello World!')
7. while True: # main game loop
8.     for event in pygame.event.get():
9.         if event.type == QUIT:
10.             pygame.quit()
11.             sys.exit()
12.     pygame.display.update()

When you run this program, a black window like this will appear:

Black window

Рис. 2.1. Black window

Yay! You've just made the world’s most boring video game! It’s just a blank window with "Hello World!" at the top of the window (in what is called the window’s title bar, which holds the caption text). But creating a window is the first step to making graphical games. When you click on the X button in the corner of the window, the program will end and the window will disappear.

Calling the print() function to make text appear in the window won't work because print() is a function for CLI programs. The same goes for input() to get keyboard input from the user. Pygame uses other functions for input and output which are explained later in this chapter. For now, let's look at each line in our "Hello World" program in more detail.

Setting Up a Pygame Program

The first few lines of code in the Hello World program are lines that will begin almost every program you write that uses Pygame.

1. import pygame, sys

Line 1 is a simple import statement that imports the pygame and sys modules so that our program can use the functions in them. All of the Pygame functions dealing with graphics, sound, and other features that Pygame provides are in the pygame module.

Note that when you import the pygame module you automatically import all the modules that are in the pygame module as well, such as pygame.images and pygame.mixer.music. There's no need to import these modules-inside-modules with additional import statements.

2. from pygame.locals import *

Line 2 is also an import statement. However, instead of the import modulename format, it uses the from modulename import * format. Normally if you want to call a function that is in a module, you must use the modulename.functionname() format after importing the module. However, with from modulename import *, you can skip the modulename. portion and simply use functionname() (just like Python's built-in functions).

The reason we use this form of import statement for pygame.locals is because pygame.locals contains several constant variables that are easy to identify as being in the pygame.locals module without pygame.locals in front of them. For all other modules, you generally want to use the regular import modulename format. (There is more information about why you want to do this at http://invpy.com/namespaces).

4. pygame.init()

Line 4 is the pygame.init() function call, which always needs to be called after importing the pygame module and before calling any other Pygame function. You don't need to know what this function does, you just need to know that it needs to be called first in order for many Pygame functions to work. If you ever see an error message like pygame.error: font not initialized, check to see if you forgot to call pygame.init() at the start of your program.

5. DISPLAYSURF = pygame.display.set_mode((400, 300))

Line 5 is a call to the pygame.display.set_mode() function, which returns the pygame.Surface object for the window. (Surface objects are described later in this chapter.) Notice that we pass a tuple value of two integers to the function: (400, 300). This tuple tells the set_mode() function how wide and how high to make the window in pixels. (400, 300) will make a window with a width of 400 pixels and height of 300 pixels.

Remember to pass a tuple of two integers to set_mode(), not just two integers themselves. The correct way to call the function is like this: pygame.display.set_mode((400, 300)). A function call like pygame.display.set_mode(400, 300) will cause an error that looks like this: TypeError: argument 1 must be 2-itcode sequence, not int

The pygame.Surface object (we will just call them Surface objects for short) returned is stored in a variable named DISPLAYSURF.

6. pygame.display.set_caption('Hello World!')

Line 6 sets the caption text that will appear at the top of the window by calling the pygame.display.set_caption() function. The string value 'Hello World!' is passed in this function call to make that text appear as the caption:

Caption

Рис. 2.2. Caption

Game Loops and Game States

7. while True: # main game loop
8.     for event in pygame.event.get():

Line 7 is a while loop that has a condition of simply the value True This means that it never exits due to its condition evaluating to False The only way the program execution will ever exit the loop is if a break statement is executed (which moves execution to the first line after the loop) or sys.exit() (which terminates the program). If a loop like this was inside a function, a return statement will also move execution out of the loop (as well as the function too).

The games in this book all have these while True loops in them along with a comment calling it the "main game loop". A game loop (also called a main loop) is a loop where the code does three things:

  1. Handles events.
  2. Updates the game state.
  3. Draws the game state to the screen.

The game state is simply a way of referring to a set of values for all the variables in a game program. In many games, the game state includes the values in the variables that tracks the player's health and position, the health and position of any enemies, which marks have been made on a board, the score, or whose turn it is. Whenever something happens like the player taking damage (which lowers their health value), or an enemy moves somewhere, or something happens in the game world we say that the game state has changed.

If you've ever played a game that let you saved, the "save state" is the game state at the point that you've saved it. In most games, pausing the game will prevent the game state from changing.

Since the game state is usually updated in response to events (such as mouse clicks or keyboard presses) or the passage of time, the game loop is constantly checking and re-checking many times a second for any new events that have happened. Inside the main loop is code that looks at which events have been created (with Pygame, this is done by calling the pygame.event.get() function). The main loop also has code that updates the game state based on which events have been created. This is usually called event handling.

pygame.event.EventObjects

Рис. 2.3. pygame.event.EventObjects

Any time the user does one of several actions (they are listed later in this chapter) such as pressing a keyboard key or moving the mouse on the program's window, a pygame.event.Event object is created by the Pygame library to record this "event". (This is a type of object called Event that exists in the event module, which itself is in the pygame module.) We can find out which events have happened by calling the pygame.event.get() function, which returns a list of pygame.event.Event objects (which we will just call Event objects for short).

The list of Event objects will be for each event that has happened since the last time the pygame.event.get() function was called (Or, if pygame.event.get() has never been called, the events that have happened since the start of the program).

7. while True: # main game loop
8.     for event in pygame.event.get():

Line 8 is a for loop that will iterate over the list of Event objects that was returned by pygame.event.get() On each iteration through the for loop, a variable named event will be assigned the value of the next event object in this list. The list of Event objects returned from pygame.event.get() will be in the order that the events happened. If the user clicked the mouse and then pressed a keyboard key, the Event object for the mouse click would be the first item in the list and the Event object for the keyboard press would be second. If no events have happened, then pygame.event.get() will return a blank list.

The QUIT Event and pygame.quit() Function

9.         if event.type == QUIT:
10.             pygame.quit()
11.             sys.exit()

Event objects have a member variable (also called attributes or properties) named type which tells us what kind of event the object represents. Pygame has a constant variable for each of possible types in the pygame.locals modules. Line 9 checks if the Event object's type is equal to the constant QUIT Remember that since we used the from pygame.locals import * form of the import statement, we only have to type QUIT instead of pygame.locals.QUIT.

If the Event object is a quit event, then the pygame.quit() and sys.exit() functions are called. The pygame.quit() function is sort of the opposite of the pygame.init() function: it runs code that deactivates the Pygame library. Your programs should always call pygame.quit() before they call sys.exit() to terminate the program. Normally it doesn't really matter since Python closes it when the program exits anyway. But there is a bug in IDLE that causes IDLE to hang if a Pygame program terminates before pygame.quit() is called.

Since we have no if statements that run code for other types of Event object, there is no event- handling code for when the user clicks the mouse, presses keyboard keys, or causes any other type of Event objects to be created. The user can do things to create these Event objects but it doesn't change anything in the program because the program does not have any event-handling code for these types of Event objects. After the for loop on line 8 is done handling all the Event objects that have been returned by pygame.event.get(), the program execution continues to line 12.

12.     pygame.display.update()

Line 12 calls the pygame.display.update() function, which draws the Surface object returned by pygame.display.set_mode() to the screen (remember we stored this object in the DISPLAYSURF variable). Since the Surface object hasn't changed (for example, by some of the drawing functions that are explained later in this chapter), the same black image is redrawn to the screen each time pygame.display.update() is called.

That is the entire program. After line 12 is done, the infinite while loop starts again from the beginning. This program does nothing besides make a black window appear on the screen, constantly check for a QUIT event, and then redraws the unchanged black window to the screen over and over again. Let's learn how to make interesting things appear on this window instead of just blackness by learning about pixels, Surface objects, Color objects, Rect objects, and the Pygame drawing functions.

< Лекция 1 || Лекция 2: 1234 || Лекция 3 >
Константин Шилов
Константин Шилов
Россия
Павел Лафицкий
Павел Лафицкий
Украина