# Ian Binstock
# 6/11/2026
# Rock Paper Scissors battle royale

# Showcases sprite utilization and inheritance

import random, pygame, sys
from pygame.locals import *

from SpriteClass import RPSsprite

# Useful Constants
SCREEN_W = 1000
SCREEN_H = 900
BALL_R = 12
NUM_BALLS = 200

# Helper functions
def myrand(low, high):
    res = 0
    while res == 0:
        res = random.randint(low, high)
    return res

def load_assets():
    size = (BALL_R * 2, BALL_R * 2)
    assets = {}
    types = [('rock', 'R.png'), ('paper', 'P.png'), ('scissors', 'S.png')]

    for name, filename in types:
        # Load img files
        img = pygame.image.load(filename).convert_alpha()
        assets[name] = pygame.transform.scale(img, size)
            
    return assets

def main():
    # Basic Set Up
    pygame.init()
    DISPLAYSURF = pygame.display.set_mode((SCREEN_W, SCREEN_H))
    pygame.display.set_caption("Rock Paper Scissors Battle Royale")
    clock = pygame.time.Clock()

    # Load assets
    global_images = load_assets()

    obj_group = pygame.sprite.Group()
    rps_options = ['rock', 'paper', 'scissors']

    # Assign types to sprites and add to sprite group
    for i in range(NUM_BALLS):

        # Somewhere on the screen
        x = random.randint(BALL_R + 10, SCREEN_W - BALL_R - 10)
        y = random.randint(BALL_R + 10, SCREEN_H - BALL_R - 10)

        # Random non-zero movement in both directions
        dx = myrand(-2, 2)
        dy = myrand(-2, 2)

        # Alternate choosing between the groups for an even split
        assigned_type = rps_options[i % 3]

        # Add obj to group
        obj = RPSsprite(x, y, dx, dy, BALL_R, assigned_type, global_images)
        obj_group.add(obj)
        
    # Game loop
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        # Black bg
        DISPLAYSURF.fill(pygame.Color("black"))

        # Update every obj
        obj_group.update(SCREEN_W, SCREEN_H)
        obj_group.draw(DISPLAYSURF)

        # Collision Handling
        objs = obj_group.sprites()
        for i in range(len(objs)):
            for j in range(i + 1, len(objs)):
                if pygame.sprite.collide_circle(objs[i], objs[j]):

                    # Swap dx, dy
                    tempDX = objs[i].dx
                    tempDY = objs[i].dy
                    objs[i].dx = objs[j].dx
                    objs[i].dy = objs[j].dy
                    objs[j].dx = tempDX
                    objs[j].dy = tempDY

                    # Retrieve types
                    type_i = objs[i].rps_type
                    type_j = objs[j].rps_type

                    if type_i != type_j:
                        # Loser type is converted into winner type
                        if (type_i == 'rock' and type_j == 'scissors') or (type_i == 'scissors' and type_j == 'paper') or (type_i == 'paper' and type_j == 'rock'):
                            objs[j].change_identity(type_i)
                        else:
                            objs[i].change_identity(type_j)

        pygame.display.update()
        clock.tick(50)

# Run it
main()