美文网首页
python 画棋盘

python 画棋盘

作者: 微笑城ios | 来源:发表于2019-04-16 18:21 被阅读0次

最近 python 上毒, 今天尝试了一下 python , 画一个五子棋的棋盘

棋盘点击效果

直接上代码 , 代码里面有注释

from tkinter import *

# 创建并添加 canvas
# 创建窗口
root = Tk()
root.title("五子棋")
gaird_width = 30
gaird_count = 17

widths = gaird_width*gaird_count + 20

root.maxsize(widths, widths)
root.minsize(widths, widths)
# 创建并添加Canvas
cv = Canvas(root, background='white')
cv.pack(fill=BOTH, expand=YES)


# 画一个外边框为白的 , 填充棋盘颜色
cv.create_rectangle(10,10,gaird_width*gaird_count + 10,gaird_width*gaird_count + 10,outline="white", fill="#CD8500")

# 在棋盘里面画 画格子
for num in range(1,gaird_count):
    cv.create_line(num*gaird_width + 10 ,
                   gaird_width + 10,
                   num*gaird_width + 10,
                   gaird_width*(gaird_count-1) + 10,
                   width=2,
                   fill="#595959")
for num in range(1,gaird_count):
    cv.create_line(gaird_width + 10 ,
                   num*gaird_width + 10,
                   (gaird_count-1)*gaird_width + 10,
                   num*gaird_width + 10,
                   width=2,
                   fill="#595959"
                   )

def paint(event):
    python_green = "black"
    x: int = int((event.x + 0.5 * gaird_width - 10)/gaird_width);
    y: int = int((event.y + 0.5 * gaird_width - 10)/gaird_width);

    print(x,y)

    x1, y1 = (x*gaird_width ), (y*gaird_width)
    x2, y2 = (x*gaird_width + 20), (y*gaird_width + 20)
    cv.create_oval(x1,y1,x2,y2, fill = python_green)

cv.bind("<Button-1>", paint)
# <Button-1>:鼠标左击事件
# <Button-2>:鼠标中击事件
# <Button-3>:鼠标右击事件
# <Double-Button-1>:双击事件
# <Triple-Button-1>:三击事件

message = Label(root, text = "press and drag the mouse to tap")
message.pack(side = BOTTOM)

root.mainloop()

未完待续

相关文章

  • python 画棋盘

    最近 python 上毒, 今天尝试了一下 python , 画一个五子棋的棋盘 直接上代码 , 代码里面有注释 ...

  • Day4:用python做2048游戏_1

    今天主要是跟着用python做2048游戏学了一些pyglet库的基础用法,比如画棋盘画格子、标签文本等用法。在搜...

  • python3 使用turtle 画围棋棋盘

  • Python:游戏:五子棋之人机对战

    开端 画棋盘 首先肯定是要画出棋盘来,用pygame画出一个 19 × 19 或 15 × 15 的棋盘并不是什么...

  • Matlab 画棋盘格

    %画棋盘格 width=1920 ; %pattern的宽 height=1080 ; %patt...

  • 用JS+canvas来实现五子棋人机大战

    1. 创建实例 2. 初始化 3. 生成canvas棋盘 4. 初始化棋盘 5. 画棋子 6. 移动聚焦 7. 算...

  • 自学Python:马踏棋盘

    国际象棋的棋盘为8×8的方格棋盘。现将“马”放在任意指定的方格中,按照“马”走棋的规则将“马”进行移动。 要求每个...

  • 纯JS实现五子棋

    棋盘界面十分简单,不怎么美观,主要实现一下功能。 首先画20*20的棋盘(横竖都可以放20个棋子),全部可放400...

  • 好好玩

    昨天晚上,突然想飞行棋,家里没有,和孩子们动手自己做棋盘和棋子。大儿子绘画比较熟练,他来画棋盘,我剪棋子,小儿子涂...

  • 随顺世缘无挂碍,涅槃生死等空花。

    明末崇祯年间,有人画了一幅画,巍然耸立的一棵松树,树下有一方大石,大石上摆着一个棋盘,棋盘上面几颗疏疏落落的棋子,...

网友评论

      本文标题:python 画棋盘

      本文链接:https://www.haomeiwen.com/subject/nyliwqtx.html