尽管我输入了指令“ pygame.display.flip()”,为什么屏幕仍然没有更新?

时间:2018-12-29 21:25:03

标签: python pygame

我正在编程国际象棋,但是尽管我放了指令“ pygame.display.flip()”,但我动棋盘时并没有更新。

据我所知,我正在使用的数组会更新作品的位置,但是由于某些原因,这不会转换为屏幕。

import pygame
import time
import os
import threading
import queue

pygame.init()

BLACK = (255, 0, 0)
WHITE = (255, 255, 255)
screen_width = 800
screen_height = 600
board_top_left_x = screen_width//5
board_top_left_y = screen_height//10
square_size = screen_height//10
screen = pygame.display.set_mode([screen_width, screen_height])
os.environ['SDL_VIDEO_WINDOWS_POS'] = '10, 10'
commands = queue.Queue


class Input(threading.Thread):
    def run(self):
        while True:
            command = input()
            commands.put(command)


class square(pygame.sprite.Sprite):
    def __init__(self, colour):
        super().__init__()
        self.image = pygame.Surface([square_size, square_size])
        self.rect = self.image.get_rect()
        pygame.draw.rect(self.image, colour, [0, 0, square_size, square_size])
        self.colour = colour


def load_image(name):
    f = pygame.image.load(name + ".png")
    return f


def set_up_board(board):
    board[0][0] = "Rw"
    board[1][0] = "Nw"
    board[2][0] = "Bw"
    board[3][0] = "Qw"
    board[4][0] = "Kw"
    board[5][0] = "Bw"
    board[6][0] = "Nw"
    board[7][0] = "Rw"
    board[0][7] = "Rb"
    board[1][7] = "Nb"
    board[2][7] = "Bb"
    board[3][7] = "Qb"
    board[4][7] = "Kb"
    board[5][7] = "Bb"
    board[6][7] = "Nb"
    board[7][7] = "Rb"
    for i in range(8):
        board[i][1] = "Pw"
        board[i][6] = "Pb"
    return board


def draw_board(board, squares):
    squares.draw(screen)
    for row in range(len(board)):
        for col in range(len(board)):
            if board[col][row] != " ":
                i = load_image(board[col][row])
                i = pygame.transform.scale(i, (square_size, square_size))
                i.set_colorkey(WHITE)
                screen.blit(i, [square_size*col + board_top_left_x, square_size*(7-row) + board_top_left_y])
    pygame.display.flip()


def main():
    squares = pygame.sprite.Group()
     col = BLACK
     I = Input()
     I.start()
     for i in range(8):
         for j in range(8):
             if col == BLACK:
                 col = WHITE
             else:
                 col = BLACK
             x = i*square_size + board_top_left_x
             y = j*square_size + board_top_left_y
             s = square(col)
             s.rect.x = x
             s.rect.y = y
             squares.add(s)
         if col == BLACK:
             col = WHITE
         else:
             col = BLACK
     board = []
     for i in range(8):
          a = []
          for j in range(8):
              a.append(' ')
          board.append(a)
     board = set_up_board(board)
     draw_board(board, squares)
     print("What move do you want to make?")
     answered = False
     while not answered:
         try:
             ans = commands.get(False)
             answered = True
         except queue.Empty:
             ans = None
     board[ord(ans[2]) - 97][int(ans[3]) - 1] = board[ord(ans[0]) - 97][int(ans[1]) - 1]
     board[ord(ans[0]) - 97][int(ans[1]) - 1] = " "
     draw_board(board, squares)
     time.sleep(5)


main()

只要合法,我希望它可以通过此操作来更新屏幕。从我的错误检查中,它执行第二条if语句,并在更改后为我提供正确的p.x和p.y。我错过了什么?

[White bishop image][1][Black bishop image][1][Black king image][1][White king image][1][Black knight image][1][White knight image][1][Black pawn image][1][White pawn image][1][Black queen image][1][White queen image][1][Black rook image][1][White rook image][1]

注意:输入的移动形式为“件的坐标,移动的坐标”,例如:d2d4

注2:我也用piece_list而不是board和指令piece_list.draw(screen)而不是嵌套的for循环尝试了此操作。它给了我完全相同的结果。

2 个答案:

答案 0 :(得分:0)

好的,我看看。你说:

  

从我的错误检查中,它执行第二个if语句,并给出   更改后,我得到了正确的p.x和p.y。我有什么   错过了吗?

但是从我的检查来看,这不会发生。您的struct A { int a; int b; void run() { auto ab = std::make_tuple(std::ref(a), std::ref(b)); int i { -1 }; std::apply([&](auto & ... vs){ ((vs = ++i), ...); }, ab); std::cout << a << " " << b << std::endl; } }; 为空。所以在这里:

piece_list

它从不输入for p in piece_list: if p.x == ord(ans[0]) - 97 and p.y == int(ans[1]) - 1: if p.move_is_possible(board, ans, piece_list, squares) is True: 的语句,并且if保持不变。 如果您实际上在两次对board的调用之间的循环中编辑board,则电路板将正确更新。

此外,您有draw_board(board, squares),但我在代码中的任何地方都找不到此方法的定义。是否在单独的文件中? pygame中定义的sprite方法吗?我不是pygame的专家,但是我无法在文档中找到任何类似的方法。

答案 1 :(得分:0)

您没有事件循环,并且仅绘制/更新屏幕是不够的(取决于您的OS /窗口管理器)。

如果要使用pygame,则需要不断调用pygame.event.get()(或pygame.event.pump(),而仅使用.get()),否则,该窗口不会在内部处理来自操作系统的事件,例如告诉窗口绘制其内容的事件。

因此,您必须在调用pygame.event.get()的地方创建一个循环。

如果您仍然希望从终端获得输入,请查看Is there a way that while running pygame, I can also run the console too?