2015/11/1 use Python to write a game. Getting started with pygame (1): installation of pygame and pythonpygame
Learning data structures and algorithms over the past two days is sometimes not as interesting as directly working on projects. I just learned how to use python. Now I am just making a small project.
Since I have always wanted to create a game, I want to use Python to create a basic game. I searched the information and found a very easy-to-use library, namely pygame.
Pygame Introduction
Pygame is a Python module built on SDL (Simple DirectMedia Layer) and a cross-platform module. SDL is written in C and can be developed in C ++. There are also many languages. pygame is a library used in Python.
Install pygame
Download from www.pygame.org and select your own system and Python version for installation. The 64-bit win system may encounter some difficulties. According to my experiment, the win32 pygame library can be used on 64-bit computers. If you encounter any problems during installation, you can use search engines to solve them.
If you have installed pygame, you can use the following code to verify the installation on your Python Interpreter:
>>> import pygame>>> print pygame.ver1.9.1release
Of course, the version may be different from me, and there is no big difference. If there is no import problem, the pygame library is available.
Use pygame
Pygame has many modules. The following is an overview:
Module name |
Function |
Pygame. cdrom |
Access the optical drive |
Pygame. cursors |
Load cursor |
Pygame. display |
Access Display Device |
Pygame. draw |
Draw shape, line, and point |
Pygame. event |
Manage events |
Pygame. font |
Font |
Pygame. image |
Attach and store images |
Pygame. joystick |
Use a game handle or something similar |
Pygame. key |
Read keyboard buttons |
Pygame. mixer |
Sound |
Pygame. mouse |
Mouse |
Pygame. movie |
Play video |
Pygame. music |
Play audio |
Pygame. overlay |
Access advanced video Overlays |
Pygame |
This is what we are learning ...... |
Pygame. rect |
Manage Rectangular areas |
Pygame. sndarray |
Operation sound data |
Pygame. sprite |
Operate mobile Images |
Pygame. surface |
Manage images and screens |
Pygame. surfarray |
Manage dot matrix image data |
Pygame. time |
Manage time and frame information |
Pygame. transform |
Scaling and moving images |
Okay, let's write a Hello, world version of pygame!
The following code is used:
#-*-Coding: utf8-*-import pygame # import the pygame library from sys import exit # use an exit function to exit the pygame program from the sys module. init () # initialize pygame to prepare for using hardware screen = pygame. display. set_mode (300,200), 0, 32) # create a window pygame. display. set_caption ("Hello, World! ") # Set the window title while True: # Main Loop for event in pygame. event. get (): if event. type = pygame. QUIT: # exit the pygame program after receiving the exit event. quit () exit () screen. fill (200,200,200) # upload the background image to pygame. display. update () # refresh the screen
After entering and running this code, you will get the following picture:
Clicking close will close normally, and the title of the window will change to Hello, World!
Many new concepts, such as events, are mentioned in this program. I will continue to describe these concepts later.
The code is annotated with enough comments. I believe that everyone familiar with Python can understand them.
If you are not familiar with Python or have not studied programming, you can read the notes on "Python core programming" in my blog or learn it by yourself.
Python is a very comfortable language.