资源简介
井字棋
代码片段和文件信息
from tkinter import *
import time
import tkinter.messagebox
tk = Tk()
tk.title(“井字棋“)
tk.resizable(0 0) # 不能随意改变棋盘大小
tk.wm_attributes(“-topmost“ 1) # 画布窗口始终放在最上面
canvas = Canvas(tk width=800 height=800 bd=0 highlightthickness=0) # 画布的宽是800高是800确保画布没有边框
canvas.pack() # 让画布按前面的调整大小
tk.update()
redorgreen = 0
colorx = “green“
position = [[0 0 0] [0 0 0] [0 0 0]]
overornot = 0
# 画棋盘
lines = [100 300 500 700]
for i in lines:
canvas.create_line(100 i 700 i)
canvas.create_line(i 100 i 700)
# canvas.bind(‘‘ baction) # 落子
# print(k)
def action(event):
global redorgreen colorx
global overornot
if overornot == 1: # 判断胜负是否已分
tkinter.messagebox.showinfo(“提示“ “游戏已结束“)
return
if (event.x <= 100 or event.x >= 700 or event.y <= 100 or event.y >= 700): # 判断是否在棋盘内部落子
tkinter.messagebox.showinfo(“提示“ “请下在棋盘内“)
return
if redorgreen == 0:
redorgreen = 1
colorx = “red“
else:
redorgreen = 0
colorx = “green“
a = 100
for i in range(0 3):
for j in range(0 3):
if event.x > 100 + i * 200 and event.x < 100 + (i + 1) * 200 and event.y < 100 + (
j + 1) * 200 and event.y > 100 + j * 200:
if position[i][j] == 0:
canvas.create_oval(100 + i * 200 100 + j * 200 100 + (i + 1) * 200 100 + (j + 1) * 200
fill=colorx)
if colorx == “red“:
position[i][j] = 1
else:
position[i][j] = 2
else:
tkinter.messagebox.showinfo(“提示“ “请勿重复下子“)
- 上一篇:xpath爬取豆瓣电影top250
- 下一篇:画一朵可自定义的花.py
评论
共有 条评论