0

I'm writing a minesweeper game using tkinter, and everything appears to function alright, but python crashes every time after a number of clicks, and I believe it is because I've added images for the "flags" used to flag where a mine is. Below is the function that draws all of the cells, which is called with each move/click.

def draw_cake_cell(canvas, row, col):
    # draws different cells on the board (blank cell, numbers, cake flags) 
    margin = 5
    cellSize = 30
    board = canvas.data['board']
    mask = canvas.data['mask']
    cake_count = canvas.data['cake_count']
    player_board = canvas.data['player_board']
    left = margin + col*cellSize
    top = margin + row*cellSize
    bottom = top + cellSize
    right = left+cellSize
    flag_img = PhotoImage(file="flag.gif")
    flag_label = Label(image = flag_img)
    flag_label.image = flag_img
    cake_img = PhotoImage(file = "cakeflag.gif")
    cake_label = Label(image = cake_img)
    cake_label.image = cake_img
    canvas.create_rectangle(left,top, right, bottom, fill = 'gray')
    if board[row][col] == -1:
       if canvas.data['isGameOver'] == True:
            canvas.create_image((left+right)/2,(top+bottom)/2,image = cake_img)
    elif mask[row][col] == -2:
        canvas.create_image((left+right)/2,(top+bottom)/2,image = flag_img)
    else:
       if cake_count[row][col] > 0 and player_board[row][col] == 1:
            value = cake_count[row][col]
            canvas.create_rectangle(left,top,right,bottom, fill = 'white')
            canvas.create_text((left+right)/2,(top+bottom)/2 , text = str(value), font = ('Helvetica',12))             
       if mask[row][col] == -2:
            canvas.create_image((left+right)/2,(top+bottom)/2,image = flag_img)
       if cake_count[row][col] == 0 and player_board[row][col] == 1:
            canvas.create_rectangle(left,top,right,bottom, fill = 'white')
Elazar
  • 20,415
  • 4
  • 46
  • 67
  • please add the stack trace, and indent your code. – Elazar May 05 '13 at 21:59
  • Python doesn't provide a stack trace, all I get is "pythonw.exe has stopped working" and then the program proceeds to crash after that. Checked the float vs. int, that doesn't seem to be the problem. Note also I previously just had canvas.create_oval for everything, and that didn't seem to cause problems. – user2352833 May 05 '13 at 22:06
  • run this code from an interpreter and you will get a stacktrace. otherwise, surround it with `try: ... except Exception as ex: print(ex)` – Elazar May 05 '13 at 22:09
  • Still not getting a stack trace... with the try/except.. I'm using Idle? – user2352833 May 05 '13 at 22:17
  • http://stackoverflow.com/questions/1623039/python-debugging-tips – Elazar May 05 '13 at 22:38
  • You say this function is "called with each move/click". You are creating a lot of images and canvas objects but I don't see you deleting them. Could you simply be running out of memory after a while? Though, assuming you have modern hardware, it would probably take a lot of clicks to use up all your memory. – Bryan Oakley May 05 '13 at 22:58

1 Answers1

0

Since you are using python3, (left+right)/2 is float, not an int. maybe that's the problem - use (left+right)//2.

Elazar
  • 20,415
  • 4
  • 46
  • 67