Snippets Collections
Sure! Here's a simple example of an RPG game powered by ChatGPT where you can interact with NPC characters using JavaScript and Phaser:

1. Initialize Phaser:

```javascript
const config = {
  type: Phaser.AUTO,
  width: 800,
  height: 600,
  scene: {
    preload: preload,
    create: create
  }
};

const game = new Phaser.Game(config);
```

2. Preload assets:

```javascript
function preload() {
  this.load.image('player', 'path/to/player.png');
  this.load.image('npc', 'path/to/npc.png');
}
```

3. Create characters and chat functionality:

```javascript
function create() {
  // Create player
  const player = this.physics.add.sprite(400, 300, 'player');

  // Create NPCs
  const npc1 = this.physics.add.sprite(200, 200, 'npc');
  const npc2 = this.physics.add.sprite(600, 400, 'npc');

  // Enable interactions on NPCs
  npc1.setInteractive();
  npc2.setInteractive();

  // Enable physics for player and NPCs
  this.physics.add.collider(player, npc1);
  this.physics.add.collider(player, npc2);

  const chatBox = document.createElement('div');
  chatBox.style.position = 'absolute';
  chatBox.style.bottom = '0';
  chatBox.style.padding = '10px';
  chatBox.style.width = '100%';
  document.body.appendChild(chatBox);

  npc1.on('pointerdown', () => {
    const message = 'Hello! How can I help you?'; // Send player's message to ChatGPT to generate NPC's response
    const npcResponse = 'Response from ChatGPT'; // Replace with actual response from ChatGPT
    displayMessage(message, npcResponse);
  });

  npc2.on('pointerdown', () => {
    const message = 'Hi! What do you want to know?'; // Send player's message to ChatGPT to generate NPC's response
    const npcResponse = 'Response from ChatGPT'; // Replace with actual response from ChatGPT
    displayMessage(message, npcResponse);
  });

  function displayMessage(playerMessage, npcMessage) {
    const message = document.createElement('p');
    message.textContent = 'Player: ' + playerMessage;
    chatBox.appendChild(message);

    const response = document.createElement('p');
    response.textContent = 'NPC: ' + npcMessage;
    chatBox.appendChild(response);
  }
}
```

Replace `path/to/player.png` and `path/to/npc.png` with the actual paths to your sprite images.

Note: This example demonstrates the basic structure of an RPG game with NPC characters. You need to implement the ChatGPT integration separately to generate NPC responses based on the player's input.

