Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.99 KB | None | 0 0
  1. import tkinter
  2. from tkinter import *
  3.  
  4. class GameTableView:
  5. def __init__(self, game, size):#onclick):
  6. #self.img = tkinter.PhotoImage(file="images/empty.png")
  7. self.window = tkinter.Tk()
  8. self.window.title('Twoja tablica')
  9. self.window.resizable(width=tkinter.FALSE, height=tkinter.FALSE)
  10. self.window.geometry('{}x{}'.format(800, 700))
  11. self.direction = 'horizontal'
  12. self.playerField = [['0'] * size for x in range(size)]
  13. for x in range(size):
  14. for y in range(size):
  15. print(self.playerField[x][y], end='')
  16. print('')
  17. self.battleshipSize = [4,3,3,2,2,2,1,1,1,1]
  18. self.battleshipCounter = 0
  19. self.wrapperframe = tkinter.Frame(self.window)
  20. self.wrapperframe.grid()
  21. self.formframe = tkinter.Frame(self.wrapperframe)
  22. self.formframe.grid(row=0, column=11)
  23. self.buttonsFrame = tkinter.Frame(self.wrapperframe)
  24. self.buttonsFrame.grid(row=0, column=0)
  25. self.buttons = [[tkinter.Button(self.buttonsFrame, width=4, height=4, bg='blue',
  26. command= lambda x=x, y=y:self.mark_area(x, y))#lambda x=x, y=y:onclick(x, y))
  27. for y in range(size)] for x in range(size)]
  28. for x in range(size):
  29. for y in range(size):
  30. self.buttons[x][y].grid(column=x, row=y)
  31.  
  32. self.label = Label(self.formframe, text = 'Kierunek statku: poziomo')
  33. self.label.grid(row=0,column=0, sticky=N)
  34. self.label2 = Label(self.formframe, text = 'Wielkosc statku: ' + str(self.battleshipSize[self.battleshipCounter]))
  35. self.label2.grid(row=1,column=0, sticky=N)
  36. self.window.bind("<Key>", self.setdirection)
  37.  
  38.  
  39. def mark_area(self, x, y):
  40. tmp = True #zmienna, ktora sprawdza czy statek poprawnie sie wykonal,
  41. # jesli nie mozna bylo ustawic statku to licznik statku nie jest inkrementowany
  42.  
  43. if (self.direction == 'horizontal'):
  44. for z in range(self.battleshipSize[self.battleshipCounter]):
  45. if (x+self.battleshipSize[self.battleshipCounter]-1 <= 9): #wykluczamy od razu gdy statek chce byc rozmieszczony poza ramy
  46. #I PRZYPADEK statki moga byc rozmieszczone po calym srodku, ale nie do ost kolumny i bez pierwszego i ostatniego wiersza
  47. if (x+self.battleshipSize[self.battleshipCounter] < 10 and y != 0 and y !=9):
  48. self.playerField[y][x+z] = 'S'
  49. self.buttons[x+z][y].config(bg='red', state="disabled")
  50. self.buttons[x-1][y].config(state="disabled")
  51. self.buttons[x-1][y+1].config(state="disabled")
  52. self.buttons[x-1][y-1].config(state="disabled")
  53. self.buttons[x+z][y+1].config(state="disabled")
  54. self.buttons[x+z][y-1].config(state="disabled")
  55. self.buttons[x+z+1][y-1].config(state="disabled")
  56. self.buttons[x+z+1][y].config(state="disabled")
  57. self.buttons[x+z+1][y+1].config(state="disabled")
  58. #II PRZYPADEK tylko gdy mamy statek na pierwszym wierszu, do ost kolumny wlacznie
  59. elif (y == 0 and x+self.battleshipSize[self.battleshipCounter] == 10):
  60. self.playerField[y][x+z] = 'S'
  61. self.buttons[x+z][y].config(bg='red', state="disabled")
  62. self.buttons[x-1][y].config(state="disabled")
  63. self.buttons[x-1][y+1].config(state="disabled")
  64. self.buttons[x+z][y+1].config(state="disabled")
  65. #III PRZYPADEK tylko gdy mamy statek na ostatnim wierszu, ale do ostatniej kolumny wlacznie
  66. elif (y == 9 and x+self.battleshipSize[self.battleshipCounter] == 10):
  67. self.playerField[y][x+z] = 'S'
  68. self.buttons[x+z][y].config(bg='red', state="disabled")
  69. self.buttons[x-1][y].config(state="disabled")
  70. self.buttons[x-1][y-1].config(state="disabled")
  71. self.buttons[x+z][y-1].config(state="disabled")
  72. #IV PRZYPADEK tylko gdy mamy statek na pierwszym wierszu, ale nie jest ustawiony do samego konca
  73. elif (y == 0 and x+self.battleshipSize[self.battleshipCounter] < 10):
  74. self.playerField[y][x+z] = 'S'
  75. self.buttons[x+z][y].config(bg='red', state="disabled")
  76. self.buttons[x+z+1][y].config(state="disabled")
  77. self.buttons[x-1][y].config(state="disabled")
  78. self.buttons[x+z+1][y+1].config(state="disabled")
  79. self.buttons[x-1][y+1].config(state="disabled")
  80. self.buttons[x+z][y+1].config(state="disabled")
  81. #V PRZYPADEK tylko gdy mamy statek na ostatnim wierszu, ale nie jest ustawiony do samego konca
  82. elif (y == 9 and x+self.battleshipSize[self.battleshipCounter] < 10):
  83. self.playerField[y][x+z] = 'S'
  84. self.buttons[x+z][y].config(bg='red', state="disabled")
  85. self.buttons[x+z+1][y].config(state="disabled")
  86. self.buttons[x-1][y].config(state="disabled")
  87. self.buttons[x+z+1][y-1].config(state="disabled")
  88. self.buttons[x-1][y-1].config(state="disabled")
  89. self.buttons[x+z][y-1].config(state="disabled")
  90. #VI PRZYPADEK gdy mamy statek rozmieszczony po calym srodku, ale do ost kolumny, bez pierwszego i ostatniego wiersza
  91. elif (x+self.battleshipSize[self.battleshipCounter] == 10 and y != 0 and y !=9):
  92. self.playerField[y][x+z] = 'S'
  93. self.buttons[x+z][y].config(bg='red', state="disabled")
  94. self.buttons[x-1][y].config(state="disabled")
  95. self.buttons[x-1][y+1].config(state="disabled")
  96. self.buttons[x-1][y-1].config(state="disabled")
  97. self.buttons[x+z][y+1].config(state="disabled")
  98. self.buttons[x+z][y-1].config(state="disabled")
  99. tmp = True
  100. else:
  101. self.label = Label(self.formframe, text= 'Nie mozna ustawic tak statku\n')
  102. self.label.grid(row=2,column=0, sticky=N)
  103. tmp = False
  104. else:
  105. for z in range(self.battleshipSize[self.battleshipCounter]):
  106. if (y+self.battleshipSize[self.battleshipCounter]-1 <= 9):
  107. if (y+self.battleshipSize[self.battleshipCounter] < 10 and x != 0 and x !=9):
  108. self.playerField[y+z][x] = 'S'
  109. self.buttons[x][y+z].config(bg='red', state="disabled")
  110. self.buttons[x][y-1].config(state="disabled")
  111. self.buttons[x][y+z+1].config(state="disabled")
  112. self.buttons[x-1][y-1].config(state="disabled")
  113. self.buttons[x+1][y-1].config(state="disabled")
  114. self.buttons[x-1][y+z+1].config(state="disabled")
  115. self.buttons[x+1][y+z+1].config(state="disabled")
  116. self.buttons[x-1][y+z].config(state="disabled")
  117. self.buttons[x+1][y+z].config(state="disabled")
  118. elif (x == 0 and y+self.battleshipSize[self.battleshipCounter] == 10):
  119. self.playerField[y+z][x] = 'S'
  120. self.buttons[x][y+z].config(bg='red', state="disabled")
  121. self.buttons[x][y-1].config(state="disabled")
  122. self.buttons[x+1][y-1].config(state="disabled")
  123. self.buttons[x+1][y+z].config(state="disabled")
  124. elif (x == 9 and y+self.battleshipSize[self.battleshipCounter] == 10):
  125. self.playerField[y+z][x] = 'S'
  126. self.buttons[x][y+z].config(bg='red', state="disabled")
  127. self.buttons[x][y-1].config(state="disabled")
  128. self.buttons[x-1][y-1].config(state="disabled")
  129. self.buttons[x-1][y+z].config(state="disabled")
  130. elif (x == 0 and y+self.battleshipSize[self.battleshipCounter] < 10):
  131. self.playerField[y+z][x] = 'S'
  132. self.buttons[x][y+z].config(bg='red', state="disabled")
  133. self.buttons[x][y-1].config(state="disabled")
  134. self.buttons[x][y+z+1].config(state="disabled")
  135. self.buttons[x+1][y-1].config(state="disabled")
  136. self.buttons[x+1][y+z+1].config(state="disabled")
  137. self.buttons[x+1][y+z].config(state="disabled")
  138. elif (x == 9 and y+self.battleshipSize[self.battleshipCounter] < 10):
  139. self.playerField[y+z][x] = 'S'
  140. self.buttons[x][y+z].config(bg='red', state="disabled")
  141. self.buttons[x][y-1].config(state="disabled")
  142. self.buttons[x][y+z+1].config(state="disabled")
  143. self.buttons[x-1][y-1].config(state="disabled")
  144. self.buttons[x-1][y+z+1].config(state="disabled")
  145. self.buttons[x-1][y+z].config(state="disabled")
  146. elif (y+self.battleshipSize[self.battleshipCounter] == 10 and x!=0 and x!=9):
  147. self.playerField[y+z][x] = 'S'
  148. self.buttons[x][y+z].config(bg='red', state="disabled")
  149. self.buttons[x][y-1].config(state="disabled")
  150. self.buttons[x-1][y-1].config(state="disabled")
  151. self.buttons[x-1][y+z].config(state="disabled")
  152. self.buttons[x+1][y-1].config(state="disabled")
  153. self.buttons[x+1][y+z].config(state="disabled")
  154. tmp = True
  155. else:
  156. self.label = Label(self.formframe, text= 'Nie mozna ustawic tak statku\n')
  157. self.label.grid(row=2,column=0, sticky=N)
  158. tmp = False
  159.  
  160. for x in range(10):
  161. for y in range(10):
  162. print(self.playerField[x][y], end='')
  163. print('')
  164.  
  165. if self.battleshipCounter == 9:
  166. for i in range(10):
  167. for j in range(10):
  168. self.buttons[i][j].config(state='disabled')
  169. self.label2.config(text = 'Wszystkie statki zostaly rozstawione\n')
  170.  
  171. elif (self.battleshipCounter !=9 and tmp == True):
  172. self.battleshipCounter += 1
  173. self.label2.config(text = 'Wielkosc statku: ' + str(self.battleshipSize[self.battleshipCounter]))
  174.  
  175.  
  176. def setdirection(self, event):
  177. if self.direction == 'horizontal':
  178. self.direction = 'vertical'
  179. self.label.config(text = 'Kierunek statku: pionowo')
  180. else:
  181. self.direction = 'horizontal'
  182. self.label.config(text = 'Kierunek statku: poziomo')
  183.  
  184.  
  185. def checkShips(self, x, y, dir):
  186. if dir == 'horizontal':
  187. for a in range(x-1, x+self.battleshipSize[self.battleshipCounter]+1):
  188. for b in range(y-1, y+2):
  189. if(self.playerField[a][b] == 'S'):
  190. print(a,b)
  191. return False
  192. return True
  193. elif dir == 'vertical':
  194. for a in range(x-1, x+2):
  195. for b in range(y-1, y+self.battleshipSize[self.battleshipCounter]+1):
  196. if(self.playerField[a][b] == 'S'):
  197. print(a,b)
  198. return False
  199. return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement