Brian Barton
Brian Barton

Reputation: 3

from Chess import ChessEngine: error on Chess Engine

I have reviewed the tutorial numerous times but still, I have this error. After the first video in the series, he is able to display the board. The error highlights ChessEngine on my import statement.

this is my primary resource: https://www.youtube.com/watch?v=EnYui0e73Rs&t=2095s

This is the error: ImportError: cannot import name 'ChessEngine' from 'Chess' (unknown location)

pls halp

my code is the following

***import pygame as p
from Chess import ChessEngine
WIDTH = HEIGHT = 512  # optional 400
DIMENSIONS = 8  # dimensions are 8x8
SQ_SIZE = HEIGHT // DIMENSIONS
MAX_FPS = 15  # For animation
IMAGES = {}
'''
Initialize global dictionary of images. this will be called once in the main
'''
def load_images():
    pieces = ["wp", "wR", "wN", "wB", "wQ", "wK", "wB", "wN", "wR", "bp", "bR", "bN", "bB", "bQ", "bK", "bB", "bN",
              "bR"]
    for piece in pieces:
        IMAGES[piece] = p.transform.scale(p.image.load("images/" + piece + "wp.png"), (SQ_SIZE, SQ_SIZE))
#         note: we can access an image by saying 'IMAGES['wp']
'''
main driver for our code  handles user input and updating graphics 
'''
def main():
    p.init()
    screen = p.display.set_mode((WIDTH, HEIGHT))
    clock = p.time.Clock()
    screen.fill(p.Color("white"))
    gs = ChessEngine.GameState()
    print(gs.board)
    load_images()  # only operated once
    running = True
    while running:
        for e in p.event.get():
            if e.type == p.QUIT:
                running = False
            drawGameState((screen, gs))
            clock.tick(MAX_FPS)
            p.display.flip()
def drawGameState(screen, gs):
    drawBoard(screen) #draw squares on board
    drawPieces(screen, gs.board)
def drawBoard(screen):
    colors = [p.Color("white", p.Color("grey"))]
    for r in range(DIMENSIONS):
        for c in range(DIMENSIONS):
            color = colors[((r+c) % 2)]
            p.draw.rect(screen, color, p.Rect(c*SQ_SIZE, r*SQ_SIZE, SQ_SIZE, SQ_SIZE))
def drawPieces(screen, board):
    '''
    draws the pieces on the board using current gameState.board
    '''
if __name__ == "__main__":
    main()***

Upvotes: 0

Views: 1499

Answers (1)

DDomen
DDomen

Reputation: 1878

If you see the folder structure of the youtube video, he has a root folder named Chess:

| Chess
| | --> images
| | --> ChessEngine.py
| | --> __init__.py 
| ...

When you state import, python will search for the calling folder (root folder) if there is a module (folder or file) that has the imported name. If a folder contains the __init__.py file it will see as a module.

For your use you have two options:

  1. Have a file named Chess.py with an exported member ChessEngine
  2. Have a folder named Chess, with a __init__.py file that exposes a ChessEngine member

I guess that if you follow the whole tutorial you linked, they will tell you how to expose a member from an __init__.py file.

If you want a better understand of how the import system (Docs Python) works you can check out the documentation.

Upvotes: 1

Related Questions