I want to make an AI simulation of hide and seek, the problem is when i spread all the tile i can't make the collision for it so the seeker and the hider keep walking across the wall while the pathfinding movement is running. so how to add map collision in it to keep seeker and hider inside the floor? and could this collision still working in any other pathfinding algorithm? or should it has a matrix map to represent the wall the floor to walk?

here is the code:

import pygame
import random
import pygame_widgets
from pygame_widgets.button import Button
from pygame_widgets.slider import Slider
from pygame_widgets.textbox import TextBox

#inisialisasi tampilan SETUP
widthmap = 640
heightmap= 640
FPS = 60
pygame.init()
win = pygame.display.set_mode((1000, 680))
pygame.display.set_caption('Hide and Seek NEWBY')
icon = pygame.image.load('logo1.png')
pygame.display.set_icon(icon)
layer = pygame.image.load('pix.png')
clock = pygame.time.Clock()

#badan program button untuk fitur
"TOMBOL MULAI"
Mulai = Button(
    win, 740, 10, 100, 40,
    text='Start',
    textColour=(250,250,250),
    fontSize=25,
    margin=20,
    radius=20,
    onClick= lambda: start_game())

"TOMBOL JEDA ATAU LANJUTKAN"    
Jeda = Button(
    win, 740, 70, 200, 40,
    text='Pause or Continue',
    textColour=(250,250,250),
    fontSize=25,
    margin=20,
    radius=20, 
    onClick= lambda: pause())

button3 = Button(
    win, 740, 130, 100, 40,
    text='Random Map',
    textColour=(250,250,250),
    fontSize=20,
    margin=20,
    radius=20,
    onClick=lambda: 
    print('Randomize...'))
button4 = Button(
    win, 740, 190, 150, 40,
    text='Random Start Red',
    textColour=(250,250,250),
    fontSize=20,
    margin=20,
    radius=20,
    onClick=lambda: 
    print('Ready'))
button5 = Button(
    win, 740, 250, 150, 40,
    text='Random Start Green',
    textColour=(250,250,250),
    fontSize=20,
    margin=20,
    radius=20,
    onClick=lambda: 
    print('Ready'))
button6 = Button(
    win, 740, 310, 150, 40,
    text='Add More Red Droid',
    textColour=(250,250,250),
    fontSize=20,
    margin=20,
    radius=20,
    onClick=lambda: 
    print('Adding Process...'))
button7 = Button(
    win,740, 370, 100, 40,
    text='Red Sight',
    textColour=(250,250,250),
    fontSize=20,
    margin=20,
    radius=20,
    onClick=lambda: 
    print('Its Clear'))
button8 = Button(
    win, 740, 430, 100, 40,
    text='Green Sight',
    textColour=(250,250,250),
    fontSize=20,
    margin=20,
    radius=20,
    onClick=lambda: 
    print('Its Dark'))
slidename = Button(
    win, 780, 510, 200, 40,
    text='GREEN SIGHT CONTROLLER',
    fontSize=20,
    margin=20,
    inactiveColour=(250, 250, 250),  # krn text itu sulit jadi pakai button, ini biar putih
    hoverColour=(250, 250, 250),  
    pressedColour=(250, 250, 250))

slider = Slider(win, 740, 490, 230, 10, min=1, max=10, step=1)
output = TextBox(win, 740, 510, 40, 40, fontSize=30)
output.disable()  # Act as label instead of textbox, textbox untuk display dinamis saja bukan     input/ketik
"BATAS KANAN DARI BUTTON ATAU AWAL LAYAR UNTUK SIMULASI HIDE AND SEEK NYA ADALAH X=1000-260 Y= 0     SAMPAI 600"


#Tile
tile_size = 32
tileset = pygame.image.load("tile.png")
tileset = pygame.transform.scale(tileset, (tile_size, tile_size))

#banyak cell
cellw= 20           #tidak full agar jika tile muncul di tengah secara banyak
cellh= 20           # dan menghalangi droid untuk berjalan maka masih adaa jalan di sekitar     pinggir peta untuk berjalan
                 #sehingga harus dikurang sederet x,y 1 cell untuk bagian kanan

