dimanche 14 mai 2017

Can I randomly select a Tkinter Checkbutton programmatically?

Disclaimer - Brand new to programming, with Python (3.x) being my choice of language to begin with.

I recently completed a Python course on codeacademy in which I created a mini battleships game. As a learning project I'm developing a GUI using Tkinter and Checkbuttons for the game board so that it can be played outside of the interpreter.

I'm now blocked as I don't know how to have Python randomly select the Checkbutton position for the battleship location - and keep it invisible from the player.

Here's my Tkinter code so far...

from tkinter import *

class gui(object):
    def __init__(self):

        master = Tk()
        master.title("Battleships!")

        w = Label(master, text="Let's play Battleships!")
        w.pack()
        w = Label(master, text="Click in a box to try and bomb the 
Battleship:")
        w.pack()

        self.checkvar1 = IntVar()

        c = Checkbutton(master, variable=self.checkvar1, command=self.cb)
        c.pack()

        #Hopefully I can use this to display messages to user as they guess
        w = Label(master, text=" ")
        w.pack()

        Button(master, text="I give up!", command=master.quit).pack()

        master.mainloop()

    def cb(self):
        print("variable is", self.checkvar1.get())
        return self.checkvar1.get()

def main():
    g = gui()

if __name__ == '__main__':
    main()

Perhaps I should first create my Checkbutton Grid (5 x 5) instead of just trying it with a single Checkbutton (I was attempting to slice up my development into logical problems)?

Here is the original game code (no GUI)...

from random import randint

board = []

for x in range(5):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print(" ".join(row))

print("Let's play Battleship!")
print_board(board)

def random_row(board):
    return randint(0, len(board) - 1)

def random_col(board):
    return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)

for turn in range(4):
    guess_row = int(input("Guess Row (between 0 & 4):"))
    guess_col = int(input("Guess Col (between 0 & 4):"))
    print("Turn", turn + 1)
    if guess_row == ship_row and guess_col == ship_col:
        print("Congratulations! You sunk my battleship!")
        break
    else:
        if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 
4):
            print("Oops, that's not even in the ocean.")
            if turn == 3:
                print("Game Over")
        elif (board[guess_row][guess_col] == "X"):
            print("You guessed that one already.")
            if turn == 3:
                print("Game Over")
        else:
            print("You missed my battleship!")
            board[guess_row][guess_col] = "X"
            if turn == 3:
                print("Game Over")
        print_board(board)

Any advice, hints, tips to aid my learning with this particular problem would be greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire