Advertisement
PhilipCander

Untitled

Oct 21st, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 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. class Block(pygame.sprite.Sprite):
  10. """
  11. This class represents the ball.
  12. It derives from the "Sprite" class in Pygame.
  13. """
  14.  
  15. def __init__(self, color, width, height):
  16. """ Constructor. Pass in the color of the block,
  17. and its x and y position. """
  18.  
  19. # Call the parent class (Sprite) constructor
  20. super().__init__()
  21.  
  22. # Create an image of the block, and fill it with a color.
  23. # This could also be an image loaded from the disk.
  24. self.image = pygame.Surface([width, height])
  25. self.image.fill(color)
  26.  
  27. # Draw the ellipse
  28. pygame.draw.ellipse(self.image, color, [0, 0, width, height])
  29.  
  30. self.rect = self.image.get_rect()
  31.  
  32. # Initialize Pygame
  33. pygame.init()
  34.  
  35. # Set the height and width of the screen
  36. screen_width = 700
  37. screen_height = 400
  38. screen = pygame.display.set_mode([screen_width, screen_height])
  39.  
  40. # This is a list of 'sprites.' Each block in the program is
  41. # added to this list.
  42. # The list is managed by a class called 'Group.'
  43. block_list = pygame.sprite.Group()
  44.  
  45. # This is a list of every sprite.
  46. # All blocks and the player block as well.
  47. all_sprites_list = pygame.sprite.Group()
  48.  
  49. for i in range(50):
  50. # This represents a block
  51. block = Block(BLACK, 20, 15)
  52.  
  53. # Set a random location for the block
  54. block.rect.x = random.randrange(screen_width)
  55. block.rect.y = random.randrange(screen_height)
  56.  
  57. # Add the block to the list of objects
  58. block_list.add(block)
  59. all_sprites_list.add(block)
  60.  
  61. # Create a RED player block
  62. player = Block(RED, 20, 15)
  63. all_sprites_list.add(player)
  64.  
  65.  
  66. # Loop until the user clicks the close button.
  67. done = False
  68.  
  69. # Used to manage how fast the screen updates
  70. clock = pygame.time.Clock()
  71.  
  72. score = 0
  73.  
  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. # Clear the screen
  81. screen.fill(WHITE)
  82.  
  83. # Get the current mouse position. This returns the position
  84. # as a list of two numbers.
  85. pos = pygame.mouse.get_pos()
  86.  
  87. # Fetch the x and y out of the list,
  88. # just like we'd fetch letters out of a string.
  89. # Set the player object to the mouse location
  90. player.rect.x = pos[0]
  91. player.rect.y = pos[1]
  92.  
  93. # See if the player block has collided with anything.
  94. blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)
  95.  
  96. # Check the list of collisions.
  97. for block in blocks_hit_list:
  98. score += 1
  99. print(score)
  100.  
  101. # Draw all the spites
  102. all_sprites_list.draw(screen)
  103.  
  104. # Limit to 60 frames per second
  105. clock.tick(60)
  106.  
  107. # Go ahead and update the screen with what we've drawn.
  108. pygame.display.flip()
  109.  
  110. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement