猫史档案馆


【Python作品分享】New Technology-4【作品秀】

用户:SFransSFrans查看:0 回复:3 评论:0 创建时间:2023-08-29T17:33:24


【作品展示】

center_image

 

【作品介绍】

类似于一个小系统,还有一张贪吃蛇头的图片需要找一张才能运行(或者说包括主界面的照片)

 

【作品源代码】

import sys
from pygame.locals import *
import random
import tkinter as t
import os
import pygame as pg
import time
import tkinter.messagebox
from PIL import Image,ImageDraw,ImageFont
import easygui as g
import requests as r
import json as j
import turtle as e
from moviepy.editor import VideoFileClip

score = 0

def hit():
    time.sleep(0.25)
    t.messagebox.showwarning(title='warning', message='warning')
    t.messagebox.askquestion(
        title='question', message='Your Windows is being invaded.Do you want to start self-help mode')
    time.sleep(0.5)
    t.messagebox.showinfo(title='info', message='Please enter instructions')
    time.sleep(0.2)
    t.messagebox.showinfo(
        title='info', message='Starting success,the great Zhang ao wei')
    time.sleep(1.75)
    t.messagebox.showerror(
        title='error', message='Wrong,Windows is saving itself again')
    time.sleep(1.25)
    t.messagebox.showinfo(
        title='anwser', message='Your Windows has saved itself')
    time.sleep(0.35)
    t.messagebox.askquestion(
        title='question', message='Do you want to record the IP address of the intruder')
    time.sleep(0.75)
    t.messagebox.showinfo(
        title='imformation', message='It was successfully recorded that the virus name was Zhang Niao virus')
    time.sleep(1)
    t.messagebox.showinfo(
        title='imformation', message='Windows found these imformations about Zhang Bird')
    time.sleep(0.25)
    pg.quit()


