IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> 我用pygame写了一个电梯程序(2)+test -> 正文阅读

[Python知识库]我用pygame写了一个电梯程序(2)+test

目录

前言

代码

?????????1.创造小人

2.小人出现

3.按电梯

3.进入电梯

4.离开电梯

5.计算时间




前言

我这次在(2)的基础上加入了测试程序,让小人按随机时间在随机楼层出现,当然小人去的楼层也是随机的。由此计算出任务完成的时间,调整电梯的安排使其运行的效率最高。


代码

为了条理清晰,我用函数完成。

1.创造小人

首先将小人创造出来,因为要用到随机数,所以先导入random这个库。

一定要用小人的状态(0为未出现,1为等电梯,2为坐电梯中,3为出电梯),这会使代码逻辑清晰,不容易乱。
import random
people = []     # 小人任务列表, 每个元素第0个为上电梯的楼层,第1个为要去的楼层,第2个为小人按的上下建,第3个为出现的时间,第4个为小人的状态(0为未出现,1为等电梯,2为坐电梯中,3为出电梯)
people_count = 5  # 小人数量
total_floor = 16  # 总楼层


def init_people():
    for i in range(0, people_count):
        people_in = random.randint(1, total_floor)
        people_out = random.randint(1, total_floor)
        if people_in > people_out:
            up_down = 2
        else:
            up_down = 1
        t = random.randint(1, 50)
        people.append([people_in, people_out, up_down, t, 1])

2.小人出现

一旦时间到了,将小人的状态更改。

# 小人出现
def people_start():
    for i in range(0, people_count):
        if people[i][3] == t:
            people[i][4] = 1

3.按电梯

小人出现时间一到,便开始按电梯。

# 输入小人进入电梯的楼层
def people_wait():
    for i in range(0, people_count):
        if people[i][3] == t:
            print(i, "号小人从", people[i][0], "楼按了电梯")
            if direction[people[i][0]] == 1:
                if people[i][2] == 2:
                    direction[people[i][0]] = 3
            if direction[people[i][0]] == 2:
                if people[i][2] == 1:
                    direction[people[i][0]] = 3
            if direction[people[i][0]] == 0:
                if people[i][2] == 1:
                    direction[people[i][0]] = 1
                if people[i][2] == 2:
                    direction[people[i][0]] = 2
            if running[people[i][0]] == 0:
                running[people[i][0]] = 1
            if running[people[i][0]] == 2:
                running[people[i][0]] = 3
            if running1[people[i][0]] == 0:
                running1[people[i][0]] = 1
            if running1[people[i][0]] == 2:
                running1[people[i][0]] = 3

3.进入电梯

电梯到达后,小人的状态变为坐电梯中。还需判断小人坐上了哪部电梯,这还是可以用楼层位置与电梯位置相等判断。

# 小人在电梯里按楼层
def people_sit():
    for j in range(0, people_count):
        if people[j][4] == 1:  # 状态
            if people[j][0] == i:  # 楼层
                people[j][4] = 2
                print(j, "号小人进入了左边电梯并按了", people[j][1], "楼")
                if running[people[j][1]] == 1:
                    running[people[j][1]] = 3
                if running[people[j][1]] == 0:
                    running[people[j][1]] = 2


def people_sit1():
    for j in range(0, people_count):
        if people[j][4] == 1 and people[j][0] == i:
            people[j][4] = 2
            print(j, "号小人进入了右边电梯并按了", people[j][1], "楼")
            if running1[people[j][1]] == 1:
                running1[people[j][1]] = 3
            if running1[people[j][1]] == 0:
                running1[people[j][1]] = 2

4.离开电梯

最后便是小人离开电梯了。

#  小人离开电梯
def people_out():
    for j in range(0, people_count):
        if people[j][4] == 2 and people[j][1] == i:
            people[j][4] = 3
            print(j, "号小人离开电梯")

5.计算时间

为了计算时间,我引用了time这个库。

import time
z = True


#  小人全部完成任务
def people_finish():
    global z
    i = 0
    for j in range(0, people_count):
        if people[j][4] != 3:
            i = 1
            break
    if i == 0 and z:
        time_end = time.time()
        z = False
        print("测试任务完成!共耗时", time_end - time_start, "秒")

最后附上全部代码。

# 调用pygame库
import pygame
from pygame.locals import *
from sys import exit
import random
import time
# 颜色值(255, 0, 0)表示红色,(0, 255, 0)表示绿色,而(0, 0, 255)表示蓝色
# 初始化pygame
pygame.init()
# 图片
up = pygame.image.load("up.png")
down = pygame.image.load("down.png")

