目录
前言
代码
1.电梯类
?2.总控类
总结
前言
为了达到多部电梯的目标,这次用面向对象的方法,使整个代码更加简洁,逻辑更清晰。
代码
我一共用两个类,一个电梯的类,代表电梯的模子。一个是总控的类,用来输入。
所用的变量基本和(1)保持一致。
1.电梯类
首先看看熟悉的变量吧。唯一不同的是多了一个电梯默认位置,它是由楼层除以电梯数量得出的,使电梯分布均匀。
class elevator:
def __init__(self, x, elevator_initial_location, elevator_number):
self.x = x # 电梯x坐标
self.y = 482 # 电梯初始y坐标
self.speed = 2 # 电梯速度
self.floors = [0] # 楼层位置
self.running = [0] # 楼层状态
self.inertia = 0 # 电梯状态(惯性)
self.move_up = False # 是否上升
self.time = 100 # 暂停时间
self.direction = [0] # 按电梯的方向键
self.elevator_initial_location = elevator_initial_location # 电梯默认位置,使电梯均匀分布
self.elevator_number = elevator_number # 电梯编号
for j in range(1, 17): # 楼层变色 1为上客状态,二为下客状态,3为上下
self.running.append(0)
self.direction.append(0)
i = 482
floor_running = True
while floor_running:
self.floors.append(i)
i = i - 32
if i == -30:
floor_running = False
那么它既然是一个电梯的模子,首先把它要自己画自己。特别要说一下当时多部电梯会变得很卡,困扰了我找了好久,最终发现问题是决定数字大小的语句重复了。
floor_font = pygame.font.Font(None, 50),在开头写一句就好了,改完后就神奇的变得不卡了。
还有颜色,调成这样
250-self.x/10, 250-self.x/6, self.x/6,是为了能出现渐变色,真的超级好看。
def draw(self):
# 创造电梯穿梭间
pygame.draw.rect(screen, [250-self.x/10, 250-self.x/6, self.x/6], [self.x-10, 0, 53, 520], 0)
# 创造电梯
screen.blit(pygame.image.load("people.png"), (self.x, self.y))
floor_font = pygame.font.Font(None, 50) # 变得不卡
# 创造电梯编号
floor_text = floor_font.render(str(self.elevator_number), True, (250-self.x/10, 250-self.x/6, self.x/6))
screen.blit(floor_text, (self.x + 5, 525))
# 画楼层
for floor in range(1, 17):
if self.running[floor] == 1:
floor_text = floor_font.render(str(floor), True, (255, 0, 0))
screen.blit(floor_text, (self.x+50, self.floors[floor]))
elif self.running[floor] == 2:
floor_text = floor_font.render(str(floor), True, (0, 255, 0))
screen.blit(floor_text, (self.x+50, self.floors[floor]))
elif self.running[floor] == 3:
floor_text = floor_font.render(str(floor), True, (0, 0, 255))
screen.blit(floor_text, (self.x+50, self.floors[floor]))
else:
floor_text = floor_font.render(str(floor), True, (250-self.x/10, 250-self.x/6, self.x/6))
screen.blit(floor_text, (self.x + 50, self.floors[floor]))
# 方向键
if self.direction[floor] == 1:
screen.blit(up, (0, self.floors[floor]))
if self.direction[floor] == 2:
screen.blit(down, (45, self.floors[floor]))
if self.direction[floor] == 3:
screen.blit(up, (0, self.floors[floor]))
screen.blit(down, (45, self.floors[floor]))
然后就到了一模一样的移动,到达,停止三连。
# 电梯移动
def move(self):
self.move_up = False
# 电梯回归初始位置
initial = 0
for i in range(1, 17):
if self.running[i] == 1:
initial = 1
elif self.running[i] == 2:
initial = 1
elif self.running[i] == 3:
initial = 1
else:
pass
if self.y <= self.elevator_initial_location and initial == 0:
self.y = self.y + self.speed
if self.y > self.elevator_initial_location and initial == 0:
self.y = self.y - self.speed
self.move_up = True
# 电梯状态检测
priority = 0
if self.inertia == 1:
for i in range(1, 17):
if self.running[i] == 1 and self.y <= self.floors[i]:
self.inertia = 1
break
elif self.running[i] == 2 and self.y <= self.floors[i]:
self.inertia = 1
break
elif self.running[i] == 3 and self.y <= self.floors[i]:
self.inertia = 1
break
else:
self.inertia = 0
if self.inertia == 0:
for i in range(1, 17):
if self.running[i] == 1 and self.y >= self.floors[i]:
self.inertia = 0
break
elif self.running[i] == 2 and self.y >= self.floors[i]:
self.inertia = 0
break
elif self.running[i] == 3 and self.y >= self.floors[i]:
self.inertia = 0
break
else:
self.inertia = 1
# 电梯移动
for i in range(1, 17):
if self.running[i] == 1 and self.y >= self.floors[i] and self.inertia == 0:
self.y = self.y - self.speed
self.move_up = True
priority = 1
break
if self.running[i] == 3 and self.y >= self.floors[i] and self.inertia == 0:
self.y = self.y - self.speed
self.move_up = True
priority = 1
break
if self.running[i] == 1 and self.y <= self.floors[i] and self.inertia == 1:
self.y = self.y + self.speed
priority = 1
break
if self.running[i] == 3 and self.y <= self.floors[i] and self.inertia == 1:
self.y = self.y + self.speed
priority = 1
break
if priority == 0:
for i in range(1, 17):
if self.running[i] == 2 and self.y >= self.floors[i] and self.inertia == 0:
self.y = self.y - self.speed
self.move_up = True
break
if self.running[i] == 2 and self.y <= self.floors[i] and self.inertia == 1:
self.y = self.y + self.speed
break
# 电梯到达
def arrive(self):
# 电梯到达下客
for i in range(1, 17):
if self.y == self.floors[i] and self.running[i] == 2:
self.running[i] = 0
self.time = 0
# 电梯到达上客
# 上升到达
if self.y == self.floors[i] and self.running[i] != 0 and self.running[i] != 2 and self.direction[i] == 1 and self.inertia == 0:
self.running[i] = 0
self.direction[i] = 0
self.time = 0
if self.y == self.floors[i] and self.running[i] != 0 and self.running[i] != 2 and self.direction[i] == 3 and self.inertia == 0:
self.direction[i] = 2
self.time = 0
# 下降到达
if self.y == self.floors[i] and self.running[i] != 0 and self.running[i] != 2 and self.direction[i] == 2 and self.inertia == 1:
self.running[i] = 0
self.time = 0
self.direction[i] = 0
if self.y == self.floors[i] and self.running[i] != 0 and self.running[i] != 2 and self.direction[i] == 3 and self.inertia == 1:
self.direction[i] = 1
self.time = 0
# 到达停止
def stop(self):
# 电梯停止
if self.time <= 50: # 电梯停止时间
if self.move_up:
self.y = self.y + self.speed
else:
self.y = self.y - self.speed
self.time = self.time + 1
else:
self.time = 100
self.move_up = False
?最后是一个运行函数,注意顺序喔。
def run(self):
self.move()
self.arrive()
self.stop()
self.draw()
?2.总控类
还是先来看一下变量吧。我把电梯对象存在列表中,这样第一个就是一号电梯,第二个就是二号电梯。
class all:
def __init__(self):
self.elevator = [0] # 电梯对象
self.elevator_total = 12 # 电梯总数
self.key_floor = [] # 输入的楼层
self.way = 1 # 输入的方向
self.direction = [0] # 楼层的方向键
self.which_elevator = 1 # 选择哪一个电梯输入楼层(数字)
self.arrive = 0 # 输入方向的操作确认
self.input_finish = False # 完成输入确认
self.create = True # 终止创造电梯
self.key_which_elevator = [] # 选择哪一个电梯输入楼层(列表)
?紧接着是创造电梯对象,方向键了。因为只运行一次,所以加个条件就好。
# 创造楼层方向键,电梯对象
def number(self):
if self.create:
self.create = False
elevator_initial_location = 482//self.elevator_total # 电梯默认位置
for i in range(1, self.elevator_total+1):
self.elevator.append(elevator(110*i-20, elevator_initial_location*i, i))
for i in range(1, 17):
self.direction.append(0)
输入部分也完全一样,不多说了。
?# 键盘事件
def keyboard_get(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN:
if event.key == CONTROLLER_BUTTON_DPAD_LEFT:
self.input_finish = True
# 将输入的电梯编号从列表转换为数字
if len(self.key_which_elevator) == 2:
self.which_elevator = 10 + self.key_which_elevator[1]
if len(self.key_which_elevator) == 1:
self.which_elevator = self.key_which_elevator[0]
while True:
if len(self.key_which_elevator) >= 1:
del self.key_which_elevator[0]
else:
break
if K_0 <= event.key <= K_9:
self.key_floor.append(event.key - 48)
if event.key == K_DOWN:
self.way = 2
self.arrive = 1
if event.key == K_UP:
self.way = 1
self.arrive = 1
if K_KP1 <= event.key <= K_KP9:
self.key_which_elevator.append(event.key - 1073741912)
if event.key == K_KP0:
self.key_which_elevator.append(0)
else:
pass
# 输入
def input(self):
# 显示输入楼层
if len(self.key_floor) == 1:
floor_font = pygame.font.Font(None, 80)
floor_text = floor_font.render(str(self.key_floor[0]), True, (0, 0, 255))
screen.blit(floor_text, (0, 100))
if len(self.key_floor) == 2:
floor_font = pygame.font.Font(None, 80)
floor_text = floor_font.render(str(self.key_floor[0]), True, (0, 0, 255))
screen.blit(floor_text, (0, 100))
floor_font = pygame.font.Font(None, 80)
floor_text = floor_font.render(str(self.key_floor[1]), True, (0, 0, 255))
screen.blit(floor_text, (30, 100))
# 输入上客楼层
if self.arrive == 1:
if self.input_finish == True:
self.arrive = 0
self.input_finish = False
if len(self.key_floor) == 1:
for i in range(1, 11):
if self.key_floor[0] == i:
if self.direction[i] == 1:
if self.way == 2:
self.direction[i] = 3
for j in range(1, self.elevator_total + 1):
self.elevator[j].direction[i] = 3
if self.direction[i] == 2:
if self.way == 1:
self.direction[i] = 3
for j in range(1, self.elevator_total + 1):
self.elevator[j].direction[i] = 3
if self.direction[i] == 0:
if self.way == 1:
self.direction[i] = 1
for j in range(1, self.elevator_total + 1):
self.elevator[j].direction[i] = 1
if self.way == 2:
self.direction[i] = 2
for j in range(1, self.elevator_total + 1):
self.elevator[j].direction[i] = 2
for j in range(1, self.elevator_total + 1):
if self.elevator[j].running[i] == 0:
self.elevator[j].running[i] = 1
if self.elevator[j].running[i] == 2:
self.elevator[j].running[i] = 3
if len(self.key_floor) == 2:
for i in range(0, 7):
if self.key_floor[1] == i:
if self.direction[i + 10] == 1:
if self.way == 2:
self.direction[i+10] = 3
for j in range(1, self.elevator_total + 1):
self.elevator[j].direction[i + 10] = 3
if self.direction[i + 10] == 2:
if self.way == 1:
self.direction[i+10] = 3
for j in range(1, self.elevator_total + 1):
self.elevator[j].direction[i + 10] = 3
if self.direction[i + 10] == 0:
if self.way == 1:
self.direction[i+10] = 1
for j in range(1, self.elevator_total + 1):
self.elevator[j].direction[i + 10] = 1
if self.way == 2:
self.direction[i+10] = 2
for j in range(1, self.elevator_total + 1):
self.elevator[j].direction[i + 10] = 2
for j in range(1, self.elevator_total + 1):
if self.elevator[j].running[i + 10] == 0:
self.elevator[j].running[i+10] = 1
if self.elevator[j].running[i + 10] == 2:
self.elevator[j].running[i+10] = 3
# 清空输入的楼层
while True:
if len(self.key_floor) >= 1:
del self.key_floor[0]
else:
break
# 输入下客楼层
if self.arrive == 0:
if self.input_finish:
self.input_finish = False
if len(self.key_floor) == 1:
for ii in range(1, 11):
if self.key_floor[0] == ii:
if self.elevator[self.which_elevator].running[ii] == 1:
self.elevator[self.which_elevator].running[ii] = 3
if self.elevator[self.which_elevator].running[ii] == 0:
self.elevator[self.which_elevator].running[ii] = 2
if len(self.key_floor) == 2:
for ii in range(0, 7):
if self.key_floor[1] == ii:
if self.elevator[self.which_elevator].running[ii + 10] == 1:
self.elevator[self.which_elevator].running[ii+10] = 3
if self.elevator[self.which_elevator].running[ii + 10] == 0:
self.elevator[self.which_elevator].running[ii + 10] = 2
# 清空输入的楼层
while True:
if len(self.key_floor) >= 1:
del self.key_floor[0]
else:
break
?
用面向对象便需多加一个“联合”。因为每一个电梯都可以当做个体,按了方向键,肯定是所有电梯都要收到任务,一个电梯完成任务,也要将所有电梯的任务取消。
# 将单个电梯传到总控制,再由总控制传回单个电梯
def unite(self):
for j in range(1, self.elevator_total+1):
for i in range(1, 17):
if self.elevator[j].direction[i] == 0 and self.direction[i] != 0:
self.direction[i] = 0
for e in range(1, self.elevator_total+1):
self.elevator[e].direction[i] = 0
if self.elevator[e].running[i] == 3:
self.elevator[e].running[i] = 2
if self.elevator[e].running[i] == 1:
self.elevator[e].running[i] = 0
if self.elevator[j].direction[i] != 0 and self.elevator[j].direction[i] != 3 and self.direction[i] == 3:
self.direction[i] = self.elevator[j].direction[i]
for e in range(1, self.elevator_total+1):
self.elevator[e].direction[i] = self.elevator[j].direction[i]
最后就是运行函数了。同样需要注意顺序。
def run(self):
self.number()
self.keyboard_get()
self.input()
for i in range(1, self.elevator_total+1):
self.elevator[i].run()
self.unite()
附上全部代码
import pygame
from pygame.locals import *
from sys import exit
class all:
def __init__(self):
self.elevator = [0] # 电梯对象
self.elevator_total = 12 # 电梯总数
self.key_floor = [] # 输入的楼层
self.way = 1 # 输入的方向
self.direction = [0] # 楼层的方向键
self.which_elevator = 1 # 选择哪一个电梯输入楼层(数字)
self.arrive = 0 # 输入方向的操作确认
self.input_finish = False # 完成输入确认
self.create = True # 终止创造电梯
self.key_which_elevator = [] # 选择哪一个电梯输入楼层(列表)
def run(self):
self.number()
self.keyboard_get()
self.input()
for i in range(1, self.elevator_total+1):
self.elevator[i].run()
self.unite()
# 创造楼层方向键,电梯对象
def number(self):
if self.create:
self.create = False
elevator_initial_location = 482//self.elevator_total # 电梯默认位置
for i in range(1, self.elevator_total+1):
self.elevator.append(elevator(110*i-20, elevator_initial_location*i, i))
for i in range(1, 17):
self.direction.append(0)
# 键盘事件
def keyboard_get(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN:
if event.key == CONTROLLER_BUTTON_DPAD_LEFT:
self.input_finish = True
# 将输入的电梯编号从列表转换为数字
if len(self.key_which_elevator) == 2:
self.which_elevator = 10 + self.key_which_elevator[1]
if len(self.key_which_elevator) == 1:
self.which_elevator = self.key_which_elevator[0]
while True:
if len(self.key_which_elevator) >= 1:
del self.key_which_elevator[0]
else:
break
if K_0 <= event.key <= K_9:
self.key_floor.append(event.key - 48)
if event.key == K_DOWN:
self.way = 2
self.arrive = 1
if event.key == K_UP:
self.way = 1
self.arrive = 1
if K_KP1 <= event.key <= K_KP9:
self.key_which_elevator.append(event.key - 1073741912)
if event.key == K_KP0:
self.key_which_elevator.append(0)
else:
pass
# 将单个电梯传到总控制,再由总控制传回单个电梯
def unite(self):
for j in range(1, self.elevator_total+1):
for i in range(1, 17):
if self.elevator[j].direction[i] == 0 and self.direction[i] != 0:
self.direction[i] = 0
for e in range(1, self.elevator_total+1):
self.elevator[e].direction[i] = 0
if self.elevator[e].running[i] == 3:
self.elevator[e].running[i] = 2
if self.elevator[e].running[i] == 1:
self.elevator[e].running[i] = 0
if self.elevator[j].direction[i] != 0 and self.elevator[j].direction[i] != 3 and self.direction[i] == 3:
self.direction[i] = self.elevator[j].direction[i]
for e in range(1, self.elevator_total+1):
self.elevator[e].direction[i] = self.elevator[j].direction[i]
# 输入
def input(self):
# 显示输入楼层
if len(self.key_floor) == 1:
floor_font = pygame.font.Font(None, 80)
floor_text = floor_font.render(str(self.key_floor[0]), True, (0, 0, 255))
screen.blit(floor_text, (0, 100))
if len(self.key_floor) == 2:
floor_font = pygame.font.Font(None, 80)
floor_text = floor_font.render(str(self.key_floor[0]), True, (0, 0, 255))
screen.blit(floor_text, (0, 100))
floor_font = pygame.font.Font(None, 80)
floor_text = floor_font.render(str(self.key_floor[1]), True, (0, 0, 255))
screen.blit(floor_text, (30, 100))
# 输入上客楼层
if self.arrive == 1:
if self.input_finish:
self.arrive = 0
self.input_finish = False
if len(self.key_floor) == 1:
for i in range(1, 11):
if self.key_floor[0] == i:
if self.direction[i] == 1:
if self.way == 2:
self.direction[i] = 3
for j in range(1, self.elevator_total + 1):
self.elevator[j].direction[i] = 3
if self.direction[i] == 2:
if self.way == 1:
self.direction[i] = 3
for j in range(1, self.elevator_total + 1):
self.elevator[j].direction[i] = 3
if self.direction[i] == 0:
if self.way == 1:
self.direction[i] = 1
for j in range(1, self.elevator_total + 1):
self.elevator[j].direction[i] = 1
if self.way == 2:
self.direction[i] = 2
for j in range(1, self.elevator_total + 1):
self.elevator[j].direction[i] = 2
for j in range(1, self.elevator_total + 1):
if self.elevator[j].running[i] == 0:
self.elevator[j].running[i] = 1
if self.elevator[j].running[i] == 2:
self.elevator[j].running[i] = 3
if len(self.key_floor) == 2:
for i in range(0, 7):
if self.key_floor[1] == i:
if self.direction[i + 10] == 1:
if self.way == 2:
self.direction[i+10] = 3
for j in range(1, self.elevator_total + 1):
self.elevator[j].direction[i + 10] = 3
if self.direction[i + 10] == 2:
if self.way == 1:
self.direction[i+10] = 3
for j in range(1, self.elevator_total + 1):
self.elevator[j].direction[i + 10] = 3
if self.direction[i + 10] == 0:
if self.way == 1:
self.direction[i+10] = 1
for j in range(1, self.elevator_total + 1):
self.elevator[j].direction[i + 10] = 1
if self.way == 2:
self.direction[i+10] = 2
for j in range(1, self.elevator_total + 1):
self.elevator[j].direction[i + 10] = 2
for j in range(1, self.elevator_total + 1):
if self.elevator[j].running[i + 10] == 0:
self.elevator[j].running[i+10] = 1
if self.elevator[j].running[i + 10] == 2:
self.elevator[j].running[i+10] = 3
# 清空输入的楼层
while True:
if len(self.key_floor) >= 1:
del self.key_floor[0]
else:
break
# 输入下客楼层
if self.arrive == 0:
if self.input_finish:
self.input_finish = False
if len(self.key_floor) == 1:
for ii in range(1, 11):
if self.key_floor[0] == ii:
if self.elevator[self.which_elevator].running[ii] == 1:
self.elevator[self.which_elevator].running[ii] = 3
if self.elevator[self.which_elevator].running[ii] == 0:
self.elevator[self.which_elevator].running[ii] = 2
if len(self.key_floor) == 2:
for ii in range(0, 7):
if self.key_floor[1] == ii:
if self.elevator[self.which_elevator].running[ii + 10] == 1:
self.elevator[self.which_elevator].running[ii+10] = 3
if self.elevator[self.which_elevator].running[ii + 10] == 0:
self.elevator[self.which_elevator].running[ii + 10] = 2
# 清空输入的楼层
while True:
if len(self.key_floor) >= 1:
del self.key_floor[0]
else:
break
class elevator:
def __init__(self, x, elevator_initial_location, elevator_number):
self.x = x # 电梯x坐标
self.y = 482 # 电梯初始y坐标
self.speed = 2 # 电梯速度
self.floors = [0] # 楼层位置
self.running = [0] # 楼层状态
self.inertia = 0 # 电梯状态(惯性)
self.move_up = False # 是否上升
self.time = 100 # 暂停时间
self.direction = [0] # 按电梯的方向键
self.elevator_initial_location = elevator_initial_location # 电梯默认位置,使电梯均匀分布
self.elevator_number = elevator_number # 电梯编号
for j in range(1, 17): # 楼层变色 1为上客状态,二为下客状态,3为上下
self.running.append(0)
self.direction.append(0)
i = 482
floor_running = True
while floor_running:
self.floors.append(i)
i = i - 32
if i == -30:
floor_running = False
def run(self):
self.move()
self.arrive()
self.stop()
self.draw()
# 电梯移动
def move(self):
self.move_up = False
# 电梯回归初始位置
initial = 0
for i in range(1, 17):
if self.running[i] == 1:
initial = 1
elif self.running[i] == 2:
initial = 1
elif self.running[i] == 3:
initial = 1
else:
pass
if self.y <= self.elevator_initial_location and initial == 0:
self.y = self.y + self.speed
if self.y > self.elevator_initial_location and initial == 0:
self.y = self.y - self.speed
self.move_up = True
# 电梯状态检测
priority = 0
if self.inertia == 1:
for i in range(1, 17):
if self.running[i] == 1 and self.y <= self.floors[i]:
self.inertia = 1
break
elif self.running[i] == 2 and self.y <= self.floors[i]:
self.inertia = 1
break
elif self.running[i] == 3 and self.y <= self.floors[i]:
self.inertia = 1
break
else:
self.inertia = 0
if self.inertia == 0:
for i in range(1, 17):
if self.running[i] == 1 and self.y >= self.floors[i]:
self.inertia = 0
break
elif self.running[i] == 2 and self.y >= self.floors[i]:
self.inertia = 0
break
elif self.running[i] == 3 and self.y >= self.floors[i]:
self.inertia = 0
break
else:
self.inertia = 1
# 电梯移动
for i in range(1, 17):
if self.running[i] == 1 and self.y >= self.floors[i] and self.inertia == 0:
self.y = self.y - self.speed
self.move_up = True
priority = 1
break
if self.running[i] == 3 and self.y >= self.floors[i] and self.inertia == 0:
self.y = self.y - self.speed
self.move_up = True
priority = 1
break
if self.running[i] == 1 and self.y <= self.floors[i] and self.inertia == 1:
self.y = self.y + self.speed
priority = 1
break
if self.running[i] == 3 and self.y <= self.floors[i] and self.inertia == 1:
self.y = self.y + self.speed
priority = 1
break
if priority == 0:
for i in range(1, 17):
if self.running[i] == 2 and self.y >= self.floors[i] and self.inertia == 0:
self.y = self.y - self.speed
self.move_up = True
break
if self.running[i] == 2 and self.y <= self.floors[i] and self.inertia == 1:
self.y = self.y + self.speed
break
# 电梯到达
def arrive(self):
# 电梯到达下客
for i in range(1, 17):
if self.y == self.floors[i] and self.running[i] == 2:
self.running[i] = 0
self.time = 0
# 电梯到达上客
# 上升到达
if self.y == self.floors[i] and self.running[i] != 0 and self.running[i] != 2 and self.direction[i] == 1 and self.inertia == 0:
self.running[i] = 0
self.direction[i] = 0
self.time = 0
if self.y == self.floors[i] and self.running[i] != 0 and self.running[i] != 2 and self.direction[i] == 3 and self.inertia == 0:
self.direction[i] = 2
self.time = 0
# 下降到达
if self.y == self.floors[i] and self.running[i] != 0 and self.running[i] != 2 and self.direction[i] == 2 and self.inertia == 1:
self.running[i] = 0
self.time = 0
self.direction[i] = 0
if self.y == self.floors[i] and self.running[i] != 0 and self.running[i] != 2 and self.direction[i] == 3 and self.inertia == 1:
self.direction[i] = 1
self.time = 0
# 到达停止
def stop(self):
# 电梯停止
if self.time <= 50: # 电梯停止时间
if self.move_up:
self.y = self.y + self.speed
else:
self.y = self.y - self.speed
self.time = self.time + 1
else:
self.time = 100
self.move_up = False
def draw(self):
# 创造电梯穿梭间
pygame.draw.rect(screen, [250-self.x/10, 250-self.x/6, self.x/6], [self.x-10, 0, 53, 520], 0)
# 创造电梯
screen.blit(pygame.image.load("people.png"), (self.x, self.y))
floor_font = pygame.font.Font(None, 50) # 变得不卡
# 创造电梯编号
floor_text = floor_font.render(str(self.elevator_number), True, (250-self.x/10, 250-self.x/6, self.x/6))
screen.blit(floor_text, (self.x + 5, 525))
# 画楼层
for floor in range(1, 17):
if self.running[floor] == 1:
floor_text = floor_font.render(str(floor), True, (255, 0, 0))
screen.blit(floor_text, (self.x+50, self.floors[floor]))
elif self.running[floor] == 2:
floor_text = floor_font.render(str(floor), True, (0, 255, 0))
screen.blit(floor_text, (self.x+50, self.floors[floor]))
elif self.running[floor] == 3:
floor_text = floor_font.render(str(floor), True, (0, 0, 255))
screen.blit(floor_text, (self.x+50, self.floors[floor]))
else:
floor_text = floor_font.render(str(floor), True, (250-self.x/10, 250-self.x/6, self.x/6))
screen.blit(floor_text, (self.x + 50, self.floors[floor]))
# 方向键
if self.direction[floor] == 1:
screen.blit(up, (0, self.floors[floor]))
if self.direction[floor] == 2:
screen.blit(down, (45, self.floors[floor]))
if self.direction[floor] == 3:
screen.blit(up, (0, self.floors[floor]))
screen.blit(down, (45, self.floors[floor]))
# 初始化pygame
pygame.init()
# 设置窗口名称
pygame.display.set_caption("电梯")
# 颜色值(255, 0, 0)表示红色,(0, 255, 0)表示绿色,而(0, 0, 255)表示蓝色
# 图片
up = pygame.image.load("up.png")
down = pygame.image.load("down.png")
# 定义画面帧率
FPS = 40
clock = pygame.time.Clock()
# 创造对象
all = all()
# 获取对显示系统的访问,并创建一个窗口,分别率位(600x500)screen:屏幕
screen = pygame.display.set_mode((all.elevator_total*140, 560))
# 主程序
while True:
pygame.display.update() # 刷新显示
clock.tick(FPS) # 最大帧率控制
# 背景颜色
screen.fill([255, 255, 255])
all.run()
总结
这全部代码才400行,而(2)两部电梯的要到500行。足以见面向对象的厉害,在有多个个体时便能显示出它的能力。
|