成都创新互联网站制作重庆分公司

利用python实现一个简单的五子棋小游戏-创新互联

利用python实现一个简单的五子棋小游戏?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

成都创新互联是一家集网站建设,惠东企业网站建设,惠东品牌网站建设,网站定制,惠东网站建设报价,网络营销,网络优化,惠东网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

具体内容如下

# -*- coding:utf-8 -*-
# @Time: 2017/8/29 0029 10:14
# @Author: assasin
# @Email: assasin0308@sina.com
 
from tkinter import *
import math
 
class chessBoard():
  def __init__(self):
    # 创建一个tk对象,窗口
    self.window = Tk()
    # 窗口名称
    self.window.title('五子棋游戏')
    # 窗口大小
    self.window.geometry('660x470')
    # 设置窗口不可缩放
    self.window.resizable(0,0)
    # 定义窗口的画布
    self.canvas = Canvas(self.window, bg="#EEE8AC", width=470, height=470)
    # 画出画布内容
    self.paint_board()
    # 定义画布所在的网格
    self.canvas.grid(row=0, column=0)
 
  def paint_board(self):
    # 画横线
    for row in range(0, 15):
      if row == 0 or row == 14:
        self.canvas.create_line(25, 25 + row * 30, 25 + 14 * 30, 25 + row * 30, width=2)
    else:
      self.canvas.create_line(25, 25 + row * 30, 25 + 14 * 30, 25 + row * 30, width=1)
 
    # 画竖线
    for column in range(0, 15):
      if column == 0 or column == 14:
        self.canvas.create_line(25 + column * 30, 25, 25 + column * 30, 25 + 14 * 30, width=2)
    else:
      self.canvas.create_line(25 + column * 30, 25, 25 + column * 30, 25 + 14 * 30, width=1)
 
    # 画圆
    self.canvas.create_oval(112, 112, 118, 118, fill="black")
    self.canvas.create_oval(352, 112, 358, 118, fill="black")
    self.canvas.create_oval(112, 352, 118, 358, fill="black")
    self.canvas.create_oval(232, 232, 238, 238, fill="black")
    self.canvas.create_oval(352, 352, 358, 358, fill="black")
 
 
 
 