people = []     # 小人任务列表, 每个元素第0个为上电梯的楼层,第1个为要去的楼层,第2个为小人按的上下建,第3个为出现的时间,第4个为小人的状态(0为未出现,1为等电梯,2为坐电梯中,3为出电梯)
people_count = 5  # 小人数量
total_floor = 16  # 总楼层


def init_people():
    for i in range(0, people_count):
        people_in = random.randint(1, total_floor)
        people_out = random.randint(1, total_floor)
        if people_in > people_out:
            up_down = 2
        else:
            up_down = 1
        t = random.randint(1, 50)
        people.append([people_in, people_out, up_down, t, 1])


init_people()

# 输入存储
input_finish = False
# 判断输入为上客楼层or下客楼层
arrive = 0

elevator_right = False


# 小人出现
def people_start():
    for i in range(0, people_count):
        if people[i][3] == t:
            people[i][4] = 1

# 输入小人进入电梯的楼层
def people_wait():
    for i in range(0, people_count):
        if people[i][3] == t:
            print(i, "号小人从", people[i][0], "楼按了电梯")
            if direction[people[i][0]] == 1:
                if people[i][2] == 2:
                    direction[people[i][0]] = 3
            if direction[people[i][0]] == 2:
                if people[i][2] == 1:
                    direction[people[i][0]] = 3
            if direction[people[i][0]] == 0:
                if people[i][2] == 1:
                    direction[people[i][0]] = 1
                if people[i][2] == 2:
                    direction[people[i][0]] = 2
            if running[people[i][0]] == 0:
                running[people[i][0]] = 1
            if running[people[i][0]] == 2:
                running[people[i][0]] = 3
            if running1[people[i][0]] == 0:
                running1[people[i][0]] = 1
            if running1[people[i][0]] == 2:
                running1[people[i][0]] = 3


# 小人在电梯里按楼层
def people_sit():
    for j in range(0, people_count):
        if people[j][4] == 1:  # 状态
            if people[j][0] == i:  # 楼层
                people[j][4] = 2
                print(j, "号小人进入了左边电梯并按了", people[j][1], "楼")
                if running[people[j][1]] == 1:
                    running[people[j][1]] = 3
                if running[people[j][1]] == 0:
                    running[people[j][1]] = 2


def people_sit1():
    for j in range(0, people_count):
        if people[j][4] == 1 and people[j][0] == i:
            people[j][4] = 2
            print(j, "号小人进入了右边电梯并按了", people[j][1], "楼")
            if running1[people[j][1]] == 1:
                running1[people[j][1]] = 3
            if running1[people[j][1]] == 0:
                running1[people[j][1]] = 2


#  小人离开电梯
def people_out():
    for j in range(0, people_count):
        if people[j][4] == 2 and people[j][1] == i:
            people[j][4] = 3
            print(j, "号小人离开电梯")


z = True


#  小人全部完成任务
def people_finish():
    global z
    i = 0
    for j in range(0, people_count):
        if people[j][4] != 3:
            i = 1
            break
    if i == 0 and z:
        time_end = time.time()
        z = False
        print("测试任务完成!共耗时", time_end - time_start, "秒")


# 键盘事件
def keyboard_get():
    global way
    global input_finish
    global arrive
    global elevator_right

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
        if event.type == pygame.KEYDOWN:
            if event.key == 13:
                time_end=time.time()
                print('totally cost', time_end - time_start)
                input_finish = True
                elevator_right = True
            if 48 <= event.key <= 57:
                key_floor.append(event.key - 48)
            if event.key == 1073741912:
                input_finish = True
            if event.key == K_DOWN:
                way = 2  # 方向为下
                arrive = 1
            if event.key == K_UP:
                way = 1
                arrive = 1
        else:
            pass
    return

# 设置窗口名称
pygame.display.set_caption("电梯")
# 获取对显示系统的访问,并创建一个窗口,分别率位(600x500)screen:屏幕
screen = pygame.display.set_mode((640, 510))

y = 482  # 电梯初始位置
y1 = 2
speed = 2  # 电梯速度

time2 = 100
time1 = 100
move_up = False
move_up1 = False
# 创造楼层位置
floors = [0]
floor_running = True
i = 482
while floor_running:
    floors.append(i)
    i = i - 32
    if i == -30:
        floor_running = False

# 定义画面帧率
FPS = 40
clock = pygame.time.Clock()

