Python – Project – Games 2 – Animation

Time:2020-10-1

Realize the game cycle and event monitoring

Project source code

In the last lecture, you need to complete this effect. If you haven’t finished, please don’t read on!! Remember, remember, remember. Say important things three times

Let’s take a look at what a game cycle is

A good understanding of the so-called game cycle is when the game will exit. Note that we understand the game in reverse. If the game does not exit, then the game will not end. If the game does not end, it means that the game is running and running

  • It ensures that the game continues to run and does not exit directly
  • It is very helpful for the drawing of images
  • Constantly check user interaction events

Write our game cycle

++++
#Game cycle
while True:
    print(i)
    i += 1
++++

Let’s take a look at the current game modules

005_游戏主模块-w600

Let’s go step by step. Now let’s set the game clock

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

  • A special class’ is defined in Pythonpygame.time.ClockIt is very convenient to set the screen drawing speed——Refresh frame rate
  • How to use it? You only need two steps
    1. stayGame initializationCreate aClock object
    2. stayGame cycleLet the clock object callTick (frame rate)method
    3. explain:tickThe method will be based onLast called time, automatic settingGame cycleDelay in
      You can do it like this
#1. Create the game clock object - you don't need to import the package. This one is provided by python, just like the timer in JS
clock = pygame.time.Clock()
i = 0

#Game cycle
while True:

    #Set screen refresh frame rate
    clock.tick(60)

    print(i)
    i += 1

Now let’s implement the animation of the first main combat aircraft

Needs analysis: we need to get the plane moving. The principle of implementation is in every drawing. Change the position of the picture, so that we complete the moving effect

  • Code logic analysis

    1. stayGame initializationDefine apygame.RectTo record the initial position of the hero
    2. stayGame cycleEvery timeheroOfy - 1——Move up
    3. y <= 0Move the hero to the bottom of the screen
    4. Note: every callupdate()Before using theAll the game images are redrawnAnd it should beFirstRedrawbackground image If you don’t listen to my advice, you’ll be at your own risk
  • Specific implementation

#1. Define the initial position of the hero
++++(we've already set the target before that.)
#Set the drawing position of our initialization
hero_rect = pygame.Rect(150, 500, 102, 126)

while True:

    #You can specify how often code is executed inside the body of the loop
    clock.tick(60)

    #2. Update hero position
    hero_rect.y -= 1

    #3. If you move out of the screen, move the top of the hero to the bottom of the screen
    if hero_rect.y <= 0:
        hero_rect.y = 700

    #4. Draw background picture
    screen.blit(bg, (0, 0))
    #5. Draw the hero image
    screen.blit(hero, hero_rect)

    #6. Update display // there are updates about the whole window that you need to put at the end
    pygame.display.update()

Eh? Is our game stuck?

As you have noticed, our cycle of business seems to be an infinite cycle, and it has no conditions to end,

  • Needs analysis: we need to exit the game when we click the X on the window
  • Code analysis:
    1. Here we need to use event;
    2. Once the game starts, we need to constantly monitor the interactive events that happen
    3. As soon as we know that there is an “exit” event, we end our game and unload our package
    4. pygameThrough thepygame.event.get()AvailableCurrent action of userIn the * * event list, we can write a function to get one of the “exit” events and exit the game according to it
    5. Note that the following code is very fixed. Almost every pyGame game has such a line of code, and their implementation principles are the same. Don’t worry, it’s not difficult
  • Code implementation
+++++(there's a lot of code to ignore. We focus on our core business requirements.)
while True:

    #Set screen refresh frame rate
    clock.tick(60)

    #1. Event monitoring
    for event in pygame.event.get():

        #2. Judge whether the user has pressed the close button
        if event.type == pygame.QUIT:
            Print ("quit the game...")
            #Uninstall all dependent packages
            pygame.quit()

            #Exit system directly
            exit()

Recommended Today

The first Python Programming challenge on the Internet (end)

Date of establishment: March 28, 2020Update Date: April 22, 2020 (end)Personal collection Tool.pywebsite:http://www.pythonchallenge.com/Note: please quote or change this article at will, just mark the source and the author. The author does not guarantee that the content is absolutely correct. Please be responsible for any consequences Title: the first Python Programming challenge on the web Find […]