def Snake():
    pg.init()
    wid_kuan = 560
    wid_height = 600
    window = pg.display.set_mode((wid_kuan, wid_height))
    cellsize = 20
    cellx = int(wid_kuan / cellsize)
    celly = int(wid_height / cellsize)
    biaoti = pg.display.set_caption('贪吃蛇')

    def drawGrid():
        for x in range(0, wid_kuan, cellsize):
            pg.draw.line(window, (40, 40, 40), (x, 0), (x, wid_height))
        for y in range(0, wid_height, cellsize):
            pg.draw.line(window, (40, 40, 40), (0, y), (wid_kuan, y))

    def drawApple(apple):
        x = apple['x'] * cellsize
        y = apple['y'] * cellsize
        appleweizhi = pg.Rect(x, y, cellsize, cellsize)
        pg.draw.rect(window, (155, 0, 0), appleweizhi)
        appleweizhi2 = pg.Rect(x+4, y+4, cellsize-8, cellsize-8)
        pg.draw.rect(window, (255, 0, 0), appleweizhi2)
        for i in range(0, 3, 1):
            appleweizhi3 = pg.Rect(x+8, y-3, cellsize-16, cellsize-13)
            pg.draw.rect(window, (0, 255, 0), appleweizhi3)

    def drawSnake(snakecoords, fangxiang):
        picture = pg.image.load('b.jpg')
        if fangxiang == 'up':  # 根据面向方向调整蛇头方向
            picture = pg.transform.rotate(picture, 90)
        elif fangxiang == 'down':
            picture = pg.transform.rotate(picture, -90)
        elif fangxiang == 'left':
            picture = pg.transform.rotate(picture, 180)
        window.blit(
            picture, (snakecoords[0]['x'] * cellsize, snakecoords[0]['y'] * cellsize))
        for i in snakecoords[1:]:
            x = i['x'] * cellsize
            y = i['y'] * cellsize
            snakeweizhi = pg.Rect(x, y, cellsize, cellsize)
            pg.draw.rect(window, (0, 155, 0), snakeweizhi)
            snakeweizhi2 = pg.Rect(x+4, y+4, cellsize-8, cellsize-8)
            pg.draw.rect(window, (0, 255, 0), snakeweizhi2)

    def checkForpress():
        if len(pg.event.get(QUIT)) > 0:
            tuichu()
        anjian = pg.event.get(KEYUP)
        if len(anjian) == 0:
            return None
        for event in pg.event.get():
            if event.type == K_ESCAPE:
                tuichu()
        return anjian[0].key

    def showstart():
        window.fill((0, 0, 0))
        fenshu(window, 'Snake!', (255, 0, 0), 65, 230, 300)
        fenshu(window, 'press a key to play', (0, 0, 255), 32, 295, 550)
        pg.display.update()
        while True:
            if checkForpress():
                pg.event.get()
                return

    def Gameover():
        fenshu(window, 'Game Over!', (255, 255, 255), 95, 120, 300)
        fenshu(window, 'press a key to play', (0, 0, 255), 32, 295, 550)
        pg.display.update()
        tuichu()

    def tuichu():
        print('您的分数是', score, '分')
        t.messagebox.showinfo(
        title='info', message='您的分数是'+str(score)+'分')
        pg.quit()

    def fenshu(a, b, color, l, x, y):
        font2 = pg.font.match_font('黑体')
        font = pg.font.Font(font2, l)
        c = font.render(b, True, color)
        a.blit(c, (x, y))

    def Rungame():
        global score
        score = 0
        t0 = time.perf_counter()  # 瞄定程序开始的时间点
        startx = random.randint(5, 20)
        starty = random.randint(5, 25)
        snakecoords = [{'x': startx, 'y': starty},
                       {'x': startx-1, 'y': starty},
                       {'x': startx-2, 'y': starty}]
        fangxiang = 'right'
        apple = {'x': random.randint(
            0, cellx-1), 'y': random.randint(0, celly-1)}
        while True:
            for event in pg.event.get():
                if event.type == QUIT:
                    tuichu()
                elif event.type == KEYDOWN:
                    if event.key == K_UP and fangxiang != 'down':
                        fangxiang = 'up'
                    elif event.key == K_DOWN and fangxiang != 'up':
                        fangxiang = 'down'
                    elif event.key == K_RIGHT and fangxiang != 'left':
                        fangxiang = 'right'
                    elif event.key == K_LEFT and fangxiang != 'right':
                        fangxiang = 'left'
                    elif event.key == K_ESCAPE:
                        tuichu()
            if snakecoords[0]['x'] == -1 or snakecoords[0]['x'] == cellx or snakecoords[0]['y'] == -1 or snakecoords[0]['y'] == celly:
                return
            for i in snakecoords[1:]:
                if i['x'] == snakecoords[0]['x'] and i['y'] == snakecoords[0]['y']:
                    print('You have eaten yourself')
                    return
            if 45-(time.perf_counter()-t0) <= 0:  # 计算程序运行时长,超过60秒退出游戏
                print('Run out of time!')
                return
            if snakecoords[0]['x'] == apple['x'] and snakecoords[0]['y'] == apple['y']:
                score += 1
                apple = {'x': random.randint(
                    0, cellx-1), 'y': random.randint(0, celly-1)}
            else:
                del snakecoords[-1]
            if fangxiang == 'up':
                newhead = {'x': snakecoords[0]
                           ['x'], 'y': snakecoords[0]['y']-1}
            elif fangxiang == 'down':
                newhead = {'x': snakecoords[0]
                           ['x'], 'y': snakecoords[0]['y']+1}
            elif fangxiang == 'right':
                newhead = {'x': snakecoords[0]
                           ['x']+1, 'y': snakecoords[0]['y']}
            elif fangxiang == 'left':
                newhead = {'x': snakecoords[0]
                           ['x']-1, 'y': snakecoords[0]['y']}
            snakecoords.insert(0, newhead)
            window.fill((0, 0, 0))
            drawGrid()
            drawSnake(snakecoords, fangxiang)  # 为蛇头传递面向方向
            drawApple(apple)
            t1 = 45-(int(time.perf_counter()-t0))  # 游戏时间
            fenshu(window, str(score), (255, 255, 255), 40, 525, 65)
            fenshu(window, str(t1), (255, 0, 0), 25, 50, 65)  # 显示时间
            pg.display.update()
            clock.tick(FPS)

    showstart()
    clock = pg.time.Clock()
    FPS = 11
    Rungame()
    Gameover()