# 楼层变色 1为上客状态,二为下客状态,3为上下
running = [0]
for i in range(1, 17):
    running.append(0)

running1 = [0]
for i in range(1, 17):
    running1.append(0)

inertia = 0  # 惯性:检测电梯正在移动方向 0为上升,1为下降
inertia1 = 0
# 输入的楼层
key_floor = []

# 上下按键 1为上,二为下,三为上下皆按
way = 0
direction = [0]
for i in range(1, 17):
    direction.append(0)

t = 0
time_start=time.time()
# 主程序
while True:
    people_finish()
    t = t + 1
    people_start()
    people_wait()

    clock.tick(FPS)  # 最大帧率控制

# --------------------------------------输出部分------------------------------------------------
    # 背景颜色
    screen.fill([255, 255, 255])
    # 创造电梯穿梭间
    pygame.draw.rect(screen, [0, 0, 0], [387, 0, 3, 660], 0)
    pygame.draw.rect(screen, [0, 0, 0], [440, 0, 3, 660], 0)
    pygame.draw.rect(screen, [0, 0, 0], [307, 0, 3, 660], 0)
    pygame.draw.rect(screen, [0, 0, 0], [360, 0, 3, 660], 0)
    # 创造电梯
    screen.blit(pygame.image.load("people.png"), (400, y))
    screen.blit(pygame.image.load("people.png"), (320, y1))
    # 绘制楼层
    floor_font = pygame.font.Font(None, 50)
    for floor in range(1, 17):
        floor_text = floor_font.render(str(floor), True, (0, 0, 0))
        screen.blit(floor_text, (450, floors[floor]))
        screen.blit(floor_text, (250, floors[floor]))

    # 楼层变色
    for floor in range(1, 17):
        if running[floor] == 1:
            floor_text = floor_font.render(str(floor), True, (255, 0, 0))
            screen.blit(floor_text, (450, floors[floor]))
        if running[floor] == 2:
            floor_text = floor_font.render(str(floor), True, (0, 255, 0))
            screen.blit(floor_text, (450, floors[floor]))
        if running[floor] == 3:
            floor_text = floor_font.render(str(floor), True, (0, 0, 255))
            screen.blit(floor_text, (450, floors[floor]))

        if direction[floor] == 1:
            screen.blit(up, (500, floors[floor]))
            screen.blit(up, (210, floors[floor]))
        if direction[floor] == 2:
            screen.blit(down, (540, floors[floor]))
            screen.blit(down, (170, floors[floor]))
        if direction[floor] == 3:
            screen.blit(up, (500, floors[floor]))
            screen.blit(down, (540, floors[floor]))
            screen.blit(up, (210, floors[floor]))
            screen.blit(down, (170, floors[floor]))

        if running1[floor] == 1:
            floor_text = floor_font.render(str(floor), True, (255, 0, 0))
            screen.blit(floor_text, (250, floors[floor]))
        if running1[floor] == 2:
            floor_text = floor_font.render(str(floor), True, (0, 255, 0))
            screen.blit(floor_text, (250, floors[floor]))
        if running1[floor] == 3:
            floor_text = floor_font.render(str(floor), True, (0, 0, 255))
            screen.blit(floor_text, (250, floors[floor]))


