简体   繁体   中英

Having a Sprite Follow a Line of a Specific Color Pygame

I am attempting to make a version of pacman where he and the ghosts can only move on roads from a google map. I understand the concept of using grids to act as boundaries, but that only works for square gameboards. Therefore I am looking for some guidance on how to get a sprite to move only on a predrawn line of a specific color.

A possible map I could use as a gameboard could be this one . Within this map you can see there are only two colors used, an off grey, and a black, plus the image has some curves in it. Any advice on how to make this work would be appreciated, thanks everyone!

There's a tostring() function in pygame.image which returns a bytes object that represents the image. More about it here: https://www.pygame.org/docs/ref/image.html#pygame.image.tostring

You can get the color of every pixel if you calculate its position correctly. Anyway, this seemed fun so I wrote a little program using the image you linked to. This code is kinda buggy and can definitly be improved, and the player goes to the darkest square he can find, not necessarily black, but this is just to give a general idea:

import pygame


def draw_player(pos):
    # Draw a red square as the player
    pygame.display.update(pygame.draw.rect(screen, (255, 0, 0), (pos[0], pos[1], 10, 10)))


def pos_rgb_sum(pos):
    str_pos = 3 * (pos[1] * bg_size[0] + pos[0])  # The position of current pixel in bg_str. Remember every pixel has 3 values
    return sum(bg_str[str_pos: str_pos + 3])


def darkest_neighbor(pos):
    # Return the darkest neighbor for the position on the board
    darkest_pos, darkest_val = None, 255 * 3 + 1  # So that every value is darker than starting value
    for x in range(pos[0] - 1, pos[0] + 2):
        for y in range(pos[1] - 1, pos[1] + 2):
            if (x, y) == pos:
                continue
            curr_darkness = pos_rgb_sum((x, y))
            if curr_darkness <= darkest_val and (x, y) not in visited:  # Try to create movement
                darkest_val = curr_darkness
                darkest_pos = (x, y)

    return darkest_pos


pygame.init()
# Load image and get its string representation
background = pygame.transform.smoothscale(pygame.image.load("wzcPy.png"), (500, 500))
bg_str = pygame.image.tostring(background, "RGB")
bg_size = background.get_size()
screen = pygame.display.set_mode(bg_size)
screen.blit(background, (0, 0))
pygame.display.update()

# A set that will contain positions visited by player
visited = set()

player_pos = None
for x in range(background.get_width()):  # Find a black square to position player
    for y in range(background.get_height()):
        if pos_rgb_sum((x, y)) == 0:
            player_pos = (x, y)
            break
    if player_pos is not None:
        break

while pygame.event.wait().type != pygame.QUIT:
    visited.add(player_pos)
    draw_player(player_pos)
    player_pos = darkest_neighbor(player_pos)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM