Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Использование спрайтов.
- Выполнил:
- 13.04.2019
- """
- import pygame
- import random
- BLACK = (0x00, 0x00, 0x00)
- WHITE = (0xff, 0xff, 0xff)
- GREEN = (0x00, 0xff, 0x00)
- RED = (0xff, 0x00, 0x00)
- class Block(pygame.sprite.Sprite):
- """
- Этот класс представляет "блок".
- Является наследником класса Sprite.
- """
- def __init__(self, color, width, height):
- """ Конструктор. Принимает цвет и размеры блока. """
- # Вызов конструктора родителя(класса Sprite).
- super().__init__()
- # Задаём атрибуты image и rect для работы класса Sprite.
- self.image = pygame.Surface([width, height])
- self.image.fill(color)
- self.rect = self.image.get_rect()
- # self.image = pygame.image.load("pl1.png").convert()
- # self.image.set_colorkey(WHITE)
- # self.rect = self.image.get_rect()
- def main():
- """ Основная функция игры. """
- pygame.init()
- size = [800, 600]
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption("My Game")
- done = False
- clock = pygame.time.Clock()
- pygame.mouse.set_visible(False)
- # Список спрайтов с которыми игрок будет взаимодействовать.
- block_list = pygame.sprite.Group()
- # Список всех спрайтов игры.
- all_sprite_list = pygame.sprite.Group()
- # Генерация блоков.
- for i in range(50):
- block = Block(BLACK, 20, 15)
- block.rect.x = random.randrange(size[0])
- block.rect.y = random.randrange(size[1])
- block_list.add(block)
- all_sprite_list.add(block)
- player = Block(RED, 20, 15)
- all_sprite_list.add(player)
- score = 0
- # -------- Main Program Loop -----------
- while not done:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- done = True
- # Обработка движения игрока с помощью мышки.
- pos = pygame.mouse.get_pos()
- player.rect.x = pos[0]
- player.rect.y = pos[1]
- # Проверка столкновений игрока и блоков.
- block_hit_list = pygame.sprite.spritecollide(player, block_list, True)
- # Начисление очков.
- for block in block_hit_list:
- score += 1
- print(score)
- screen.fill(WHITE)
- all_sprite_list.draw(screen)
- pygame.display.flip()
- clock.tick(60)
- pygame.quit()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement