1

I am creating a chess game that uses multiple different python files. I am met with the error of video systems not initialized, relating to a pygame.display.update(). I have tried so many things to get initialize the program and am unsure of the issue. Could the issue be to do with a missing line of code or something?

Already attempted reinstalling pygame, making sure there are initialization s for each file.

here is the executable file, but not the other related files for the program, I can attach these if necessary: Here is an extract of the program:

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

What is expected: The window to open and for chess to be able to be played.

furas
  • 134,197
  • 12
  • 106
  • 148
  • select code and use button `{}` to correctly format it. – furas May 13 '19 at 08:58
  • What do you mean? At what part of the code? –  May 13 '19 at 08:59
  • 1
    `video system not initialized` may mean you try display before you use `pygame.init()`. If you have code in many files then maybe you try to draw/display in some file before you run `pygame.init()` in main file. – furas May 13 '19 at 09:02
  • 1
    it can be other problem. I see you import `threading`. Only main thread may display/draw. You can't draw in other threads. – furas May 13 '19 at 09:03
  • as for button `{}` I mean you have to select all code in question and press button `{}` in editor in Stackoverflow and it will display it as block of code using colors. But I see @M.R. already edited your question. – furas May 13 '19 at 09:06
  • so how do you reckon i can sort this issue –  May 13 '19 at 09:06
  • I am unsure of what the issue could be? –  May 13 '19 at 09:33
  • I don't know all your code. First you should read full error - there can be information which line of code (and in which file) makes problem. And then you will know where is the problem. If in other files you have code which is not in function then it is executed automatically when you import files and there can be line which is executed before `pygame.init()`. You can also use `print()` before `pygame.init()` and other functions and files to see which functions/files are executed before `pygame.init()`. – furas May 13 '19 at 11:35
  • you can also open python shell and run `import pygame ; pygame.init()` to check if `pygame.init()` has no problem to init video card. – furas May 13 '19 at 11:36
  • the error is said to be in the chess.py file that I am executing, and the error is related to pygame.display.update() and play() (name to execute class play –  May 13 '19 at 12:56
  • 2
    @8goalsIsDecent You're importing `chess.py` before you've initialized pygame with `pygame.init()`. So you're probably calling `pygame.display.update()` before pygame is initialized. But how did you get to that error? Try to trace back your steps. – Ted Klein Bergman May 13 '19 at 17:48
  • always put full error message (starting at word "Traceback") in question (as text, not screenshot). There are other useful information. – furas May 14 '19 at 07:17
  • as I said before and @TedKleinBergman repeated it - in imported file you may execute `pygame.display.update()` before you execute `play()` in main file. So don't execute any function in `chess.py` - do it in main file. If you have code in `chess.py` which is not in functions then put them in functions so you could execute them after `play()`. – furas May 14 '19 at 07:20
  • Can I send you my program and its modules for you to check out? –  May 17 '19 at 09:30

0 Answers0