使用 graphics.py 在 Python 中查找矩形框的颜色
Find the color of a rectangle box in Python using graphics.py
我正在 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")
这段代码有两个严重的问题。首先是这一行:
if m_board[move] == "black":
您知道 m_board
是 Rectangle
个实例的列表,为什么您期望它等于 "black"
?我们可以通过这种方式获得填充颜色:
if m_board[move].config["fill"] == "black":
另一种方法是用您自己的对象 class 包装 Rectangle
实例,以跟踪您需要 query/change 的事情。第二个问题是这个组合:
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
是float,所以结果是float,使用float列表索引,即m_board[move]
,导致错误。简单的解决方法是让 make_move()
调用 int()
它 returns.
我对所有代码的返工:
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")
我正在 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")
这段代码有两个严重的问题。首先是这一行:
if m_board[move] == "black":
您知道 m_board
是 Rectangle
个实例的列表,为什么您期望它等于 "black"
?我们可以通过这种方式获得填充颜色:
if m_board[move].config["fill"] == "black":
另一种方法是用您自己的对象 class 包装 Rectangle
实例,以跟踪您需要 query/change 的事情。第二个问题是这个组合:
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
是float,所以结果是float,使用float列表索引,即m_board[move]
,导致错误。简单的解决方法是让 make_move()
调用 int()
它 returns.
我对所有代码的返工:
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")