|
| 1 | +# Imports |
| 2 | +import sys |
| 3 | +import pygame |
| 4 | + |
| 5 | +# We will work with the Vector2 because it has some useful functions. |
| 6 | +from pygame.math import Vector2 |
| 7 | + |
| 8 | +from random import randrange |
| 9 | + |
| 10 | +import ctypes |
| 11 | + |
| 12 | +# Enable High Dots Per Inch so the image displayed on the window is sharper. |
| 13 | +ctypes.windll.shcore.SetProcessDpiAwareness(1) |
| 14 | + |
| 15 | +# Configuration |
| 16 | +pygame.init() |
| 17 | +fps = 60 |
| 18 | +fpsClock = pygame.time.Clock() |
| 19 | + |
| 20 | +# Window Size |
| 21 | +windowdim = Vector2(800, 800) |
| 22 | +screen = pygame.display.set_mode((int(windowdim.x), int(windowdim.y))) |
| 23 | + |
| 24 | +# all the Planets are stored here |
| 25 | +# They will append themselves. |
| 26 | +planets = [] |
| 27 | + |
| 28 | +# The Planet Class which will handle drawing and calculating planets. |
| 29 | +class Planet(): |
| 30 | + def __init__(self, position, delta=Vector2(0, 0), radius=10, imovable=False): |
| 31 | + |
| 32 | + # Where the planet is at the moment |
| 33 | + self.position = position |
| 34 | + |
| 35 | + # The Radius determines how much this planet effects others |
| 36 | + self.radius = radius |
| 37 | + |
| 38 | + # The Velocity |
| 39 | + self.delta = delta |
| 40 | + |
| 41 | + # If this planet is moving |
| 42 | + self.imovable = imovable |
| 43 | + |
| 44 | + # If this planet can be eaten by others. |
| 45 | + self.eatable = False |
| 46 | + |
| 47 | + |
| 48 | + # Appending itself to the list so its process |
| 49 | + # function will later be called in a loop. |
| 50 | + planets.append(self) |
| 51 | + |
| 52 | + |
| 53 | + def process(self): |
| 54 | + # This function will be called once every frame |
| 55 | + # and it is responsible for calculating where the planet will go. |
| 56 | + |
| 57 | + # No Movement Calculations will happen if the planet doesnt move at all. |
| 58 | + # it also wont be eaten. |
| 59 | + if not self.imovable: |
| 60 | + for i in planets: |
| 61 | + if not i is self: |
| 62 | + try: |
| 63 | + if self.eatable: |
| 64 | + if self.position.distance_to(i.position) < self.radius + i.radius: |
| 65 | + print('Eaten') |
| 66 | + i.radius += self.radius |
| 67 | + planets.remove(self) |
| 68 | + dir_from_obj = (i.position - self.position).normalize() * 0.01 * (i.radius / 10) |
| 69 | + self.delta += dir_from_obj |
| 70 | + except: |
| 71 | + print('In the same spot') |
| 72 | + |
| 73 | + self.position += self.delta |
| 74 | + |
| 75 | + # Drawing the planet at the current position. |
| 76 | + pygame.draw.circle( |
| 77 | + screen, |
| 78 | + [255, 255, 255], |
| 79 | + self.position, |
| 80 | + self.radius, |
| 81 | + ) |
| 82 | + |
| 83 | +# Sun and two opposing Planets |
| 84 | +""" Planet(Vector2(400, 400), radius=50, imovable=True) |
| 85 | +
|
| 86 | +Planet(Vector2(400, 200), delta=Vector2(3, 0), radius=10) |
| 87 | +Planet(Vector2(400, 600), delta=Vector2(-3, 0), radius=10) """ |
| 88 | + |
| 89 | +# Sun and four opposing Planets |
| 90 | +Planet(Vector2(400, 400), radius=50, imovable=True) |
| 91 | + |
| 92 | +Planet(Vector2(400, 200), delta=Vector2(3, 0), radius=10) |
| 93 | +Planet(Vector2(400, 600), delta=Vector2(-3, 0), radius=10) |
| 94 | +Planet(Vector2(600, 400), delta=Vector2(0, 3), radius=10) |
| 95 | +Planet(Vector2(200, 400), delta=Vector2(0, -3), radius=10) |
| 96 | + |
| 97 | +# Two Suns and two planets |
| 98 | +""" Planet(Vector2(600, 400), radius=20, imovable=True) |
| 99 | +Planet(Vector2(200, 400), radius=20, imovable=True) |
| 100 | +
|
| 101 | +Planet(Vector2(400, 200), delta=Vector2(0, 0), radius=10) |
| 102 | +Planet(Vector2(400, 210), delta=Vector2(1, 2), radius=5) """ |
| 103 | + |
| 104 | +# Grid |
| 105 | +# gridDimension = 10 |
| 106 | +# gridgap = 80 |
| 107 | +# for x in range(gridDimension): |
| 108 | +# for y in range(gridDimension): |
| 109 | +# Planet(Vector2(gridgap * x + 40, gridgap * y + 40), radius=3, imovable=True) |
| 110 | + |
| 111 | +# Planet(Vector2(200, 200), delta=Vector2(randrange(-3, 3), 2), radius=5) |
| 112 | + |
| 113 | +# Game loop. |
| 114 | +while True: |
| 115 | + screen.fill((0, 0, 0)) |
| 116 | + for event in pygame.event.get(): |
| 117 | + if event.type == pygame.QUIT: |
| 118 | + pygame.quit() |
| 119 | + sys.exit() |
| 120 | + |
| 121 | + for p in planets: |
| 122 | + p.process() |
| 123 | + |
| 124 | + pygame.display.flip() |
| 125 | + fpsClock.tick(fps) |
0 commit comments