def wenjian():

    file = open('C:/Users/Administrator/Desktop/a.txt', 'w')
    ml = ('请输入创建数量', ' ')
    minecraft3 = g.multenterbox(msg='Check', title='Times', fields=ml)
    h = int(minecraft3[0])
    for i in range(0, h+1, 1):
        with open('C:/Users/Administrator/Desktop/a.txt') as f:
            a = f.readline()
            b = f.readline()
            c = f.read()
        wendang = 'C:/Users/Administrator/Desktop/a'+str(i)+'.txt'
        with open(wendang, 'w') as f1:
            f1.write(' '+a)
            f1.write(' '+b)
            f1.write(c)
    f2 = open('C:/Users/Administrator/Desktop/a.txt', 'r')
    s = f2.read()
    f2.close()
    print(s)
    t.messagebox.showinfo(
        title='info', message='现在看看桌面吧')
    time.sleep(0.2)
    b = os.listdir()
    mo = ('请输入指令','')
    minecraft4 = g.multenterbox(msg='Check', title='Times', fields=mo)
    h1 = minecraft4[0]
    if h1 == 'remove':
        for i in range(0,h+1,1):
            path = ('C:/Users/Administrator/Desktop/a'+str(i)+'.txt')
            os.remove(path)
        time.sleep(0.1)
        t.messagebox.showinfo(
        title='info', message='运行成功')

