资源简介
python实现的GUI井字棋游戏,精美的开始界面及开始按钮
游戏功能:
机器人:简单的AI人工智能算法
玩家:鼠标操作

代码片段和文件信息
import tkinter as tk
from tkinter import messagebox
class Window(object):
def __init__(self master):
self.master = master
self.game = Game()
self.can = None
self.start_game()
def start_game(self):
global imag1 imag2
self.lf = tk.Labelframe(self.master)
self.lbl = tk.Label(self.lf image=imag1)
self.lbl.pack()
self.btn = tk.Button(self.lf image=imag2 command=self.create_board)
self.btn.place(x=95 y=140)
self.lf.pack()
def create_board(self):
global imag3
if self.lf:
self.lf.destroy()
if self.can:
self.can.destroy()
self.game.over = False
self.game.board = [i for i in range(9)]
self.can = tk.Canvas(self.master width=300 height=300)
self.can.create_line((30 30) (270 30) width=2)
self.can.create_line((270 30) (270 270) width=2)
self.can.create_line((270 270) (30 270) width=2)
self.can.create_line((30 270) (30 30) width=2)
for i in range(3):
self.can.create_line((30 (i + 1) * 80 + 30) (270 (i + 1) * 80 + 30) width=2)
for i in range(3):
self.can.create_line(((i + 1) * 80 + 30 30) ((i + 1) * 80 + 30 270) width=2)
self.can.pack(expand=‘YES‘ fill=‘both‘)
self.can.bind(““ self.func)
self.btn1 = tk.Button(self.master text=‘重新开始‘ command=self.create_board)
self.btn1.place(x=120 y=273)
def func(self event):
if self.game.over:
tk.messagebox.showinfo(‘提示‘ ‘游戏已结束!‘)
return
sum = 0
flag = False
for i in range(30 191 80):
for j in range(30 191 80):
sum += 1
if j <= event.x < j + 80 and i <= event.y < i + 80:
self.can.create_oval((j i) (j + 80 i + 80) width=2 outline=‘red‘)
self.game.board[sum - 1] = ‘O‘
flag = True
break
if flag:
break
if flag:
if self.game.isWinner(self.game.board):
tk.messagebox.showinfo(‘提示‘ ‘玩家胜利!‘)
self.game.over = True
elif self.game.is_Tie():
tk.messagebox.showinfo(‘提示‘ ‘平局!游戏结束‘)
self.game.over = True
else:
self.robot_draw()
def robot_draw(self):
move = self.game.Rob_play() + 1
sum = 0
flag = False
for i in range(30 191 80):
for j in range(30 191 80):
sum += 1
if sum == move:
self.can.create_line((j i) (j + 80 i + 80) width=2 fill=‘blue‘)
self.can.create_line((j i + 80) (j + 80 i) width=2 fill=‘blue‘)
self.game.board[sum - 1] = ‘X‘
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 11377 2020-07-07 19:02 GUI-井字棋\image\井字棋-开始游戏.png
文件 23435 2020-07-07 18:59 GUI-井字棋\image\井字棋界面.png
文件 5331 2020-07-20 10:23 GUI-井字棋\Window.py
文件 4823 2020-07-07 20:18 GUI-井字棋\__pycache__\Window.cpython-38.pyc
目录 0 2020-07-20 10:18 GUI-井字棋\image
目录 0 2020-07-07 20:18 GUI-井字棋\__pycache__
目录 0 2020-07-20 10:23 GUI-井字棋
----------- --------- ---------- ----- ----
44966 7
相关资源
- 易语言python支持库
- Python-BDD100K大规模多样化驾驶视频数据
- Instant Pygame for Python Game Development How
- Biopython Tutorial
- Think Python 2nd
- 一个小小的表白程序(python)
- Python课堂笔记(高淇400集第一季)
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
评论
共有 条评论