Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter
- from tkinter import *
- class GameTableView:
- def __init__(self, game, size):#onclick):
- #self.img = tkinter.PhotoImage(file="images/empty.png")
- self.window = tkinter.Tk()
- self.window.title('Twoja tablica')
- self.window.resizable(width=tkinter.FALSE, height=tkinter.FALSE)
- self.window.geometry('{}x{}'.format(800, 700))
- self.direction = 'horizontal'
- self.playerField = [['0'] * size for x in range(size)]
- for x in range(size):
- for y in range(size):
- print(self.playerField[x][y], end='')
- print('')
- self.battleshipSize = [4,3,3,2,2,2,1,1,1,1]
- self.battleshipCounter = 0
- self.wrapperframe = tkinter.Frame(self.window)
- self.wrapperframe.grid()
- self.formframe = tkinter.Frame(self.wrapperframe)
- self.formframe.grid(row=0, column=11)
- self.buttonsFrame = tkinter.Frame(self.wrapperframe)
- self.buttonsFrame.grid(row=0, column=0)
- self.buttons = [[tkinter.Button(self.buttonsFrame, width=4, height=4, bg='blue',
- command= lambda x=x, y=y:self.mark_area(x, y))#lambda x=x, y=y:onclick(x, y))
- for y in range(size)] for x in range(size)]
- for x in range(size):
- for y in range(size):
- self.buttons[x][y].grid(column=x, row=y)
- self.label = Label(self.formframe, text = 'Kierunek statku: poziomo')
- self.label.grid(row=0,column=0, sticky=N)
- self.label2 = Label(self.formframe, text = 'Wielkosc statku: ' + str(self.battleshipSize[self.battleshipCounter]))
- self.label2.grid(row=1,column=0, sticky=N)
- self.window.bind("<Key>", self.setdirection)
- def mark_area(self, x, y):
- tmp = True #zmienna, ktora sprawdza czy statek poprawnie sie wykonal,
- # jesli nie mozna bylo ustawic statku to licznik statku nie jest inkrementowany
- if (self.direction == 'horizontal'):
- for z in range(self.battleshipSize[self.battleshipCounter]):
- if (x+self.battleshipSize[self.battleshipCounter]-1 <= 9): #wykluczamy od razu gdy statek chce byc rozmieszczony poza ramy
- #I PRZYPADEK statki moga byc rozmieszczone po calym srodku, ale nie do ost kolumny i bez pierwszego i ostatniego wiersza
- if (x+self.battleshipSize[self.battleshipCounter] < 10 and y != 0 and y !=9):
- self.playerField[y][x+z] = 'S'
- self.buttons[x+z][y].config(bg='red', state="disabled")
- self.buttons[x-1][y].config(state="disabled")
- self.buttons[x-1][y+1].config(state="disabled")
- self.buttons[x-1][y-1].config(state="disabled")
- self.buttons[x+z][y+1].config(state="disabled")
- self.buttons[x+z][y-1].config(state="disabled")
- self.buttons[x+z+1][y-1].config(state="disabled")
- self.buttons[x+z+1][y].config(state="disabled")
- self.buttons[x+z+1][y+1].config(state="disabled")
- #II PRZYPADEK tylko gdy mamy statek na pierwszym wierszu, do ost kolumny wlacznie
- elif (y == 0 and x+self.battleshipSize[self.battleshipCounter] == 10):
- self.playerField[y][x+z] = 'S'
- self.buttons[x+z][y].config(bg='red', state="disabled")
- self.buttons[x-1][y].config(state="disabled")
- self.buttons[x-1][y+1].config(state="disabled")
- self.buttons[x+z][y+1].config(state="disabled")
- #III PRZYPADEK tylko gdy mamy statek na ostatnim wierszu, ale do ostatniej kolumny wlacznie
- elif (y == 9 and x+self.battleshipSize[self.battleshipCounter] == 10):
- self.playerField[y][x+z] = 'S'
- self.buttons[x+z][y].config(bg='red', state="disabled")
- self.buttons[x-1][y].config(state="disabled")
- self.buttons[x-1][y-1].config(state="disabled")
- self.buttons[x+z][y-1].config(state="disabled")
- #IV PRZYPADEK tylko gdy mamy statek na pierwszym wierszu, ale nie jest ustawiony do samego konca
- elif (y == 0 and x+self.battleshipSize[self.battleshipCounter] < 10):
- self.playerField[y][x+z] = 'S'
- self.buttons[x+z][y].config(bg='red', state="disabled")
- self.buttons[x+z+1][y].config(state="disabled")
- self.buttons[x-1][y].config(state="disabled")
- self.buttons[x+z+1][y+1].config(state="disabled")
- self.buttons[x-1][y+1].config(state="disabled")
- self.buttons[x+z][y+1].config(state="disabled")
- #V PRZYPADEK tylko gdy mamy statek na ostatnim wierszu, ale nie jest ustawiony do samego konca
- elif (y == 9 and x+self.battleshipSize[self.battleshipCounter] < 10):
- self.playerField[y][x+z] = 'S'
- self.buttons[x+z][y].config(bg='red', state="disabled")
- self.buttons[x+z+1][y].config(state="disabled")
- self.buttons[x-1][y].config(state="disabled")
- self.buttons[x+z+1][y-1].config(state="disabled")
- self.buttons[x-1][y-1].config(state="disabled")
- self.buttons[x+z][y-1].config(state="disabled")
- #VI PRZYPADEK gdy mamy statek rozmieszczony po calym srodku, ale do ost kolumny, bez pierwszego i ostatniego wiersza
- elif (x+self.battleshipSize[self.battleshipCounter] == 10 and y != 0 and y !=9):
- self.playerField[y][x+z] = 'S'
- self.buttons[x+z][y].config(bg='red', state="disabled")
- self.buttons[x-1][y].config(state="disabled")
- self.buttons[x-1][y+1].config(state="disabled")
- self.buttons[x-1][y-1].config(state="disabled")
- self.buttons[x+z][y+1].config(state="disabled")
- self.buttons[x+z][y-1].config(state="disabled")
- tmp = True
- else:
- self.label = Label(self.formframe, text= 'Nie mozna ustawic tak statku\n')
- self.label.grid(row=2,column=0, sticky=N)
- tmp = False
- else:
- for z in range(self.battleshipSize[self.battleshipCounter]):
- if (y+self.battleshipSize[self.battleshipCounter]-1 <= 9):
- if (y+self.battleshipSize[self.battleshipCounter] < 10 and x != 0 and x !=9):
- self.playerField[y+z][x] = 'S'
- self.buttons[x][y+z].config(bg='red', state="disabled")
- self.buttons[x][y-1].config(state="disabled")
- self.buttons[x][y+z+1].config(state="disabled")
- self.buttons[x-1][y-1].config(state="disabled")
- self.buttons[x+1][y-1].config(state="disabled")
- self.buttons[x-1][y+z+1].config(state="disabled")
- self.buttons[x+1][y+z+1].config(state="disabled")
- self.buttons[x-1][y+z].config(state="disabled")
- self.buttons[x+1][y+z].config(state="disabled")
- elif (x == 0 and y+self.battleshipSize[self.battleshipCounter] == 10):
- self.playerField[y+z][x] = 'S'
- self.buttons[x][y+z].config(bg='red', state="disabled")
- self.buttons[x][y-1].config(state="disabled")
- self.buttons[x+1][y-1].config(state="disabled")
- self.buttons[x+1][y+z].config(state="disabled")
- elif (x == 9 and y+self.battleshipSize[self.battleshipCounter] == 10):
- self.playerField[y+z][x] = 'S'
- self.buttons[x][y+z].config(bg='red', state="disabled")
- self.buttons[x][y-1].config(state="disabled")
- self.buttons[x-1][y-1].config(state="disabled")
- self.buttons[x-1][y+z].config(state="disabled")
- elif (x == 0 and y+self.battleshipSize[self.battleshipCounter] < 10):
- self.playerField[y+z][x] = 'S'
- self.buttons[x][y+z].config(bg='red', state="disabled")
- self.buttons[x][y-1].config(state="disabled")
- self.buttons[x][y+z+1].config(state="disabled")
- self.buttons[x+1][y-1].config(state="disabled")
- self.buttons[x+1][y+z+1].config(state="disabled")
- self.buttons[x+1][y+z].config(state="disabled")
- elif (x == 9 and y+self.battleshipSize[self.battleshipCounter] < 10):
- self.playerField[y+z][x] = 'S'
- self.buttons[x][y+z].config(bg='red', state="disabled")
- self.buttons[x][y-1].config(state="disabled")
- self.buttons[x][y+z+1].config(state="disabled")
- self.buttons[x-1][y-1].config(state="disabled")
- self.buttons[x-1][y+z+1].config(state="disabled")
- self.buttons[x-1][y+z].config(state="disabled")
- elif (y+self.battleshipSize[self.battleshipCounter] == 10 and x!=0 and x!=9):
- self.playerField[y+z][x] = 'S'
- self.buttons[x][y+z].config(bg='red', state="disabled")
- self.buttons[x][y-1].config(state="disabled")
- self.buttons[x-1][y-1].config(state="disabled")
- self.buttons[x-1][y+z].config(state="disabled")
- self.buttons[x+1][y-1].config(state="disabled")
- self.buttons[x+1][y+z].config(state="disabled")
- tmp = True
- else:
- self.label = Label(self.formframe, text= 'Nie mozna ustawic tak statku\n')
- self.label.grid(row=2,column=0, sticky=N)
- tmp = False
- for x in range(10):
- for y in range(10):
- print(self.playerField[x][y], end='')
- print('')
- if self.battleshipCounter == 9:
- for i in range(10):
- for j in range(10):
- self.buttons[i][j].config(state='disabled')
- self.label2.config(text = 'Wszystkie statki zostaly rozstawione\n')
- elif (self.battleshipCounter !=9 and tmp == True):
- self.battleshipCounter += 1
- self.label2.config(text = 'Wielkosc statku: ' + str(self.battleshipSize[self.battleshipCounter]))
- def setdirection(self, event):
- if self.direction == 'horizontal':
- self.direction = 'vertical'
- self.label.config(text = 'Kierunek statku: pionowo')
- else:
- self.direction = 'horizontal'
- self.label.config(text = 'Kierunek statku: poziomo')
- def checkShips(self, x, y, dir):
- if dir == 'horizontal':
- for a in range(x-1, x+self.battleshipSize[self.battleshipCounter]+1):
- for b in range(y-1, y+2):
- if(self.playerField[a][b] == 'S'):
- print(a,b)
- return False
- return True
- elif dir == 'vertical':
- for a in range(x-1, x+2):
- for b in range(y-1, y+self.battleshipSize[self.battleshipCounter]+1):
- if(self.playerField[a][b] == 'S'):
- print(a,b)
- return False
- return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement