Crew AI Game Builder
Published: 2024-01-28 05:56:52

**Crew AI Game Builder**

git clone https://github.com/joaomdmoura/crewAI-examples/

cd crewAI-examples/game-builder-crew
pip install crewai langchain-community langchain langchain-core langchain-openai

export OPENAI_API_KEY=xxxxxxxxxxxxxxx

python main.py

Prompt

Shooter game where the player can shoot projectiles at enemies and move around the screen. Enemies should appear at a reasonable pace randomly and increase the pace the appear over time so the game get more difficult. Enemies will get destroyed if the player shoots projectiles that hits them. If the player hit an enemy it's game over. There should be a score on how many enemies the player hits. Your Final answer must be the python code, only the python code and nothing else. 

Output

import pygame
import random

# Screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

# Player attributes
PLAYER_WIDTH = 50
PLAYER_HEIGHT = 50
PLAYER_COLOR = (0,0,255)
PLAYER_SPEED = 5

# Enemy attributes
ENEMY_WIDTH = 50
ENEMY_HEIGHT = 50
ENEMY_COLOR = (255,0,0)
ENEMY_SPEED = 3

# Projectile attributes
PROJECTILE_WIDTH = 10
PROJECTILE_HEIGHT = 10
PROJECTILE_COLOR = (0,255,0)
PROJECTILE_SPEED = 10

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.Surface([PLAYER_WIDTH, PLAYER_HEIGHT])
        self.image.fill(PLAYER_COLOR)
        self.rect = self.image.get_rect()
        self.score = 0

    def update(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            self.rect.x -= PLAYER_SPEED
        if keys[pygame.K_RIGHT]:
            self.rect.x += PLAYER_SPEED
        if keys[pygame.K_UP]:
            self.rect.y -= PLAYER_SPEED
        if keys[pygame.K_DOWN]:
            self.rect.y += PLAYER_SPEED

        if self.rect.x < 0:
            self.rect.x = 0
        if self.rect.x > SCREEN_WIDTH - PLAYER_WIDTH:
            self.rect.x = SCREEN_WIDTH - PLAYER_WIDTH
        if self.rect.y < 0:
            self.rect.y = 0
        if self.rect.y > SCREEN_HEIGHT - PLAYER_HEIGHT:
            self.rect.y = SCREEN_HEIGHT - PLAYER_HEIGHT

class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.Surface([ENEMY_WIDTH, ENEMY_HEIGHT])
        self.image.fill(ENEMY_COLOR)
        self.rect = self.image.get_rect()
        self.rect.x = random.randint(0, SCREEN_WIDTH - ENEMY_WIDTH)
        self.rect.y = -ENEMY_HEIGHT

    def update(self):
        self.rect.y += ENEMY_SPEED
        if self.rect.y > SCREEN_HEIGHT:
            self.rect.y = -ENEMY_HEIGHT
            self.rect.x = random.randint(0, SCREEN_WIDTH - ENEMY_WIDTH)

class Projectile(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()
        self.image = pygame.Surface([PROJECTILE_WIDTH, PROJECTILE_HEIGHT])
        self.image.fill(PROJECTILE_COLOR)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def update(self):
        self.rect.y -= PROJECTILE_SPEED
        if self.rect.y < -PROJECTILE_HEIGHT:
            self.kill()

def game():
    pygame.init()
    screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])

    player = Player()
    player.rect.x = SCREEN_WIDTH // 2
    player.rect.y = SCREEN_HEIGHT // 2

    enemies = pygame.sprite.Group()
    projectiles = pygame.sprite.Group()

    all_sprites = pygame.sprite.Group()
    all_sprites.add(player)

    clock = pygame.time.Clock()

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    projectile = Projectile(player.rect.x, player.rect.y)
                    projectiles.add(projectile)
                    all_sprites.add(projectile)

        if random.randint(0, 50) == 0:
            enemy = Enemy()
            enemies.add(enemy)
            all_sprites.add(enemy)

        player.update()
        enemies.update()
        projectiles.update()

        for projectile in pygame.sprite.groupcollide(projectiles, enemies, True, True).keys():
            player.score += 1
            print(f"Score: {player.score}")

        if pygame.sprite.spritecollide(player, enemies, False):
            print("Game Over!")
            running = False

        screen.fill((0,0,0))
        all_sprites.draw(screen)
        pygame.display.flip()
        clock.tick(60)

    pygame.quit()

if __name__ == "__main__":
    game()