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写了一个电梯程序(1) -> 正文阅读

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

前言

Hello,我现在是准大一,是妥妥的一个小白,暑假用pygame做了个电梯运动的程序。嘛听说这对于新手来说是很好的练手材料。这也是我的第一篇文章,请大家多多指教。

?

一、写整个代码的思路

代码主要由三部分组成,输入——逻辑——输出。输入部分为检测你的键盘输入的数字,逻辑部分进行判断决定你的电梯是上行or下行或者禁止,而输出部分则是画出整个电梯窗口。一开始的逻辑不够清晰:是想着先画出电梯等等图像,接着是写该如何控制电梯的移动,最后再加入键盘输入。是根据想象中已经做出来的电梯程序来编写。

二、代码

键盘事件检测是尤为重要的一环。我用了一个函数来写,其实不用也可以。第一个if是让鼠标点窗口的小叉叉会关闭。第二个if是检测键盘被按下,将(举栗子)K_0选中右键选go to declaration or usanges 可以将每个键对应的数值看的清清楚楚。

操作:按数字+Enter——表示下客楼层

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

# 键盘事件
def keyboard_get():
    global way
    global input_finish
    global arrive
    for event in pygame.event.get():
        if event.type == pygame.QUIT:  # 窗口关闭
            exit()
        if event.type == pygame.KEYDOWN:
            if event.key == CONTROLLER_BUTTON_DPAD_LEFT:
                input_finish = True
            if K_0 <= event.key <= K_9:
                key_floor.append(event.key - 48)
            if event.key == K_DOWN:
                way = 2  # 表示按了下
                arrive = 1  # 为上客楼层
            if event.key == K_UP:
                way = 1
                arrive = 1
        else:
            pass
    return

我用running这个列表来存储楼层的任务,1为上客状态是红色,2为下客状态是绿色,3是上客下客都有为蓝色。

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

输出部分是窗口的颜色,电梯,楼层,方向键组成,这都直接用pygame库里的函数。

# --------------------------------------输出部分-----------------------------------------
    # 背景颜色
    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)
    # 创造电梯
    screen.blit(pygame.image.load("people.png"), (400, y))
    # 绘制楼层
    for floor in range(1, 17):
        floor_font = pygame.font.Font(None, 50)
        floor_text = floor_font.render(str(floor), True, (255, 255, 255))
        screen.blit(floor_text, (450, floors[floor]))
    # 楼层变色
    for floor in range(1, 17):
        if running[floor] == 1:
            floor_font = pygame.font.Font(None, 50)
            floor_text = floor_font.render(str(floor), True, (255, 0, 0))
            screen.blit(floor_text, (450, floors[floor]))
        if running[floor] == 2:
            floor_font = pygame.font.Font(None, 50)
            floor_text = floor_font.render(str(floor), True, (0, 255, 0))
            screen.blit(floor_text, (450, floors[floor]))
        if running[floor] == 3:
            floor_font = pygame.font.Font(None, 50)
            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, (300, floors[floor]))
        if direction[floor] == 2:
            screen.blit(down, (340, floors[floor]))
        if direction[floor] == 3:
            screen.blit(up, (300, floors[floor]))
            screen.blit(down, (340, floors[floor]))

输入部分主要分为两块,一是输入上客楼层,二是输入下客楼层。当你按上下键时,就选择了上客楼层,用arrive这个变量选择。而当输入完成时,用while循环将列表里的数字全部清空,这样可以防止输错了或者输多了。

   # --------------------------------------输入部分---------------------------------------------

    # 输入上乘客的楼层
    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 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
            while True:
                if len(key_floor) >= 1:
                    del key_floor[0]
                else:
                    break
    # 输入下客楼层
    if arrive == 0:
        if input_finish:
            input_finish = False
            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

最后便是逻辑部分,分为四大块。第一是电梯回到初始位置,检测到所有楼层无任务,就执行。第二是电梯状态的检测,也是最重要的思想,判断好了电梯的状态,我们才能更好决定它是上是下,如果上面有任务,它将一直上升。同时也很方便的解决了当电梯下行时有人按上电梯不会停的判断。第三便是电梯上行还是下行。第四是电梯到达,电梯的位置与楼层的位置相同就到达了,将调节帧率调小作为电梯停止。

# ---------------------------------------逻辑部分--------------------------------------------
    # 电梯回归初始位置一楼
    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

    # 电梯状态的检测(惯性)
    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
            priority = 1
            break
        if running[i] == 3 and y >= floors[i] and inertia == 0:
            y = y - speed
            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
                break
            if running[i] == 2 and y <= floors[i] and inertia == 1:
                y = y + speed
                break
