尝试使用tkinter在python中创建游戏扫雷器,但在查找瓷砖周围的邻居数量时遇到问题

fae0ux8s  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(316)
  1. from tkinter import *
  2. from random import choice
  3. root = Tk()
  4. root.geometry("544x544")
  5. # tile class
  6. class Tile:
  7. def __init__(self, x, y, state=0):
  8. self.x = x
  9. self.y = y
  10. self.state = state
  11. self.button = Button(root,command=self.button_command, image=empty_block, height=28, width=28)
  12. self.listOfNeighbors = []
  13. self.neighbors = 0
  14. def button_command(self):
  15. print(self.x, self.y)
  16. def findNeighbors(self):
  17. self.listOfNeighbors.append(board[self.y-1][self.x])
  18. self.listOfNeighbors.append(board[self.y][self.x-1])
  19. self.listOfNeighbors.append(board[self.y-1][self.x-1])
  20. try: self.listOfNeighbors.append(board[self.y+1][self.x])
  21. except: pass
  22. try: self.listOfNeighbors.append(board[self.y+1][self.x-1])
  23. except: pass
  24. try: self.listOfNeighbors.append(board[self.y-1][self.x+1])
  25. except: pass
  26. try: self.listOfNeighbors.append(board[self.y+1][self.x+1])
  27. except: pass
  28. try: self.listOfNeighbors.append(board[self.y][self.x+1])
  29. except: pass
  30. self.sortNeighbors()
  31. def sortNeighbors(self):
  32. for i in self.listOfNeighbors:
  33. if self.y == 0:
  34. if i.y == 15: self.listOfNeighbors.remove(i);print(self.x, self.y," ", i.x, i.y)
  35. elif self.x == 0:
  36. if i.x == 15: self.listOfNeighbors.remove(i);print(self.x, self.y," ", i.x, i.y)
  37. self.neighbors = len(self.listOfNeighbors)
  38. self.button.config(image=neighbors_images[self.neighbors])
  39. # variable
  40. empty_block = PhotoImage(file="images/empty-block.png")
  41. bomb_unclicked = PhotoImage(file="images/unclicked-bomb.png")
  42. bomb_clicked = PhotoImage(file="images/bomb-at-clicked-block.png")
  43. neighbors_images = [
  44. PhotoImage(file="images/0.png"),
  45. PhotoImage(file="images/1.png"),
  46. PhotoImage(file="images/2.png"),
  47. PhotoImage(file="images/3.png"),
  48. PhotoImage(file="images/4.png"),
  49. PhotoImage(file="images/5.png"),
  50. PhotoImage(file="images/6.png"),
  51. PhotoImage(file="images/7.png"),
  52. PhotoImage(file="images/8.png"),
  53. ]
  54. board = []
  55. for y in range(16):
  56. temp = []
  57. for x in range(16):
  58. temp.append(Tile(x, y))
  59. temp[-1].button.grid(row=y, column=x)
  60. board.append(temp)
  61. for i in range(40):
  62. choice(choice(board)).state = 1
  63. for y in board:
  64. for x in y:
  65. x.findNeighbors()
  66. root.mainloop()

数字显示了平铺有多少个邻居,并且应该显示:每个角上有3个邻居,所有四条边上有5个邻居,其他所有8个邻居。但是出于某种原因,右边缘显示的是6而不是5。
这是我刚刚写的一个图像:图像
据我所知,问题来自于witch中的第42行,我试图从邻居列表中删除任何位于板外的瓷砖。

f87krz0w

f87krz0w1#

你让自己更难受了。我建议明确,避免添加和删除不必要的邻居:

  1. def findNeighbors(self):
  2. NEIGHBOURS = [
  3. (-1, -1),
  4. (-1, 0),
  5. (-1, 1),
  6. (0, -1),
  7. (0, 1),
  8. (1, -1),
  9. (1, 0),
  10. (1, 1)
  11. ]
  12. for dx, dy in NEIGHBOURS:
  13. if 0 <= self.x + dx < len(board[0]) and 0 <= self.y + dy < len(board):
  14. self.listOfNeighbors.append(board[self.y + dy][self.x + dx])
  15. self.sortNeighbors()
  16. def sortNeighbors(self):
  17. self.neighbors = len(self.listOfNeighbors)
  18. self.button.config(image=neighbors_images[self.neighbors])
展开查看全部

相关问题