我用python中的pygame写了一个电梯程序V2.0:两部电梯

文章目录



前言

这次将电梯增加到了两个,我直接用最简单粗暴的办法,增加变量。


代码

在上一部电梯变量后面加1,当做第二部电梯的变量。再增加了输入到达楼层的电梯选择,刚好用两个Enter选择。

操作:按数字+大键盘Enter——表示下客楼层(左边电梯)

           按数字+小键盘Enter——表示下客楼层(右边电梯)

           按数字+方向(箭头上下)+Enter——表示上客楼层

因为有两部电梯,不能用改变帧率的方法了,所以增加了变量time,time1分别控制两部电梯,电梯到达是time改为1,,还有move_up,move_up1来检测电梯原本要上行还是下行。

# 电梯停止
    if time <= 50:  # 电梯停止时间
        if move_up:
            y = y + speed
        else:
            y = y - speed
        time = time + 1
    else:
        time = 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

最后附上所有代码

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

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

elevator_left = False
# 键盘事件
def keyboard_get():
    global way
    global input_finish
    global arrive
    global elevator_left
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
        if event.type == pygame.KEYDOWN:
            if event.key == 13:
                input_finish = True
                elevator_left = True
            if 48 <= event.key <= 57:
                key_floor.append(event.key - 48)
            # 2号电梯
            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  # 电梯速度

time = 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)

# 主程序
while True:

    pygame.display.update()  # 刷新显示
    clock.tick(FPS)  # 最大帧率控制

# --------------------------------------输出部分------------------------------------------------
    # 背景颜色
    screen.fill([100, 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, (255, 255, 255))
        screen.blit(floor_text, (450, floors[floor]))
    for floor in range(1, 17):
        floor_text = floor_font.render(str(floor), True, (255, 255, 255))
        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
            time = 0

        if y1 == floors[i] and running1[i] == 2:
            running1[i] = 0
            time1 = 0

# 电梯到达上客
        # 上升到达
        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
            time = 0
        if y == floors[i] and running[i] != 0 and running[i] != 2 and direction[i] == 3 and inertia == 0:
            direction[i] = 2
            time = 0

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

        if y == floors[i] and running[i] != 0 and running[i] != 2 and direction[i] == 3 and inertia == 1:
            direction[i] = 1
            time = 0
        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
        if y1 == floors[i] and running1[i] != 0 and running1[i] != 2 and direction[i] == 3 and inertia1 == 0:
            direction[i] = 2
            time1 = 0
        # 下降到达
        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
        if y1 == floors[i] and running1[i] != 0 and running1[i] != 2 and direction[i] == 3 and inertia1 == 1:
            direction[i] = 1
            time1 = 0
# 电梯停止
    if time <= 50:  # 电梯停止时间
        if move_up:
            y = y + speed
        else:
            y = y - speed
        time = time + 1
    else:
        time = 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_left:
                elevator_left = 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



总结

显而易见,继续用面向过程的方法已经比较繁琐了,而想达到可以任意设置电梯数的目标还将更加繁琐。因此,需要一个更好的方法。

猜你喜欢

转载自blog.csdn.net/Night_Journey/article/details/120037049