# ---------------------------------------逻辑部分--------------------------------------------
    # 电梯回归初始位置一楼
    initial = 0
    for i in range(1, 17):
        if running[i] == 1:
            initial = 1
        elif running[i] == 2:
            initial = 1
        elif running[i] == 3:
            initial = 1
        else:
            pass
    if y < 482 and initial == 0:
        y = y + speed
        move_up = False
    initial1 = 0
    for i in range(1, 17):
        if running1[i] == 1:
            initial1 = 1
        elif running1[i] == 2:
            initial1 = 1
        elif running1[i] == 3:
            initial1 = 1
        else:
            pass
    if y1 > 2 and initial1 == 0:
        y1 = y1 - speed
        move_up1 = True

    # 电梯移动
    priority = 0
    if inertia == 1:
        for i in range(1, 17):
            if running[i] == 1 and y <= floors[i]:
                inertia = 1
                break
            elif running[i] == 2 and y <= floors[i]:
                inertia = 1
                break
            elif running[i] == 3 and y <= floors[i]:
                inertia = 1
                break
            else:
                inertia = 0
    if inertia == 0:
        for i in range(1, 17):
            if running[i] == 1 and y >= floors[i]:
                inertia = 0
                break
            elif running[i] == 2 and y >= floors[i]:
                inertia = 0
                break
            elif running[i] == 3 and y >= floors[i]:
                inertia = 0
                break
            else:
                inertia = 1

    for i in range(1, 17):
        if running[i] == 1 and y >= floors[i] and inertia == 0:
            y = y - speed
            move_up = True
            priority = 1
            break
        if running[i] == 3 and y >= floors[i] and inertia == 0:
            y = y - speed
            move_up = True
            priority = 1
            break
        if running[i] == 1 and y <= floors[i] and inertia == 1:
            y = y + speed
            priority = 1
            break
        if running[i] == 3 and y <= floors[i] and inertia == 1:
            y = y + speed
            priority = 1
            break
    for i in range(1, 17):
        if priority == 0:
            if running[i] == 2 and y >= floors[i] and inertia == 0:
                y = y - speed
                move_up = True
                break
            if running[i] == 2 and y <= floors[i] and inertia == 1:
                y = y + speed
                break

    priority1 = 0
    if inertia1 == 1:
        for i in range(1, 17):
            if running1[i] == 1 and y1 <= floors[i]:
                inertia1 = 1
                break
            elif running1[i] == 2 and y1 <= floors[i]:
                inertia1 = 1
                break
            elif running1[i] == 3 and y1 <= floors[i]:
                inertia1 = 1
                break
            else:
                inertia1 = 0
    if inertia1 == 0:
        for i in range(1, 17):
            if running1[i] == 1 and y1 >= floors[i]:
                inertia1 = 0
                break
            elif running1[i] == 2 and y1 >= floors[i]:
                inertia1 = 0
                break
            elif running1[i] == 3 and y1 >= floors[i]:
                inertia1 = 0
                break
            else:
                inertia1 = 1

    for i in range(1, 17):
        if running1[i] == 1 and y1 >= floors[i] and inertia1 == 0:
            y1 = y1 - speed
            move_up1 = True
            priority1 = 1
            break
        if running1[i] == 3 and y1 >= floors[i] and inertia1 == 0:
            y1 = y1 - speed
            move_up1 = True
            priority1 = 1
            break
        if running1[i] == 1 and y1 <= floors[i] and inertia1 == 1:
            y1 = y1 + speed

            priority1 = 1
            break
        if running1[i] == 3 and y1 <= floors[i] and inertia1 == 1:
            y1 = y1 + speed

            priority1 = 1
            break
    for i in range(1, 17):
        if priority1 == 0:
            if running1[i] == 2 and y1 >= floors[i] and inertia1 == 0:
                y1 = y1 - speed
                move_up1 = True
                break
            if running1[i] == 2 and y1 <= floors[i] and inertia1 == 1:
                y1 = y1 + speed

                break
    # 电梯到达下客
    for i in range(1, 17):
        if y == floors[i] and running[i] == 2:
            running[i] = 0
            time2 = 0
            people_out()
        if y == floors[i] and running[i] == 3:
            running[i] = 1
            people_out()
        if y1 == floors[i] and running1[i] == 2:
            running1[i] = 0
            time1 = 0
            people_out()
        if y1 == floors[i] and running1[i] == 3:
            running1[i] = 1
            people_out()
# 电梯到达上客
        # 上升到达
        if y == floors[i] and running[i] != 0 and running[i] != 2 and direction[i] == 1 and inertia == 0:
            running[i] = 0
            running1[i] = 0
            direction[i] = 0
            time2 = 0
            people_sit()

        if y == floors[i] and running[i] != 0 and running[i] != 2 and direction[i] == 3 and inertia == 0:
            direction[i] = 2
            time2 = 0
            people_sit()

        # 下降到达
        if y == floors[i] and running[i] != 0 and running[i] != 2 and direction[i] == 2 and inertia == 1:
            running[i] = 0
            time2 = 0
            running1[i] = 0
            direction[i] = 0
            people_sit()

        if y == floors[i] and running[i] != 0 and running[i] != 2 and direction[i] == 3 and inertia == 1:
            direction[i] = 1
            time2 = 0
            people_sit()
        if y1 == floors[i] and running1[i] != 0 and running1[i] != 2 and direction[i] == 1 and inertia1 == 0:
            running1[i] = 0
            running[i] = 0
            direction[i] = 0
            time1 = 0
            people_sit1()
        if y1 == floors[i] and running1[i] != 0 and running1[i] != 2 and direction[i] == 3 and inertia1 == 0:
            direction[i] = 2
            time1 = 0
            people_sit1()
        # 下降到达
        if y1 == floors[i] and running1[i] != 0 and running1[i] != 2 and direction[i] == 2 and inertia1 == 1:
            running1[i] = 0
            running[i] = 0
            direction[i] = 0
            time1 = 0
            people_sit1()
        if y1 == floors[i] and running1[i] != 0 and running1[i] != 2 and direction[i] == 3 and inertia1 == 1:
            direction[i] = 1
            time1 = 0
            people_sit1()
