Python Forum
[PyGame] "Floating Blobs" experiment
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] "Floating Blobs" experiment
#1
I made a simple game (experiment) where there are different (round) blobs with random sizes from 4 to 16 pixels in width and height. Each blob moves with a speed of r/2 (where r is the radius of the blob) in a random direction (right, left, down, up). When two blobs collide, the smaller one get killed and the bigger one gains size depending on the killed blob's radius, which means that the radius of the smaller blob gets added to the radius of the bigger one; if two blobs collide and they have the same size, nothing happens. The game ends when one blobs is left.
You can change the amount of initial blobs when the game starts, by changing '25' in 'add_blobs(25)' in the '# Other initializations' section.

(I made this game because I was inspired from the game that "sentdex" used to teach OOP in his "Intermediate" python tutorials, if you know his Youtube channel.)

import pygame, random, math

# Constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 60
BLACK = 0, 0, 0
WHITE = 255, 255, 255
RED = 255, 0, 0
GREEN = 0, 255, 0
BLUE = 0, 0, 255
YELLOW = 255, 255, 0
PURPLE = 255, 0, 255
CYAN = 0, 255, 255

# Classes
class Blob(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.radius = random.randint(2, 8)
        self.image = pygame.Surface((self.radius * 2, self.radius * 2))
        self.image.fill(WHITE)
        self.color = BLACK
        self.rect = self.image.get_rect()
        self.rect.center = (random.randint(self.radius, SCREEN_WIDTH - self.radius),
                            random.randint(self.radius, SCREEN_HEIGHT - self.radius))

    def speed(self):
        return self.radius // 2

    def move(self):
        xory = random.choice(('x', 'y'))
        x_direction = random.choice(('right', 'left'))
        y_direction = random.choice(('down', 'up'))
        if xory == 'x':
            if x_direction == 'right':
                self.rect.x += self.speed() \
                               if self.rect.right + self.speed() <= SCREEN_WIDTH \
                               else SCREEN_WIDTH - self.rect.right
            else:
                self.rect.x -= self.speed() \
                               if self.rect.left - self.speed() >= 0 \
                               else self.rect.x
        if xory == 'y':
            if y_direction == 'down':
                self.rect.y += self.speed() \
                               if self.rect.bottom + self.speed() <= SCREEN_HEIGHT \
                               else SCREEN_HEIGHT - self.rect.bottom
            else:
                self.rect.y -= self.speed() \
                               if self.rect.top - self.speed() >= 0 \
                               else self.rect.y

    def check_collisions(self):
        for blob in blobs:
            if pygame.sprite.collide_circle(self, blob) and blob != self:
                difference = blob.radius - self.radius
                if difference > 0:
                    blob.radius += self.radius
                    self.kill()
                elif difference < 0:
                    self.radius += blob.radius
                    blob.kill()
        if self.alive():
            self.image = pygame.Surface((self.radius * 2, self.radius * 2))
            self.image.fill(WHITE)
            temp = self.rect.center
            self.rect = self.image.get_rect()
            self.rect.center = temp

    def update(self):
        self.check_collisions()
        self.move()

# Functions definitions
def add_blobs(num):
    for n in range(num):
        blobs.add(Blob())

def build_blobs():
    for blob in blobs:
        pygame.draw.circle(screen, blob.color, blob.rect.center, blob.radius)
    
# Essential initializations
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Test")
clock = pygame.time.Clock()

# Other initializations
blobs = pygame.sprite.Group()
add_blobs(25)

running = True
# Game loop
while running:
    # Timing
    clock.tick(FPS)

    # Event handling
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Other checks
    if len(blobs) == 1:
        running = False

    # Updating
    blobs.update()

    # Rendering
    screen.fill(WHITE)
    blobs.draw(screen)
    build_blobs()
    pygame.display.update()

# Quiting
pygame.quit()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020