猫史档案馆


【Python作品分享】小程序【作品秀】

用户:你の名字你の名字查看:1 回复:1 评论:1 创建时间:2020-04-15T21:31:32


【作品展示】

center_image

 

【作品介绍】

挺好玩的

 

【作品源代码】

'''
注:请在海龟编辑器中运行
建议在海龟编辑器中下载库(datetime  sys  random  pygame)
步骤如下
1.打开海龟编辑器
2.点击库管理
3.搜索并下载 datetime库、sys库、random库、pygame库  
4.完成下载之后方可运行程序
'''
import datetime as d
import sys
import random 
import pygame
import turtle as t
import time
import easygui as g
from pygame.locals import *
#账号:小程序,密码:LZSSWZ
p = g.enterbox("请输入账号", "登录界面")
q = g.enterbox("请输入密码", "登录界面")
if p == "小程序" and q == "LZSSWZ":
    g.msgbox("登录成功", "登录界面", "点击进入")
    print("1.五子棋   2.钟表")
    a = input("请输入你想使用的功能")
    if a == "1":
        player = 1  # 试图对付换棋子颜色的问题

        qipan = [[0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0]]  #创立棋盘列表。

        qipan_tmp = [[0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0]]  #创立棋盘列表。

        #画棋盘
        def huaqipan(screen):
            screen.fi喵])  #把新窗口的颜色填充为白色

            for x in range(50,550,50):    #循环用的不多说
                pygame.draw.line(screen, [255,0,0], [50,x],[500,x], 2)  #划线的指令。screen用来告诉电脑在哪里画线。[255,0,0]定义颜色。
                pygame.draw.line(screen, [255,0,0], [x,50],[x,500], 2)  #接上:[100,x],[x,300]是线段起点和终点的坐标。2代表线的宽度。

            pygame.display.flip()   #刷新pygame

        #坐标变换之屏幕转棋盘
        def s2p(screenpos):
            if screenpos % 50 < 26:
                qipanpos = int(screenpos/50)-1
            else:
                qipanpos = int(screenpos/50)
            if qipanpos < 0:
                qipanpos = 0
            if qipanpos > 9:
                qipanpos = 9
            return (qipanpos)


        #画小棋子
        def huaxiaoqizi(qipan, x, y):
            mousex = x * 50 + 50
            mousey = y * 50 + 50
            if qipan[x][y] == 1:
                pygame.draw.circle(screen,[255,255,255],[mousex,mousey],7,7)    #这句话画圆
                pygame.draw.circle(screen,[0,0,0],[mousex,mousey],8,1)     #加个外框
            else:
                pygame.draw.circle(screen,[0,0,0],[mousex,mousey],7,7)    #这句话画圆
                pygame.draw.circle(screen,[0,0,0],[mousex,mousey],8,1)     #加个外框
            pygame.display.flip()#这句话自动刷新pygame


        #判断输赢
        def panduanshuyin(player):
            for x in range (0,10,1):
                for y in range (0,6,1):                      
                    if qipan[x][y] == player:
                        if qipan[x][y+1] == player:
                            if qipan[x][y+2] == player:
                                if qipan[x][y+3] == player:
                                    if qipan[x][y+4] == player:
                                        return True

            for x in range (0,6,1):
                for y in range (0,10,1):                      
                    if qipan[x][y] == player:
                        if qipan[x+1][y] == player:
                            if qipan[x+2][y] == player:
                                if qipan[x+3][y] == player:
                                    if qipan[x+4][y] == player:
                                        return True

            for x in range (0,6,1):
                for y in range (0,6,1):
                    if qipan[x][y] == player: 
                        if qipan[x+1][y+1] == player:
                            if qipan[x+2][y+2] == player:
                                if qipan[x+3][y+3] == player:
                                    if qipan[x+4][y+4] == player:
                                        return True

            for x in range (4,10,1):
                for y in range (0,6,1):                      
                    if qipan[x][y] == player:
                        if qipan[x-1][y+1] == player:
                            if qipan[x-2][y+2] == player:
                                if qipan[x-3][y+3] == player:
                                    if qipan[x-4][y+4] == player:
                                        return True
            return False

        #判断连棋
        def player2move():

            qipanxingshi = []
            for player in range(1, 3, 1):
                #方向1,左上到右下
                for x in range (0,10,1):
                    for y in range (0,10,1):
                        if qipan[x][y] == player:
                            qipan_tmp[x][y] = 1
                        else:
                            qipan_tmp[x][y] = 0

                for x in range (0,9,1):
                    for y in range (0,9,1):
                        if qipan_tmp[x][y] >= 1:
                            if qipan_tmp[x+1][y+1] == 1:
                                qipan_tmp[x+1][y+1] = qipan_tmp[x][y] + 1
                                qipan_tmp[x][y] = 0

                for x in range (0,10,1):
                    for y in range (0,10,1):
                        if qipan_tmp[x][y] > 0:
                            if x+1 < 10 and y+1 < 10:
                                if qipan[x+1][y+1] == 0:
                                    high_end = 'o'
                                else:
                                    high_end = 'x'
                            else:
                                high_end = 'x'
                            if x-qipan_tmp[x][y] > -1 and y-qipan_tmp[x][y] > -1:
                                if qipan[x-qipan_tmp[x][y]][y-qipan_tmp[x][y]] == 0:
                                    low_end = 'o'
                                else:
                                    low_end = 'x'
                            else:
                                low_end = 'x'
                                    
                            qipanxingshi.append([player, 1, qipan_tmp[x][y], x, y, low_end, high_end])

                #方向2,右上到左下
                for x in range (0,10,1):
                    for y in range (0,10,1):
                        if qipan[x][y] == player:
                            qipan_tmp[x][y] = 1
                        else:
                            qipan_tmp[x][y] = 0

                for x in range (9,0,-1):
                    for y in range (0,9,1):
                        if qipan_tmp[x][y] >= 1:
                            if qipan_tmp[x-1][y+1] == 1:
                                qipan_tmp[x-1][y+1] = qipan_tmp[x][y] + 1
                                qipan_tmp[x][y] = 0
                for x in range (0,10,1):
                    for y in range (0,10,1):
                        if qipan_tmp[x][y] > 0:
                            if x-1 > -1 and y+1 < 10:
                                if qipan[x-1][y+1] == 0:
                                    high_end = 'o'
                                else:
                                    high_end = 'x'
                            else:
                                high_end = 'x'
                            if x+qipan_tmp[x][y] < 10 and y-qipan_tmp[x][y] > -1:
                                if qipan[x+qipan_tmp[x][y]][y-qipan_tmp[x][y]] == 0:
                                    low_end = 'o'
                                else:
                                    low_end = 'x'
                            else:
                                low_end = 'x'
                                    
                            qipanxingshi.append([player, 2, qipan_tmp[x][y], x, y, low_end, high_end])

                #方向3,左到右
                for x in range (0,10,1):
                    for y in range (0,10,1):
                        if qipan[x][y] == player:
                            qipan_tmp[x][y] = 1
                        else:
                            qipan_tmp[x][y] = 0

                for x in range (0,9,1):
                    for y in range (0,10,1):
                        if qipan_tmp[x][y] >= 1:
                            if qipan_tmp[x+1][y] == 1:
                                qipan_tmp[x+1][y] = qipan_tmp[x][y] + 1
                                qipan_tmp[x][y] = 0
                for x in range (0,10,1):
                    for y in range (0,10,1):
                        if qipan_tmp[x][y] > 0:
                            if x+1 < 10:
                                if qipan[x+1][y] == 0:
                                    high_end = 'o'
                                else:
                                    high_end = 'x'
                            else:
                                high_end = 'x'
                            if x-qipan_tmp[x][y] > -1:
                                if qipan[x-qipan_tmp[x][y]][y] == 0:
                                    low_end = 'o'
                                else:
                                    low_end = 'x'
                            else:
                                low_end = 'x'
                                    
                            qipanxingshi.append([player, 3, qipan_tmp[x][y], x, y, low_end, high_end])

                #方向4,上到下
                for x in range (0,10,1):
                    for y in range (0,10,1):
                        if qipan[x][y] == player:
                            qipan_tmp[x][y] = 1
                        else:
                            qipan_tmp[x][y] = 0

                for x in range (0,10,1):
                    for y in range (0,9,1):
                        if qipan_tmp[x][y] >= 1:
                            if qipan_tmp[x][y+1] == 1:
                                qipan_tmp[x][y+1] = qipan_tmp[x][y] + 1
                                qipan_tmp[x][y] = 0
                for x in range (0,10,1):
                    for y in range (0,10,1):
                        if qipan_tmp[x][y] > 0:
                            if y+1 < 10:
                                if qipan[x][y+1] == 0:
                                    high_end = 'o'
                                else:
                                    high_end = 'x'
                            else:
                                high_end = 'x'
                            if y-qipan_tmp[x][y] > -1:
                                if qipan[x][y-qipan_tmp[x][y]] == 0:
                                    low_end = 'o'
                                else:
                                    low_end = 'x'
                            else:
                                low_end = 'x'
                                    
                            qipanxingshi.append([player, 4, qipan_tmp[x][y], x, y, low_end, high_end])

            #for item in qipanxingshi:
                #print(item)

            for item in qipanxingshi:
                if item[0] == 1 and item[2] == 4 and item[5] == 'x' and item[6] == 'o':
                    if item[1] == 1:
                        return [item[3]+1,item[4]+1]
                    elif item[1] == 2:
                        return [item[3]-1,item[4]+1]
                    elif item[1] == 3:
                        return [item[3]+1,item[4]]
                    elif item[1] == 4:
                        return [item[3],item[4]+1]
            for item in qipanxingshi:
                if item[0] == 1 and item[2] == 4 and item[5] == 'o' and item[6] == 'x':
                    if item[1] == 1:
                        return [item[3]-item[2],item[4]-item[2]]
                    elif item[1] == 2:
                        return [item[3]+item[2],item[4]-item[2]]
                    elif item[1] == 3:
                        return [item[3]-item[2],item[4]]
                    elif item[1] == 4:
                        return [item[3],item[4]-item[2]]
            for item in qipanxingshi:
                if item[0] == 1 and item[2] == 3 and item[5] == 'o' and item[6] == 'o':
                    if item[1] == 1:
                        return [item[3]+1,item[4]+1]
                    elif item[1] == 2:
                        return [item[3]-1,item[4]+1]
                    elif item[1] == 3:
                        return [item[3]+1,item[4]]
                    elif item[1] == 4:
                        return [item[3],item[4]+1]
            for item in qipanxingshi:
                if item[0] == 1 and item[2] == 3 and item[5] == 'x' and item[6] == 'o':
                    if item[1] == 1:
                        return [item[3]+1,item[4]+1]
                    elif item[1] == 2:
                        return [item[3]-1,item[4]+1]
                    elif item[1] == 3:
                        return [item[3]+1,item[4]]
                    elif item[1] == 4:
                        return [item[3],item[4]+1]
            for item in qipanxingshi:
                if item[0] == 1 and item[2] == 3 and item[5] == 'o' and item[6] == 'x':
                    if item[1] == 1:
                        return [item[3]-item[2],item[4]-item[2]]
                    elif item[1] == 2:
                        return [item[3]+item[2],item[4]-item[2]]
                    elif item[1] == 3:
                        return [item[3]-item[2],item[4]]
                    elif item[1] == 4:
                        return [item[3],item[4]-item[2]]

            #自己多连,棋盘上有4个
            for item in qipanxingshi:
                if item[0] == 2 and item[2] == 4 and item[5] == 'o' and item[6] == 'o':
                    if item[1] == 1:
                        return [item[3]-item[2],item[4]-item[2]]
                    elif item[1] == 2:
                        return [item[3]+item[2],item[4]-item[2]]
                    elif item[1] == 3:
                        return [item[3]-item[2],item[4]]
                    elif item[1] == 4:
                        return [item[3],item[4]-item[2]]
            for item in qipanxingshi:
                if item[0] == 2 and item[2] == 4 and item[5] == 'o' and item[6] == 'x':
                    if item[1] == 1:
                        return [item[3]-item[2],item[4]-item[2]]
                    elif item[1] == 2:
                        return [item[3]+item[2],item[4]-item[2]]
                    elif item[1] == 3:
                        return [item[3]-item[2],item[4]]
                    elif item[1] == 4:
                        return [item[3],item[4]-item[2]]
            for item in qipanxingshi:
                if item[0] == 2 and item[2] == 4 and item[5] == 'x' and item[6] == 'o':
                    if item[1] == 1:
                        return [item[3]+1,item[4]+1]
                    elif item[1] == 2:
                        return [item[3]-1,item[4]+1]
                    elif item[1] == 3:
                        return [item[3]+1,item[4]]
                    elif item[1] == 4:
                        return [item[3],item[4]+1]
            #自己多连,棋盘上有3个
            for item in qipanxingshi:
                if item[0] == 2 and item[2] == 3 and item[5] == 'o' and item[6] == 'o':
                    if item[1] == 1:
                        return [item[3]-item[2],item[4]-item[2]]
                    elif item[1] == 2:
                        return [item[3]+item[2],item[4]-item[2]]
                    elif item[1] == 3:
                        return [item[3]-item[2],item[4]]
                    elif item[1] == 4:
                        return [item[3],item[4]-item[2]]
            for item in qipanxingshi:
                if item[0] == 2 and item[2] == 3 and item[5] == 'o' and item[6] == 'x':
                    if item[1] == 1:
                        return [item[3]-item[2],item[4]-item[2]]
                    elif item[1] == 2:
                        return [item[3]+item[2],item[4]-item[2]]
                    elif item[1] == 3:
                        return [item[3]-item[2],item[4]]
                    elif item[1] == 4:
                        return [item[3],item[4]-item[2]]
            for item in qipanxingshi:
                if item[0] == 2 and item[2] == 3 and item[5] == 'x' and item[6] == 'o':
                    if item[1] == 1:
                        return [item[3]+1,item[4]+1]
                    elif item[1] == 2:
                        return [item[3]-1,item[4]+1]
                    elif item[1] == 3:
                        return [item[3]+1,item[4]]
                    elif item[1] == 4:
                        return [item[3],item[4]+1]
            #自己多连,棋盘上有2个
            for item in qipanxingshi:
                if item[0] == 2 and item[2] == 2 and item[5] == 'o' and item[6] == 'o':
                    if item[1] == 1:
                        return [item[3]-item[2],item[4]-item[2]]
                    elif item[1] == 2:
                        return [item[3]+item[2],item[4]-item[2]]
                    elif item[1] == 3:
                        return [item[3]-item[2],item[4]]
                    elif item[1] == 4:
                        return [item[3],item[4]-item[2]]
            for item in qipanxingshi:
                if item[0] == 2 and item[2] == 2 and item[5] == 'o' and item[6] == 'x':
                    if item[1] == 1:
                        return [item[3]-item[2],item[4]-item[2]]
                    elif item[1] == 2:
                        return [item[3]+item[2],item[4]-item[2]]
                    elif item[1] == 3:
                        return [item[3]-item[2],item[4]]
                    elif item[1] == 4:
                        return [item[3],item[4]-item[2]]
            for item in qipanxingshi:
                if item[0] == 2 and item[2] == 2 and item[5] == 'x' and item[6] == 'o':
                    if item[1] == 1:
                        return [item[3]+1,item[4]+1]
                    elif item[1] == 2:
                        return [item[3]-1,item[4]+1]
                    elif item[1] == 3:
                        return [item[3]+1,item[4]]
                    elif item[1] == 4:
                        return [item[3],item[4]+1]
            #自己多连,棋盘上有1个
            for item in qipanxingshi:
                if item[0] == 2 and item[2] == 1 and item[5] == 'o' and item[6] == 'o':
                    if item[1] == 1:
                        return [item[3]-item[2],item[4]-item[2]]
                    elif item[1] == 2:
                        return [item[3]+item[2],item[4]-item[2]]
                    elif item[1] == 3:
                        return [item[3]-item[2],item[4]]
                    elif item[1] == 4:
                        return [item[3],item[4]-item[2]]
            for item in qipanxingshi:
                if item[0] == 2 and item[2] == 1 and item[5] == 'o' and item[6] == 'x':
                    if item[1] == 1:
                        return [item[3]-item[2],item[4]-item[2]]
                    elif item[1] == 2:
                        return [item[3]+item[2],item[4]-item[2]]
                    elif item[1] == 3:
                        return [item[3]-item[2],item[4]]
                    elif item[1] == 4:
                        return [item[3],item[4]-item[2]]
            for item in qipanxingshi:
                if item[0] == 2 and item[2] == 1 and item[5] == 'x' and item[6] == 'o':
                    if item[1] == 1:
                        return [item[3]+1,item[4]+1]
                    elif item[1] == 2:
                        return [item[3]-1,item[4]+1]
                    elif item[1] == 3:
                        return [item[3]+1,item[4]]
                    elif item[1] == 4:
                        return [item[3],item[4]+1]

            #随机放一个棋子
            kongwei = []
            for x in range(0,10,1):
                for y in  range(0,10,1):
                    if qipan[x][y] == 0:
                        kongwei.append([x,y])
            suiji = random.randint(0,len(kongwei)-1)
            return kongwei[suiji]


        pygame.init()   #这句话对游戏模块进行初始化,在使用pygame模块的其他函数之前需要做这一步

        screen=pygame.display.set_mode([550,550])    #这句话作用是在屏幕上建立一个大小为550*550大小的新窗口

        huaqipan(screen)

        gameover = False
        while not gameover:
            for event in pygame.event.get():
                #print (event) # 这是调试程序的常用手段
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit() #这句话退出pygame

                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        pygame.quit()
                        sys.exit() #这句话退出pygame
                
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    qipanx=s2p(event.pos[0])
                    qipany=s2p(event.pos[1])
                    if qipan[qipanx][qipany] == 0:
                        if (player == 1) :
                            qipan[qipanx][qipany] = 1
                        else:
                            qipan[qipanx][qipany] = 2
                        huaxiaoqizi(qipan, qipanx, qipany)
                        player2qizi = player2move()
                        pygame.time.delay(100)
                        qipan[player2qizi[0]][player2qizi[1]] = 2
                        huaxiaoqizi(qipan, player2qizi[0], player2qizi[1])
                        if panduanshuyin(1):
                            xuanze = easygui.buttonbox("你赢了!还想玩吗?",
                                            choices = ['再玩一盘', '不玩了'])
                            if xuanze == '不玩了':
                                gameover = True
                            else:
                                for x in range(0,10,1):
                                    for y in range(0,10,1):
                                        qipan[x][y] = 0
                                huaqipan(screen)
                        if panduanshuyin(2):
                            xuanze = easygui.buttonbox("你输了!还想玩吗?",
                                            choices = ['再玩一盘', '不玩了'])
                            if xuanze == '不玩了':
                                gameover = True
                            else:
                                for x in range(0,10,1):
                                    for y in range(0,10,1):
                                        qipan[x][y] = 0
                                huaqipan(screen)

        pygame.quit()
    else:
        def skip(step):  # 抬笔,跳到一个地方
            t.penup()
            t.forward(step)
            t.pendown()


        def drawClock(radius):  # 画表盘
            t.speed(0)
            t.mode("logo")  # 以Logo坐标、角度方式
            t.hideturtle()
            t.pensize(7)
            t.home()  # 回到圆点
            for j in range(60):
                skip(radius)
                if (j % 5 == 0):
                    t.forward(20)
                    skip(-radius - 20)
                else:
                    t.dot(5)
                    skip(-radius)
                t.right(6)


        def makePoint(pointName, len):  # 钟的指针,时针、分针、秒针
            t.penup()
            t.home()
            t.begin_poly()
            t.back(0.1 * len)
            t.forward(len * 1.1)
            t.end_poly()
            poly = t.get_poly()
            t.register_shape(pointName, poly)  # 注册为一个shape


        def drawPoint():  # 画指针
            global hourPoint, minPoint, secPoint, fontWriter
            makePoint("hourPoint", 100)
            makePoint("minPoint", 120)
            makePoint("secPoint", 140)
            hourPoint = t.Pen()  # 每个指针是一只新turtle
            hourPoint.shape("hourPoint")
            hourPoint.shapesize(1, 1, 6)
            minPoint = t.Pen()
            minPoint.shape("minPoint")
            minPoint.shapesize(1, 1, 4)
            secPoint = t.Pen()
            secPoint.shape("secPoint")
            secPoint.pencolor('red')
            fontWriter = t.Pen()
            fontWriter.pencolor('gray')
            fontWriter.hideturtle()


        def getWeekName(weekday):
            weekName = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
            return weekName[weekday]


        def getDate(year, month, day):
            return "%s-%s-%s" % (year, month, day)


        def realTime():
            curr = d.datetime.now()
            curr_year = curr.year
            curr_month = curr.month
            curr_day = curr.day
            curr_hour = curr.hour
            curr_minute = curr.minute
            curr_second = curr.second
            curr_weekday = curr.weekday()
            t.tracer(False)
            secPoint.setheading(360 / 60 * curr_second)
            minPoint.setheading(360 / 60 * curr_minute)
            hourPoint.setheading(360 / 12 * curr_hour + 30 / 60 * curr_minute)
            fontWriter.clear()
            fontWriter.home()
            fontWriter.penup()
            fontWriter.forward(80)
            # 用turtle写文字
            fontWriter.write(getWeekName(curr_weekday), align="center",
                            font=("Courier", 14, "bold"))
            fontWriter.forward(-160)
            fontWriter.write(getDate(curr_year, curr_month, curr_day),
                            align="center", font=("Courier", 14, "bold"))
            t.tracer(True)

            t.ontimer(realTime, 100)  # 每隔100毫秒调用一次realTime()


        def main():
            t.tracer()
            drawClock(160)
            drawPoint()
            realTime()
            t.tracer(True)
            t.mainloop()


        if __name__ == '__main__':
            main()
else:
    g.msgbox("登录失败", "登录界面", "退出")

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
kkk_AC工作室toby小号kkk_AC工作室toby小号

除了pygame其他都不用下载知道吗…

点赞0


评论