2015-10-29 1 views
0

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") 
+0

Utiliser 'cget()' si vous voulez trouver la couleur d'un certain widget et vous pouvez l'appliquer dans votre code. – Inkblot

+0

Est-ce une méthode graphics.py dans python 3.x? –

+0

Si les utilisateurs tkinter alors il le devrait. – Inkblot

Répondre

1

Il existe deux problèmes importants avec ce code. La première est cette ligne:

if m_board[move] == "black": 

Vous savez que m_board est une liste de Rectangle cas, pourquoi vous attendez qu'il soit égal à "black"? Nous pouvons obtenir la couleur de remplissage de cette façon:

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

Une autre approche aurait été pour envelopper les Rectangle cas avec votre propre classe d'objets qui garde la trace des choses dont vous avez besoin pour interroger/changement. Le deuxième problème est cette combinaison:

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

Bien que double barre oblique (//) ne partage pas reste, puisque pos.x & pos.y sont float, le résultat est un flotteur et en utilisant un index de liste flottante, c.-à-m_board[move], les causes une erreur. La solution simple est d'avoir make_move() appel int() sur ce qu'il renvoie.

Mon réusinage de tout le code:

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") 
+0

Cela fait assez longtemps que je suis revenu à ce projet. Merci pour l'aide. –