Pygame游戏:七巧板拼数字

滑动数字到位的益智游戏。点击空方块相邻的瓷砖来交换位置。你能让瓷砖从左到右、
从上到下,顺序数出一到十五吗?

from random import *
from turtle import *
from freegames import floor, vector

tiles = {}
neighbors = [
    vector(100, 0),
    vector(-100, 0),
    vector(0, 100),
    vector(0, -100),
]

def load():
    "Load tiles and scramble."
    count = 1

    for y in range(-200, 200, 100):
        for x in range(-200, 200, 100):
            mark = vector(x, y)
            tiles[mark] = count
            count += 1

    tiles[mark] = None

    for count in range(1000):
        neighbor = choice(neighbors)
        spot = mark + neighbor

        if spot in tiles:
            number = tiles[spot]
            tiles[spot] = None
            tiles[mark] = number
            mark = spot

def square(mark, number):
    "Draw white square with black outline and number."
    import turtle
    turtle.up()
    turtle.goto(mark.x, mark.y)
    turtle.down()

    turtle.color('black', 'white')
    turtle.begin_fill()
    for count in range(4):
        turtle.forward(99)
        turtle.left(90)
    turtle.end_fill()

    if number is None:
        return
    elif number < 10:
        turtle.forward(20)

    turtle.write(number, font=('Arial', 60, 'normal'))

def tap(x, y):
    "Swap tile and empty square."
    x = floor(x, 100)
    y = floor(y, 100)
    mark = vector(x, y)

    for neighbor in neighbors:
        spot = mark + neighbor

        if spot in tiles and tiles[spot] is None:
            number = tiles[mark]
            tiles[spot] = number
            square(spot, number)
            tiles[mark] = None
            square(mark, None)

def draw():
    "Draw all tiles."
    for mark in tiles:
        square(mark, tiles[mark])
    update()

turtle.setup(420, 420, 370, 0)
turtle.hideturtle()
turtle.tracer(False)
load()
draw()
turtle.onscreenclick(tap)
done()

有点上手,会上瘾,老少皆宜



作者:Python_Camp
链接:https://www.jianshu.com/p/9deb1bdd48e7
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值