Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. import pygame, random
  2.  
  3. # Define some colors
  4. black = (0, 0, 0)
  5. white = (255, 255, 255)
  6. red = (255, 0, 0)
  7. green = (0, 255, 0)
  8. blue = (0, 0, 255)
  9.  
  10. pygame.init()
  11.  
  12. # Set the width and height of the screen
  13. map_width = 800
  14. map_height = 800
  15. size = [map_width, map_height]
  16. screen = pygame.display.set_mode(size)
  17.  
  18. # Display name
  19. pygame.display.set_caption("Natural Selection Game")
  20.  
  21. # Loop until the user clicks the close button.
  22. done = False
  23.  
  24. # Used to manage how fast the screen updates
  25. clock = pygame.time.Clock()
  26.  
  27. # -------- Organism -----------
  28. class Organism:
  29.  
  30. def __init__(self):
  31. # Speed and direction
  32. self.change_x = random.randrange(0,6)
  33. self.change_y = random.randrange(0,6)
  34.  
  35. # Dimensions
  36. self.width = random.randrange(5,50)
  37. self.height = random.randrange(5,50)
  38.  
  39. # Diet (WIP)
  40. self.color = random.choice([red, green, blue])
  41.  
  42. # Starting position
  43. self.x = random.randrange(0 + self.width, 800 - self.width)
  44. self.y = random.randrange(0 + self.height, 800 - self.height)
  45.  
  46. # Spawn
  47. def spawn(self, screen):
  48. pygame.draw.ellipse(screen, self.color, [self.x, self.y, self.width, self.height])
  49.  
  50. # Initiate movement
  51. def move(self):
  52. self.x += self.change_x
  53. self.y += self.change_y
  54.  
  55. # Bounce
  56. def bounce(self, map_width, map_height):
  57. if self.x < 0 or self.x > map_width - self.width:
  58. self.change_x = self.change_x * -1
  59. if self.y < 0 or self.y > map_height - self.height:
  60. self.change_y = self.change_y * -1
  61.  
  62. # Initial spawning conditions
  63. org_list = []
  64. org_number = 15
  65.  
  66. for i in range(0,org_number):
  67. org = Organism()
  68. org_list.append(org)
  69.  
  70. # -------- Main Program Loop -----------
  71. while not done:
  72. # ---- Event Processing ----
  73. for event in pygame.event.get():
  74. if event.type == pygame.QUIT:
  75. done = True
  76.  
  77. # ---- Logic ----
  78. # Movement
  79. for i in org_list:
  80. i.move()
  81. i.bounce(map_width, map_height)
  82.  
  83. # Reproduction
  84.  
  85. # ---- Drawing ----
  86. # Set the screen background
  87. screen.fill(white)
  88.  
  89. # Spawn organisms
  90. for i in org_list:
  91. i.spawn(screen)
  92.  
  93. # --- Wrap-up
  94. # Limit to 60 frames per second
  95. clock.tick(60)
  96.  
  97. # Go ahead and update the screen with what we've drawn.
  98. pygame.display.flip()
  99.  
  100. # Close everything down
  101. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement