pygame学习之精灵入门

上代码和注释

#继承了pygame.sprite.Sprite(精灵 之后,有很多东西是它带来的 
#例如 self.rect 它规定了精灵的position 
#block_list = pygame.sprite.Group() 加入小组的东西 ,先当于变成了一组,直接处理组
#精灵之间,可以执行碰撞处理

import pygame
 
# Define some colorsimport random

BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)
RED   = (255,   0,   0)
 
class Block(pygame.sprite.Sprite):
    """
    This class represents the ball.
    It derives from the "Sprite" class in Pygame.
    """
 
    def __init__(self, color, width, height):
        """ Constructor. Pass in the color of the block,
        and its x and y position. """
 
        # Call the parent class (Sprite) constructor
        super().__init__()
 
        # Create an image of the block, and fill it with a color.
        # This could also be an image loaded from the disk.
        self.image = pygame.Surface([width, height])
        #底下两行,选择精灵的背景颜色 , 然后把他隐藏,效果是把方块隐藏了,
        #换成第三行期望的椭圆(椭圆颜色和RED不一样才能显示
        self.image.fill(RED)
        self.image.set_colorkey(RED)
        # Fetch the rectangle object that has the dimensions of the image
        # image.
        # Update the position of this object by setting the values
        # of rect.x and rect.y
        pygame.draw.ellipse(self.image, color, [0, 0, width, height])
        self.rect = self.image.get_rect()
    def reset_pos(self):
        self.rect.y = random.randrange(-300,-20)
        self.rect.x = random.randrange(0,screen_width)
        
    def update(self):
        self.rect.y += 1
        if self.rect.y >screen_height:
            self.reset_pos()
 
# Initialize Pygame
pygame.init()
 
# Set the height and width of the screen
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])
 
# This is a list of 'sprites.' Each block in the program is
# added to this list. The list is managed by a class called 'Group.'

#创建两个空列表,用于存放精灵,这是固定写法,就是必须这么写
#它的作用是,把很多精灵变成组,然后成组处理(避免了for去遍历

block_list = pygame.sprite.Group()
 
# This is a list of every sprite. 
# All blocks and the player block as well.
all_sprites_list = pygame.sprite.Group()

for i in range(50):
    # This represents a block
    block = Block(BLACK, 20, 15)
 
    # Set a random location for the block
    block.rect.x = random.randrange(screen_width)#取随机值成坐标
    block.rect.y = random.randrange(screen_height)
 
    # Add the block to the list of objects
    block_list.add(block)
    all_sprites_list.add(block)
 
# Create a RED player block
player = Block(RED, 20, 15)
all_sprites_list.add(player)
 
# Loop until the user clicks the close button.
done = False
 
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
 
score = 0
 
# -------- Main Program Loop -----------
while not done:
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            done = True
 
    # Clear the screen
    screen.fill(WHITE)
 
    # Get the current mouse position. This returns the position
    # as a list of two numbers.
    pos = pygame.mouse.get_pos()
 
    # Fetch the x and y out of the list,
       # just like we'd fetch letters out of a string.
    # Set the player object to the mouse location
    player.rect.x = pos[0]
    player.rect.y = pos[1]
 
    # See if the player block has collided with anything.
    #返回和player碰撞的精灵,True不会加入已经碰撞过的
    #加入列表的,将不会继续显示出来
    #all_sprites_list.update()
    blocks_hit_list = pygame.sprite.spritecollide(player, block_list, False)
     
    # Check the list of collisions.
    for block in blocks_hit_list:
        score += 1
        print(score)
        block.reset_pos()
    block_list.update()
    # Draw all the spites
    all_sprites_list.draw(screen)
    
    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()
 
    # Limit to 60 frames per second
    clock.tick(60)
 
pygame.quit()
类也可以这样做,导入图片,这样的话宽度颜色等参数就是图片自带的参数
class Block(pygame.sprite.Sprite):
    """
    This class represents the ball.
    It derives from the "Sprite" class in Pygame.
    """
 
    def __init__(self):
        """ Constructor. Pass in the color of the block,
        and its x and y position. """
 
        # Call the parent class (Sprite) constructor
        super().__init__()
 
        # Create an image of the block, and fill it with a color.
        # This could also be an image loaded from the disk.
        self.image = pygame.image.load(r'').convert()
        #设置图片,也可以本地导入
        
        self.image.set_colorkey(WHITE)
        #设某颜色为透明(不显示
        
        # Fetch the rectangle object that has the dimensions of the image
        # image.
        # Update the position of this object by setting the values
        # of rect.x and rect.y
        self.rect = self.image.get_rect()
 


猜你喜欢

转载自blog.csdn.net/qq_37591656/article/details/79002422