[Pyhon] 成發-Gobang
時隔這麼久,我終於停止耍廢了(X
為了高二的成發,所以我花了這幾天學了下tkinter,然後寫了一個PvP的五子棋遊戲出來了,嗯嗯,反正也沒有甚麼技術成分,只是有時候把_打成i然後de了半小時的code之類的,sad,總之最後弄出來了啦XD,有想過要弄得很精美,不過我有好多障礙所以還是算了,背景色有點糟糕,可能會再調吧,總之,嗯,就這樣吧。
下面附上code,猜到了嗎?對! 就是這不是用C++寫的喔>_<
為了高二的成發,所以我花了這幾天學了下tkinter,然後寫了一個PvP的五子棋遊戲出來了,嗯嗯,反正也沒有甚麼技術成分,只是有時候把_打成i然後de了半小時的code之類的,sad,總之最後弄出來了啦XD,有想過要弄得很精美,不過我有好多障礙所以還是算了,背景色有點糟糕,可能會再調吧,總之,嗯,就這樣吧。
下面附上code,猜到了嗎?對! 就是這不是用C++寫的喔>_<
import tkinter as tk import tkinter.messagebox import numpy as np class Chess_Board(): def __init__(self): self.start_X = 200 self.start_Y = 150 self.board_width = 30 self.board_num = 19 self.board_line_width = 2 self.piece_size = 10 self.canvas_width = 1024 self.canvas_height = 876 self.window = tk.Tk() self.window.title('Gobang') self.window.configure(bg = '#bb5e00') #self.window.geometry('1400x968') self.count = 0 self.win_num = 5 self.canvas = tk.Canvas(self.window,bg = '#bb5e00',width = self.canvas_width, height = self.canvas_height) self.element = [] self.button = tk.Button(self.window,text = 'New Game',width = 15, height = 5,command = self.new_game) self.button.pack() def has_winner(self,x,y): d = np.array([0,1,-1]) for i in range(3): for j in range(3): if d[i] == 0 and d[j] == 0: continue dx,dy,cnt = x,y,0 for _ in range(self.win_num): if self.judge[dx][dy] == self.judge[x][y]: cnt += 1 dx, dy = dx+d[i], dy+d[j] if dx < 0 or dx >= self.board_num or dy < 0 or dy >= self.board_num: break if cnt == self.win_num: return self.judge[x][y] return False def paint(self,event): px,py = (event.x-self.start_X)//self.board_width, (event.y-self.start_Y)//self.board_width x, y = px * self.board_width + self.board_width/2 + self.start_X, py * self.board_width + self.board_width/2 + self.start_Y if x-self.piece_size < self.start_X or x+self.piece_size > self.board_num*self.board_width+self.start_X or y-self.piece_size < self.start_Y or y+self.piece_size > self.board_num*self.board_width+self.start_Y or self.judge[px][py] != 0: return self.judge[px][py] = 1 if self.count %2 == 0 else -1 piece_color = 'black' if self.count % 2 == 0 else 'white' self.element.append(self.canvas.create_oval(x-self.piece_size,y-self.piece_size,x+self.piece_size,y+self.piece_size,fill = piece_color)) self.count += 1 flag = self.has_winner(px,py) winner = 'Black' if flag == 1 else 'White' if flag != 0: tk.messagebox.showinfo(title = 'game',message = winner + ' Win') self.clear_game() def new_game(self): for num in range(self.board_num+1): self.canvas.create_line(num*self.board_width+self.start_X,self.start_Y,num*self.board_width+self.start_X,self.board_width*self.board_num+self.start_Y,width = self.board_line_width) self.canvas.create_line(self.start_X,num*self.board_width+self.start_Y,self.board_width*self.board_num+self.start_X,num*self.board_width+self.start_Y,width = self.board_line_width) self.judge = np.zeros((self.board_num+1,self.board_num+1)) self.play_game() def play_game(self): self.clear_game() self.canvas.bind('<Button-1>',self.paint) self.canvas.pack(expand = tk.YES, fill = tk.BOTH) self.window.mainloop() def clear_game(self): for ele in self.element: self.canvas.delete(ele) self.judge = np.zeros((self.board_num+1,self.board_num+1)) self.count = 0 chess_board = Chess_Board() chess_board.new_game()
留言
張貼留言