def chess():
    def fenshu2(a, b, color, l, x, y):
        font2 = pg.font.match_font('黑体')
        font = pg.font.Font(font2, l)
        c = font.render(b, True, color)
        a.blit(c, (x, y))
    pg.init()
    game_window = pg.display.set_mode((750, 600))
    game_window.fill((0, 0, 0))
    pg.display.set_caption('Five Sons Chess')
    fenshu2(game_window, 'Five Sons Chess', (255, 255, 255), 75, 225, 300)
    pg.display.update()
    time.sleep(2)
    pg.quit()

    class chessboard:  # 设置棋盘类
        def __init__(s):  # 初始化
            s.grid_lenght = 26  # 棋盘格子的边长
            s.grid_count = 20  # 棋盘底的长度为20个格子

            s.start_x = 150  # 棋盘左上角初始点X坐标
            s.start_y = 50  # 棋盘左上角初始点Y坐标

            s.edge_lenght = s.grid_lenght/2  # 棋盘周围边缘的长度=13

            s.piece = "black"  # 黑色棋子先手
            s.winner = None  # 用于存储胜利者,最开始设置胜利者是空
            s.gameover = False  # 游戏最开始没有结束是False 游戏结束为True

            s.grid = []  # 空列表,用于存储各个格子的状态
            for i in range(s.grid_count):  # 将一个20*20的嵌套列表全部填充为. 表示为空,还没有棋子
                s.grid.append(list("." * s.grid_count))

        def handle_event(s, e):  # 事件处理函数 将鼠标的位置换算成格子,落子后判断棋子状态
            origin_x = s.start_x-s.edge_lenght  # 棋盘底左上角初始点X坐标
            origin_y = s.start_y-s.edge_lenght  # 棋盘底左上角初始点Y坐标

            mouse_pos = e.pos  # 鼠标的位置 mouse_pos[0]是X轴 mouse_pos[1]是Y轴
            #如果鼠标的位置在整个棋盘的总大小的位置内
            if (mouse_pos[0] >= origin_x and mouse_pos[0] <= origin_x+s.grid_lenght*s.grid_count) and (mouse_pos[1] >= origin_y and mouse_pos[1] <= origin_y+s.grid_lenght*s.grid_count):
                if not s.gameover:  # 游戏未结束
                    x = mouse_pos[0]-origin_x  # X轴方向距离
                    xgrid = int(x/s.grid_lenght)  # 换算出X轴第几格

                    y = mouse_pos[1]-origin_y  # Y轴方向距离
                    ygrid = int(y/s.grid_lenght)  # 换算出Y轴第几格

                    if s.set_piece(xgrid, ygrid):  # 判断棋子状态
                        s.check_win(xgrid, ygrid)  # 检查胜利条件

        def set_piece(s, xgrid, ygrid):  # 判断是否可落子以及切换黑白子
            if s.grid[ygrid][xgrid] == '.':
                s.grid[ygrid][xgrid] = s.piece
                if s.piece == "black":
                    s.piece = "white"
                else:
                    s.piece = "black"
                return True
            return False

        def check_win(s, xgrid, ygrid):  # 检查获胜条件
            n_count = s.get_count(ygrid, xgrid, -1, 0)  # 上方相同颜色棋子数量
            s_count = s.get_count(ygrid, xgrid, 1, 0)  # 下方相同颜色棋子数量
            w_count = s.get_count(ygrid, xgrid, 0, -1)  # 左方相同颜色棋子数量
            e_count = s.get_count(ygrid, xgrid, 0, 1)  # 右方相同颜色棋子数量
            wn_count = s.get_count(ygrid, xgrid, -1, -1)  # 左上方相同颜色棋子数量
            en_count = s.get_count(ygrid, xgrid, -1, 1)  # 右上方相同颜色棋子数量
            ws_count = s.get_count(ygrid, xgrid, 1, -1)  # 左下方相同颜色棋子数量
            es_count = s.get_count(ygrid, xgrid, 1, 1)  # 右下方相同颜色棋子数量
            #如果数量和大于等于5(加的1是棋子本身)
            if (n_count+s_count+1 >= 5) or (e_count+w_count+1 >= 5) or (wn_count+es_count+1 >= 5) or (en_count+ws_count+1 >= 5):
                #把grid列表的元素也就是此时的棋子颜色赋值给winner
                s.winner = s.grid[ygrid][xgrid]
                #游戏结束
                s.gameover = True

        def get_count(s, ygrid, xgrid, dy, dx):
            piece = s.grid[ygrid][xgrid]  # 记录目前的格子颜色
            result = 0  # 计算相同颜色棋子的个数
            i = 1
            while True:
                new_y = ygrid+dy*i
                new_x = xgrid+dx*i
                #如果新值在网格数量范围内
                if 0 <= new_y <= s.grid_count and 0 <= new_x < s.grid_count:
                    #如果新值在grid列表中的元素值和piece变量相等
                    if s.grid[new_y][new_x] == piece:
                        result += 1
                    else:
                        break
                else:
                    break
                i += 1  # 扩大判断棋子相同颜色的范围,如果前面break了就执行不到这步
            return result  # 返回相同颜色棋子数量

        def draw(s, screen):
            #绘制褐色棋盘底
            pg.draw.rect(screen, (185, 122, 87), [
                         s.start_x-s.edge_lenght, s.start_y-s.edge_lenght, s.grid_count*s.grid_lenght, s.grid_count*s.grid_lenght], 0)
            for i in range(s.grid_count):  # 棋盘的横线 黑色
                y = s.start_y + i*s.grid_lenght
                pg.draw.line(screen, (0, 0, 0), [s.start_x, y], [
                             s.start_x+s.grid_lenght*(s.grid_count-1), y], 2)
            for j in range(s.grid_count):  # 棋盘的竖线 黑色
                x = s.start_x + j*s.grid_lenght
                pg.draw.line(screen, (0, 0, 0), [x, s.start_y], [
                             x, s.start_y+s.grid_lenght*(s.grid_count-1)], 2)
            #棋子颜色
            for i in range(s.grid_count):
                for j in range(s.grid_count):
                    piece = s.grid[i][j]
                    if piece != ".":
                        if piece == "black":
                            color = (0, 0, 0)
                        else:
                            color = (255, 255, 255)
                        x = s.start_x+j*s.grid_lenght  # 棋子坐标
                        y = s.start_y+i*s.grid_lenght
                        pg.draw.circle(screen, color, [x, y], s.grid_lenght//2)

    class Gomoku:
        def __init__(s):
            pg.init()
            s.screen = pg.display.set_mode((800, 600))  # 游戏窗口大小
            pg.display.set_caption("五子棋对战")
            s.clock = pg.time.Clock()  # 时间
            s.font = pg.font.Font(None, 24)
            s.going = True
            s.chessboard = chessboard()

        def loop(s):  # 主循环
            while s.going:
                s.update()
                s.draw()
                s.clock.tick(50)
            pg.quit()

        def update(s):
            for e in pg.event.get():
                if e.type == pg.QUIT:
                    s.going = False
                elif e.type == pg.MOUSEBUTTONDOWN:
                    s.chessboard.handle_event(e)

        def draw(s):
            s.screen.fi喵))
            s.chessboard.draw(s.screen)
            if s.chessboard.gameover:
                s.screen.blit(s.font.render("{0}Win".format(
                    "black" if s.chessboard.winner == "black" else "white"), True, (0, 0, 0)), (500, 10))
            pg.display.update()

    if __name__ == "__main__":
        game = Gomoku()
        game.loop()


