Python - Project - Game 3 - Wizard?Blue elves?

Keywords: Python Windows

Wait, it looks like we've just drawn a moving picture

I don't know if you found it. It seems like we've just drawn a moving picture, just a picture!!!That's all the code. If there are 100 10000 + pieces?So we're so tall?

Don't worry, pygame offers us a solution--the elves and the elves

Spirit?Elf group?Blue elves?Pikachu?

Spirit

The object that displays images in game development is the genie

  1. Don't worry. Let's take a look. It's really a class. Here's its class diagram

  1. Effect:
    • pygame.sprite.Sprite - A ** object that stores image data images and location rect s
    • pygame.sprite.Group, which stores objects previously created by pygame.sprite.Sprite and draws them uniformly in the main window program
  2. Analyse the composition of this class
    • The elves need two important attributes
    • Image to be displayed, rect image to be displayed on the screen
    • The default update() method does nothing, and subclasses can override it to update the elf location each time the screen is refreshed
  3. Be careful of the pits!
    • pygame.sprite.Sprite does not provide image and rect attributes
    • Programmers are required to derive subclasses from pygame.sprite.Sprite
    • And set the image and rect properties in the initialization method of the subclass

sprite groups

  1. A group of elves can contain multiple elf objects
  2. Call the update() method of the elf group object
    • You can automatically call the update() method for each elf in the group
  3. Call the draw (screen object) method of the elf group object
    • You can draw the image of each elf in the group at the rect position
  4. Generally speaking, unified command and unified action, but each item has its own independent movement method and attributes
Group(*sprites) -> Group

Instance code:

# 1. New `plane_sprites.py'file
# 2. Definition `GameSprite` inherits from `pygame.sprite.Sprite`
import pygame

# 3. Written in parentheses means inheriting the parent class
class GameSprite(pygame.sprite.Sprite):
    """The game genie in the airplane battle, according to design UML Write code"""
#Note that to override the init method here, we pass in the init initialization method (row function)
    def __init__(self, image_name, speed=1):

        # Call the initialization method of the parent class, and be sure to call the super() object to invoke the initialization inint method of the parent class when our parent is not an object base class
        super().__init__()

        # Define the properties of the object, which record the elf's position speed and movement
        # Load Image
        self.image = pygame.image.load(image_name)
        # Set Size
        self.rect = self.image.get_rect()
        # Recording speed
        self.speed = speed

    def update(self):

        # Move vertically on the screen
        self.rect.y += self.speed

How do we use them in our previous host program?

What?You don't know what our main program is?Okay, I forgot to tell you that our main program is the one we wrote to draw windows before.

It's actually very simple, you just need to import the packages in the main game program
How do I finish adding enemy sprites, make them move, and put them in the game loop?

  1. Be careful:
Let's first clarify the division of work between the elves and the elves
 1. Elves
    *Encapsulation**image**,**Location rect** and**speed**
    *Provide `update()` method to **update location rect** according to game requirements
 2. Elf Group
    *Contains ** Multiple ** ** Elf Objects **
    * `update` method to have all the elves in the elf group call the `update` method to update location
    * `draw(screen)` method, draws all the elves in the elf group on `screen`
  1. Complete code samples
import pygame
# You need to import forms and import to use inprom imports. To use forms, use the tools provided by the module directly.
# They both implement the import module functionality
from plane_sprites import *


# Initialization of the game
pygame.init()

# Create Game Window 480 * 700
screen = pygame.display.set_mode((480, 700))

# Draw background image
bg = pygame.image.load("./images/background.png")
screen.blit(bg, (0, 0))
# pygame.display.update()

# Draw a Hero's Airplane
hero = pygame.image.load("./images/me1.png")
screen.blit(hero, (150, 300))

# update method can be called uniformly after all drawing is done
pygame.display.update()

# Create Clock Object
clock = pygame.time.Clock()

# 1. Define rect to record the initial position of the aircraft
hero_rect = pygame.Rect(150, 300, 102, 126)



# Start our business logic
# Create enemy elves
# Enemy fighter object
enemy = GameSprite("./images/enemy1.png")
enemy1 = GameSprite("./images/enemy1.png", 2)
# Creating an enemy plane's elf group, we can use multiple values to transfer the elf combination, and with this elf group, we can use the elf method directly, drawing all the images at once
enemy_group = pygame.sprite.Group(enemy, enemy1)

    # # Let the elf group call two methods
    # # Update - Let all the elves in the group update their location, this is the update location
    # enemy_group.update()

    # # draw - Draws all the Wizards on screen, this is drawing
    # enemy_group.draw(screen)
    # We just called it once, and now we throw it into our game loop

# Game Cycle - > means the official start of the game!
while True:

    # You can specify how often code inside a loop executes
    clock.tick(60)

    # Listen for events
    for event in pygame.event.get():

        # Determine if the event type is an exit event
        if event.type == pygame.QUIT:
            print("Game Exit...")

            # quit uninstalls all modules
            pygame.quit()

            # exit() directly terminates the currently executing program
            exit()

    # 2. Modify the position of the aircraft
    hero_rect.y -= 1

    # Judging the position of an aircraft
    if hero_rect.y <= 0:
        hero_rect.y = 700

    # 3. Call blit method to draw image
    screen.blit(bg, (0, 0))
    screen.blit(hero, hero_rect)

    # Let the elf group call two methods
    # Update - Let all the elves in the group update their location, this is the update location
    enemy_group.update()

    # draw - Draws all the Wizards on screen, this is drawing
    enemy_group.draw(screen)


    # 4. Call the update method to update the display
    pygame.display.update()

pygame.quit()

Posted by sapna on Sun, 22 Mar 2020 22:09:06 -0700