为什么不检查每个图块?

问题描述 投票:0回答:1

我正在为我正在制作的平台游戏编写生命逻辑游戏代码,但它不会检查每个图块,并且只给我一个对象位置

"<scripts.tilemap.Tilemap object at 0x000001FFBEE1DC40>"

这是我的游戏逻辑:

def is_tile_alive(self, pos):   
    tile_loc = (int(pos[0] // self.tile_size), int(pos[1] // self.tile_size))
    tile_key = f"{tile_loc[0]};{tile_loc[1]}"
    if tile_key in self.tilemap and self.tilemap[tile_key]['type'] in ALIVE_TILE_TYPES:
        print('tile is there')
        return True
    return False

def logic(self, pos, offset):
    neighbors = 0
    tile_loc = (int(pos[0] // self.tile_size), int(pos[1] // self.tile_size))

    for offset in NEIGHBOR_OFFSETS:
        neighbor_pos = (tile_loc[0] + offset[0], tile_loc[1] + offset[1])
        if self.is_tile_alive(neighbor_pos):
            print('neighbor is alive')
            neighbors += 1

    current_tile_alive = self.is_tile_alive(tile_loc)
    if current_tile_alive and (neighbors < 2 or neighbors > 3):           
        load_image('tiles/ntile/ntile.png')
    elif not current_tile_alive and neighbors == 3:
            self.render(self.tile)
    elif current_tile_alive and neighbors == 3:
        pass

这很可能是我的问题

if tile_key in self.tilemap and self.tilemap[tile_key]['type'] in ALIVE_TILE_TYPES:

我尝试了不同的方法来更改它,它改变了位置,但仍然没有给我所有的图块位置

这是我的瓷砖地图脚本

import json
import pygame
from scripts.utils import load_image, load_images

NEIGHBOR_OFFSETS = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
PHYSICS_TILE = {'tiles'}
ALIVE_TILE_TYPES = {'tile.png'}

class Tilemap:
    def __init__(self, game, tile_size=16):
        self.game = game
        self.tile_size = tile_size
        self.tilemap = {}
        self.offgrid_tiles = []
        self.timer_event = pygame.USEREVENT + 1  
        pygame.time.set_timer(self.timer_event, 5000)
        
    def extract(self, id_pairs, keep=False):
        matches = []
        for tile in self.offgrid_tiles.copy():
            if (tile['type'], tile['variant']) in id_pairs:
                matches.append(tile.copy())
                if not keep:
                    self.offgrid_tiles.remove(tile)
                    
        for loc in self.tilemap:
            tile = self.tilemap[loc]
            if (tile['type'], tile['variant']) in id_pairs:
                matches.append(tile.copy())
                matches[-1]['pos'] = matches[-1]['pos'].copy()
                matches[-1]['pos'][0] *= self.tile_size
                matches[-1]['pos'][1] *= self.tile_size
                if not keep:
                    del self.tilemap[loc]
        
        return matches

    def tiles_around(self, pos):
        tiles = []
        tile_loc = (int(pos[0] // self.tile_size), int(pos[1] // self.tile_size))
        for offset in NEIGHBOR_OFFSETS:
            check_loc = str(tile_loc[0] + offset[0]) + ';' + str(tile_loc[1] + offset[1])
            if check_loc in self.tilemap:
                tiles.append(self.tilemap[check_loc])
        return tiles
    
    def save(self, path):
        f = open(path, 'w')
        json.dump({'tilemap': self.tilemap, 'tile_size': self.tile_size, 'offgrid': self.offgrid_tiles}, f)
        f.close()
        
    def load(self, path):
        f = open(path, 'r')
        map_data = json.load(f)
        f.close()
        
        self.tilemap = map_data['tilemap']
        self.tile_size = map_data['tile_size']
        self.offgrid_tiles = map_data['offgrid']
    
    def physics_rects_around(self, pos):
        rects = []
        for tile in self.tiles_around(pos):
            if tile['type'] in PHYSICS_TILE:
                rects.append(pygame.Rect(tile['pos'][0] * self.tile_size -8, tile['pos'][1] * self.tile_size, self.tile_size, self.tile_size))
        return rects

    def render(self, surf, offset=(0, 0)):
        for tile in self.offgrid_tiles:
            surf.blit(self.game.assets[tile['type']], (tile['pos'][0] - offset[0], tile['pos'][1] - offset[1]))
            
        for x in range(offset[0] // self.tile_size, (offset[0] + surf.get_width()) // self.tile_size + 1):
            for y in range(offset[1] // self.tile_size, (offset[1] + surf.get_height()) // self.tile_size + 1):
                loc = str(x) + ';' + str(y)
                if loc in self.tilemap:
                    tile = self.tilemap[loc]
                    surf.blit(self.game.assets[tile['type']], (tile['pos'][0] * self.tile_size - offset[0], tile['pos'][1] * self.tile_size - offset[1]))

    def is_tile_alive(self, pos):   
        tile_loc = (int(pos[0] // self.tile_size), int(pos[1] // self.tile_size))
        tile_key = f"{tile_loc[0]};{tile_loc[1]}"
        if tile_key in self.tilemap and self.tilemap[tile_key]['type'] in ALIVE_TILE_TYPES:
            print('tile is there')
            return True
        return False

    def logic(self, pos, offset):
        neighbors = 0
        tile_loc = (int(pos[0] // self.tile_size), int(pos[1] // self.tile_size))
    
        for offset in NEIGHBOR_OFFSETS:
            neighbor_pos = (tile_loc[0] + offset[0], tile_loc[1] + offset[1])
            if self.is_tile_alive(neighbor_pos):
                print('neighbor is alive')
                neighbors += 1

        current_tile_alive = self.is_tile_alive(tile_loc)
        if current_tile_alive and (neighbors < 2 or neighbors > 3):           
            load_image('tiles/ntile/ntile.png')
        elif not current_tile_alive and neighbors == 3:
                self.render(self.tile)
        elif current_tile_alive and neighbors == 3:
            pass 
python pygame
1个回答
0
投票

使用numpy它可以工作 def is_alive(self, pos):
return str(np.sum(grid[pos[0]-1:pos[0]+2,pos[1]-1:pos[1]+2])) - grid[pos[0]][pos[ 1]]

© www.soinside.com 2019 - 2024. All rights reserved.