Make sure you have Phaser installed to run this code. You can install it using npm: `npm install phaser`.
data = [
    {
        'name': 'Instagram',
        'follower_count': 346,
        'description': 'Social media platform',
        'country': 'United States'
    },
    {
        'name': 'Cristiano Ronaldo',
        'follower_count': 215,
        'description': 'Footballer',
        'country': 'Portugal'
    },
    {
        'name': 'Ariana Grande',
        'follower_count': 183,
        'description': 'Musician and actress',
        'country': 'United States'
    },
    {
        'name': 'Dwayne Johnson',
        'follower_count': 181,
        'description': 'Actor and professional wrestler',
        'country': 'United States'
    },
    {
        'name': 'Selena Gomez',
        'follower_count': 174,
        'description': 'Musician and actress',
        'country': 'United States'
    },
    {
        'name': 'Kylie Jenner',
        'follower_count': 172,
        'description': 'Reality TV personality and businesswoman and Self-Made Billionaire',
        'country': 'United States'
    },
    {
        'name': 'Kim Kardashian',
        'follower_count': 167,
        'description': 'Reality TV personality and businesswoman',
        'country': 'United States'
    },
    {
        'name': 'Lionel Messi',
        'follower_count': 149,
        'description': 'Footballer',
        'country': 'Argentina'
    },
    {
        'name': 'Beyoncé',
        'follower_count': 145,
        'description': 'Musician',
        'country': 'United States'
    },
    {
        'name': 'Neymar',
        'follower_count': 138,
        'description': 'Footballer',
        'country': 'Brasil'
    },
    {
        'name': 'National Geographic',
        'follower_count': 135,
        'description': 'Magazine',
        'country': 'United States'
    },
    {
        'name': 'Justin Bieber',
        'follower_count': 133,
        'description': 'Musician',
        'country': 'Canada'
    },
    {
        'name': 'Taylor Swift',
        'follower_count': 131,
        'description': 'Musician',
        'country': 'United States'
    },
    {
        'name': 'Kendall Jenner',
        'follower_count': 127,
        'description': 'Reality TV personality and Model',
        'country': 'United States'
    },
    {
        'name': 'Jennifer Lopez',
        'follower_count': 119,
        'description': 'Musician and actress',
        'country': 'United States'
    },
    {
        'name': 'Nicki Minaj',
        'follower_count': 113,
        'description': 'Musician',
        'country': 'Trinidad and Tobago'
    },
    {
        'name': 'Nike',
        'follower_count': 109,
        'description': 'Sportswear multinational',
        'country': 'United States'
    },
    {
        'name': 'Khloé Kardashian',
        'follower_count': 108,
        'description': 'Reality TV personality and businesswoman',
        'country': 'United States'
    },
    {
        'name': 'Miley Cyrus',
        'follower_count': 107,
        'description': 'Musician and actress',
        'country': 'United States'
    },
    {
        'name': 'Katy Perry',
        'follower_count': 94,
        'description': 'Musician',
        'country': 'United States'
    },
    {
        'name': 'Kourtney Kardashian',
        'follower_count': 90,
        'description': 'Reality TV personality',
        'country': 'United States'
    },
    {
        'name': 'Kevin Hart',
        'follower_count': 89,
        'description': 'Comedian and actor',
        'country': 'United States'
    },
    {
        'name': 'Ellen DeGeneres',
        'follower_count': 87,
        'description': 'Comedian',
        'country': 'United States'
    },
    {
        'name': 'Real Madrid CF',
        'follower_count': 86,
        'description': 'Football club',
        'country': 'Spain'
    },
    {
        'name': 'FC Barcelona',
        'follower_count': 85,
        'description': 'Football club',
        'country': 'Spain'
    },
    {
        'name': 'Rihanna',
        'follower_count': 81,
        'description': 'Musician and businesswoman',
        'country': 'Barbados'
    },
    {
        'name': 'Demi Lovato',
        'follower_count': 80,
        'description': 'Musician and actress',
        'country': 'United States'
    },
    {
        'name': "Victoria's Secret",
        'follower_count': 69,
        'description': 'Lingerie brand',
        'country': 'United States'
    },
    {
        'name': 'Zendaya',
        'follower_count': 68,
        'description': 'Actress and musician',
        'country': 'United States'
    },
    {
        'name': 'Shakira',
        'follower_count': 66,
        'description': 'Musician',
        'country': 'Colombia'
    },
    {
        'name': 'Drake',
        'follower_count': 65,
        'description': 'Musician',
        'country': 'Canada'
    },
    {
        'name': 'Chris Brown',
        'follower_count': 64,
        'description': 'Musician',
        'country': 'United States'
    },
    {
        'name': 'LeBron James',
        'follower_count': 63,
        'description': 'Basketball player',
        'country': 'United States'
    },
    {
        'name': 'Vin Diesel',
        'follower_count': 62,
        'description': 'Actor',
        'country': 'United States'
    },
    {
        'name': 'Cardi B',
        'follower_count': 67,
        'description': 'Musician',
        'country': 'United States'
    },
    {
        'name': 'David Beckham',
        'follower_count': 82,
        'description': 'Footballer',
        'country': 'United Kingdom'
    },
    {
        'name': 'Billie Eilish',
        'follower_count': 61,
        'description': 'Musician',
        'country': 'United States'
    },
    {
        'name': 'Justin Timberlake',
        'follower_count': 59,
        'description': 'Musician and actor',
        'country': 'United States'
    },
    {
        'name': 'UEFA Champions League',
        'follower_count': 58,
        'description': 'Club football competition',
        'country': 'Europe'
    },
    {
        'name': 'NASA',
        'follower_count': 56,
        'description': 'Space agency',
        'country': 'United States'
    },
    {
        'name': 'Emma Watson',
        'follower_count': 56,
        'description': 'Actress',
        'country': 'United Kingdom'
    },
    {
        'name': 'Shawn Mendes',
        'follower_count': 57,
        'description': 'Musician',
        'country': 'Canada'
    },
    {
        'name': 'Virat Kohli',
        'follower_count': 55,
        'description': 'Cricketer',
        'country': 'India'
    },
    {
        'name': 'Gigi Hadid',
        'follower_count': 54,
        'description': 'Model',
        'country': 'United States'
    },
    {
        'name': 'Priyanka Chopra Jonas',
        'follower_count': 53,
        'description': 'Actress and musician',
        'country': 'India'
    },
    {
        'name': '9GAG',
        'follower_count': 52,
        'description': 'Social media platform',
        'country': 'China'
    },
    {
        'name': 'Ronaldinho',
        'follower_count': 51,
        'description': 'Footballer',
        'country': 'Brasil'
    },
    {
        'name': 'Maluma',
        'follower_count': 50,
        'description': 'Musician',
        'country': 'Colombia'
    },
    {
        'name': 'Camila Cabello',
        'follower_count': 49,
        'description': 'Musician',
        'country': 'Cuba'
    },
    {
        'name': 'NBA',
        'follower_count': 47,
        'description': 'Club Basketball Competition',
        'country': 'United States'
    }
]
logo = """
    __  ___       __             
   / / / (_)___ _/ /_  ___  _____
  / /_/ / / __ `/ __ \/ _ \/ ___/
 / __  / / /_/ / / / /  __/ /    
/_/ ///_/\__, /_/ /_/\___/_/     
   / /  /____/_      _____  _____
  / /   / __ \ | /| / / _ \/ ___/
 / /___/ /_/ / |/ |/ /  __/ /    
/_____/\____/|__/|__/\___/_/     
"""

