2015/11/1 writing games with Python, Pygame primer (1): Pygame installation

Source: Internet
Author: User

These two days of learning data structures and algorithms sometimes feel not as interesting as doing projects directly. Just finished the basic use of Python, now just hit the strike to do a small project.

As I have always wanted to make a game, I want to use Python to make a basic game. Search the information, see a very easy to use library, that is, Pygame.

Pygame Introduction

Pygame is a Python module built on the SDL (Simple DirectMedia Layer) and is a cross-platform module. SDL is written in C, can also be developed in C + +, and there are many languages, Pygame is a library used in Python.

Installing Pygame

Download from www.pygame.org, choose the appropriate system and Python version of the installation, 64-bit win system may encounter some difficulties, according to my experiment, using Win32-bit version of the Pygame library can be used on 64-bit computers. Specific installation encountered problems, the use of search engines can be resolved, not to repeat this.

If you have Pygame installed, you can use the following code to verify that it is installed on your own Python interpreter:

Import Pygame Print Pygame.ver1.9.1release

Of course, the version that appears may be different from mine, and there is not much difference. Once you have determined that the import is not a problem, you have the Pygame library.

Using Pygame

Pygame has many modules, the following is an overview:

Module Name function
Pygame.cdrom Accessing the optical drive
Pygame.cursors Load cursor
Pygame.display Accessing Display Devices
Pygame.draw Draw shapes, lines, and points
Pygame.event Manage 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 Voice
Pygame.mouse Mouse
Pygame.movie Play Video
Pygame.music Play Audio
Pygame.overlay Access Advanced Video Overlays
Pygame That's the thing we're learning.
Pygame.rect Managing rectangular Areas
Pygame.sndarray Manipulating sound data
Pygame.sprite Manipulate 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

All right, let's write a pygame version of hello,world!.

Here's the code:

#-*-Coding:utf8-*-ImportPygame#Import Pygame Library fromSysImportExit#borrow an exit function from the SYS module to exit the programPygame.init ()#Initialize Pygame, prepare for hardware use Screen= Pygame.display.set_mode ((300, 200), 0, 32)#A window was createdPygame.display.set_caption ("Hello, world!.")#Set Window caption  whileTrue:#Main Loop      forEventinchpygame.event.get ():ifEvent.type = =Pygame. QUIT:#exit the program after receiving the exit eventPygame.quit () exit () Screen.fill ((200,200,200))    #put the background picture uppygame.display.update ()#Refresh the screen .

After you enter and run this code, you will get a picture like this:

Clicking Close closes normally, and the title of the window becomes Hello, world!

In this procedure, a lot of new concepts, such as events, are mentioned. I will continue to explain these concepts later.

The code gives enough comments and believes that every friend who knows Python can read it.

If you are not in touch with Python or have not learned to program, you can read my blog before the "Python Core programming" study notes, you can also learn.

Python is a very comfortable language.

2015/11/1 writing games with Python, Pygame primer (1): Pygame installation

Related Article

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.