def video():
    time.sleep(0.75)

    def fenshu3(a, b, color, l, x, y):
        font2 = pg.font.match_font('黑体')
        font = pg.font.Font(font2, l)
        c = font.render(b, True, color)
        a.blit(c, (x, y))
    pg.init()
    VideoandDraw = pg.display.set_mode((750, 600))
    VideoandDraw.fill((0, 0, 0))
    pg.display.set_caption('Video and Draw')
    fenshu3(VideoandDraw, 'Video and Draw', (255, 255, 255), 85, 225, 300)
    pg.display.update()
    time.sleep(2)
    pg.quit()
    time.sleep(1)
    e.fillcolor('black')
    e.penup()
    e.goto((-375), (-350))
    e.pendown()
    e.begin_fill()
    e.left(90)
    e.forward(750)
    e.right(90)
    e.forward(750)
    e.right(90)
    e.forward(750)
    e.right(90)
    e.forward(750)
    e.end_fill()
    e.speed(0)
    d = random.randint(100, 200)
    s = 0
    e.pencolor('cyan')
    while (s < d):
        e.penup()
        e.goto(0, 0)
        n = random.randint(1, 340)
        e.forward(n)
        e.left(90)
        e.pendown()
        e.circle(n, random.randint(10, 280))
        s += 1
    e.hideturtle()
    time.sleep(0.2)
    e.exitonclick()

def weather():
    m2 = ('请输入您要查询的城市',' ')
    city = g.multenterbox(msg='City', title='Weather', fields=m2)
    r1 = r.ge喵api/?version=v6&appid=55666532&appsecret=91mMqGR3&city='+str(city[0]))
    t.messagebox.showinfo(
        title='info', message=j.loads(r1.text))

def ball():
    pg.init()
    game_window = pg.display.set_mode((1000, 800))
    pg.display.set_caption('接球游戏')
    move_x = 2
    move_y = 2
    point = 1
    count = 1
    ballscore = 0
    font = pg.font.Font(None, 82)
    ball_x, ball_y = (500, 40)
    window_color = (0, 0, 0)
    ball_color = (0, 0, 255)
    rect_color = (255, 0, 0)
    while True:
        game_window.fill(window_color)
        my_score = font.render(str(ballscore), False, (0, 0, 255))
        game_window.blit(my_score, (890, 50))
        for event in pg.event.get():
            if event.type == pg.QUIT:
                sys.exit()
        mouse_x, mouse_y = pg.mouse.get_pos()
        pg.draw.circle(game_window, ball_color, (ball_x, ball_y), 30)
        pg.draw.rect(game_window, rect_color, (mouse_x, 775, 100, 25))
        ball_x += move_x
        ball_y += move_y
        if ball_y >= 770 and (ball_x <= mouse_x - 30 or ball_x >= mouse_x+130):
            ball_y = 800
            t.messagebox.showinfo(
                title='info', message='您获得的分数是'+str(ballscore)+'分')
            pg.quit()
            break
        if ball_x <= 30 or ball_x >= 970:
            move_x = -move_x
        if ball_y <= 30:
            move_y = -move_y
        elif (ball_x > mouse_x and ball_x < mouse_x+100) and ball_y >= 745:
            ball_y += -15
            move_y = -move_y
            count += 1
            ballscore += point
            if count == 3:
                count = 0
                point += 1
                if move_x > 0:
                    move_x += 1
                    move_y -= 1
                else:
                    move_x -= 1
                    move_y -= 1
        pg.display.update()
        time.sleep(0.0022)


