Advertisement
yen881215

turn on light_correct

Dec 13th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.42 KB | None | 0 0
  1. import sys, time
  2. import random
  3.  
  4. import pygame
  5. from pygame.locals import *
  6.  
  7. FPS = 60
  8.  
  9. class get_color(pygame.sprite.Sprite):
  10. def __init__(self,on_off):
  11. super(get_color,self).__init__()
  12. self.surf = pygame.Surface((25,25))
  13. if on_off%2 == 0 : self.surf.fill((0,0,0))
  14. if on_off%2 == 1 : self.surf.fill((255,255,255))
  15. self.rect = self.surf.get_rect()
  16.  
  17. def main():
  18. a1 = random.randint(0,1)
  19. a2 = random.randint(0,1)
  20. a3 = random.randint(0,1)
  21. a4 = random.randint(0,1)
  22. a5 = random.randint(0,1)
  23. a6 = random.randint(0,1)
  24. a7 = random.randint(0,1)
  25. a8 = random.randint(0,1)
  26. a9 = random.randint(0,1)
  27.  
  28. print(a1,a2,a3,a4,a5,a6,a7,a8,a9)
  29.  
  30. b1 = 0
  31. b2 = 0
  32. b3 = 0
  33. b4 = 0
  34. b5 = 0
  35. b6 = 0
  36. b7 = 0
  37. b8 = 0
  38. b9 = 0
  39.  
  40. q1_on_off = (a1 + a2 + a4)%2
  41. q2_on_off = (a1 + a2 + a3 + a5)%2
  42. q3_on_off = (a2 + a3 + a6)%2
  43. q4_on_off = (a1 + a4 + a5 + a7)%2
  44. q5_on_off = (a2 + a4 + a5 + a6 + a8)%2
  45. q6_on_off = (a3 + a5 + a6 + a9)%2
  46. q7_on_off = (a4 + a7 + a8)%2
  47. q8_on_off = (a5 + a7 + a8 + a9)%2
  48. q9_on_off = (a6 + a8 + a9)%2
  49.  
  50. w1_on_off = (b1 + b2 + b4)%2
  51. w2_on_off = (b1 + b2 + b3 + b5)%2
  52. w3_on_off = (b2 + b3 + b6)%2
  53. w4_on_off = (b1 + b4 + b5 + b7)%2
  54. w5_on_off = (b2 + b4 + b5 + b6 + b8)%2
  55. w6_on_off = (b3 + b5 + b6 + b9)%2
  56. w7_on_off = (b4 + b7 + b8)%2
  57. w8_on_off = (b5 + b7 + b8 + b9)%2
  58. w9_on_off = (b6 + b8 + b9)%2
  59.  
  60. q1 = get_color(q1_on_off)
  61. q2 = get_color(q2_on_off)
  62. q3 = get_color(q3_on_off)
  63. q4 = get_color(q4_on_off)
  64. q5 = get_color(q5_on_off)
  65. q6 = get_color(q6_on_off)
  66. q7 = get_color(q7_on_off)
  67. q8 = get_color(q8_on_off)
  68. q9 = get_color(q9_on_off)
  69.  
  70. w1 = get_color(w1_on_off)
  71. w2 = get_color(w2_on_off)
  72. w3 = get_color(w3_on_off)
  73. w4 = get_color(w4_on_off)
  74. w5 = get_color(w5_on_off)
  75. w6 = get_color(w6_on_off)
  76. w7 = get_color(w7_on_off)
  77. w8 = get_color(w8_on_off)
  78. w9 = get_color(w9_on_off)
  79.  
  80. pygame.init()
  81. running = True
  82. screen = pygame.display.set_mode((800,600))
  83. pygame.display.set_caption('Turn on the Light')
  84. main_clock = pygame.time.Clock()
  85. screen.fill((255,100,255))
  86. # 宣告 font 文字物件
  87. head_font = pygame.font.SysFont(None, 50)
  88. # 渲染方法會回傳 surface 物件
  89. text1 = head_font.render('Goal:', True, (0, 0, 0))
  90. # blit 用來把其他元素渲染到另外一個 surface 上,這邊是 window 視窗
  91. screen.blit(text1, (25,25))
  92.  
  93. # 宣告 font 文字物件
  94. head_font = pygame.font.SysFont(None, 50)
  95. # 渲染方法會回傳 surface 物件
  96. text2 = head_font.render('Your Light:', True, (0, 0, 0))
  97. # blit 用來把其他元素渲染到另外一個 surface 上,這邊是 window 視窗
  98. screen.blit(text2, (300,25))
  99.  
  100. # 这一行表示:将surf画到屏幕 x:400.y:300的坐标上
  101. screen.blit(q1.surf,(100,100))
  102. screen.blit(q2.surf,(125,100))
  103. screen.blit(q3.surf,(150,100))
  104. screen.blit(q4.surf,(100,125))
  105. screen.blit(q5.surf,(125,125))
  106. screen.blit(q6.surf,(150,125))
  107. screen.blit(q7.surf,(100,150))
  108. screen.blit(q8.surf,(125,150))
  109. screen.blit(q9.surf,(150,150))
  110.  
  111. # 这一行表示:将surf画到屏幕 x:400.y:300的坐标上
  112. screen.blit(w1.surf,(475,100))
  113. screen.blit(w2.surf,(500,100))
  114. screen.blit(w3.surf,(525,100))
  115. screen.blit(w4.surf,(475,125))
  116. screen.blit(w5.surf,(500,125))
  117. screen.blit(w6.surf,(525,125))
  118. screen.blit(w7.surf,(475,150))
  119. screen.blit(w8.surf,(500,150))
  120. screen.blit(w9.surf,(525,150))
  121. # 主循环
  122. while running:
  123. for event in pygame.event.get():
  124. # check for QUIT event: if QUIT, set running to false
  125. if event.type == QUIT:
  126. pygame.quit()
  127. sys.exit()
  128. elif event.type == MOUSEBUTTONDOWN:
  129. if 475 < pygame.mouse.get_pos()[0] < 500 and 100 < pygame.mouse.get_pos()[1] < 125:
  130. b1 +=1
  131. if 500 < pygame.mouse.get_pos()[0] < 525 and 100 < pygame.mouse.get_pos()[1] < 125:
  132. b2 +=1
  133. if 525 < pygame.mouse.get_pos()[0] < 550 and 100 < pygame.mouse.get_pos()[1] < 125:
  134. b3 +=1
  135. if 475 < pygame.mouse.get_pos()[0] < 500 and 125 < pygame.mouse.get_pos()[1] < 150:
  136. b4 +=1
  137. if 500 < pygame.mouse.get_pos()[0] < 525 and 125 < pygame.mouse.get_pos()[1] < 150:
  138. b5 +=1
  139. if 525 < pygame.mouse.get_pos()[0] < 550 and 125 < pygame.mouse.get_pos()[1] < 150:
  140. b6 +=1
  141. if 475 < pygame.mouse.get_pos()[0] < 500 and 150 < pygame.mouse.get_pos()[1] < 175:
  142. b7 +=1
  143. if 500 < pygame.mouse.get_pos()[0] < 525 and 150 < pygame.mouse.get_pos()[1] < 175:
  144. b8 +=1
  145. if 525 < pygame.mouse.get_pos()[0] < 550 and 150 < pygame.mouse.get_pos()[1] < 175:
  146. b9 +=1
  147.  
  148. w1_on_off = (b1 + b2 + b4)%2
  149. w2_on_off = (b1 + b2 + b3 + b5)%2
  150. w3_on_off = (b2 + b3 + b6)%2
  151. w4_on_off = (b1 + b4 + b5 + b7)%2
  152. w5_on_off = (b2 + b4 + b5 + b6 + b8)%2
  153. w6_on_off = (b3 + b5 + b6 + b9)%2
  154. w7_on_off = (b4 + b7 + b8)%2
  155. w8_on_off = (b5 + b7 + b8 + b9)%2
  156. w9_on_off = (b6 + b8 + b9)%2
  157.  
  158. w1 = get_color(w1_on_off)
  159. w2 = get_color(w2_on_off)
  160. w3 = get_color(w3_on_off)
  161. w4 = get_color(w4_on_off)
  162. w5 = get_color(w5_on_off)
  163. w6 = get_color(w6_on_off)
  164. w7 = get_color(w7_on_off)
  165. w8 = get_color(w8_on_off)
  166. w9 = get_color(w9_on_off)
  167.  
  168. elif event.type == KEYDOWN:
  169. # if the Esc KEY has been pressed set running to false to exit the main loop
  170. if event.key == K_ESCAPE:
  171. running = False
  172.  
  173.  
  174. screen.fill((255,100,255))
  175. # 宣告 font 文字物件
  176. head_font = pygame.font.SysFont(None, 50)
  177. # 渲染方法會回傳 surface 物件
  178. text1 = head_font.render('Goal:', True, (0, 0, 0))
  179. # blit 用來把其他元素渲染到另外一個 surface 上,這邊是 window 視窗
  180. screen.blit(text1, (25,25))
  181.  
  182. # 宣告 font 文字物件
  183. head_font = pygame.font.SysFont(None, 50)
  184. # 渲染方法會回傳 surface 物件
  185. text2 = head_font.render('Your Light:', True, (0, 0, 0))
  186. # blit 用來把其他元素渲染到另外一個 surface 上,這邊是 window 視窗
  187. screen.blit(text2, (300,25))
  188. if q1_on_off == w1_on_off and q2_on_off == w2_on_off and q3_on_off == w3_on_off and q4_on_off == w4_on_off and q5_on_off == w5_on_off and q5_on_off == w5_on_off and q6_on_off == w6_on_off and q7_on_off == w7_on_off and q8_on_off == w8_on_off and q9_on_off == w9_on_off:
  189. # 宣告 font 文字物件
  190. head_font = pygame.font.SysFont(None, 50)
  191. # 渲染方法會回傳 surface 物件
  192. text3 = head_font.render('Congratuation', True, (0, 0, 0))
  193. # blit 用來把其他元素渲染到另外一個 surface 上,這邊是 window 視窗
  194. screen.blit(text3, (200,300))
  195.  
  196.  
  197. # 这一行表示:将surf画到屏幕 x:400.y:300的坐标上
  198. screen.blit(q1.surf,(100,100))
  199. screen.blit(q2.surf,(125,100))
  200. screen.blit(q3.surf,(150,100))
  201. screen.blit(q4.surf,(100,125))
  202. screen.blit(q5.surf,(125,125))
  203. screen.blit(q6.surf,(150,125))
  204. screen.blit(q7.surf,(100,150))
  205. screen.blit(q8.surf,(125,150))
  206. screen.blit(q9.surf,(150,150))
  207.  
  208. # 这一行表示:将surf画到屏幕 x:400.y:300的坐标上
  209. screen.blit(w1.surf,(475,100))
  210. screen.blit(w2.surf,(500,100))
  211. screen.blit(w3.surf,(525,100))
  212. screen.blit(w4.surf,(475,125))
  213. screen.blit(w5.surf,(500,125))
  214. screen.blit(w6.surf,(525,125))
  215. screen.blit(w7.surf,(475,150))
  216. screen.blit(w8.surf,(500,150))
  217. screen.blit(w9.surf,(525,150))
  218. # 更新
  219. pygame.display.flip()
  220. # 事件队列中的循环
  221.  
  222.  
  223.  
  224.  
  225. if __name__ == '__main__':
  226. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement