snowden_web

Untitled

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