微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Sublime Text 3 中运行 pygame

如何解决在 Sublime Text 3 中运行 pygame

我目前正在使用 Python 编写国际象棋程序。我使用 Sublime Text 3 作为 IDE,但它似乎不能正常用于 pygame。我创建了一个棋盘,每当我在 Sublime Text 中运行它时,棋盘都不会出现。不知何故,它似​​乎类似于我无法在 Sublime Text 中输入的问题。 (虽然从控制台运行脚本可以正常工作,但板子应该显示出来)。我会在下面附上我的代码,以防你想看看,但我认为问题不在于代码依赖...... 所以我的问题是是否有办法从 Sublime Text 运行 python 脚本,以便用户输入和使用 pygame 的这些功能正常工作?

谢谢!

"""
This is the main driver file. It is responsible for handling user input and displaying current GameState object
"""
import pygame as p
import chess_engine

p.init()
WIDTH = HEIGHT = 512 ##400 is another option
DIMENSION = 8
SQ_SIZE = HEIGHT // DIMENSION
MAX_FPS = 15
IMAGES = {}

'''
Initialize a global dictionary of images. This will be called exactly once in the main! It is its own method (not in the main)
so we are flexible
'''
def loadImages():
    pieces = ["wP","wR","wN","wB","wK","wQ","bP","bR","bN","bB","bK","bQ"]
    for piece in pieces:
        IMAGES[piece] = p.transform.scale(p.image.load("pieces/" + piece + ".png"),(SQ_SIZE,SQ_SIZE)) ##load pictures into IMAGES dictionary
    #pygame.image.load() function returns us a Surface with the ball data
    #Note: we can access an image by saying 'IMAGES['wp']'
    #we also scale the images so it fits the square nicely

'''
The main driver for the code. It handles user inpute and updates the graphics.
'''
def main():
    p.init()
    screen = p.display.set_mode((WIDTH,HEIGHT))
    #creates a new Surface object that represents the actual displayed graphics. Any drawing you do to this Surface will become visible on the monitor.
    clock = p.time.Clock()
    screen.fill(p.Color("white"))
    gs = chess_engine.GameState()
    #print(gs.board)
    loadImages() #only do this once,before the while loop

    ##starting here,program is inizalized and ready to run
    ##inside an infinite loop we check if a QUIT event has happened. If so,we exit the programm
    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()
    # This makes everything we have drawn on the screen Surface become visible
    # This buffering makes sure we only see completely drawn frames on the screen. 
    # Without it,the user would see the half completed parts of the screen as they are being created.

'''
Responsible for all the graphics within a current game state.
'''
def drawGameState(screen,gs):
    #order in which we call is important! First draw the board,then the pieces!!
    drawBoard(screen) #draw squares on the board
    #add in piece highlighting for move suggestions (later)
    drawPieces(screen,gs.board) #draw pieces on top of those squares

'''
Draw the squares on the board. The top left square is always light (no matter the perspective)
'''
def drawBoard(screen):
    colors = [p.Color("white"),p.Color("gray")]
    for r in range(DIMENSION):
        for c in range(DIMENSION):
            color = colors[((r+c) % 2)] #sum of tuple of coordinates of white squares are always even! (0,0),(0,2),(1,1),...
            p.draw.rect(screen,color,p.Rect(c*SQ_SIZE,r*SQ_SIZE,SQ_SIZE,SQ_SIZE))

'''
Draw the pieces on the board using the urrent GameState.board
'''
def drawPieces(screen,board):
    for r in range(DIMENSION):
        for c in range(DIMENSION):
            piece = board[r][c]
            if piece != "--": #not empty square
                screen.blit(IMAGES[piece],SQ_SIZE))


if __name__ == "__main__":
    main()
"""
This class is responsible for storing all the information about the current state of a chess game.
"""

class GameState():
    def __init__(self):
        #board is a 8x8 2d list,each element has 2 chars
        #first char represents color of piece
        #second char represents type
        #"--" represents an empty space
        self.board = [
            ["bR","bQ","bR"],["bP","bP"],["--","--","--"],["wP","wP","wP"],["wR","wR"]
        ]
        self.whitetoMove = True
        self.moveLog = []

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。