[파이썬] pygame 캐릭터 선택 및 커스터마이제이션

Pygame is a popular library for creating games in Python. In this blog post, we will explore how to implement character selection and customization in a pygame game.

Setting up the Game Window

Before we dive into character selection and customization, let’s first set up the basic game window using pygame.

import pygame

pygame.init()

width = 800
height = 600

win = pygame.display.set_mode((width, height))
pygame.display.set_caption("Character Selection Demo")

In the above code, we import the pygame library, initialize it, and create a game window with a specified width and height. We also set the window caption to “Character Selection Demo”.

Implementing Character Selection

To allow players to select a character, we can display a list of available characters and let the player choose one. We can use pygame’s sprite class to represent each character.

class Character(pygame.sprite.Sprite):
    def __init__(self, name, image, x, y):
        super().__init__()
        self.name = name
        self.image = pygame.image.load(image)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
    
    def draw(self, win):
        win.blit(self.image, (self.rect.x, self.rect.y))

character_list = [
    Character("Character 1", "character1.png", 100, 100),
    Character("Character 2", "character2.png", 300, 100),
    Character("Character 3", "character3.png", 500, 100)
]

selected_character = None
character_selected = False

running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pos = pygame.mouse.get_pos()
            for character in character_list:
                if character.rect.collidepoint(mouse_pos):
                    selected_character = character
                    character_selected = True

    win.fill((255, 255, 255))

    if not character_selected:
        for character in character_list:
            character.draw(win)
    else:
        selected_character.draw(win)

    pygame.display.update()

pygame.quit()

In the above code, we create a Character class that inherits from pygame.sprite.Sprite. Each character is represented as an instance of this class, with a name, image, and position on the screen.

We define a list of characters and loop through them to check if the player has clicked on a character. If a character is clicked, we set it as the selected_character and set character_selected to true.

In the main game loop, we first check if character_selected is false, in which case we display the list of characters. If character_selected is true, we only display the selected character.

Adding Customization Options

Now that we have implemented character selection, let’s add some customization options.

class Character:
    def __init__(self, name, image, x, y):
        self.name = name
        self.image = pygame.image.load(image)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

        self.customizations = {
            "hairstyle": "default",
            "clothing": "default",
            "weapon": "default"
        }
    
    def draw(self, win):
        win.blit(self.image, (self.rect.x, self.rect.y))

    def customize(self, customization_type, option):
        self.customizations[customization_type] = option

character = Character("Character 1", "character1.png", 100, 100)

available_hairstyles = ["hairstyle1.png", "hairstyle2.png", "hairstyle3.png"]
available_clothing = ["clothing1.png", "clothing2.png", "clothing3.png"]
available_weapons = ["weapon1.png", "weapon2.png", "weapon3.png"]

running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pos = pygame.mouse.get_pos()

            # Logic for customizing the character's hairstyle
            if 100 <= mouse_pos[0] <= 180 and 400 <= mouse_pos[1] <= 480:
                hairstyle_index = (mouse_pos[0] - 100) // 20
                character.customize("hairstyle", available_hairstyles[hairstyle_index])

            # Logic for customizing the character's clothing
            elif 250 <= mouse_pos[0] <= 330 and 400 <= mouse_pos[1] <= 480:
                clothing_index = (mouse_pos[0] - 250) // 20
                character.customize("clothing", available_clothing[clothing_index])

            # Logic for customizing the character's weapon
            elif 400 <= mouse_pos[0] <= 480 and 400 <= mouse_pos[1] <= 480:
                weapon_index = (mouse_pos[0] - 400) // 20
                character.customize("weapon", available_weapons[weapon_index])

    win.fill((255, 255, 255))

    # Draw the character
    character.draw(win)

    # Draw customization options
    for i, hairstyle in enumerate(available_hairstyles):
        win.blit(pygame.image.load(hairstyle), (100 + i * 20, 400))
    
    for i, clothing in enumerate(available_clothing):
        win.blit(pygame.image.load(clothing), (250 + i * 20, 400))
    
    for i, weapon in enumerate(available_weapons):
        win.blit(pygame.image.load(weapon), (400 + i * 20, 400))

    pygame.display.update()

pygame.quit()

In the above code, we have added a customizations dictionary in the Character class to store the current customization options for the character. We also define lists of available options for each customization category (hairstyle, clothing, and weapon).

In the main game loop, we check if the player has clicked on a specific area on the screen corresponding to each customization category. If a click is detected, we update the character’s customization option accordingly.

We also display the available customization options on the screen using the blit function.

Conclusion

In this blog post, we explored how to implement character selection and customization in a pygame game. By allowing players to choose and customize their character, you can add an extra layer of interactivity and personalization to your game. Happy coding!