汇智学堂-python系列小游戏(井字游戏之五)

2.7不同位置交替落子
现在我们要在棋盘上的不同位置点击,交替颜色画棋子。下面是我们要做的事情。
1、在第一方格内点击时,落子在第一方格内。
2、每次单击后,颜色交替。
3、依据第一步骤的方案,将其他八个方格的落子完成。

实现代码如下:

def action(event):
global redorgreen
global colorx

#if redorgreen0 and event.x>100 and event.x<300 and event.y<300 and event.y>100:
# canvas.create_oval(100, 100, 300, 300, fill =colorx )
if redorgreen
0:
redorgreen=1
colorx=“red”

else :
    redorgreen=0
    colorx="green"
    
#tkinter.messagebox.showinfo('提示',redorgreen)

if event.x>100 and event.x<300 and event.y<300 and event.y>100:
    canvas.create_oval(100, 100, 300, 300, fill =colorx )
if event.x>300 and event.x<500 and event.y<300 and event.y>100:
    canvas.create_oval(300, 100, 500, 300, fill =colorx )
if event.x>500 and event.x<700 and event.y<300 and event.y>100:
    canvas.create_oval(500, 100, 700, 300, fill =colorx )  

if event.x>100 and event.x<300 and event.y<500 and event.y>300:
    canvas.create_oval(100, 300, 300, 500, fill =colorx )
if event.x>300 and event.x<500 and event.y<500 and event.y>300:
    canvas.create_oval(300, 300, 500, 500, fill =colorx )
if event.x>500 and event.x<700 and event.y<500 and event.y>300:
    canvas.create_oval(500, 300, 700, 500, fill =colorx )

if event.x>100 and event.x<300 and event.y<700 and event.y>500:
    canvas.create_oval(100, 500, 300, 700, fill =colorx )
if event.x>300 and event.x<500 and event.y<700 and event.y>500:
    canvas.create_oval(300, 500, 500, 700, fill =colorx )
if event.x>500 and event.x<700 and event.y<700 and event.y>500:
    canvas.create_oval(500, 500, 700, 700, fill =colorx )

if event.x>100 and event.x<300 and event.y<300 and event.y>100:if作为条件判断,根据鼠标点击的位置不同,将棋子画在棋盘的不同位置上。

将代码整合起来,整合后完整代码如下:

#-- coding:GBK --

from tkinter import *
import time

import tkinter.messagebox #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)
canvas.pack()
tk.update()

redorgreen=0
colorx=“green”

#画棋盘
canvas.create_line(100,100,700,100)
canvas.create_line(100,300,700,300)
canvas.create_line(100,500,700,500)
canvas.create_line(100,700,700,700)

canvas.create_line(100,100,100,700)
canvas.create_line(300,100,300,700)
canvas.create_line(500,100,500,700)
canvas.create_line(700,100,700,700)

def action(event):
global redorgreen
global colorx

#if redorgreen0 and event.x>100 and event.x<300 and event.y<300 and event.y>100:
# canvas.create_oval(100, 100, 300, 300, fill =colorx )
if redorgreen
0:
redorgreen=1
colorx=“red”

else :
    redorgreen=0
    colorx="green"
    
#tkinter.messagebox.showinfo('提示',redorgreen)

if event.x>100 and event.x<300 and event.y<300 and event.y>100:
    canvas.create_oval(100, 100, 300, 300, fill =colorx )
if event.x>300 and event.x<500 and event.y<300 and event.y>100:
    canvas.create_oval(300, 100, 500, 300, fill =colorx )
if event.x>500 and event.x<700 and event.y<300 and event.y>100:
    canvas.create_oval(500, 100, 700, 300, fill =colorx )  

if event.x>100 and event.x<300 and event.y<500 and event.y>300:
    canvas.create_oval(100, 300, 300, 500, fill =colorx )
if event.x>300 and event.x<500 and event.y<500 and event.y>300:
    canvas.create_oval(300, 300, 500, 500, fill =colorx )
if event.x>500 and event.x<700 and event.y<500 and event.y>300:
    canvas.create_oval(500, 300, 700, 500, fill =colorx )

if event.x>100 and event.x<300 and event.y<700 and event.y>500:
    canvas.create_oval(100, 500, 300, 700, fill =colorx )
if event.x>300 and event.x<500 and event.y<700 and event.y>500:
    canvas.create_oval(300, 500, 500, 700, fill =colorx )
if event.x>500 and event.x<700 and event.y<700 and event.y>500:
    canvas.create_oval(500, 500, 700, 700, fill =colorx )

canvas.bind(’’, action)
while 1:
tk.update_idletasks()
tk.update()
time.sleep(0.01)

运行这段代码,在3*3方格的任意位置点击,棋子就会出现在棋盘上的不同方格中了。见图2-12
在这里插入图片描述
图2-12

猜你喜欢

转载自blog.csdn.net/weixin_39593940/article/details/88345448
今日推荐