【问题标题】:How do I move elements of a matrix in python?如何在python中移动矩阵的元素?
【发布时间】:2020-12-07 01:28:41
【问题描述】:

我想用键盘移动对象 1,使用 pygame 中的事件,例如:pygame.key.get_pressed()。但我不知道如何将它放在我的代码中。

import pygame
pygame.init()
tela = pygame.display.set_mode((1000, 560))
jogo = True

class Tabuleiro:
    def __init__(self, tela, x, y):
        self.x = x
        self.y = y
        self.tela = tela
        self.maze = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                     [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
                     [1, 0, 1, 1, 0, 1, 3, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1],
                     [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1],
                     [1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1],
                     [1, 0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1],
                     [1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1],
                     [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1],
                     [1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1],
                     [1, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
                     [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

    def show(self):
        for col in range(20):
            for lin in range(11):
                if self.maze[lin][col] == 1:
                    self.tela.fill((255, 255, 255), rect=[self.x + col * 49, self.y + lin * 49, 49, 49])
                if self.maze[lin][col] == 2:
                    self.tela.fill((255, 255, 0), rect=[self.x + col * 30, self.y + lin * 30, 25, 25])
                if self.maze[lin][col] == 3:
                    self.tela.fill((255, 0, 0), rect=[self.x + col * 50, self.y + lin * 50, 25, 25])


maze = tabuleiro.Tabuleiro(tela, 10, 10)

while jogo:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            jogo = False

    tela.fill(0)
    maze.show()
    pygame.display.update()

而且我也不知道如何使用碰撞函数,这段代码是用于我在学校作业中必须做的吃豆人游戏。如果我也能得到一些关于它的提示,我很高兴。谢谢。

【问题讨论】:

    标签: python matrix pygame


    【解决方案1】:

    首先,您需要修复迷宫的表示。每个单元格的左上角是self.x + col * 50, self.y + lin * 50

    class Tabuleiro:
        # [...]
    
        def show(self):
            for col in range(20):
                for lin in range(11):
                    if self.maze[lin][col] == 1:
                        self.tela.fill((255, 255, 255), rect=[self.x + col * 50, self.y + lin * 50, 50, 50])
                    if self.maze[lin][col] == 2:
                        self.tela.fill((255, 255, 0), rect=[self.x + col * 50 + 12, self.y + lin * 50 + 12, 25, 25])
                    if self.maze[lin][col] == 3:
                        self.tela.fill((255, 0, 0), rect=[self.x + col * 50 + 12, self.y + lin * 50 + 12, 25, 25])
    

    您需要在Tabuleiro 类中添加一些方法。添加一个可以在网格中找到特定number的方法:

    class Tabuleiro:
        # [...]
    
        def findFirst(self, number):
            for row, rowlist in enumerate(self.maze):
                for col, cell in enumerate(rowlist):
                    if cell == number:
                        return row, col
            return None, None
    

    添加一个测试行索引 (row) 和列索引 (col) 是否有效的方法:

    class Tabuleiro:
        # [...]
    
        def testIndex(self, row, col):
            if row == None or col == None:
                return False
            if 0 <= row < len(self.maze) and 0 <= col < len(self.maze[row]):
                return True
            return False
    

    添加一个方法来测试一个单元格的内容是否是特定的number

    class Tabuleiro:
        # [...]
    
        def isFieldEqual(self, row, col, number):
            if self.testIndex(row, col):
                return self.maze[row][col] == number
            return False 
    

    添加一个交换两个单元格内容的方法:

    class Tabuleiro:
        # [...]
    
        def swapFileds(self, row1, col1, row2, col2):
            if self.testIndex(row1, col1) and self.testIndex(row2, col2):
                self.maze[row1][col1], self.maze[row2][col2] = self.maze[row2][col2], self.maze[row1][col1]
    

    在 Pygame 中有几种检测按键的方法。见How to get if a key is pressed pygame

    例如,使用 `KEYDOWN 事件。无论何时要移动对象,都需要测试要移动到的单元格是否为空 (0)。移动时,只交换单元格的内容:

    while jogo:
    
        row, col = maze.findFirst(2)
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                jogo = False
    
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    if maze.isFieldEqual(row, col-1, 0):
                        maze.swapFileds(row, col-1, row, col)
                if event.key == pygame.K_RIGHT:
                    if maze.isFieldEqual(row, col+1, 0):
                        maze.swapFileds(row, col+1, row, col)
                if event.key == pygame.K_UP:
                    if maze.isFieldEqual(row-1, col, 0):
                        maze.swapFileds(row, col, row-1, col)
                if event.key == pygame.K_DOWN:
                    if maze.isFieldEqual(row+1, col, 0):
                        maze.swapFileds(row+1, col, row, col)
    

    小例子:

    import pygame
    pygame.init()
    tela = pygame.display.set_mode((1000, 560))
    jogo = True
    
    class Tabuleiro:
        def __init__(self, tela, x, y):
            self.x = x
            self.y = y
            self.tela = tela
            self.maze = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                         [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
                         [1, 0, 1, 1, 0, 1, 3, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1],
                         [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1],
                         [1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1],
                         [1, 0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1],
                         [1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1],
                         [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1],
                         [1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1],
                         [1, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
                         [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
    
        def show(self):
            for col in range(20):
                for lin in range(11):
                    if self.maze[lin][col] == 1:
                        self.tela.fill((255, 255, 255), rect=[self.x + col * 50, self.y + lin * 50, 50, 50])
                    if self.maze[lin][col] == 2:
                        self.tela.fill((255, 255, 0), rect=[self.x + col * 50 + 12, self.y + lin * 50 + 12, 25, 25])
                    if self.maze[lin][col] == 3:
                        self.tela.fill((255, 0, 0), rect=[self.x + col * 50 + 12, self.y + lin * 50 + 12, 25, 25])
    
        def findFirst(self, number):
            for row, rowlist in enumerate(self.maze):
                for col, cell in enumerate(rowlist):
                    if cell == number:
                        return row, col
            return None, None
    
        def testIndex(self, row, col):
            if row == None or col == None:
                return False
            if 0 <= row < len(self.maze) and 0 <= col < len(self.maze[row]):
                return True
            return False
    
        def isFieldEqual(self, row, col, number):
            if self.testIndex(row, col):
                return self.maze[row][col] == number
            return False 
        
        def swapFileds(self, row1, col1, row2, col2):
            if self.testIndex(row1, col1) and self.testIndex(row2, col2):
                self.maze[row1][col1], self.maze[row2][col2] = self.maze[row2][col2], self.maze[row1][col1]
    
    
    maze = Tabuleiro(tela, 10, 10)
    
    while jogo:
    
        row, col = maze.findFirst(2)
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                jogo = False
    
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    if maze.isFieldEqual(row, col-1, 0):
                        maze.swapFileds(row, col-1, row, col)
                if event.key == pygame.K_RIGHT:
                    if maze.isFieldEqual(row, col+1, 0):
                        maze.swapFileds(row, col+1, row, col)
                if event.key == pygame.K_UP:
                    if maze.isFieldEqual(row-1, col, 0):
                        maze.swapFileds(row, col, row-1, col)
                if event.key == pygame.K_DOWN:
                    if maze.isFieldEqual(row+1, col, 0):
                        maze.swapFileds(row+1, col, row, col)
    
        tela.fill(0)
        maze.show()
        pygame.display.update()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多