前言
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
|