如何使pygame.display.update()方法保持显示窗口?

时间:2020-03-08 14:57:46

标签: python pygame

我使用pygame模块制作了黑白棋游戏。该脚本有效,但是有一个小问题。我要实现的效果是在播放器移动后显示此移动的结果,暂停一段时间(例如2秒),然后显示计算机的移动和结果。但是运行脚本后,玩家移动后会立即显示计算机的移动和结果。我尝试了time.sleep(),但是没有用。经过测试,我找到了问题的原因。 Pygame.display.update()只能显示一帧屏幕。如果没有事件循环,则无法连续显示屏幕。如何解决这个问题?
我是编程的初学者,并且知道我编写的代码不够流畅和高效。这是我的代码。我认为问题出在代码的最后几行。

import pygame, os, time, random
from pygame.locals import *

def initBoardList():
    boardList = []       
    for x in range(8):
        boardList.append([])
        for y in range(8):
            boardList[x].append(' ')        
    boardList[3][3] = 'X'
    boardList[3][4] = 'O'
    boardList[4][3] = 'O'
    boardList[4][4] = 'X'
    return boardList

def move(boardList, coordinate, symbol):
    if isValidMove(boardList, coordinate, symbol):
        x = coordinate[0] - 1
        y = coordinate[1] - 1
        boardList[x][y] = symbol
        flipableTiles = detectaAll(boardList, coordinate, symbol)
        flipTile(boardList, flipableTiles)        

def drawBoard(boardList, boardSurface, tileColour1, tileColour2):
    for x in range(8):
        for  y in range(8):
            if boardList[x][y] == 'O':
                pygame.draw.circle(boardSurface, tileColour1, (80 * x + 120, 80 * y + 120), 30, 0)
            if boardList[x][y] == 'X':
                pygame.draw.circle(boardSurface, tileColour2, (80 * x + 120, 80 * y + 120), 30, 0)

def isFree(boardList, coodinate):
    x = coodinate[0] - 1
    y = coodinate[1] - 1
    return boardList[x][y] == ' '

def isFull(boardList):
    for x in range(8):
        for y in range(8):
            if boardList[x][y] == ' ':
                return False
    return True

def flipTile(boardList, flipableTiles):
    for coordinate in flipableTiles:
        x = coordinate[0] - 1
        y = coordinate[1] - 1
        if boardList[x][y] == 'X':
            boardList[x][y] = 'O'
        elif boardList[x][y] == 'O':
            boardList[x][y] = 'X'

def reverseTile(symbol):
    if symbol == 'O':
        return 'X'
    if symbol == 'X':
        return 'O'

def detectOnedirection(boardList, coordinate, symbol, delta):
    flipableTiles = []
    deltaX = delta[0]
    deltaY = delta[1]
    x = coordinate[0] -1
    y = coordinate[1] -1    
    while True:        
        x += deltaX
        y += deltaY
        if x < 0 or x > 7 or y < 0 or y > 7 or boardList[x][y] == ' ':
            return []        
        elif boardList[x][y] == reverseTile(symbol):
            flipableTiles.append((x+1, y+1))  
        elif boardList[x][y] == symbol:
            return flipableTiles      



def detectaAll(boardList, coordinate, symbol):
    deltas = [(0, 1),(0, -1),(1, 1),(1, -1),(1, 0),(-1, 0),(-1, 1),(-1, -1)]
    flipableTiles = []
    for delta in deltas:
        flipableTiles += detectOnedirection(boardList, coordinate, symbol, delta)
    return flipableTiles

def getComputerMove(boardList,symbol):
    bestMove = None
    highestScore = 0
    cMoveable = moveable(boardList, symbol)
    for coordinate in cMoveable:
        if coordinate in [(1,1),(1,8),(8,1),(8,8)]:
            return coordinate
        else:
            score = len(detectaAll(boardList, coordinate, symbol))
            if score > highestScore:
                bestMove = coordinate
                highestScore = score
    return bestMove 


def isValidMove(boardList, coordinate, symbol):
    x = coordinate[0] -1
    y = coordinate[1] -1  
    return 0 < coordinate[0] < 9 and 0 < coordinate[1] < 9 and \
        isFree(boardList, coordinate) and len(detectaAll(boardList, coordinate, symbol)) != 0

def countScore(boardList, symbol):
    score = 0
    for x in range(8):
        for y in range(8):
            if boardList[x][y] == symbol:
                score += 1
    return score

def moveable(boardList, symbol):
    moveable = []
    for y in range(1,9):
        for x in range(1,9):
            if detectaAll(boardList, (x, y), symbol) != [] and isFree(boardList, (x,y)):
                moveable.append((x, y))
    random.shuffle(moveable)
    return moveable  



boardList = initBoardList()
black = (0, 0, 0)
white = (255, 255, 255)
blue = (0, 0, 255)
turn = 'player'
pygame.init()
boardSurface = pygame.display.set_mode((800,800), 0, 32)
boardSurface.fill(blue)
pygame.display.set_caption('Reversi')
for i in range(9):
        pygame.draw.line(boardSurface, black, (80, 80 + 80*i), (720, 80 + 80*i), 2)
        pygame.draw.line(boardSurface, black, (80 + 80*i, 80), (80 + 80*i, 720), 2)


drawBoard(boardList, boardSurface, black, white)                
pygame.display.update()
while True:

    for event in pygame.event.get():
        if  event.type == QUIT:
            pygame.quit()
            os._exit(1)
        if event.type == KEYUP:
            if event.key == K_ESCAPE:
                pygame.quit()
                os._exit(1)

        if event.type == MOUSEBUTTONUP and turn == 'player':
            x = (event.pos[0] - 80)// 80 + 1
            y = (event.pos[1] - 80)// 80 + 1
            if isValidMove(boardList, (x, y), 'O'):                
                move(boardList, (x, y), 'O')       
                turn = 'computer'  
    drawBoard(boardList, boardSurface, black, white)                
    pygame.display.update()    


    if turn == 'computer':    
        cm = getComputerMove(boardList, 'X')
        move(boardList, cm, 'X')            
        turn = 'player'  
        drawBoard(boardList, boardSurface, black, white)      
        pygame.display.update()

1 个答案:

答案 0 :(得分:0)

您最初将time.sleep()放在哪里? 我把它放在这里:

    if turn == 'computer':    
        cm = getComputerMove(boardList, 'X')
        move(boardList, cm, 'X')            
        turn = 'player'  
        drawBoard(boardList, boardSurface, black, white)
        time.sleep(2)
        pygame.display.update()

,效果很好。在您之后,计算机不再立即播放。