By using
pygaem.sprite module, we can manage and draw game objects easily. There are two
main classes in sprites module.
1.
pygame.sprite.Sprite
: It is the base class for all game objects.
2.
pygame.sprite.Group
: It is a container class to hold and manage multiple Sprite objects.
I am going
to develop simple application using sprites. I am going to generate circle objects
and place them on window randomly, you have to collect them using mouse. Score
is printed to the console.
# import pygame import pygame import random import time # initialize game engine pygame.init() # Initialize values for color (RGB format) WHITE=(255,255,255) RED=(255,0,0) GREEN=(0,255,0) BLUE=(0,0,255) BLACK=(0,0,0) window_width=800 window_height=500 clock_tick_rate=20 totalBlocks=20 # Open a window size = (window_width, window_height) screen = pygame.display.set_mode(size) screen.fill(WHITE) # Set title to the window pygame.display.set_caption("Hello World") dead=False clock = pygame.time.Clock() block_list=pygame.sprite.Group() # Class used to draw circle game objects class CircleSprite(pygame.sprite.Sprite): def __init__(self, width, height, color): super().__init__() self.image = pygame.Surface([width, height]) self.image.fill(WHITE) self.rect = self.image.get_rect() pygame.draw.ellipse(self.image, color, [0, 0, 20, 20]) # Define all circle game objects for i in range(totalBlocks): myCircle=CircleSprite(20, 20, BLUE) myCircle.rect.x = random.randrange(window_width-20) myCircle.rect.y = random.randrange(window_height-20) block_list.add(myCircle) mouseSprite = CircleSprite(20, 20, BLUE) score=0 # Program start time start_time = time.time() while(dead==False): for event in pygame.event.get(): if event.type == pygame.QUIT: dead = True screen.fill(WHITE) pos = pygame.mouse.get_pos() mouseSprite.rect.x = pos[0] mouseSprite.rect.y = pos[1] blocks_hit_list = pygame.sprite.spritecollide(mouseSprite, block_list, True) num=len(blocks_hit_list) if(num > 0): score+=num print("Score : ", score) block_list.draw(screen) pygame.display.flip() clock.tick(clock_tick_rate) if(score == totalBlocks): print("You finished game in ", (time.time() - start_time), " seconds") dead = True pygame.quit()
Move the
mouse to circle objects, once you eat all the circles, you will get following
kind of output.
Score : 1
Score : 2
Score : 3
Score : 4
Score : 5
Score : 6
Score : 7
Score : 8
Score : 9
Score : 10
Score : 11
Score : 12
Score : 13
Score : 14
Score : 15
Score : 17
Score : 18
Score : 19
Score : 20
You finished
game in 9.701625108718872 seconds
Program Explanation
class CircleSprite(pygame.sprite.Sprite):
CircleSprite
is a sub class of pygame.sprite.Sprite. All the default functionality of the
Sprite class will now be a part of the CircleSprite class.
block_list=pygame.sprite.Group()
Define a
container object to hold and manage multiple Sprite objects.
blocks_hit_list =
pygame.sprite.spritecollide(mouseSprite, block_list, True)
All Sprites
that collide will be removed from all Groups. Return a list containing all
Sprites in a Group that intersect with another Sprite.
No comments:
Post a Comment