# Mengacak posisi tile/wall
def acak_tile():
    tile_map = [[0] * cellh for _ in range(cellw)]
    byk_tile = random.randint(50, 150)                #mulai dari 50 hingga 150 tile
    for _ in range(byk_tile):
        x = random.randint(0, cellw - 1)     # dari angka 1 --> untuk mengurangi 1 cell x,y disebelah kiri peta yaitu tile  dimulai dari cell ke-2 x,y [row] [col]   
        y = random.randint(0, cellh - 1)     # jika dari angka nol maka dimulai dari cell 1 x,y
        tile_map[x][y] = 1
    return tile_map

tile_map = acak_tile()

# Hider
hijauImg = pygame.image.load('Hijau.png')
hijauX = random.randint(0,608)
hijauY = random.randint(0,608)
hijauX_change = 1 
hijauY_change = 0

# Seeker
merahImg = pygame.image.load('Merah.png')
merahX = random.randint(0,608) #karena 640px-32px
merahY = random.randint(0,608)   

#fov
fov_img = pygame.image.load("kuning.png")
fov_radius = 200

def hijau(x, y):
    win.blit(hijauImg, (x, y))

def merah(x, y):
    win.blit(merahImg, (x, y))

# Fungsi untuk menggambar tile
def tile(x, y):
    win.blit(tileset, (x * tile_size, y * tile_size))

def hijau_fov():
    fov_rect = fov_img.get_rect()
    fov_rect.center = hijauX+16,hijauY+16 #ditambah 16 krn setengah dri 32 adalah 16 yg mna x     dan y fov harus ditengah droid
    win.blit(fov_img, fov_rect)


# Fungsi untuk mengubah arah langkah
def change_direction():
    return random.choice(['up', 'down', 'left', 'right'])

# Inisialisasi arah langkah awal
direction = change_direction()

#status    
game_started = False
game_paused = False


# Fungsi untuk memulai
def start_game():
    global game_started
    game_started = True

# Fungsi untuk jeda
def pause():
    global game_paused
    game_paused = not game_paused #yaitu bukan false atau sama dengan true

#perulangan
run = True
while run:
           
    events = pygame.event.get()   
    for event in events:
        if event.type == pygame.QUIT:
            pygame.quit()
            run = False
            quit()
        
#background warna dasar putih                         
    win.fill((255, 255, 255))
    win.blit(layer, (-2, -2))
    hijau_fov()   

    # Gambar tile berdasarkan data peta
    for x in range(cellw):
        for y in range(cellh):
            if tile_map[x][y] == 1:
                tile(x, y)
                            
    #saat mulai
    if game_started and not game_paused:            
   #gerak si hijau random walk
        if random.randint(0, 60) == 0:
            direction = change_direction()

        if direction == 'up':
            hijauY -= 1
        elif direction == 'down':
            hijauY += 1
        elif direction == 'left':
            hijauX -= 1
        elif direction == 'right':
            hijauX += 1
        
        hijauX = max(0, min(hijauX, 608 - 1))
        hijauY = max(0, min(hijauY, 608 - 1))
    
    #dynamic following si merah
        x3 = hijauX - merahX
        y3 = hijauY - merahY
    
        if merahX > hijauX: 
            merahX -= 1
            merahX == x3
        elif merahX < hijauX: 
            merahX += 1
            merahX == x3
        if merahY > hijauY:    
            merahY -= 1 
            merahY == y3
        elif merahY < hijauY:    
            merahY += 1 
            merahY == y3
        #if merahX == hijauX and merahY == hijauY:
        #    run = False
    
#memanggil loop dan render/penyatuan    
    pygame_widgets.update(events)
    output.setText(slider.getValue())
    hijau(hijauX, hijauY)
    merah(merahX, merahY)
    pygame.display.flip()
#Menampilkan hasil gambaran/program di layar
    pygame.display.update()
#frame per detik
    clock.tick(FPS)
0

There are 0 best solutions below