#定义五子棋游戏类
#0为黑子 , 1为白子 , 2为空位
class Gobang() :
  #初始化
  def __init__(self) :
    self.board = chessBoard()
    self.game_print = StringVar()
    self.game_print.set("")
    # 16*16的二维列表,保证不会out of index
    self.db = [([2] * 16) for i in range(16)]
    # 悔棋用的顺序列表
    self.order = []
    # 棋子颜色
    self.color_count = 0
    self.color = 'black'
    # 清空与赢的初始化,已赢为1,已清空为1
    self.flag_win = 1
    self.flag_empty = 1
    self.options()
 
    # 黑白互换
  def change_color(self):
    self.color_count = (self.color_count + 1) % 2
    if self.color_count == 0:
      self.color = "black"
    elif self.color_count == 1:
      self.color = "white"
 
  # 落子
  def chess_moving(self,event):
    # 不点击“开始”与“清空”无法再次开始落子
    if self.flag_win == 1 or self.flag_empty == 0:
      return
    # 坐标转化为下标
    x, y = event.x - 25, event.y - 25
    x = round(x / 30)
    y = round(y / 30)
    # 点击位置没用落子,且没有在棋盘线外,可以落子
    while self.db[y][x] == 2 and self.limit_boarder(y, x):
      self.db[y][x] = self.color_count
    self.order.append(x + 15 * y)
    self.board.canvas.create_oval(25 + 30 * x - 12, 25 + 30 * y - 12, 25 + 30 * x + 12, 25 + 30 * y + 12,fill=self.color, tags="chessman")
    if self.game_win(y, x, self.color_count):
      print(self.color, "获胜")
      self.game_print.set(self.color + "获胜")
    else:
      self.change_color()
      self.game_print.set("请" + self.color + "落子")
 
  # 保证棋子落在棋盘上
  def limit_boarder(self, y, x):
    if x < 0 or x > 14 or y < 0 or y > 14:
      return False
    else:
      return True
 
  # 计算连子的数目,并返回大连子数目
  def chessman_count(self, y, x, color_count):
    count1, count2, count3, count4 = 1, 1, 1, 1
    # 横计算
    for i in range(-1, -5, -1):
      if self.db[y][x + i] == color_count:
        count1 += 1
      else:
        break
 
    for i in range(1, 5, 1):
      if self.db[y][x + i] == color_count:
        count1 += 1
      else:
        break
    # 竖计算
    for i in range(-1, -5, -1):
      if self.db[y + i][x] == color_count:
        count2 += 1
      else:
        break
    for i in range(1, 5, 1):
      if self.db[y + i][x] == color_count:
        count2 += 1
      else:
        break
    # /计算
    for i in range(-1, -5, -1):
      if self.db[y + i][x + i] == color_count:
        count3 += 1
      else:
        break
    for i in range(1, 5, 1):
      if self.db[y + i][x + i] == color_count:
        count3 += 1
      else:
        break
 
    # \计算
    for i in range(-1, -5, -1):
      if self.db[y + i][x - i] == color_count:
        count4 += 1
      else:
        break
    for i in range(1, 5, 1):
      if self.db[y + i][x - i] == color_count:
        count4 += 1
      else:
        break
 
    return max(count1, count2, count3, count4)
 
 
  # 判断输赢
  def game_win(self , y , x , color_count ):
    if self.chessman_count(y, x, color_count) >= 5:
      self.flag_win = 1
      self.flag_empty = 0
      return True
    else:
      return False
 
  #悔棋,清空棋盘,再画剩下的n-1个棋子
  def withdraw(self):
    if len(self.order) == 0 or self.flag_win == 1:
      return
    self.board.canvas.delete("chessman")
    z = self.order.pop()
    x = z % 15
    y = z // 15
    self.db[y][x] = 2
    self.color_count = 1
    for i in self.order:
      ix = i % 15
    iy = i // 15
    self.change_color()
    self.board.canvas.create_oval(25 + 30 * ix - 12, 25 + 30 * iy - 12, 25 + 30 * ix + 12, 25 + 30 * iy + 12,
                   fill=self.color, tags="chessman")
    self.change_color()
    self.game_print.set("请" + self.color + "落子")
 
  # 清空
  def empty_all(self) :
    self.board.canvas.delete("chessman")
    # 还原初始化
    self.db = [([2] * 16) for i in range(16)]
    self.order = []
    self.color_count = 0
    self.color = 'black'
    self.flag_win = 1
    self.flag_empty = 1
    self.game_print.set("")
 
  #将self.flag_win置0才能在棋盘上落子
  def game_start(self):
    # 没有清空棋子不能置0开始
    if self.flag_empty == 0:
      return
    self.flag_win = 0
    self.game_print.set("请" + self.color + "落子")
 
  def options(self):
    self.board.canvas.bind("", self.chess_moving)
    Label(self.board.window, textvariable=self.game_print, font=("Arial", 20)).place(relx=0, rely=0, x=495, y=200)
    Button(self.board.window, text="开始游戏", command=self.game_start, width=13, font=("Verdana", 12)).place(relx=0,rely=0,x=495,y=15)
    Button(self.board.window, text="我要悔棋", command=self.withdraw, width=13, font=("Verdana", 12)).place(relx=0,rely=0,x=495, y=60)
    Button(self.board.window, text="清空棋局", command=self.empty_all, width=13, font=("Verdana", 12)).place(relx=0,rely=0,x=495,y=105)
    Button(self.board.window, text="结束游戏", command=self.board.window.destroy, width=13, font=("Verdana", 12)).place(relx=0, rely=0, x=495, y=420)
    self.board.window.mainloop()
 
 
if __name__ == '__main__':
  chess_game = Gobang()

标题名称:利用python实现一个简单的五子棋小游戏-创新互联
新闻来源:http://cxhlcq.cn/article/dcjohh.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部