![](/img/trans.png)
[英]Difference between pygame.display.update and pygame.display.flip
[英]Why does the screen not update despite my putting the instruction “pygame.display.flip()”?
我正在編程國際象棋,但是盡管我放了指令“ 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語句,並在更改后為我提供正確的px和py。 我錯過了什么?
[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循環嘗試了此操作。 它給了我完全相同的結果。
好的,我看看。 你說:
從我的錯誤檢查中,它執行第二條if語句,並在更改后為我提供正確的px和py。 我錯過了什么?
但是從我的檢查來看,這不會發生。 您的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
s語句,並且board
保持不變。 如果您實際在兩次調用draw_board(board, squares)
之間的循環之外編輯board
,則該板將正確更新。
另外,您有p.move_is_possible(board, ans, piece_list, squares)
但是我在代碼中的任何地方都找不到此方法的定義。 是否在單獨的文件中? pygame中定義的sprite方法嗎? 我不是pygame的專家,但是我無法在文檔中找到任何類似的方法。
您沒有事件循環,並且僅繪制/更新屏幕是不夠的(取決於您的OS /窗口管理器)。
如果要使用pygame,則需要不斷調用pygame.event.get()
(或pygame.event.pump()
,而僅使用.get()
),否則,該窗口不會在內部處理來自操作系統的事件,例如告訴窗口繪制其內容的事件。
因此,您必須創建一個調用pygame.event.get()
的循環。
如果您仍然想從終端獲取輸入,請查看運行pygame時是否還可以運行控制台?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.