Python programming from entry to practice--snip--

In the process of learning Python, you will encounter program code samples with –snip–,
such as setting the background color in 12.3.2 of the “Alien Invasion” project, and the–snip– that appears when you modify the code of alien_invasion.py again, as follows

--snip--
def run_game():
    --snip--
    pygame.display.set_caption("Alien Invasion")
    # 设置背景色
    bg_color = (230, 230, 230)
    # 开始游戏主循环.
    while True:
    # 监听键盘和鼠标事件
        --snip--
        # 每次循环时都重绘屏幕
        screen.fill(bg_color)
        # 让最近绘制的屏幕可见
        pygame.display.flip()
run_game()

At this point, compare the previous alien_invasion.py

import sys
import pygame
def run_game():
    # 初始化游戏并创建一个屏幕对象
    pygame.init() 
    screen = pygame.display.set_mode((1200, 800))
    pygame.display.set_caption("Alien Invasion")
    # 开始游戏的主循环
    while True:
        # 监视键盘和鼠标事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
               sys.exit()
        # 让最近绘制的屏幕可见
        pygame.display.flip()
run_game()

In fact, just add the original one at –snip–, as follows

import sys
import pygame      # 此处有个snip,补上原来的
def run_game():
    # 初始化游戏并创建一个屏幕对象
    pygame.init() 
    screen = pygame.display.set_mode((1200, 800))      # 此处有个snip,补上原来的
    pygame.display.set_caption("Alien Invasion")
    # 设置背景色
    bg_color = (230, 230, 230)
    # 开始游戏主循环.
    while True:
    # 监听键盘和鼠标事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
               sys.exit()                             # 此处有个snip,补上原来的
        # 每次循环时都重绘屏幕
        screen.fill(bg_color)
        # 让最近绘制的屏幕可见
        pygame.display.flip()
run_game()

That is to say, if –snip- appears, just replace the previous one.

Guess you like

Origin blog.csdn.net/frank_mercy/article/details/127708161