0

我正在使用 python 中的 graphics.py 设计游戏。我最初设置了所有东西,除了游戏包括点击一个可以翻转盒子颜色的盒子。例如:如果单击的框的颜色是白色,它会变成黑色。我的代码适用于将白盒变为黑色,但不会将黑盒转换为白色。我知道我在 while 循环中的 if 语句是错误的。我想知道如何在 graphics.py 中获取矩形颜色的值,以便我可以做出正确的 if 语句。

# _______________________IMPORTS_________________________
from graphics import *
import random
#________________________________________________________

win = None
m_board = []

# Description:
#  Wait for the user to enter a valid move via the mouse. If the player selects a position
#  outside the valid range or selects an occupied board space, the player is asked again.
#  The function returns the move only when it's valid.
# Return value:
#  An integer in the range 0 to 99 representing the move

def make_move():

    pos = win.getMouse()
    x_axis = pos.x // 50
    y_axis = pos.y // 50
    move = y_axis * 10 + x_axis


    return move


# Description:
#   Creating the initial board with random black and white boxes
# Return Value:
#   None

def draw_board():
    global win, m_board

    color = ["white", "black"]        #Creating list for the random black/white
    win = GraphWin("LOGICX", 500, 600)

    for y in range(0, 500, 50):
        for x in range(0, 500, 50):

            board_box = Rectangle(Point(x, y), Point(x + 50, y + 50))                    
            #Setting the boxes with random black/white
            board_box.setFill(color[random.randint(0, 1)])            
            #Adding each box to the empty list
            m_board.append(board_box)            
            #Setting outline color to differentiate individual boxes
            board_box.setOutline("grey")
            board_box.draw(win)



    game_running = True
    while game_running:

        move = make_move()
        if m_board[move] == "black":
            m_board[move].setFill("white")

        else:
            m_board[move].setFill("black")
4

1 回答 1

1

这段代码有两个重大问题。首先是这一行:

if m_board[move] == "black":

您知道这m_board是一个Rectangle实例列表,为什么您希望它等于"black"?我们可以这样得到填充颜色:

if m_board[move].config["fill"] == "black":

另一种方法是Rectangle用您自己的对象类包装实例,以跟踪您需要查询/更改的内容。第二个问题是这种组合:

x_axis = pos.x // 50
y_axis = pos.y // 50
move = y_axis * 10 + x_axis
...
m_board[move].setFill("white")

虽然双斜线 (//) 进行非余数除法,但由于pos.x&pos.y是浮点数,因此结果是浮点数,并且使用浮点数列表索引,即m_board[move],会导致错误。简单的解决方法是make_move()调用int()它返回的内容。

我对所有代码的返工:

import random
from graphics import *

WINDOW_WIDTH, WINDOW_HEIGHT = 500, 500

COLUMNS, ROWS = 10, 10

TILE_WIDTH, TILE_HEIGHT = WINDOW_WIDTH // COLUMNS, WINDOW_HEIGHT // ROWS

COLORS = ["white", "black"]  # Creating list for the random black/white

def make_move():

    pos = win.getMouse()

    x_axis = pos.x // TILE_WIDTH
    y_axis = pos.y // TILE_HEIGHT

    return int(y_axis * COLUMNS + x_axis)

def draw_board():
    board = []

    for y in range(0, WINDOW_HEIGHT, TILE_HEIGHT):
        for x in range(0, WINDOW_WIDTH, TILE_WIDTH):
            board_box = Rectangle(Point(x, y), Point(x + TILE_WIDTH, y + TILE_HEIGHT))
            # Set the boxes with random black/white
            board_box.setFill(random.choice(COLORS))
            # Set outline color to differentiate individual boxes
            board_box.setOutline("grey")
            # Add each box to the empty list
            board.append(board_box)
            board_box.draw(win)

    return board

win = GraphWin("LOGICX", WINDOW_WIDTH, WINDOW_HEIGHT)

m_board = draw_board()

game_running = True

while game_running:

    move = make_move()

    if m_board[move].config["fill"] == "black":
        m_board[move].setFill("white")
    else:
        m_board[move].setFill("black")
于 2017-04-25T17:55:57.293 回答