The history of Pygame
??
Pygame is a game library using the SDL Library, SDL, full name Simple DirectMedia Layer, is a "Sam Lantinga" written by Daniel, It is said that he was to let Loki (dedicated to the Linux transplant Windows game of a big good company, unfortunately has been closed, alas, good people do not long-lived ah ... More effective work to create this stuff.
SDL is written in C, but it can also be developed using C + +, and there are many other languages, Pygame is a library used in Python. Pygame has been around for a lot of time, and many good programmers have joined in, making Pygame more and more good.
Installing Pygame
You can download Pygame from www.pygame.org, choose the right OS and the right version, then install it (what, you don't even have python?). You may not be suitable to see this series, but if you insist on learning, very good! Go to www.python.org and download it! )。 Once you have installed it, you can use the following method to confirm that there is no successful installation:
Python
>> Import Pygame
>> Print Pygame.ver
1.9.1release
1
2
3
>> Import Pygame
>> Print Pygame.ver
1.9.1release
Your version may be different from mine, it doesn't matter. The version of the book that I translated is still 1.7.1 ... So if there are outdated things out there, please don't hesitate to point them out!
If you say why you want to introduce such an "outdated" thing, the real knowledge is not outdated, only the technology will. Here is mainly rely on Pygame to introduce the game development of all aspects, not that we can rely on this to make what great game (of course, not to say NO)!
In addition, as far as products are concerned, Pygame is more committed to the development of 2D games, that is to say, you can write a plant vs. Zombies with Pygame, but writing a World of Warcraft is quite difficult ... Please do not despise the eyes, the underlying things are always the same, and for beginners, from the simple 2D is the way to go.
Using Pygame
Pygame has a lot of modules, here is a list:
Module name function
Pygame.cdrom Accessing the optical drive
Pygame.cursors Loading cursor
Pygame.display Accessing display Devices
Pygame.draw drawing shapes, lines, and points
Pygame.event Managing Events
Pygame.font using Fonts
Pygame.image Loading and storing pictures
Pygame.joystick using GamePad or something like that
Pygame.key reading keyboard keys
Pygame.mixer Sound
Pygame.mouse Mouse
Pygame.movie playing video
Pygame.music playing Audio
Pygame.overlay access to advanced video overlays
Pygame is the thing we are learning.
Pygame.rect Managing rectangular Areas
Pygame.sndarray Operating Sound data
Pygame.sprite Manipulating moving images
Pygame.surface managing images and screens
Pygame.surfarray Managing Bitmap Image data
Pygame.time managing time and frame information
Pygame.transform zooming and moving images
Some modules may not exist on some platforms and you can test them with none.
Python
If Pygame.font is None:
Print "The font module is not available!"
Exit ()
1
2
3
If Pygame.font is None:
Print "The font module is not available!"
Exit ()
The new Hello World
We always write a Hello World program at the beginning of the program, but that's just two words on the screen, now let's get a little more handsome! This will be the result when you write it well:
Python
#!/usr/bin/env python
Background_image_filename = ' sushiplate.jpg '
Mouse_image_filename = ' Fugu.png '
#指定图像文件名称
Import Pygame
#导入pygame库
From pygame.locals Import *
#导入一些常用的函数和常量
From sys import exit
#向sys模块借一个exit函数用来退出程序
Pygame.init ()
#初始化pygame, preparing for the use of hardware
Screen = Pygame.display.set_mode ((640, 480), 0, 32)
#创建了一个窗口
Pygame.display.set_caption ("Hello, world!")
#设置窗口标题
Background = Pygame.image.load (background_image_filename). CONVERT ()
Mouse_cursor = Pygame.image.load (mouse_image_filename). Convert_alpha ()
#加载并转换图像
While True:
#游戏主循环
for event in pygame.event.get(): if event.type == QUIT: #接收到退出事件后退出程序 exit()screen.blit(background, (0,0))#将背景图画上去x, y = pygame.mouse.get_pos()#获得鼠标位置x-= mouse_cursor.get_width() / 2y-= mouse_cursor.get_height() / 2#计算光标的左上角位置screen.blit(mouse_cursor, (x, y))#把光标画上去pygame.display.update()#刷新一下画面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
Background_image_filename = ' sushiplate.jpg '
Mouse_image_filename = ' Fugu.png '
#指定图像文件名称
Import Pygame
#导入pygame库
From pygame.locals Import *
#导入一些常用的函数和常量
From sys import exit
#向sys模块借一个exit函数用来退出程序
Pygame.init ()
#初始化pygame, preparing for the use of hardware
Screen = Pygame.display.set_mode ((640, 480), 0, 32)
#创建了一个窗口
Pygame.display.set_caption ("Hello, world!")
#设置窗口标题
Background = Pygame.image.load (background_image_filename). CONVERT ()
Mouse_cursor = Pygame.image.load (mouse_image_filename). Convert_alpha ()
#加载并转换图像
While True:
#游戏主循环
for event in pygame.event.get(): if event.type == QUIT: #接收到退出事件后退出程序 exit()screen.blit(background, (0,0))#将背景图画上去x, y = pygame.mouse.get_pos()#获得鼠标位置x-= mouse_cursor.get_width() / 2y-= mouse_cursor.get_height() / 2#计算光标的左上角位置screen.blit(mouse_cursor, (x, y))#把光标画上去pygame.display.update()#刷新一下画面
This app requires two images, which you can find at the end of this article, although you can also find two. For best results, the sushiplate.jpg of the background should have a 640x480 resolution, and the cursor's fugu.png should be approximately 80x80, and have an alpha channel (if you don't know what this is, download it ...). )。
Note: Comments in the code I use in Chinese, if the error is executed, can be deleted directly.
In the game I have written for each line of comments, in addition, if you intend to learn, it is strongly recommended that you enter it again instead of copy and paste!
Explain a little bit more about the important parts:
Set_mode will return a surface object that represents the window that appears on the desktop, the first of the three parameters is Ganso, which represents the resolution (must), the second is a flag bit, the specific meaning is shown in the table below, if you do not have any characteristics, specify 0, and the third is a color depth.
Flag bit function
Fullscreen creating a full-screen window
Doublebuf creates a "double-buffered" window that is recommended for use in Hwsurface or OpenGL
Hwsurface Create a hardware-accelerated window that must be used in conjunction with fullscreen
OpenGL creates an OpenGL-rendered window
Resizable create a window that can change size
Noframe Create a window with no borders
The CONVERT function converts the image data into surface objects, which should be done every time the image is loaded (in fact, because it is so often used, if you do not write pygame); Convert_alpha retains alpha compared to convert The channel information (which can be easily understood as a transparent part) so that our cursors can be irregular shapes.
The main loop of the game is an infinite loop until the user jumps out. What you do in this main loop is to constantly draw the background and update the cursor position, although the background is not moving, we still need to draw it every time, or the mouse over the position can not be restored to normal.
Blit is an important function, the first parameter is a surface object, and the second is the upper-left corner position. You must remember to update with update when you finish painting, otherwise the picture is dark.
This is the most general impression of the Pygame program, and then we will learn more deep-seated things, and we can really read all the sentences.
Two picture resources used in this session:
Background: sushiplate.jpg
Cursor: fugu.png
Writing games with Python and pygame 1th lesson