视频系统未初始化

2024-04-26 00:15:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个使用多个不同python文件的象棋游戏。我遇到了视频系统未初始化的错误,与pygame.display.update()有关。我已经尝试了这么多的事情,以获得初始化程序和不确定的问题。问题是否与代码行缺失有关

已经尝试重新安装pygame,确保每个文件都有初始化

这是可执行文件,但不是程序的其他相关文件,如果需要,我可以附加这些文件: 以下是节目摘要:

import pygame
from pygame.locals import *
from chessboard import cboard
from chessboard.move import movement
#import subprocess
import threading
from player.minmax import minimax



class play():

     game = None
     clock = None
     board1 = None       #'first' board
     allsquares = None
     allPieces = None       
     player = None  #current player
     square_parameters = None

     image_select = None
     legal_select = None

     c_reset = None
     mx = None
     my = None
     prevx = None
     prevy = None
     endgame = None
     ai = None      #ai 'board'


     def __init__(self):
          pygame.init()       #initialises window
          self.game = pygame.display.set_mode((800,800)) #tuple window size
          pygame.display.set_caption('Chess World!')   #title
          self.clock = pygame.time.Clock()   #initialises clock

          #calls to board class in chessboard.py
          self.board1 = cboard.Board()
          self.board1.draw_board()       #executes fucntion draw_board in class Board

          #following initialisations
          self.allsquares = []
          self.allPieces = []
          self.player = self.board1.player
          self.square_parameters = self.create_squareParams
          #self.draw_pieces()

          self.image_select = None
          self.legal_select = None
          self.c_reset = []
          self.mx, self.my = pygame.mouse.get_pos()    #attachs mouse to pieces in mx,my
          self.prevx, self.prevy = [0, 0]

          self.ai = False
          self.endgame = False

          while not self.endgame:
               for event in pygame.event.get():

                    if event.type == pygame.QUIT:
                         pygame.quit()  #closes window
                         #quit()

               #handled piece moving legally

                    elif (event.type == pygame.MOUSEBUTTONDOWN) and not (self.ai):
                         if self.image_select == None:
                              self.mx, self.my = pygame.mouse.get_pos()
                              for piece in range(len(self.allPieces)):
                                   if self.allPieces[piece][2].relate == self.player:
                                        if self.allPieces[piece][1][0] < self.mx < self.allPieces[piece][1][0] + 100:
                                             if self.allPieces[piece][1][1] < self.my < self.allPieces[piece][1][1] + 100:
                                                  self.image_select = piece
                                                  self.prevx = self.allPieces[piece][1][0]
                                                  self.prevy = self.allPieces[piece][1][1]
                                                  self.legal_select = self.allPieces[self.image_select][2].legal_moves(self.board1)
                                                  for legal in self.legal_select:
                                                       self.c_reset.append([legal, self.allsquares[legal][0]])
                                                       if self.allsquares[legal][0] == (66, 134, 244) or self.allsquares[legal][0] == (29, 81, 39):
                                                            self.allsquares[legal][0] = (135, 46, 40)
                                                       else:
                                                            self.allsquares[legal][0] = (183, 65, 56)

                    #dragging piece
                    if event.type == pygame.MOUSEBUTTONDOWN and not self.image_select == None and not self.ai:
                         self.mx, self.my = pygame.mouse.get_pos()
                         self.allsquares[self.image_select][1][0] = self.mx - 50
                         self.allsquares[self.image_select][1][1] = self.my - 50

                    #dropping piece to destination
                    if event.type == pygame.MOUSEBUTTONUP and not self.ai:
                         if not self.image_select == None:
                              for resets in self.c_reset:
                                   self.allsquares[resets[0]][0] = resets[1]

                              try:
                                   move_piece = self.allPieces[self.image_select][2].legal_movevs(self.board1)
                                   lgl = False
                                   the_move = 0
                                   for usermove in move_piece:
                                        if self.square_parameters[usermove][0] < self.allPieces[self.image_select][1][0] + 50 < self.square_parameters[usermove][1]:
                                             if self.square_parameters[usermove][2] < self.allPieces[self.image_select][1][1] + 50 < self.square_parameters[usermove][3]:
                                                  lgl = True
                                                  the_move = usermove

                                   if lgl == False:
                                        self.allPieces[self.image_select][1][0] = self.prevx
                                        self.allPieces[self.image_select][1][1] = self.prevy

                                   else:
                                        self.allPieces[self.image_select][1][0] = self.square_parameters[the_move][0]
                                        self.allPieces[self.image_select][1][1] = self.square_parameters[the_move][2]

                                        move_i = movement(self.board1, self.allPieces[self.image_select][2], the_move)
                                        newBoard = move_i.create_board()
                                        if not newBoard == False:
                                             self.board1 = newBoard

                                        new = self.update_pieces()
                                        self.allPieces = new
                                        self.player = newBoard.player

                                        thread = threading.Thread(target=self.miniMaxMove, args = [])
                                        thread.start()

                              except:
                                   pass

                              self.prevx = 0
                              self.prevy = 0
                              self.image_select = None

                    #draws squares in frame
                    for info in self.allsquares:
                         pygame.draw.rect(self.game, info[0], info[1])

                    #drawing pieces each frame
                    for image in self.allPieces:
                         self.game.blit(img[0], img[1])

                    #manages game loop
                    pygame.display.update()
                    self.clock.tick(60)

     def create_squareParams(self):
          square_ranges = []
          xmin = 0
          xmax = 100
          ymin = 0
          ymax = 100
          for n in range(8):
               for n in range(8):
                    square_ranges.append([xmin, xmax, ymin, ymax])
                    xmin += 100
                    xmax += 100
               xmin = 0
               xmax = 100
               ymin += 100
               ymax += 100
          return square_ranges

     def spaces(self, x, y, w, h, colour):
          pygame.draw.rect(self.game, colour, [x, y, w, h])
          self.allsquares.append([colour, [x, y, w, h]])

     def draw_pieces(self):
          xpos = 0
          ypos = 0
          colour = 0
          height = 75
          width = 75
          black = (66, 134, 244)
          white = (143, 155, 175)
          num = 0
          for n in range(8):
               for n in range(8):
                    if colour % 2 == 0:
                         self.spaces(xpos, ypos, height, width, white)
                         if not self.board1.squares[num].onSquare.e_string() == '-':
                              image = pygame.image.load('./images/' + self.board1.squares[num].onSquare.relate.upper()+ self.board1.squares[num].onSquare.e_string().upper() + 'png')
                              image = pygame.transform.sclae(image, (100, 100))
                              self.allPieces.append([image, [xpos, ypos], self.board1.squares[num].onSquare])
                         xpos += 100
                    else:
                         self.spaces(xpos, ypos, height, width, black)
                         if not self.board1.squares[num].onSquare.e_string() == '-':
                              image = pygame.image.load('./images/' + self.board1.squares[num].onSquare.relate[0].upper() + self.board1.squares[num].onSquare.e_string().upper() + '.png')
                              image = pygame.transform.sclae(image, (100, 100))
                              self.allPieces.append([image, [xpos, ypos], self.board1.sqaures[num].onSquare])
                         xpos += 100

                    colour += 1
                    num += 1
               colour += 1
               xpos = 0
               ypos += 100

     def update_pieces(self):
          xpos = 0
          ypos = 0
          num = 0
          new_pieces = []

          for n in range(8):
               for n in range(8):
                    if not self.board1.squares[num].onSquare.e_string() == '-':
                         image = pygame.image.load('./images/' + self.board1.squares[num].onSquare.relate[0].upper() + self.board1.squares[num].onSquare.e_string().upper() + '.png')
                         image = pygame.transform.scale(image, (100, 100))

                         new_pieces.append([image, [xpos, ypos], self.board1.squares[num].onSquare])
                    xpos += 100
                    num += 1
               xpos = 0
               ypos += 100

          return new_pieces

     def miniMaxMove(self):
          if self.player == 'Red':
               print('AI is thinking...')
               self.ai = True
               minimax = minimax(self.board1, 1)
               self.ai = minimax.get_move()
               self.board1 = self.ai
               new = self.update_pieces
               self.allPieces = new
               self.player = self.ai.player
               self.ai = False

play() 

pygame.display.update()

pygame.error: video system not initialized

期望: 打开的窗口,可以下棋


Tags: inimageselfnoneforpieceifselect