diff --git a/.gitignore b/.gitignore index 597cfbb..2268b48 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ + +image.jpg + .env pacman_beginning.wav pacman_chomp.wav diff --git a/binary_tree.py b/binary_tree.py new file mode 100644 index 0000000..946d051 --- /dev/null +++ b/binary_tree.py @@ -0,0 +1,8 @@ +class Node: + def __init__(self, img, val, level, state = None): + self.image = img # img + self.val = val # string/story for this image + self.level = level # depth [0, 1, or 2] + self.state = state # win/lose for all levels except 0 + self.left = None # lose + self.right = None # win diff --git a/generate.py b/generate.py new file mode 100644 index 0000000..88e8e66 --- /dev/null +++ b/generate.py @@ -0,0 +1,199 @@ +import cohere +import os +from image_captioning import predict_step +from dotenv import load_dotenv +from binary_tree import * +load_dotenv() + +api_key = os.getenv('COHERE_API_KEY') +co = cohere.Client(api_key) + + +def generate_text(captions, temp=0): + + prompt=f"""Predict what I experienced based on these captions in order using first person narrative (I): + {captions[0]}, + {captions[1]}, + {captions[2]} + + Ensure that for each caption, you generate exactly 4 sentences. Separate the output for each caption based + on the format below. Strictly adhere to the format and ensure you don't generate anything that strays from the + format below. Ensure that the storyline between the outputs for the captions is consistent. + + Strictly adhere to the following output format and don't deviate from it: + " + [sentences for caption 1] + ### + [sentences for caption 2] + ### + [sentences for caption 3] + " + + The output should be parse-able using [s.replace('\n', '').replace('"', '') for s in coo.split('###')]. + """ + + # chat_hist = [] + response = co.generate( + model='command', + prompt=prompt, + temperature=0.0, + ) + # chat_hist.append(response.generations[0].text) + return response.generations[0].text + + +def generate_text_level_two(captions, prev, temp=0): + + prompt=f"""Predict what I experienced based on these captions in order using first person narrative (I): + {captions[0]}, + {captions[1]}, + {captions[2]} + + The output for the first caption is: {prev[0]} + + Ensure that for the remaining captions, you generate exactly 4 sentences. Separate the output for each caption based + on the format below. Strictly adhere to the format and ensure you don't generate anything that strays from the + format below. Ensure that the storyline between the outputs for the remaining captions is consistent with the + story introduced by the first caption. + + Strictly adhere to the following output format and don't deviate from it: + " + {prev[0]} + ### + [sentences for caption 2] + ### + [sentences for caption 3] + " + + The output should be parse-able using [s.replace('\n', '').replace('"', '') for s in coo.split('###')]. + """ + + # chat_hist = [] + response = co.generate( + model='command', + prompt=prompt, + temperature=0.0, + ) + # chat_hist.append(response.generations[0].text) + return response.generations[0].text + + + +def generate_text_level_three(captions, prev, temp=0): + + prompt=f"""Predict what I experienced based on these captions in order using first person narrative (I): + {captions[0]}, + {captions[1]}, + {captions[2]} + + The output for the first caption is: {prev[0]} + The output for the second caption is: {prev[1]} + + Ensure that for the remaining captions, you generate exactly 4 sentences. Separate the output for each caption based + on the format below. Generate only the sentences. Strictly adhere to the format and don't generate any additional + text asking acknowledging the instructions or asking if the output is satisfactory. Ensure that the storyline between + the outputs for the remaining captions is consistent with the story introduced by the first caption and the second caption. + + Strictly adhere to the following output format and don't deviate from it: + " + {prev[0]} + ### + {prev[1]} + ### + [sentences for caption 3] + " + + The output should be parse-able using [s.replace('\n', '').replace('"', '') for s in coo.split('###')]. + """ + + # chat_hist = [] + response = co.generate( + model='command', + prompt=prompt, + temperature=0.0, + ) + # chat_hist.append(response.generations[0].text) + return response.generations[0].text + + +def process_co_output(coo): + print(coo) + return [s.replace('\n', '').replace('"', '') for s in coo.split('###')] + + +def create_game_tree(imgs): + root = Node(imgs[0], '', 0) + + # level 1 + root.left = Node(imgs[1], '', 1, 'lose') + root.right = Node(imgs[1], '', 1, 'right') + + # level 2 + root.left.left, root.left.right = Node(imgs[2], '', 2, 'lose'), Node(imgs[2], '', 2, 'right') + root.right.left, root.right.right = Node(imgs[2], '', 2, 'lose'), Node(imgs[2], '', 2, 'right') + + return root + + +''' +class Node: + def __init__(self, img, val, level, state = None): + self.image = img # img_path + self.caption = '' + self.val = val # string/story for this image + self.level = level # depth [0, 1, or 2] + self.state = state # win/lose for all levels except 0 + self.left = None # lose + self.right = None # win +''' + +def populate_tree(root, captions): + # assume generate_text generates winning text + gen = process_co_output(generate_text(captions)) + + root.caption, root.val = captions[0], gen[0] + root.right.caption, root.right.val = captions[1], gen[1] + root.right.right.caption, root.right.right.val = captions[2], gen[2] + + gen2 = process_co_output(generate_text_level_three(captions, gen[:2])) + root.right.left.caption, root.right.left.val = captions[2], gen2[2] + + gen_left_from_root = process_co_output(generate_text_level_two(captions, gen[:1])) + root.left.caption, root.left.val = captions[1], gen_left_from_root[1] + root.left.right.caption, root.left.right.val = captions[2], gen_left_from_root[2] + + gen_remaining = process_co_output(generate_text_level_three(captions, gen_left_from_root[:2])) + root.left.left.caption, root.left.left.val = captions[2], gen_remaining[2] + +# def _expand_tree(node, current_level, max_level): +# if current_level == max_level: +# return + +# win_story = generate_text(f"Win story for level {current_level - 1}") +# lose_story = generate_text(f"Lose story for level {current_level - 1}") + +# node.left = Node(generate_text(prompt + ""), current_level + 1, 'win') +# node.right = Node(generate_text(prompt + ""), current_level + 1, 'lose') + +# # _expand_tree(node.left, current_level + 1, max_level) +# # _expand_tree(node.right, current_level + 1, max_level) + +def print_tree(root, level=0, prefix="Root: ", state=""): + if root is not None: + if level == 0: + print(f"{prefix}{root.caption} - {root.state} ({root.level})") + else: + print(f"{' ' * (level * 4)}|-- {root.caption} - {root.state} ({root.level})") + + print_tree(root.left, level + 1, "Left: ", root.state) + print_tree(root.right, level + 1, "Right: ", root.state) + + +if __name__ == "__main__": + image_paths_3 = ['./images/biking.jpg', './images/monke.jpg', './images/rohan.jpeg'] + + captions = predict_step(image_paths_3) + + root = create_game_tree(image_paths_3) + populate_tree(root, captions) + print_tree(root) diff --git a/image_captioning.py b/image_captioning.py index c3f04d2..77afa8b 100644 --- a/image_captioning.py +++ b/image_captioning.py @@ -39,7 +39,7 @@ def predict_step(image_paths): if __name__ == "__main__": - image_paths_3 = ['./images/biking.jpg', './images/climbing.jpg', './images/rohan.jpeg'] + image_paths_3 = ['./images/biking.jpg', './images/monke.jpg', './images/rohan.jpeg'] image_paths_1 = ['./images/biking.jpg'] print(predict_step(image_paths_3)) diff --git a/images/monke.jpg b/images/monke.jpg new file mode 100644 index 0000000..2c54931 Binary files /dev/null and b/images/monke.jpg differ diff --git a/images/ppl.JPG b/images/ppl.JPG new file mode 100644 index 0000000..3fc8900 Binary files /dev/null and b/images/ppl.JPG differ diff --git a/images/testingShard/bottom_left.jpg b/images/testingShard/bottom_left.jpg new file mode 100644 index 0000000..d5e41be Binary files /dev/null and b/images/testingShard/bottom_left.jpg differ diff --git a/images/testingShard/bottom_right.jpg b/images/testingShard/bottom_right.jpg new file mode 100644 index 0000000..1e8d53e Binary files /dev/null and b/images/testingShard/bottom_right.jpg differ diff --git a/images/testingShard/top_left.jpg b/images/testingShard/top_left.jpg new file mode 100644 index 0000000..9fb0e1d Binary files /dev/null and b/images/testingShard/top_left.jpg differ diff --git a/images/testingShard/top_right.jpg b/images/testingShard/top_right.jpg new file mode 100644 index 0000000..ba760a8 Binary files /dev/null and b/images/testingShard/top_right.jpg differ diff --git a/node.py b/node.py new file mode 100644 index 0000000..5f37fa7 --- /dev/null +++ b/node.py @@ -0,0 +1,8 @@ +class Node: + def __init__(self, val, level, state): + self.val = val + self.level = level + self.state = state + self.left = None + self.right = None + self.nodeChatHistory = "" diff --git a/pacman.py b/pacman.py index 973c30c..0878413 100644 --- a/pacman.py +++ b/pacman.py @@ -4,54 +4,70 @@ import pygame import math from trees import Story, Tree +from splitImage import split_image pygame.init() +SPLIT_COUNT = 3 + class Shard(): - def __init__(self, sprite, image): + def __init__(self, sprite, image, split): self.sprite = sprite self.image = image + self.split = split # split in ["left", "right", "up", "down"] + - - -def change_screen_shard_collected(screen, width, height): - screen = pygame.display.set_mode([width, height]) - - screen.fill('aqua') - cont = True - while cont: - for event in pygame.event.get(): - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_r: - cont = False - break - - return None - -def change_screen_shard_collected(screen, width, height, shard): +def change_screen_shard_collected(screen, width, height, shard_list): + global SPLIT_COUNT screen = pygame.display.set_mode([width, height]) - image = pygame.image.load(shard.image) - + image = shard_list[SPLIT_COUNT] + SPLIT_COUNT -= 1 + cont = True while cont: - for event in pygame.event.get(): - if event.type == pygame.QUIT: + if event.type == pygame.QUIT: cont = False if event.type == pygame.KEYDOWN: - if event.key == pygame.K_r: + if event.key == pygame.K_r: cont = False screen.blit(image, (0, 0)) pygame.display.update() return None +# ======= +# def change_screen_shard_collected(screen, width, height): +# screen = pygame.display.set_mode([width, height]) +# screen.fill('aqua') +# cont = True +# while cont: +# for event in pygame.event.get(): +# if event.type == pygame.KEYDOWN: +# if event.key == pygame.K_r: +# cont = False +# break + +# return None + +# # def change_screen_shard_collected(screen, width, height, shard): +# # screen = pygame.display.set_mode([width, height]) +# # image = pygame.image.load(shard.image) +# # cont = True +# # while cont: + +# # for event in pygame.event.get(): +# # if event.type == pygame.QUIT: +# # cont = False +# # if event.type == pygame.KEYDOWN: +# # if event.key == pygame.K_r: +# # cont = False +# >>>>>>> develop -def run_game( - board = None -): + +def run_game(board = None): WIDTH = 900 HEIGHT = 950 screen = pygame.display.set_mode([WIDTH, HEIGHT]) @@ -91,7 +107,7 @@ def run_game( turns_allowed = [False, False, False, False] direction_command = 0 player_speed = 2 - score = 0 + score = [0] powerup = False power_counter = 0 eaten_ghost = [False, False, False, False] @@ -110,10 +126,25 @@ def run_game( lives = 3 game_over = False game_won = False + img_pp = pygame.image.load("photo_piece.png").convert_alpha() img_pp = pygame.transform.scale(img_pp, (30,30)) + split_image('./images/rohan.jpeg', './images', 'testingShard') + + img_pp1 = pygame.image.load('./images/testingShard/top_left.jpg').convert_alpha() + img_pp2 = pygame.image.load('./images/testingShard/top_right.jpg').convert_alpha() + img_pp3 = pygame.image.load('./images/testingShard/bottom_left.jpg').convert_alpha() + img_pp4 = pygame.image.load('./images/testingShard/top_left.jpg').convert_alpha() + shard_list = [img_pp1, img_pp2, img_pp3, img_pp4] + + # shard1 = Shard(sprite=None, image='./images/testingShard/top_left.jpg', split='left') + # shard2 = Shard(sprite=None, image='./images/testingShard/top_right.jpg', split='right') + # shard3 = Shard(sprite=None, image='./images/testingShard/bottom_left.jpg', split='up') + # shard4 = Shard(sprite=None, image='./images/testingShard/top_right.jpg', split='down') + # shard_list = [shard1.image, shard2.image, shard3.image, shard4.image] + mute = False - shards = 0 + shards = [0] # Each Ghost's class, each ghost has its own movement, this can be changed to just one movement way @@ -716,8 +747,8 @@ def draw_misc(): nonlocal shards font = pygame.font.Font('freesansbold.ttf', 25) - score_text = font.render(f'Score: {score}', True, 'white') - shards_text = font.render(f'Shards: {shards} / 4', True, 'white') + score_text = font.render(f'Score: {score[0]}', True, 'white') + shards_text = font.render(f'Shards: {shards[0]} / 4', True, 'white') screen.blit(score_text, (WIDTH // 2.26, HEIGHT // 2.35)) screen.blit(shards_text, (WIDTH // 2.4, HEIGHT // 2.15)) @@ -745,17 +776,21 @@ def check_collisions(scor, power, power_count, eaten_ghosts): if 0 < player_x < 870: if level[center_y // num1][center_x // num2] == 1: level[center_y // num1][center_x // num2] = 0 - scor += 10 + scor[0] += 10 if level[center_y // num1][center_x // num2] == 2: level[center_y // num1][center_x // num2] = 0 - scor += 50 + scor[0] += 50 power = True power_count = 0 eaten_ghosts = [False, False, False, False] if level[center_y // num1][center_x // num2] == 10: level[center_y // num1][center_x // num2] = 0 - shards += 1 - change_screen_shard_collected(screen, WIDTH, HEIGHT) + shards[0] += 1 + change_screen_shard_collected(screen, WIDTH, HEIGHT, shard_list) +# ======= +# shards[0] += 1 +# change_screen_shard_collected(screen, WIDTH, HEIGHT) +# >>>>>>> develop return scor, power, power_count, eaten_ghosts # Draw the board from board.py @@ -952,7 +987,6 @@ def get_targets(blink_x, blink_y, ink_x, ink_y, pink_x, pink_y, clyd_x, clyd_y): run = True delay_duration = 640 last_play_time = 0 - while run: pygame.mixer.init() @@ -1207,19 +1241,19 @@ def get_targets(blink_x, blink_y, ink_x, ink_y, pink_x, pink_y, clyd_x, clyd_y): if powerup and player_circle.colliderect(blinky.rect) and not blinky.dead and not eaten_ghost[0]: blinky_dead = True eaten_ghost[0] = True - score += (2 ** eaten_ghost.count(True)) * 100 + score[0] += (2 ** eaten_ghost.count(True)) * 100 if powerup and player_circle.colliderect(inky.rect) and not inky.dead and not eaten_ghost[1]: inky_dead = True eaten_ghost[1] = True - score += (2 ** eaten_ghost.count(True)) * 100 + score[0] += (2 ** eaten_ghost.count(True)) * 100 if powerup and player_circle.colliderect(pinky.rect) and not pinky.dead and not eaten_ghost[2]: pinky_dead = True eaten_ghost[2] = True - score += (2 ** eaten_ghost.count(True)) * 100 + score[0] += (2 ** eaten_ghost.count(True)) * 100 if powerup and player_circle.colliderect(clyde.rect) and not clyde.dead and not eaten_ghost[3]: clyde_dead = True eaten_ghost[3] = True - score += (2 ** eaten_ghost.count(True)) * 100 + score[0] += (2 ** eaten_ghost.count(True)) * 100 for event in pygame.event.get(): if event.type == pygame.QUIT: @@ -1259,7 +1293,7 @@ def get_targets(blink_x, blink_y, ink_x, ink_y, pink_x, pink_y, clyd_x, clyd_y): inky_dead = False clyde_dead = False pinky_dead = False - score = 0 + score[0] = 0 lives = 3 level = copy.deepcopy(boards) game_over = False @@ -1278,7 +1312,7 @@ def get_targets(blink_x, blink_y, ink_x, ink_y, pink_x, pink_y, clyd_x, clyd_y): mute = not mute if event.key == pygame.K_w: # automatic win condition for demo and stuff - shards = 4 + shards[0] = 4 if direction_command == 0 and turns_allowed[0]: direction = 0 diff --git a/splitImage.py b/splitImage.py new file mode 100644 index 0000000..d6a8dd2 --- /dev/null +++ b/splitImage.py @@ -0,0 +1,40 @@ +from PIL import Image +import os + +def split_image(image_path, destination_path, name): + image = Image.open(image_path) + folder_name = name + folder_path = os.path.join(destination_path, folder_name) + os.makedirs(folder_path, exist_ok=True) + + # Get dimensions + width, height = image.size + + # Calculate the size of each piece + half_width = width // 2 + half_height = height // 2 + + # Define the four pieces + top_left = (0, 0, half_width, half_height) + top_right = (half_width, 0, width, half_height) + bottom_left = (0, half_height, half_width, height) + bottom_right = (half_width, half_height, width, height) + + # Crop the image into four pieces + image_top_left = image.crop(top_left) + image_top_right = image.crop(top_right) + image_bottom_left = image.crop(bottom_left) + image_bottom_right = image.crop(bottom_right) + + image_top_left.save(os.path.join(folder_path, 'top_left.jpg')) + image_top_right.save(os.path.join(folder_path, 'top_right.jpg')) + image_bottom_left.save(os.path.join(folder_path, 'bottom_left.jpg')) + image_bottom_right.save(os.path.join(folder_path, 'bottom_right.jpg')) + + +if __name__ == '__main__': + image_path = "./images/rohan.jpeg" + destination_path = './images' + name = "biking" + + split_image(image_path, destination_path, name) \ No newline at end of file