vs = """
 _    __    
| |  / /____
| | / / ___/
| |/ (__  ) 
|___/____(_)
"""
# Import art & game data
from art import logo, vs
from game_data import data
import random
# from replit import clear
import os    # use system clear to clear screen


# Linux version
clear = lambda: os.system('clear')
# Windows version
# clear = lambda: os.system('cls')


def new_high_low():
    """
    Generate unique new item(s) to compare
    """
    
    compare["A"] = compare["C"]
    compare["B"] = random.choice(data)
    # Ensure the values are NOT the same!
    while compare["B"] == compare["A"]:
        compare["B"] = random.choice(data)

        
def display_compare():
    """
    Function to display comparisons, collect and return choice
    """
    
    clear()
    print(logo)
    vowels = ["a","e","i","o","u"]
    new_high_low()
    if score:
        print(f"You're right! Current score: {score}.")
    print(f"Compare A: {compare['A']['name']}, a{'n' if compare['A']['description'][0].lower() in vowels else ''} {compare['A']['description']}, {compare['A']['country']}")
    print(vs)
    print(f"Against B: {compare['B']['name']}, a{'n' if compare['B']['description'][0].lower() in vowels else ''} {compare['B']['description']}, {compare['B']['country']}")
    choice = input("\nWho has more followers? Type 'A' or 'B': ").upper()
    if choice == "A":
        return "A","B"
    return "B","A"


def higher_lower(answer):
    """
    Return True/False whether answer correct and, if True, capture answer
    """
    if compare[answer[0]]["follower_count"] > compare[answer[1]]["follower_count"]:
        compare["C"] = compare[answer[0]]
        return True
    return False

    
#Initialise Dictionary to hold values for comparison
compare = {}
compare["C"] = random.choice(data)

# Start game here...

play_again = True
while play_again:
    score = 0
    end_game = False
    
    while not end_game:
        if higher_lower(display_compare()):
            score += 1
        else:
            end_game = True
            clear()
            print(logo)
            if input(f"Sorry, that's wrong. Final score: {score}.\nGame over. Play again? Type 'y' or 'n': ").lower() == 'n':
                play_again = False
            
star

Thu Aug 03 2023 00:36:43 GMT+0000 (Coordinated Universal Time)

#game #rpg #npc
star

Sat Sep 03 2022 15:59:13 GMT+0000 (Coordinated Universal Time)

#python #higher_lower #game #game_data.py
star

Sat Sep 03 2022 15:56:54 GMT+0000 (Coordinated Universal Time)

#python #higher_lower #game #art.py
star

Sat Sep 03 2022 15:55:49 GMT+0000 (Coordinated Universal Time)

#python #higher_lower #game #code

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension