Alien invasion armed spaceship (1)

Table of contents

Table of contents

Preface

mind Mapping

1. Planning projects

2. Install pygame

3. Start the game project

3.1. Create Pygame window and respond to user input

3.1.1. Import module

3.1.2. Create game running function and initialization screen 

3.1.3. Set game name 

3.1.4. Start the main loop of the game

3.1.5. Continuously update the screen to create a smooth effect

 3.1.6, run the game

3.1.7, complete code

4. Summary


Preface

  After several months of study, I have some experience in learning Python. I want to train and evaluate my development capabilities through several slightly larger projects. I will start learning and practicing Python programming from entry to practice today. Projects in this book.

  In this project, I will interpret some of the methods and structures used in the project, and what I learn is my own.

mind Mapping

1. Planning projects

                            This is the background of the game and the general details of the game.

    In the game Alien Invasion, the player controls a spaceship that initially appears in the bottom center of the screen. player
You can use the arrow keys to move the ship left and right, and the space bar to shoot. When the game starts, a group of aliens appear
Now in the sky, they move down the screen. The player's task is to shoot these aliens. Players will all aliens
After everyone is eliminated, a new group of aliens will appear, and they will move faster. As long as an alien hits
If the player's ship reaches the bottom of the screen, the player loses a ship. The game ends after the player loses three ships.
In the first phase of development, we will create a ship that can move left and right and launch when the user presses the space bar.
fire. With this behavior set up, we were able to turn our attention to the aliens and improve the playability of the game.  
  In the first phase of development, we will create a ship that can move left and right and launch when the user presses the space bar.
fire. With this behavior set up, we were able to turn our attention to the aliens and improve the playability of the game.

2. Install pygame

Here we use

pip install pygame

http://t.csdn.cn/sPwIvhttp://t.csdn.cn/sPwIv

    If you are using pygame, you can install it according to the installation method in an article I wrote in the link above.

3. Start the game project

3.1. Create Pygame window and respond to user input

3.1.1. Import module

First, we import the module             first , and the usage content corresponding to the module has been written out in comments.

# 创建 Pygame 窗口以及响应用户输入
import sys  # 使用sys模块进行退出游戏
import pygame  # 我们整个项目的重要模块

3.1.2. Create game running function and initialization screen 

       Next we create a function to run the game , and then initialize and create the screen object 

# 定义一个运行游戏的函数
def run_game():
    # 初始化游戏并创建一个屏幕对象
    pygame.init()  # 初始化背景设置,让Pygame能够正确地工作

Next there is an important knowledge point,

  The object screen is a surface (Surface is an object used to draw images. It represents a visible area on which drawing operations can be performed and displayed on the screen. Surface objects can be windows, pictures, text or other Visual elements. ) In Pygame, a surface is the part of the screen used to display game elements. In this game, every element (such as an alien or a spaceship) is a surface. The surface returned by display.set_mode() represents the entire game window. After we activate the game's animation loop, the surface will be automatically redrawn every time it passes through the loop.


    # 创建一个名为screen的显示窗口,实参(1200, 800)是一个元组,指定了游戏窗口的尺寸,宽1200像素,高800像素
    screen = pygame.display.set_mode((1200, 800))

3.1.3. Set game name 

                                             It’s the application name in the upper left corner of the game

 

pygame.display.set_caption("外星人入侵")

3.1.4. Start the main loop of the game

 # 开始游戏的主循环
    while True:  # 永真使得游戏一直执行

        # 监听键盘和鼠标事件
        for event in pygame.event.get():  # 使用方法pygame.event.get()。所有键盘和鼠标事件都将促使for循环运行
            if event.type == pygame.QUIT:  # 如果玩家单机游戏窗口关闭按钮,则将检测到pygame.QUIT事件
                sys.exit()  # 坚持到事件后,退出游戏

Here are just a few lines of code to explain:

The first line: Let the game continue to loop first. The first function is to keep monitoring, and the second is the line that has not been shown yet.

The second line, loops to obtain the keyboard and mouse key operations, and loops to the event.

In the third line, if the event type is click to close, then call the exit() method in the sys module to exit the game.

3.1.5. Continuously update the screen to create a smooth effect

   Pygame command to make the most recently drawn screen visible. Here it draws an empty screen every time the while loop is executed and erases the old screen so that only the new screen is visible . As we move game elements, pygame.display.flip() will continuously update the screen to show the element's new position and hide the element in its original position, creating a smooth moving effect. 


        # 让最近绘制的屏幕可见
        pygame.display.flip()

 3.1.6, run the game

Call the run game function


# 运行游戏
run_game()

Effect after running

3.1.7, complete code

# 创建 Pygame 窗口以及响应用户输入
import sys  # 使用sys模块进行退出游戏
import pygame


# 定义一个运行游戏的函数
def run_game():
    # 初始化游戏并创建一个屏幕对象
    pygame.init()  # 初始化背景设置,让Pygame能够正确地工作
    # 创建一个名为screen的显示窗口,实参(1200, 800)是一个元组,指定了游戏窗口的尺寸,宽1200像素,高800像素
    screen = pygame.display.set_mode((1200, 800))
    """对象screen是一个surface(Surface 是一个用于绘制图像的对象。它表示一个可见的区域,可以在其上进行绘制操作,并将其显示在屏幕上。Surface 对象可以是窗口、图片、文本或其他可视元素。)
    。在Pygame中,surface是屏幕的一部分,用于显示游戏元素。在这个游戏中,每个元素(如外星人或飞船)都是一个surface。display.set_mode()返回的surface表
示整个游戏窗口。我们激活游戏的动画循环后,每经过一次循环都将自动重绘这个surface。"""
    pygame.display.set_caption("外星人入侵")

    # 开始游戏的主循环
    while True:  # 永真使得游戏一直执行

        # 监听键盘和鼠标事件
        for event in pygame.event.get():  # 使用方法pygame.event.get()。所有键盘和鼠标事件都将促使for循环运行
            if event.type == pygame.QUIT:  # 如果玩家单机游戏窗口关闭按钮,则将检测到pygame.QUIT事件
                sys.exit()  # 坚持到事件后,退出游戏

        # 让最近绘制的屏幕可见
        pygame.display.flip()
        """命令Pygame让最近绘制的屏幕可见。在这里,它在每次执行while循环时都绘制一个空屏幕,并擦去旧屏幕,使得只有新屏幕可见。在我们移动游戏元
素时,pygame.display.flip()将不断更新屏幕,以显示元素的新位置,并在原来的位置隐藏元素,从而营造平滑移动的效果。"""


# 运行游戏
run_game()

                    At this point, we have completed creating the window and responding to user input.

4. Summary

This production mainly uses the method functions in pygame to create windows, and obtains keyboard and mouse events to promote the game. For further operations, the sys module method is used to exit the game, and a framework for running the game is built. Next, we will proceed step by step until the entire project is completed.

A word a day

Although it is very difficult now, this experience may bear fruit in the future.

  If the notes of my study project are useful to you, you might as well like and save them. Thank you for your support. Of course, you are also welcome to give me suggestions or supplement the shortcomings in the notes. It will be of great help to my study. Thank you.  

Guess you like

Origin blog.csdn.net/weixin_72543266/article/details/132517009