如何在Pygame绘制的6×6网格中显示数组值并实现交互逻辑
解决方案
1. 在网格单元格中显示matrix文本
要在单元格内渲染文本,需给Grid类添加文本绘制逻辑:
- 让
Grid接收外部传入的matrix数组 - 初始化Pygame字体,计算文本在单元格内的居中位置,将对应值渲染到单元格中
2. 检测玩家所在单元格并执行操作
通过玩家坐标计算对应的网格索引,获取matrix中的值后触发自定义逻辑:
- 用玩家的x/y坐标除以单元格宽高,得到所在的列和行
- 转换为
matrix的一维索引(行号×列数 + 列号) - 根据值的类型(如"P"、数字等)执行对应操作
修改后的完整代码
import pygame # 全局初始化字体 pygame.font.init() FONT = pygame.font.SysFont(None, 30) # 补全6×6网格所需的36个元素 matrix = ["1", "0", "0", "P", "2", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "4", "3", "0", "0", "0", "0", "0", "0"] class Grid: def __init__(self, width, height, rows, cols, matrix): self.width = width self.height = height self.rows = rows self.cols = cols self.matrix = matrix # 存储网格数据 def draw(self, screen): # 绘制网格边框+文本 for col in range(self.cols): for row in range(self.rows): # 绘制单元格边框 pygame.draw.rect(screen, (255, 255, 255), (col*self.width, row*self.height, self.width, self.height), 1) # 获取当前单元格的值 cell_value = self.matrix[row*self.cols + col] if cell_value == "0": continue # 渲染文本并居中绘制 text_surface = FONT.render(cell_value, True, (255, 255, 255)) text_x = col*self.width + (self.width - text_surface.get_width())//2 text_y = row*self.height + (self.height - text_surface.get_height())//2 screen.blit(text_surface, (text_x, text_y)) class Player: def __init__(self, x, y, width, height, color): self.x = x self.y = y self.width = width self.height = height self.color = color self.score = 0 # 用实例属性替代全局变量 def draw(self, screen): pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height)) def move(self, direction, grid): # 移动边界限制 if direction == "up" and self.y > 0: self.y -= self.height elif direction == "down" and self.y < (grid.rows-1)*self.height: self.y += self.height elif direction == "left" and self.x > 0: self.x -= self.width elif direction == "right" and self.x < (grid.cols-1)*self.width: self.x += self.width # 检测当前所在单元格的值 current_col = self.x // grid.width current_row = self.y // grid.height cell_index = current_row * grid.cols + current_col cell_value = grid.matrix[cell_index] # 示例操作逻辑 if cell_value == "P": print("触发事件:找到隐藏道具!") grid.matrix[cell_index] = "0" # 拾取后清空单元格 elif cell_value in ["1","2","3","4"]: self.score += int(cell_value) print(f"获得{cell_value}分,总分:{self.score}") grid.matrix[cell_index] = "0" def show_grid(): pygame.init() screen = pygame.display.set_mode((500, 500)) pygame.display.set_caption("Text Adventure Game") grid = Grid(50, 50, 6, 6, matrix) player = Player(0, 0, 50, 50, (255, 127, 0)) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: player.move("up", grid) elif event.key == pygame.K_DOWN: player.move("down", grid) elif event.key == pygame.K_LEFT: player.move("left", grid) elif event.key == pygame.K_RIGHT: player.move("right", grid) screen.fill((0,0,0)) grid.draw(screen) player.draw(screen) pygame.display.update() pygame.quit() if __name__ == "__main__": show_grid()
关键改动说明
- Grid类:新增
matrix属性存储网格数据,在绘制网格时同步渲染文本,确保每个单元格的值对应正确位置 - Player类:将全局
steps改为实例属性score避免变量污染,移动后自动检测当前单元格的值并执行对应逻辑 - 边界优化:修正了移动时的边界判断,确保玩家不会超出6×6网格范围
内容的提问来源于stack exchange,提问作者Shuvro Basu
相关产品推荐
相关产品推荐

