Advertisement
zicaentu

Sprite

Apr 20th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. """
  2. Использование спрайтов.
  3.  
  4. Выполнил:
  5.  
  6. 13.04.2019
  7. """
  8. import pygame
  9. import random
  10.  
  11.  
  12. BLACK = (0x00, 0x00, 0x00)
  13. WHITE = (0xff, 0xff, 0xff)
  14. GREEN = (0x00, 0xff, 0x00)
  15. RED =   (0xff, 0x00, 0x00)
  16.  
  17. class Block(pygame.sprite.Sprite):
  18.     """
  19.    Этот класс представляет "блок".
  20.    Является наследником класса Sprite.
  21.    """
  22.     def __init__(self, color, width, height):
  23.         """ Конструктор. Принимает цвет и размеры блока. """
  24.        
  25.         # Вызов конструктора родителя(класса Sprite).
  26.         super().__init__()
  27.  
  28.         # Задаём атрибуты image и rect для работы класса Sprite.
  29.         self.image = pygame.Surface([width, height])
  30.         self.image.fill(color)
  31.         self.rect = self.image.get_rect()
  32.  
  33.         # self.image = pygame.image.load("pl1.png").convert()
  34.         # self.image.set_colorkey(WHITE)
  35.         # self.rect = self.image.get_rect()
  36.  
  37.  
  38. def main():
  39.     """ Основная функция игры. """
  40.     pygame.init()
  41.  
  42.     size = [800, 600]
  43.     screen = pygame.display.set_mode(size)
  44.  
  45.     pygame.display.set_caption("My Game")
  46.  
  47.     done = False
  48.  
  49.     clock = pygame.time.Clock()
  50.  
  51.     pygame.mouse.set_visible(False)
  52.    
  53.     # Список спрайтов с которыми игрок будет взаимодействовать.
  54.     block_list = pygame.sprite.Group()
  55.  
  56.     # Список всех спрайтов игры.
  57.     all_sprite_list = pygame.sprite.Group()
  58.    
  59.     # Генерация блоков.
  60.     for i in range(50):
  61.         block = Block(BLACK, 20, 15)
  62.  
  63.         block.rect.x = random.randrange(size[0])
  64.         block.rect.y = random.randrange(size[1])
  65.  
  66.         block_list.add(block)
  67.         all_sprite_list.add(block)
  68.  
  69.  
  70.     player = Block(RED, 20, 15)
  71.     all_sprite_list.add(player)
  72.  
  73.     score = 0
  74.     # -------- Main Program Loop -----------
  75.     while not done:
  76.         for event in pygame.event.get():
  77.             if event.type == pygame.QUIT:
  78.                 done = True
  79.  
  80.         # Обработка движения игрока с помощью мышки.
  81.         pos = pygame.mouse.get_pos()
  82.         player.rect.x = pos[0]
  83.         player.rect.y = pos[1]
  84.  
  85.         # Проверка столкновений игрока и блоков.
  86.         block_hit_list = pygame.sprite.spritecollide(player, block_list, True)
  87.  
  88.         # Начисление очков.
  89.         for block in block_hit_list:
  90.             score += 1
  91.             print(score)
  92.  
  93.  
  94.         screen.fill(WHITE)
  95.         all_sprite_list.draw(screen)
  96.         pygame.display.flip()
  97.  
  98.         clock.tick(60)
  99.     pygame.quit()
  100.  
  101. if __name__ == "__main__":
  102.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement