Python-- project - drawn animation game 2-

Implement game loop as well as monitor events

In the last lecture you need to complete this such an effect, if you have not done, please do not read on! ! Remember remember remember. The important thing to say three times

We look at what is the game loop

The so-called good understanding of the game loop is, when the game will quit, pay attention to, ah, we do not understand the game is the reverse of the exit, then the game is not over, not over, it means that the game is running, is running the state

  • It ensures continuous operation of the game does not directly exit
  • There are a great help rendered image
  • Constantly check the event user interaction ...

Write us out of the game loop

++++
# 游戏循环
while True:
    print(i)
    i += 1
++++

We look at current games module

005_ game master module -w600

Our step by step, and now we have to set the game clock

The so-called game clock actually refers to the refresh rate

  • We define a special class 'in python pygame.time.Clockcan be very easily set the speed of screen drawing - refresh frame rate '
  • How to use it? You only need two steps
    1. In the game initialization to create a clock object
    2. In the game loop so that the clock object invocation tick(帧率)method
    3. Note: tickThe method will be based on the last time is called , automatically set the game cycle of delay
      you can carry out his so it
# 1. 创建游戏时钟对象-你不需要导入包,这个是python自带的,就像js里面的定时器
clock = pygame.time.Clock()
i = 0

# 游戏循环
while True:

    # 设置屏幕刷新帧率
    clock.tick(60)

    print(i)
    i += 1

Now we have to achieve the first airplane battle animation

Requirements Analysis: We need to get the aircraft rolling. Realization of the principle that in every rendering. The position of the image changes, thus completing our performance moving

  • Code logic Analysis

    1. In the game initialize a definition pygame.Rectof the initial position variable record Heroes
    2. In the game loop each time to make a hero of the y - 1- move up
    3. y <= 0 The hero is moved to the bottom of the screen
    4. Note: each call update()before the method, we need to all game images are redrawn again and should be the first to re-draw the background image , if you do not listen to my advice at your peril
  • Specific implementation

# 1. 定义英雄的初始位置
++++(在这之前我们已经定于了对象)
# 设置我们的初始化的绘制位置
hero_rect = pygame.Rect(150, 500, 102, 126)

while True:

    # 可以指定循环体内部的代码执行的频率
    clock.tick(60)

    # 2.更新英雄位置
    hero_rect.y -= 1

    # 3. 如果移出屏幕,则将英雄的顶部移动到屏幕底部
    if hero_rect.y <= 0:
        hero_rect.y = 700

    # 4.绘制背景图片
    screen.blit(bg, (0, 0))
    # 5.绘制英雄图像
    screen.blit(hero, hero_rect)

    # 6.更新显示//有关于整个窗口的更新 你需要放在最后
    pygame.display.update()

Huh? Our games are not stuck up?

You've noticed it, our business cycle seems to be an infinite loop, it is not over conditions

  • Requirements Analysis: when we need x number click on the window to exit the game
  • Code analysis:
    1. Here we need to use the event (event);
    2. Once the game starts we need to continue to monitor the occurrence of interaction events
    3. Once the decision to have 'quit' event when he ended our game unloading our bags
    4. pygameBy pygame.event.get()available user actions made the current list of events **, we can write a function to get into one of these events is the 'exit' event, according to it to exit the game
    5. Note that the following code is fixed, almost every game has such a pygame one line of code, their implementation principle is the same, do not worry it is not difficult
  • Code implementation
+++++(上面还有好多代码就忽略了,我们专注我们的核心业务需求)
while True:

    # 设置屏幕刷新帧率
    clock.tick(60)

    # 1.事件监听
    for event in pygame.event.get():

        # 2.判断用户是否点击了关闭按钮
        if event.type == pygame.QUIT:
            print("退出游戏...")
            # 卸载所有的依赖包
            pygame.quit()

            # 直接退出系统
            exit()

Guess you like

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