def draw():
    __Pen = e.Pen()

    def tree(branchLen, mn):
        if (branchLen > 3):
            if (8 <= branchLen and branchLen <= 12):
                if (random.randint(0, 2) == 0):
                    mn.pencolor('snow')
                else:
                    mn.pencolor('lightcoral')
                mn.pensize((branchLen / 3))
            elif (branchLen < 8):
                if (random.randint(0, 1) == 0):
                    mn.pencolor('snow')
                else:
                    mn.pencolor('lightcoral')
                mn.pensize((branchLen / 2))
            else:
                mn.pencolor('sienna')
                mn.pensize((branchLen / 10))
            mn.forward(branchLen)
            a = (1.5 * (random.random()))
            mn.right((20 * a))
            b = (1.5 * (random.random()))
            tree(branchLen - 10 * b, mn)
            mn.left((40 * a))
            tree(branchLen - 10 * b, mn)
            mn.right((20 * a))
            mn.penup()
            mn.backward(branchLen)
            mn.pendown()

    def petal(m, mn):
        for i in range(m):
            a = (200 - 400 * (random.random()))
            b = (10 - 20 * (random.random()))
            mn.penup()
            mn.forward(b)
            mn.left(90)
            mn.forward(a)
            mn.pendown()
            mn.pencolor('lightcoral')
            mn.circle(1)
            mn.penup()
            mn.backward(a)
            mn.right(90)
            mn.backward(b)

    def main():
        mn = e.Pen()
        __Pen.hideturtle()
        mn.speed(0)
        e.bgcolor('wheat')
        mn.left(90)
        mn.penup()
        mn.backward(150)
        mn.pendown()
        mn.pencolor('sienna')
        tree(60, mn)
        petal(100, mn)
        e.done()

    main()

def video1():
    clip = VideoFileClip('C:/Users/Administrator/Desktop/老八1.mp4')
    clip.preview()
    time.sleep(0.75)
    clip2 = VideoFileClip('C:/Users/Administrator/Desktop/老八2.mp4')
    clip2.preview()

def c():
    d = t.Tk()
    d.title('Windows')
    d.geometry('1200x500+300+200')
    d.resizable(1, 1)
    photo = t.PhotoImage(file='m.gif')
    u = t.Label(d, image=photo)
    u.pack()
    menu = t.Menu(d)
    menu1 = t.Menu(menu, tearoff=True)
    menu.add_cascade(label='Menu', menu=menu1)
    menu1.add_command(label='Create', command=wenjian)
    menu1.add_command(label='Chess', command=chess)
    menu1.add_command(label='Snake', command=Snake)
    menu1.add_command(label='Picture', command=video)
    menu1.add_command(label='Attention', command=c)
    menu1.add_command(label='Weather', command=weather)
    menu1.add_command(label='Ball', command=ball)
    menu1.add_command(label='Draw', command=draw)
    menu1.add_command(label='Video', command=video1)
    d.config(menu=menu)
    time.sleep(0.5)
    t.messagebox.showinfo(
        title='info', message='为防止机器人侵入,系统将为您分发账号密码,并由您手动输入登录来验证')
    time.sleep(0.2)
    t.messagebox.showwarning(title='warning', message='账号为123456,密码为123')
    time.sleep(0.25)
    t.messagebox.askquestion(title='question', message='账号密码你记清楚了吗?')
    mc = ('请输入管喵账号', '请输入管喵密码')
    mk = g.multenterbox(msg='Check', title='Login', fields=mc)
    if mk[0] == '123456' and mk[1] == '123':
        hit()
    else:
        t.messagebox.showerror(title='error', message='输入错误')
        time.sleep
        t.messagebox.showinfo(
            title='info', message='please try again')
    d.mainloop()

c()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
SFransSFrans

center_image

点赞1


评论


SFransSFrans

center_image

点赞1


评论


SFransSFrans

只需将程序保存到和两张图片的同一路径下即可运行,另外播放的视频需要请私聊

点赞1


评论