The use of Sprite class in Pygame 2_Cotton Monkey's Blog-CSDN blog mentioned that the movement of a zombie can be achieved through the custom class Zombie derived from the pygame.sprite.Sprite class. The management of multiple Zombie class instances can be achieved through the pygame.sprite.Group class, that is, the movement of multiple zombies can be achieved.
1 Introduction to pygame.sprite.Group class
The pygame.sprite.Group class is a container that can contain multiple instances of the Sprite class. Sprite instances in the Group class can be drawn and updated in the same way, which simplifies the code.
2 Creation of pygame.sprite.Group class instance
zombie_group = pygame.sprite.Group()
Among them, zombie_group is the instance of the created Group class.
3 Add Sprite to Group
First, you need to modify the use of the Sprite class in Pygame 2_Mianhou's blog - the code of the Zombie class in the CSDN blog. Because you want to display multiple zombies, you need to add an icon representing the zombie icon to the __init__() method of the Zombie class. The code Figure 1 shows:
Figure 1 Adding parameters to the __init__() method
After that, create two instances of the Zombie class, set different positions and import different zombie pictures respectively. The code is shown in Figure 2.
Figure 2 Create two zombie instances
Finally, call the add() method of the Group class through zombie_group to add the created zombie instance to the Group. The code is shown in Figure 3.
Figure 3 Add the instance to the group
4 Manage Sprite instances through Group
After that, the draw() and update() methods of the Group class can be called through zombie_group. In fact, the draw() and update() methods of the two Sprite instances are called to display the two zombie images. The code is shown in Figure 4. Show.
Figure 4 Managing Sprite instances through Group
5 Let the Sprite instance move
The same method as in "Using the Sprite Class in Pygame 2", in the while True loop, change the positions of the two Sprite instances respectively to make the Sprite instances move.
First, add the move() method to the Zombie class, in which the x coordinate of the rect attribute of the instance is changed. The code is shown in Figure 5.
Figure 5 move() method of Zombie class
Next, in the while True loop, specify different moving speeds for the two zombies, and finally call the draw() and update() methods of the Group class to display the zombies at the new position. The code is shown in Figure 6.
Figure 6 Zombie movement
6 Complete code
The complete code mentioned above is shown below.
import pygame, os
from pygame.locals import *
class Zombie(pygame.sprite.Sprite):
def __init__(self, x, y, image):
super().__init__()
self.image = pygame.image.load(image)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def move(self, step):
self.rect.x -= step
def draw(self, screen):
screen.blit(self.image, self.rect)
def update(self):
pygame.display.update()
pygame.init()
screen = pygame.display.set_mode((400, 500))
screen.fill((255,255,255))
clock = pygame.time.Clock()
zombie_group = pygame.sprite.Group()
z1 = Zombie(350, 80, '1.png')
z2 = Zombie(350, 130, '2.png')
zombie_group.add(z1)
zombie_group.add(z2)
zombie_group.draw(screen)
zombie_group.update()
while True:
screen.fill((255,255,255))
z1.move(10)
z2.move(5)
zombie_group.draw(screen)
zombie_group.update()
clock.tick(10)
pygame.quit()