Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. import random, pygame, sys
  2. from pygame.locals import *
  3. import time
  4.  
  5. size = [600, 600]
  6. clock = pygame.time.Clock()
  7. pygame.init()
  8. screen = pygame.display.set_mode(size)
  9. FLOORNUMBER=6
  10. FloorHeight = size[1]/FLOORNUMBER
  11.  
  12. BLACK = ( 0, 0, 0)
  13. RED = (255, 0, 0)
  14. GREEN = ( 0, 255, 0)
  15. BLUE = ( 0, 0, 255)
  16. WHITE = (255, 255, 255)
  17.  
  18. class Passenger():
  19. def __init__(self, StartFloor, DestinationFloor):
  20. self.StartFloor=StartFloor
  21. self.DestinationFloor=DestinationFloor
  22. def getStartFloor(self): return self.StartFloor
  23. def getDestinationFloor(self): return self.DestinationFloor
  24.  
  25. class Elevator(pygame.sprite.Sprite ):
  26. def __init__(self, FloorNumber):
  27. self.FloorNumber=FloorNumber
  28. self.Passenger=[]
  29. self.CurrentFloor=0
  30. self.StopList=[]
  31. self.image=pygame.image.load("lift.jpg").convert_alpha()
  32. self.rect=self.image.get_rect()
  33. self.rect.x=100
  34. self.rect.y=size[0]-self.getRect()[3]
  35. def addPassenger(self, utas):
  36. self.Passenger.append(utas)
  37. #self.StopList.append(utas.getStartFloor())
  38. def addStop(self, floor):
  39. if floor not in self.StopList:
  40. self.StopList.append(floor)
  41. def appendlist(self, num):
  42. self.StopList.append(num)
  43. def move(self, num):
  44. self.CurrentFloor+=num
  45. tempy=self.rect.y
  46. if num == 1:
  47. print("fel: " + str(self.CurrentFloor))
  48. else:
  49. print("le: " + str(self.CurrentFloor))
  50. def printCurrentFloor(self):
  51. print(self.CurrentFloor)
  52. def getCurrentFloor(self):
  53. return self.CurrentFloor
  54. def getPassenger(self):
  55. return self.Passenger
  56. def getStopList(self):
  57. return self.StopList
  58. def getImage(self):
  59. return self.image
  60. def getRect(self):
  61. return self.rect
  62. def draw(self,surface):
  63. surface.blit(lift.getImage(),lift.getRect())
  64. def moveUp(self, pixels):
  65. if self.rect.y>0:
  66. self.rect.y-=pixels
  67. def moveDown(self, pixels):
  68. self.rect.y+=pixels
  69.  
  70. utas1=Passenger(0,2)
  71. utas2=Passenger(2,1)
  72. utas3=Passenger(5,0)
  73. utas4=Passenger(4,6)
  74. lift=Elevator(FLOORNUMBER)
  75. lift.addPassenger(utas1)
  76. lift.addPassenger(utas2)
  77. lift.addPassenger(utas3)
  78. lift.printCurrentFloor()
  79. for i in lift.getPassenger():
  80. lift.appendlist(i.getStartFloor())
  81. lift.appendlist(i.getDestinationFloor())
  82. print(lift.getStopList())
  83.  
  84. lines=[]
  85. for i in range(FLOORNUMBER):
  86. lines.append([(0, i*FloorHeight), (size[0], i*FloorHeight)])
  87.  
  88. def drawlines():
  89. for i in lines:
  90. pygame.draw.line(screen, BLUE, i[0], i[1], 1)
  91.  
  92. """while True:
  93. if lift.rect.y==lift.rect.y-int(FloorHeight):
  94. break
  95. lift.moveUp(1)
  96. print(lift.rect.y)
  97. print(lift.rect.y-int(FloorHeight))"""
  98. while True:
  99. clock.tick(30)
  100. screen.fill(WHITE)
  101. lift.draw(screen)
  102. drawlines()
  103. pygame.display.update()
  104. if len(lift.getStopList())==0:
  105. break
  106. elif lift.getCurrentFloor() == lift.getStopList()[0]:
  107. lift.getStopList().pop(0)
  108. #print("valami történik")
  109. elif lift.getCurrentFloor() < lift.getStopList()[0]:
  110. tempy=lift.rect.y
  111. while True:
  112. if lift.rect.y==tempy-FloorHeight:
  113. break
  114. lift.moveUp(1)
  115. print("update")
  116. time.sleep(0.01)
  117. screen.blit(lift.getImage(),lift.getRect())
  118. pygame.display.update()
  119. lift.move(1)
  120. else:
  121. tempy=lift.rect.y
  122. while True:
  123. if lift.rect.y==tempy+FloorHeight:
  124. break
  125. lift.moveDown(1)
  126. time.sleep(0.01)
  127. screen.blit(lift.getImage(),lift.getRect())
  128. pygame.display.update()
  129. lift.move(-1)
  130. #lift.printCurrentFloor()
  131. # print(lift.getStopList())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement