Python-- project - small game

Our project began World War II aircraft

Early experience as well as preparation of project 1

  1. Experience the beginning of the game screen

  1. Verify local third-party packages have not imported
  python3 -m pygame.examples.aliens
  如果没有出现游戏画面请先安装这个包
  sudo pip3 install pygame
  1. Began to build the directory structure
    • Create a main program to import material picture, the new name is: "The plane Wars' project
    • Establish the main programmer ganme_main.py
    • The material pictures are imported into the project folder
  2. Game logic
    • The main logic is to determine a series of pictures of a moving collision, resulting in different events, which produce different results

2. Background and start drawing window

  1. First, we have to note two important pygame method is a method to initialize and unloading
import pygame

pygame.init()
#游戏代码
pygame,quit(),卸载所有的pygame模块 在游戏结束之前调用!如何才是真正的结束游戏

2. pygame中的 游戏中的坐标系?
     - 简单的结束一下这种属性

![](https://img2020.cnblogs.com/blog/1547034/202003/1547034-20200316235513358-1507055261.png)

     

     - pygame中专门准备了一个类Rect用于描述矩形区域,在本项目中,所有的图片都是Rect的创建出来的矩形对象。注意:即使你不初始化它 你也一样可以使用它 ,注意这个类你需要传入坐标值 还有其大小 

![](https://img2020.cnblogs.com/blog/1547034/202003/1547034-20200316235606639-557997315.png)


    ```python
    juxin = pygame.Rect(100,50,120,12)
    返回的就是/
    print ( '%d %d' %juxin.size )//获取的是大小,......具体的属性还有返回值请去查看api文档size是一个元祖属性
  1. How to create a game out of the window?
    • By the same token, we also offer a special pygame module to handle, display module. Under this method the two modules may draw game window
    • Sample Code
    注意一下这个方法返回的是一个窗口。窗口默认与屏幕一样大。为此我们可以来设置一些
    +++
    第一个参数类型是元祖(),其它的两个参数就不管它了。如果你感兴趣就去查阅python文档
     screen =  pygame.dispaly.set_mode( (480,700), )
     while True:
        pass
    +++
    ```
  2. How to draw up the picture window?
    • Specific core is:
      1. Load picture data pygame.image.load ( "graphics directory"), into memory
      1. Blit using the method of the screen object to get the specified position of FIG.
      1. Use pygame.dispaly.update () to update the entire display screen,
        the core code examples
    +++
    bg = pygame.image.load('./images/background.jpg')
    screen.bit(bg,(0,0))
    pygame.dispaly.update()
    +++
  3. Draw another picture?
    • In fact, this is relatively easy to understand, you can go to draw graphics on the background before the draw. Note Note that all images should be rectangular. Different pictures just set up some transparent, if you are not well understood. You can look at this chart
      igm, the code does not knock

3. source code analysis

  1. If we have more than N picture you want to paint it?
    • In fact, I can re-do all the drawing axis again call pygame.dispaly.update ()
    • Objects in the source code screen is a screen data object in memory, it can be understood as a canvas,
    • screen.blit can then draw a lot on the canvas graphics, these graphics may occur coverage, we draw in virtual memory
    • Finally, we go up again updated. In order to improve performance dispaly.update ()

4. The principle Animation

原理就是 动画帧 ,说白了就是快速的图片变化。利用视觉差搞出动画效果
- 在源代码中 ,update方法实际上就产生了一帧,而要完成连贯的动画 需要最少60帧/s
- 

```

Guess you like

Origin www.cnblogs.com/BM-laoli/p/12508085.html