# 电梯到达
    # 电梯到达下客
    for i in range(1, 17):
        if y == floors[i] and running[i] == 2:
            running[i] = 0
            clock.tick(1)  # 电梯到达后短暂停留
    # 电梯到达上客
        # 上升到达
        if y == floors[i] and running[i] != 0 and running[i] != 2 and direction[i] == 1 and inertia == 0:
            running[i] = 0
            direction[i] = 0
        if y == floors[i] and running[i] != 0 and running[i] != 2 and direction[i] == 3 and inertia == 0:
            direction[i] = 2
        # 下降到达
        if y == floors[i] and running[i] != 0 and running[i] != 2 and direction[i] == 2 and inertia == 1:
            running[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

最后附上全部的代码

# 调用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


# 键盘事件
def keyboard_get():
    global way
    global input_finish
    global arrive
    for event in pygame.event.get():
        if event.type == pygame.QUIT:  # 窗口关闭
            exit()
        if event.type == pygame.KEYDOWN:
            if event.key == CONTROLLER_BUTTON_DPAD_LEFT:
                input_finish = True
            if K_0 <= event.key <= K_9:
                key_floor.append(event.key - 48)
            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  # 电梯初始位置
speed = 1  # 电梯速度
# 创造楼层位置
floors = [0]
floor_running = True
i = 482
while floor_running:
    floors.append(i)
    i = i - 32
    if i == -30:
        floor_running = False

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

# 惯性(电梯的状态):检测电梯正在移动方向 0为上升,1为下降
inertia = 0
# 输入的楼层
key_floor = []
# 上下按键 1为上,二为下,三为上下皆按
way = 0
direction = [0]
for i in range(1, 17):
    direction.append(0)

# 定义画面帧率
FPS = 60
clock = pygame.time.Clock()
# 主程序
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)
    # 创造电梯
    screen.blit(pygame.image.load("people.png"), (400, y))
    # 绘制楼层
    for floor in range(1, 17):
        floor_font = pygame.font.Font(None, 50)
        floor_text = floor_font.render(str(floor), True, (255, 255, 255))
        screen.blit(floor_text, (450, floors[floor]))
    # 楼层变色
    for floor in range(1, 17):
        if running[floor] == 1:
            floor_font = pygame.font.Font(None, 50)
            floor_text = floor_font.render(str(floor), True, (255, 0, 0))
            screen.blit(floor_text, (450, floors[floor]))
        if running[floor] == 2:
            floor_font = pygame.font.Font(None, 50)
            floor_text = floor_font.render(str(floor), True, (0, 255, 0))
            screen.blit(floor_text, (450, floors[floor]))
        if running[floor] == 3:
            floor_font = pygame.font.Font(None, 50)
            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, (300, floors[floor]))
        if direction[floor] == 2:
            screen.blit(down, (340, floors[floor]))
        if direction[floor] == 3:
            screen.blit(up, (300, floors[floor]))
            screen.blit(down, (340, 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

    # 电梯状态的检测(惯性)
    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
            priority = 1
            break
        if running[i] == 3 and y >= floors[i] and inertia == 0:
            y = y - speed
            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
                break
            if running[i] == 2 and y <= floors[i] and inertia == 1:
                y = y + speed
                break
# 电梯到达
    # 电梯到达下客
    for i in range(1, 17):
        if y == floors[i] and running[i] == 2:
            running[i] = 0
            clock.tick(1)  # 电梯到达后短暂停留
    # 电梯到达上客
        # 上升到达
        if y == floors[i] and running[i] != 0 and running[i] != 2 and direction[i] == 1 and inertia == 0:
            running[i] = 0
            direction[i] = 0
        if y == floors[i] and running[i] != 0 and running[i] != 2 and direction[i] == 3 and inertia == 0:
            direction[i] = 2
        # 下降到达
        if y == floors[i] and running[i] != 0 and running[i] != 2 and direction[i] == 2 and inertia == 1:
            running[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

    # --------------------------------------输入部分---------------------------------------------

    # 输入上乘客的楼层
    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 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
            while True:
                if len(key_floor) >= 1:
                    del key_floor[0]
                else:
                    break
    # 输入下客楼层
    if arrive == 0:
        if input_finish:
            input_finish = False
            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-08-26 12:04:23  更:2021-08-26 12:05:00 
 
开发: 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年12日历 -2024/12/26 23:22:36-

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