当前位置: 首页 > 工具软件 > 变色方块 > 使用案例 >

定格蓝方块小游戏

闻人宝
2023-12-01

定格蓝方块作品

作品简介:
点击方块,会使此方块和周围相邻的方块变色,直到所有方块都变成蓝色
是一个益智小游戏
from turtle import *
import turtle
import random
import time

tracer(0)
# 边框
t1 = Turtle()
t1.speed(0)
t1.hideturtle()

t1.penup()
t1.goto(-300,-300)
t1.pendown()

for i in range(4):
    t1.fd(600)
    t1.lt(90)

# 画九宫格
def nine(x,y,z):
    t1.penup()
    t1.goto(x,y)
    t1.pendown()
    t1.setheading(z)
    t1.fd(600)

nine(-300,100,0)
nine(-300,-100,0)
nine(-100,300,-90)
nine(100,300,-90)
update()
# 填充第一个格子
def gezi(c,x,y):
    t1.penup()
    t1.fillcolor(c)
    t1.begin_fill()
    t1.goto(x,y)
    t1.setheading(0)
    for i in range(4):
        t1.fd(600/3)
        t1.rt(90)
    t1.end_fill()

# 创建格子
# gezi('blue',-250,250)
def everyone(x,y,x1,y1):
    if b[x][y] == 1:
        gezi('cyan', x1, y1)
        b[x][y] = 0
    else:
        gezi('blue', x1, y1)
        b[x][y] = 1

def f(x,y):
    global b
    # ---------第一排-----------
    '''
    everyone(0, 0, -300, 300)
    everyone(0, 1, -100, 300)
    everyone(0, 2,  100, 300)
    everyone(1, 0, -300, 100)
    everyone(1, 1, -100, 100)
    everyone(1, 2,  100, 100)
    everyone(2, 0, -300,-100)
    everyone(2, 1, -100,-100)
    everyone(2, 2,  100,-100)
    '''
    # 第一排第一个
    if -300 <x< -100 and 100< y < 300:
        everyone(0,0,-300,300)
        everyone(0,1,-100,300)
        everyone(1,0,-300,100)
        update()
    # 第一排第二个
    elif -100 <x< 100 and 100< y < 300:
        everyone(0,1,-100,300)
        everyone(0,0,-300,300)
        everyone(0,2,100,300)
        everyone(1,1,-100,100)
        update()
    # 第一排第三个
    elif 100 <x< 300 and 100< y < 300:
        everyone(0,2,100,300)
        everyone(0,1,-100,300)
        everyone(1,2,100,100)
        update()
    # ---------第二排-----------
    # 第二行第一个
    elif -300 <x< -100 and -100< y < 100:
        everyone(1,0,-300,100)
        everyone(0,0,-300,300)
        everyone(1,1,-100,100)
        everyone(2,0,-300,-100)
        update()

    # 第二行第二个
    elif -100 <x< 100 and -100< y < 100:
        everyone(1, 1, -100, 100)
        everyone(0, 1, -100, 300)
        everyone(1, 0, -300, 100)
        everyone(1, 2, 100, 100)
        everyone(2, 1, -100, -100)
        update()
    # 第二行第三个
    elif 100 <x< 300 and -100< y < 100:
        everyone(1, 2, 100, 100)
        everyone(0, 2,  100, 300)
        everyone(1, 1, -100, 100)
        everyone(2, 2, 100, -100)
        update()
    # ---------第三排-----------
    # 第三排第一个
    elif -300 <x< -100 and -300< y < -100:
        everyone(2, 0, -300, -100)
        everyone(1, 0, -300, 100)
        everyone(2, 1, -100, -100)
        update()
    # 第三排第二个
    elif -100 <x< 100 and -300< y < -100:
        everyone(2, 1, -100, -100)
        everyone(2, 0, -300, -100)
        everyone(2, 2, 100, -100)
        everyone(1, 1, -100, 100)
        update()
    # 第三排第三个
    elif 100 <x< 300 and -300< y < -100:
        everyone(2, 2, 100, -100)
        everyone(2, 1, -100, -100)
        everyone(1, 2, 100, 100)
        update()

    if b == [[1, 1, 1], [1, 1, 1], [1, 1, 1]]:
        w = Turtle()
        w.pensize(20)
        w.penup()
        w.goto(-100,10)
        w.pendown()
        w.pencolor('purple')
        w.write('游戏胜利!!!',font=('微软雅黑',30))
        time.sleep(3)
        exit()

# 游戏功能部分
x = [-300,-100,100]
y = [300,100,-100]
c = ['blue','cyan']
b = [[1,1,1],[1,1,1],[1,1,1]]
for i in range(3):
    for j in range(3):
        color = random.choice(c)
        if color == 'blue':
            b[i][j] = 1
        else:
            b[i][j] = 0
        gezi(color,x[j],y[i])

turtle.onscreenclick(f)
update()



mainloop()
 类似资料: