Start making pygame games (1)

Source: Internet
Author: User
Start making pygame games (1)

Start to enter the world of Python

1. About this series

This will be a series of articles. It is my experience reading makeing games with Python & pygame. Since the book is in English, I can organize it into an article in Chinese for your convenience. The author also wrote another book "invent your own computer games", which focuses on the game programming on the command line interface and the basics of Python. The two books are posted on the author's website.

2 reader requirements

In general, this series of articles are not high-level articles. As Long As You Like programming and want to write simple 2D games (such as snakes and Tetris), there is no easy library to implement. This article introduces Python's pygame module as an option. A little Python basics are required. We will introduce it in detail later.

3. About "my" 1

I am just a college student and I like programming and pay attention to C ++ Python scheme Linux, but I like game programming best, as the author of makeing Games said:

  "There was a long gap after I first learned programming but didn't really know how to use that skill to make something cool."

Recently, pygame has been found to be a treasure. We also found the tutorial. So I started my research immediately. I hope you will feel the same after watching pygame.

4. About Python

Python is an object-oriented scripting language. The syntax is concise and clear. I have read and recommended "Python basics tutorial (version 2nd)" And later I will be able to read "Python cookbook" and "Python source code analysis". Python official website http://www.python.org can see a variety of Python information. The focus is on downloading Python documents. This series is strongly recommendedPracticeTherefore, we strongly recommend that you take a look at the basic tutorial and know how to run it.
The helloword script of the Python version is followed by this article. 3

5. Environment

All the programs in this series are tested only in my small notebook (opensuse 12.3, Python 2.7, pygame1.9.1), but in principle python is cross-platform. Pygame installation can refer to the http://www.pygame.org. Note that there are still some differences between Python 3.2 and Python 2.7. When downloading, you must check the version. The editor can use idle, but the editor I use is Emacs for programming and writing this series of articles. About Emacs
To configure the python environment, refer to search for python results in the http://www.emacswiki.org. Org-mode is used to write an article. The following is a screenshot of the program. There will be more in this series later. After all, you must have a picture to get the truth.

6. general process of this series

After briefly introducing the basic functions of pygame, you will enter the pygame mini-game instance. Compared with the Python language, it is more important to understand the game development methods in the instance. The following instances can be viewed separately.

7. Let's get started. 7.1 use the python interactive interface

Now let's assume that the python environment has been set up.

  • Enter python in the command line interface to enter the python interaction interface. Input:
1:  import pygame2:  pygame.version.ver

  • Get my original
Out[10]: '1.9.1release'

The above description shows that python and pygame are all normal. The prompt is different because I installed ipython. This is an enhanced version of Python interactive tool, which is very easy to use.

7.2 zen about Python

Let's do something interesting.

  • Enter import this in the python interactive interface to get the theme of Python, such as some design concepts. You can find out the specific Chinese meaning on the Internet.
1:  import this

  • Get my original
The Zen of Python, by Tim PetersBeautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.Complex is better than complicated.Flat is better than nested.Sparse is better than dense.Readability counts.Special cases aren't special enough to break the rules.Although practicality beats purity.Errors should never pass silently.Unless explicitly silenced.In the face of ambiguity, refuse the temptation to guess.There should be one-- and preferably only one --obvious way to do it.Although that way may not be obvious at first unless you're Dutch.Now is better than never.Although never is often better than *right* now.If the implementation is hard to explain, it's a bad idea.If the implementation is easy to explain, it may be a good idea.Namespaces are one honking great idea -- let's do more of those!

  • Interestingly, this module also intentionally hides the text. After importing this module, you can
1:  dir(this)  

  • Obtain the names under this module.
Out[2]: ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'c', 'd', 'i', 's']

  • You can enter the python interaction environment by name. S is the body, but it is encrypted. This. D is a dictionary ing relationship (note that this ing does not include spaces and carriage return only letters). Can it be decrypted by matching it? Continue entering in the interaction environment

  • Haha, it's really a Zen of Python

  • Further Exploration
1:  this.__file__

  • You can find out what files this module is. In this example, we can get the following information:
Out[6]: '/usr/lib/python2.7/this.pyc'

  • *. PyC is the result of Python script compilation, which can speed up execution. It is generally in the same directory as the source file. Immediately open the directory/usr/lib/python2.7/This. py. In front of the so-called source code, there is no secret ^_^

7.3 try a script

Next, let's try a classic quick sorting. Even if you don't know what the quick sorting is, it doesn't matter. The point is to see what the Python code will look like and how to run it.

  • Save the following script as quicksort. py
 1:   2:  #!/usr/bin/env python 3:   4:  import random 5:   6:  LENGTH = 10 7:  UPPER  = 100 8:  LOWER  = 0 9:  10:  def quicksort(lst):11:      if len(lst) >= 2:12:          lowerList = [x for x in lst[1:] if x <= lst[0]]13:          upperList = [x for x in lst[1:] if x > lst[0]]14:          return quicksort(lowerList) + lst[0:1] + quicksort(upperList)15:      return lst16:  17:  def main():18:      random.seed()19:      lst = [random.randint(LOWER, UPPER) for x in range(0, LENGTH)]20:      print lst21:      print quicksort(lst)22:  23:  if __name__ == "__main__":24:      main()25:  

  • Enter Python quicksort. py in the command line (not the python interactive interface) to run the command.
  • In this document, I got [53, 86, 34, 94, 99, 6, 90, 57, 37, 12] [6, 12, 34, 37, 53, 57, 86, 90, 94, 99]
7.4 check the examples of pygame.
  • On the pygame official website, download the HTML document of pygame.
  • Open the pygame document and find examples above. Some sample code has been packaged. Follow the instructions in the document and use the following command in the command line:
1:  python -m pygame.examples.<example name> <example arguments>

  • My stars running result
1:  python -m pygame.examples.stars

8 is over

This is the first time. Next time I enter chapter 2nd of makeing games, I will officially access some pygame functions.

Footnotes:

2. Check it out.

1 fatboy no matter whether it is pygame or this series of articles, you can email anything you want to say.

2 http://inventwithpython.com thanks to book author AI Sweigart

3 These books I found in Sina shared information http://ishare.iask.sina.com.cn

Author: fatboy <thisisafatboy@gmail.com>

Date:

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.