2015/11/1 use Python to write a game. Getting started with pygame (1): installation of pygame and pythonpygame

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.