Sweptile

Simon-Pygame

Dec 27th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.41 KB | None | 0 0
  1. #Imports
  2. import pygame
  3. import time
  4. import random
  5.  
  6.  
  7. pygame.init()
  8.  
  9.  
  10. #Important Variables
  11. display_width = 900
  12. display_height = 550
  13. display_side = 550
  14.  
  15. gameExit = False
  16. gameOver = False
  17.  
  18. gap = display_side/15
  19. side = display_side/2 - 3*gap/2
  20.  
  21. s_x = (display_width - display_side)/2
  22. s_y = (display_height - display_side)/2
  23.  
  24. XnY = [[s_x + gap, s_y + gap], [s_x + gap + side + gap, s_y + gap], [s_x + gap, s_y + gap + side + gap], [s_x + gap + side + gap, s_y + gap + side + gap]]
  25.  
  26. check_one = 0
  27. check_two = 0
  28. check_three = 0
  29. check_four = 0
  30. check_five = 0
  31.  
  32. round_no = 1
  33.  
  34. random_array = []
  35. user_array = []
  36.  
  37. randDone = False
  38.  
  39. choice = 0
  40.  
  41. button_width = 110
  42. button_height = 50
  43.  
  44. dialog_side = display_side*9/10
  45.  
  46. smallfont = pygame.font.SysFont('comicsansms', 25)
  47. medfont = pygame.font.SysFont('comicsansms', 45)
  48. larfont = pygame.font.SysFont('comicsansms', 80)
  49.  
  50.  
  51. #Colours
  52. white = (255,255,255)
  53. black = (0,0,0)
  54. red = (150,20,20)
  55. br_red = (255,0,0)
  56. mid_red = (165, 35, 35)
  57. green = (78,164,40)
  58. br_green = (0,255,0)
  59. mid_green = (65, 190, 18)
  60. blue = (46,46,158)
  61. br_blue = (0,100,255)
  62. mid_blue = (20, 20, 182)
  63. gray = (200,200,200)
  64. dr_gray = (100,100,100)
  65. yellow = (150, 150, 43)
  66. br_yellow = (255,255,0)
  67. mid_yellow = (165, 165, 54)
  68. orange = (255, 168, 64)
  69. brown = (128, 64, 0)
  70.  
  71.  
  72. gameDisplay = pygame.display.set_mode((display_width,display_height))
  73. pygame.display.set_caption('Simon')
  74.  
  75.  
  76. clock = pygame.time.Clock()
  77. FPS = 15
  78.  
  79. #Classes
  80. class Square:
  81. def __init__(self, side, colour, mid_colour, br_colour, x, y):
  82. self.side = side
  83. self.colour = colour
  84. self.midcolour = mid_colour
  85. self.brcolour = br_colour
  86. self.x = x
  87. self.y = y
  88. def draw(self):
  89. pygame.draw.rect(gameDisplay, self.colour, [self.x, self.y, self.side, self.side])
  90. def update(self):
  91. pygame.draw.rect(gameDisplay, self.brcolour, [self.x, self.y, self.side, self.side])
  92. def mid_update(self):
  93. pygame.draw.rect(gameDisplay, self.midcolour, [self.x, self.y, self.side, self.side])
  94.  
  95. #Object Declarations
  96. #Class Square
  97. first = Square(side, red, mid_red, br_red, XnY[0][0], XnY[0][1])
  98. second = Square(side, blue, mid_blue, br_blue, XnY[1][0], XnY[1][1])
  99. third = Square(side, green, mid_green, br_green, XnY[2][0], XnY[2][1])
  100. fourth = Square(side, yellow, mid_yellow, br_yellow, XnY[3][0], XnY[3][1])
  101.  
  102. #Functions
  103. def mouseGlow():
  104. #Global Declarations
  105. global check_one
  106. global check_two
  107.  
  108. cur = pygame.mouse.get_pos()
  109. click = pygame.mouse.get_pressed()
  110.  
  111. check_one = 0
  112. while check_one < 4:
  113. if XnY[check_one][0] <= cur[0] <= XnY[check_one][0] + side and XnY[check_one][1] <= cur[1] <= XnY[check_one][1] + side:
  114. cur = pygame.mouse.get_pos()
  115. click = pygame.mouse.get_pressed()
  116.  
  117. if check_one == 0:
  118. first.mid_update()
  119. if click[0] == 1:
  120. first.update()
  121. check_two = True
  122. while click[0] == 0 and check_two == True:
  123. user_array.append(check_one + 1)
  124. check_two = False
  125. first.update()
  126. pygame.display.update()
  127. time.sleep(1)
  128. if check_one == 1:
  129. second.mid_update()
  130. if click[0] == 1:
  131. second.update()
  132. check_two = True
  133. while click[0] == 0 and check_two == True:
  134. user_array.append(check_one + 1)
  135. check_two = False
  136. second.update()
  137. pygame.display.update()
  138. time.sleep(1)
  139. if check_one == 2:
  140. third.mid_update()
  141. if click[0] == 1:
  142. third.update()
  143. check_two = True
  144. while click[0] == 0 and check_two == True:
  145. user_array.append(check_one + 1)
  146. check_two = False
  147. third.update()
  148. pygame.display.update()
  149. time.sleep(1)
  150. if check_one == 3:
  151. fourth.mid_update()
  152. if click[0] == 1:
  153. fourth.update()
  154. check_two = True
  155. while click[0] == 0 and check_two == True:
  156. user_array.append(check_one + 1)
  157. check_two = False
  158. fourth.update()
  159. pygame.display.update()
  160. time.sleep(1)
  161. check_one += 1
  162.  
  163. def random_input():
  164. #Global Declaration
  165. global choice
  166. global random_array
  167. global randDone
  168. global round_no
  169. global user_array
  170.  
  171. while not randDone:
  172. while len(random_array) < round_no:
  173. choice = round(random.randrange(1, 5))
  174. random_array.append(choice)
  175. if choice == 1:
  176. first.update()
  177. pygame.display.update()
  178. time.sleep(1.5)
  179. if choice == 2:
  180. second.update()
  181. pygame.display.update()
  182. time.sleep(1.5)
  183. if choice == 3:
  184. third.update()
  185. pygame.display.update()
  186. time.sleep(1.5)
  187. if choice == 4:
  188. fourth.update()
  189. pygame.display.update()
  190. time.sleep(1.5)
  191. first.draw()
  192. second.draw()
  193. third.draw()
  194. fourth.draw()
  195. pygame.display.update()
  196. time.sleep(0.5)
  197. randDone = True
  198. user_array = []
  199.  
  200. def gameOver_func():
  201. #Global Declarations
  202. global gameOver
  203. global dialog_side
  204.  
  205. first.draw()
  206. second.draw()
  207. third.draw()
  208. fourth.draw()
  209. pygame.display.update()
  210. while gameOver:
  211. pygame.display.update()
  212. pygame.draw.rect(gameDisplay, brown, [(s_x + display_side/2 - dialog_side/2), (s_y + display_side/2 - dialog_side/2), dialog_side, dialog_side])
  213. message_to_screen("GAME OVER", white, (s_x + display_side/2), (s_y + display_side/2 - 2*dialog_side/5 + 65), 'large')
  214. Button('Quit', (s_x + display_side/2 + 85), (s_y + display_side/2 + 165), yellow, orange, black, 'quit')
  215. Button('Play Again', (s_x + display_side/2 - 170), (s_y + display_side/2 + 165), yellow, orange, black, 'play', 40)
  216.  
  217. #The events
  218. for event in pygame.event.get():
  219. if event.type == pygame.QUIT:
  220. pygame.quit()
  221. quit()
  222.  
  223. def start():
  224. start = True
  225. while start:
  226. for event in pygame.event.get():
  227. if event.type == pygame.QUIT:
  228. pygame.quit()
  229. quit()
  230.  
  231. gameDisplay.fill(mid_yellow)
  232. pygame.draw.rect(gameDisplay, mid_red, [0, 0, display_width/2, display_height/2])
  233. pygame.draw.rect(gameDisplay, mid_blue, [display_width/2, 0, display_width/2, display_height/2])
  234. pygame.draw.rect(gameDisplay, mid_green, [0, display_height/2, display_width/2, display_height/2])
  235.  
  236. message_to_screen('SIMON', white, (display_width / 2), (display_height / 2), 'large')
  237. cur = pygame.mouse.get_pos()
  238. click = pygame.mouse.get_pressed()
  239.  
  240. Button("Play", display_width/2 - 3*button_width/2 - 150, 450, brown, orange, black, action = 'play')
  241. Button("Quit", display_width/2 + button_width/2 + 150, 450, brown, orange, black, action = 'quit')
  242.  
  243.  
  244. pygame.display.update()
  245. clock.tick(FPS)
  246.  
  247. def text_objects(text, colour,size):
  248. if size == 'small':
  249. textSurface=smallfont.render(text,True, colour)
  250. if size == 'medium':
  251. textSurface=medfont.render(text,True, colour)
  252. if size == 'large':
  253. textSurface=larfont.render(text,True, colour)
  254. return textSurface, textSurface.get_rect()
  255.  
  256. def message_to_screen(msg,colour,x_displace = 0, y_displace = 0, size ='small'):
  257. textSurf, textRect = text_objects(msg,colour, size)
  258. textRect.center = x_displace, y_displace
  259. gameDisplay.blit(textSurf,textRect)
  260.  
  261. def text_to_button(msg, colour, topX, topY, size = 'small'):
  262. textSurf, textRect = text_objects(msg, colour, size)
  263. textRect.center = (topX + button_width/2), (topY + button_height/2)
  264. gameDisplay.blit(textSurf, textRect)
  265.  
  266. def Button(msg, x, y, act_colour, inact_colour, textcolour, action, x_increase = 0):
  267.  
  268. cur = pygame.mouse.get_pos()
  269. click = pygame.mouse.get_pressed()
  270.  
  271. if x + button_width >= cur[0] >= x and y + button_height >= cur[1] >= y:
  272. pygame.draw.rect(gameDisplay, act_colour, (x - x_increase/2, y, button_width + x_increase, button_height))
  273. if click[0] == 1:
  274. if action == 'rand_input':
  275. first.draw()
  276. second.draw()
  277. third.draw()
  278. fourth.draw()
  279. text_to_button(msg, textcolour, x, y)
  280. pygame.display.update()
  281. random_input()
  282. elif action == 'quit':
  283. pygame.quit()
  284. quit()
  285. elif action == 'controls':
  286. pass
  287. elif action == 'back':
  288. start()
  289. elif action == 'play':
  290. gameLoop()
  291. else:
  292. pygame.draw.rect(gameDisplay, inact_colour, (x - x_increase/2, y, button_width + x_increase, button_height))
  293.  
  294. text_to_button(msg, textcolour, x, y)
  295.  
  296.  
  297.  
  298. #The Game Loop
  299. def gameLoop():
  300. #Global Declarations
  301. global gameExit
  302. global gameOver
  303. global randDone
  304. global random_array
  305. global user_array
  306. global round_no
  307. global check_one
  308. global check_two
  309. global check_three
  310. global check_four
  311. global check_five
  312. global choice
  313.  
  314. gameExit = False
  315. gameOver = False
  316. check_one = 0
  317. check_two = 0
  318. check_three = 0
  319. check_four = 0
  320. check_five = 0
  321. round_no = 1
  322. random_array = []
  323. user_array = []
  324. randDone = False
  325. choice = 0
  326. gameDisplay.fill(gray)
  327.  
  328.  
  329. while not gameExit:
  330. gameDisplay.fill(gray)
  331. pygame.draw.rect(gameDisplay, dr_gray, [s_x, s_y, display_side, display_side])
  332. message_to_screen("Round " + str(round_no), black, 9*display_width/10 + 5, display_height/2, 'medium')
  333. if not randDone:
  334. Button("Start", 30, 50, yellow, orange, black, 'rand_input')
  335. Button('Back', 30, display_height/2, yellow, orange, black, 'back')
  336. Button("Quit", 30, display_height - 40 - button_height, yellow, orange, black, 'quit')
  337.  
  338. #The events
  339. for event in pygame.event.get():
  340. if event.type == pygame.QUIT:
  341. pygame.quit()
  342. quit()
  343.  
  344. #The main working body
  345. first.draw()
  346. second.draw()
  347. third.draw()
  348. fourth.draw()
  349.  
  350. mouseGlow()
  351.  
  352. if user_array == random_array and random_array != []:
  353. user_array = []
  354. random_array = []
  355. round_no += 1
  356. randDone = False
  357.  
  358. if random_array != []:
  359. while check_three < len(user_array):
  360. if user_array[check_three] != random_array[check_three] and user_array[check_three] != 0:
  361. gameOver = True
  362. gameOver_func()
  363. check_three += 1
  364. check_three = 0
  365.  
  366. pygame.display.update()
  367. clock.tick(FPS)
  368.  
  369.  
  370.  
  371. start()
  372. pygame.quit()
  373. quit()
Add Comment
Please, Sign In to add comment