Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.16 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. #тут текстурки с нлошкой не хватает.((
  3. import pygame
  4. import random
  5.  
  6. # Define some colors
  7. BLACK    = (   0,   0,   0)
  8. WHITE    = ( 255, 255, 255)
  9. RED      = ( 255,   0,   0)
  10.  
  11. # This class represents the ball        
  12. # It derives from the "Sprite" class in Pygame
  13. class Block(pygame.sprite.Sprite):
  14.      
  15.     # READ BEFORE USING:
  16.     # This constructor lets you use any graphic:
  17.     # my_sprite = Block("any_graphic.png")
  18.     # But if you DON'T want any graphic, use the following instead:
  19.     '''
  20.   def __init__(self):
  21.       super().__init__()
  22.  
  23.       self.image = pygame.image.load("my_graphic.png").convert()
  24.  
  25.       # Set background color to be transparent. Adjust to WHITE if your
  26.       # background is WHITE.
  27.       self.image.set_colorkey(BLACK)
  28.  
  29.       self.rect = self.image.get_rect()
  30.   '''
  31.     def __init__(self, filename):
  32.         # Call the parent class (Sprite) constructor
  33.         super().__init__()
  34.  
  35.         # Create an image of the block, and fill it with a color.
  36.         # This could also be an image loaded from the disk.
  37.         self.image = pygame.image.load(filename).convert()
  38.  
  39.         # Set background color to be transparent. Adjust to WHITE if your
  40.         # background is WHITE.
  41.         self.image.set_colorkey(BLACK)
  42.  
  43.         # Fetch the rectangle object that has the dimensions of the image
  44.         # image.
  45.         # Update the position of this object by setting the values
  46.         # of rect.x and rect.y
  47.         self.rect = self.image.get_rect() #наши нлошки из класса блок, тут мы говорим типа создай этот блок по размеру текстурки
  48.  
  49. # Initialize Pygame
  50. pygame.init()
  51.  
  52. # Set the height and width of the screen
  53. screen_width = 1700
  54. screen_height = 1500
  55. screen = pygame.display.set_mode([screen_width, screen_height])
  56.  
  57. # This is a list of 'sprites.' Each block in the program is
  58. # added to this list. The list is managed by a class called 'Group.'
  59. block_list = pygame.sprite.Group()
  60.  
  61. # This is a list of every sprite. All blocks and the player block as well.
  62. all_sprites_list = pygame.sprite.Group()
  63.  
  64. for i in range(50):
  65.     # This represents a block
  66.     block = Block("alien.png")
  67.  
  68.     # Set a random location for the block
  69.     block.rect.x = random.randrange(screen_width) #распредели нлошек по всей ширине и высоте окна, можно задать в пикселях
  70.     block.rect.y = random.randrange(screen_height)
  71.      
  72.     # Add the block to the list of objects
  73.     block_list.add(block)
  74.     all_sprites_list.add(block)
  75.      
  76. # Create a RED player block
  77. player = Block("ufo.png")
  78. all_sprites_list.add(player)
  79.  
  80. # Hide the mouse cursor
  81. pygame.mouse.set_visible(0)
  82.  
  83. #Loop until the user clicks the close button.
  84. done = False
  85.  
  86. # Used to manage how fast the screen updates
  87. clock = pygame.time.Clock()
  88.  
  89.  
  90. # -------- Main Program Loop -----------
  91. while not done:
  92.     for event in pygame.event.get(): # User did something
  93.         if event.type == pygame.QUIT: # If user clicked close
  94.             done = True # Flag that we are done so we exit this loop
  95.  
  96.     # Clear the screen
  97.     screen.fill(WHITE)
  98.  
  99.     # Get the current mouse position. This returns the position
  100.     # as a list of two numbers.
  101.     pos = pygame.mouse.get_pos()
  102.      
  103.     # Fetch the x and y out of the list,
  104.        # just like we'd fetch letters out of a string.
  105.     # Set the player object to the mouse location
  106.     player.rect.x = pos[0]
  107.     player.rect.y = pos[1]
  108.      
  109.     # See if the player block has collided with anything.
  110.     blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)  
  111.           #spritecollide - взаимодействие
  112.      #если наш игрок соприкасается с нло - они исчезают (труе - параметр исчезновения)
  113.    
  114.    
  115.    
  116.     # Draw all the spites
  117.     all_sprites_list.draw(screen)
  118.      
  119.     # Limit to 60 frames per second
  120.     clock.tick(60)
  121.  
  122.     # Go ahead and update the screen with what we've drawn.
  123.     pygame.display.flip()
  124.  
  125. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement