Je suis en train de concevoir un jeu utilisant graphics.py en python. J'ai tout d'abord tout configuré sauf, le jeu consiste à cliquer sur une case qui retournerait la couleur de la boîte. Par exemple: si la couleur de la case cliquée était blanche, elle deviendrait noire. Mon code fonctionne pour transformer les boîtes blanches en noir, mais ne convertira pas les boîtes noires en blanc. Je sais que mon instruction if dans while loop est incorrecte. Je veux savoir comment vous pourriez obtenir la valeur de la couleur du rectangle dans graphics.py afin que je puisse faire une bonne instruction if.Trouver la couleur d'une boîte de rectangle en Python en utilisant graphics.py
# _______________________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")
Utiliser 'cget()' si vous voulez trouver la couleur d'un certain widget et vous pouvez l'appliquer dans votre code. – Inkblot
Est-ce une méthode graphics.py dans python 3.x? –
Si les utilisateurs tkinter alors il le devrait. – Inkblot