# 电梯停止
    if time2 <= 50:  # 电梯停止时间
        if move_up:
            y = y + speed
        else:
            y = y - speed
        time2 = time2 + 1
    else:
        time2 = 100

    if time1 <= 50:  # 电梯停止时间
        if move_up1:
            y1 = y1 + speed
        else:
            y1 = y1 - speed
        time1 = time1 + 1
    else:
        time1 = 100
    move_up = False
    move_up1 = False
    # --------------------------------------输入部分---------------------------------------------

    # 输入上乘客的楼层
    keyboard_get()

    # 显示输入楼层
    if len(key_floor) == 1:
        floor_font = pygame.font.Font(None, 50)
        floor_text = floor_font.render(str(key_floor[0]), True, (0, 0, 255))
        screen.blit(floor_text, (150, 100))
    if len(key_floor) == 2:
        floor_font = pygame.font.Font(None, 50)
        floor_text = floor_font.render(str(key_floor[0]), True, (0, 0, 255))
        screen.blit(floor_text, (150, 100))
        floor_font = pygame.font.Font(None, 50)
        floor_text = floor_font.render(str(key_floor[1]), True, (0, 0, 255))
        screen.blit(floor_text, (170, 100))
    pygame.display.update()  # 刷新显示
    # 输入上客楼层
    if arrive == 1:
        if input_finish:
            arrive = 0
            input_finish = False
            if len(key_floor) == 1:
                for i in range(1, 11):
                    if key_floor[0] == i:
                        if direction[i] == 1:
                            if way == 2:
                                direction[i] = 3
                        if direction[i] == 2:
                            if way == 1:
                                direction[i] = 3
                        if direction[i] == 0:
                            if way == 1:
                                direction[i] = 1
                            if way == 2:
                                direction[i] = 2
                        if running[i] == 0:
                            running[i] = 1
                        if running[i] == 2:
                            running[i] = 3
                        if running1[i] == 0:
                            running1[i] = 1
                        if running1[i] == 2:
                            running1[i] = 3
            if len(key_floor) == 2:
                for i in range(0, 7):
                    if key_floor[1] == i:
                        if direction[i + 10] == 1:
                            if way == 2:
                                direction[i + 10] = 3
                        if direction[i + 10] == 2:
                            if way == 1:
                                direction[i + 10] = 3
                        if direction[i + 10] == 0:
                            if way == 1:
                                direction[i + 10] = 1
                            if way == 2:
                                direction[i + 10] = 2
                        if running[i + 10] == 0:
                            running[i + 10] = 1
                        if running[i + 10] == 2:
                            running[i + 10] = 3
                        if running1[i + 10] == 0:
                            running1[i + 10] = 1
                        if running1[i + 10] == 2:
                            running1[i + 10] = 3
            while True:
                if len(key_floor) >= 1:
                    del key_floor[0]
                else:
                    break
    # 输入下客楼层
    if arrive == 0:
        if input_finish:
            input_finish = False
            if elevator_right:
                elevator_right = False
                if len(key_floor) == 1:
                    for ii in range(1, 11):
                        if key_floor[0] == ii:
                            if running1[ii] == 1:
                                running1[ii] = 3
                            if running1[ii] == 0:
                                running1[ii] = 2

                if len(key_floor) == 2:
                    for ii in range(0, 7):
                        if key_floor[1] == ii:
                            if running1[ii + 10] == 1:
                                running1[ii + 10] = 3
                            if running1[ii + 10] == 0:
                                running1[ii + 10] = 2
            else:
                if len(key_floor) == 1:
                    for ii in range(1, 11):
                        if key_floor[0] == ii:
                            if running[ii] == 1:
                                running[ii] = 3
                            if running[ii] == 0:
                                running[ii] = 2

                if len(key_floor) == 2:
                    for ii in range(0, 7):
                        if key_floor[1] == ii:
                            if running[ii + 10] == 1:
                                running[ii + 10] = 3
                            if running[ii + 10] == 0:
                                running[ii + 10] = 2
            while True:
                if len(key_floor) >= 1:
                    del key_floor[0]
                else:
                    break

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-09-02 11:19:16  更:2021-09-02 11:21:18 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/15 12:17:45-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码