Advertisement
SamGauths

Memory Game in Python

May 21st, 2021
1,596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.92 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4. pygame.init()
  5. background_colour = (255,255,255)
  6. (width, height) = (640, 480)
  7. screen = pygame.display.set_mode((width, height))
  8. pygame.display.set_caption('The BGR Game')
  9. screen.fill(background_colour)
  10. pygame.display.flip()
  11. running = True
  12.  
  13. # COLORS
  14. BLUE = (0,0,255)
  15. LIGHT_BLUE = (65,105,225)
  16. GREEN = (0,255,0)
  17. LIGHT_GREEN = (0,204,102)
  18. RED = (255,0,0)
  19. LIGHT_RED = (228,113,122)
  20. BLACK = (0,0,0)
  21. WHITE = (255,255,255)
  22.  
  23. CENTER_WINDOW_H = 480/2
  24.  
  25. total_good = 0
  26. total_bad = 0
  27.  
  28. font1 = pygame.font.Font('tahoma.ttf', 32)
  29. start_text = font1.render('Press [SPACE] to start', True, BLACK, WHITE)
  30. good_text = font1.render("Good Answers: ", True, BLACK, WHITE)
  31. bad_text = font1.render("Bad Answers: ", True, BLACK, WHITE)
  32. start_text_rect = start_text.get_rect()
  33. good_text_rect = start_text.get_rect()
  34. bad_text_rect = start_text.get_rect()
  35. start_text_rect.center = (640 // 2, 350)
  36. good_text_rect.center = (640 // 2, 50)
  37. bad_text_rect.center = (640 // 2, 100)
  38.  
  39. click = False
  40. press_space = False
  41.  
  42. timeA = 0
  43. timeB = 0
  44.  
  45. counter = 0
  46.  
  47. stage = 1
  48. limit = 0
  49. good_clicks = 0
  50. num_click = 0
  51. random_num = 0
  52.  
  53. list1 = [1]
  54.  
  55.  
  56. while running:
  57.   for event in pygame.event.get():
  58.     if event.type == pygame.QUIT:
  59.       running = False
  60.     elif event.type == pygame.KEYDOWN:
  61.         if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
  62.             if stage == 1:
  63.               print("PRESS START!")
  64.               stage = 2
  65.     if event.type == pygame.MOUSEBUTTONUP:
  66.       if stage == 3:
  67.         if x > 640 / 4 - 25 and x < 640 / 4 + 25 and y > CENTER_WINDOW_H - 25 and y < CENTER_WINDOW_H + 25:
  68.           if list1[num_click] == 1:
  69.             print("GOOD")
  70.             total_good += 1
  71.           else:
  72.             print("BAD")
  73.             total_bad += 1
  74.           num_click += 1
  75.         if x > (640 / 4) * 2 - 25 and x < (640 / 4) * 2 + 25 and y > CENTER_WINDOW_H - 25 and y < CENTER_WINDOW_H + 25:
  76.           if list1[num_click] == 2:
  77.             print("GOOD")
  78.             total_good += 1
  79.           else:
  80.             print("BAD")
  81.             total_bad += 1
  82.           num_click += 1
  83.         if x > (640 / 4) * 3 - 25 and x < (640 / 4) * 3 + 25 and y > CENTER_WINDOW_H - 25 and y < CENTER_WINDOW_H + 25:
  84.           if list1[num_click] == 3:
  85.             print("GOOD")
  86.             total_good += 1
  87.           else:
  88.             print("BAD")
  89.             total_bad += 1
  90.           num_click += 1
  91.         if num_click == limit:
  92.           num_click = 0
  93.           click = False
  94.           stage = 1
  95.  
  96.  
  97.   # GET MOUSE POSITION
  98.   x, y = pygame.mouse.get_pos()
  99.   # print(x, y)
  100.  
  101.   # TIMER
  102.   timeA = pygame.time.get_ticks()
  103.   # print(timeA)
  104.  
  105.   # BACKGROUND
  106.   pygame.draw.rect(screen, WHITE, pygame.Rect(0, 0, 640, 480))
  107.  
  108.   if stage == 2:
  109.     if timeA - timeB < 1000:
  110.       pygame.draw.rect(screen, LIGHT_BLUE, pygame.Rect(640 / 4 - 25, CENTER_WINDOW_H - 25, 50, 50))
  111.       pygame.draw.rect(screen, LIGHT_GREEN, pygame.Rect((640/4) * 2 - 25, CENTER_WINDOW_H - 25, 50, 50))
  112.       pygame.draw.rect(screen, LIGHT_RED, pygame.Rect((640 / 4) * 3 - 25, CENTER_WINDOW_H - 25, 50, 50))
  113.     if timeA - timeB > 999 and timeA - timeB < 2000:
  114.       if list1[counter] == 1:
  115.         pygame.draw.rect(screen, BLUE, pygame.Rect(640 / 4 - 25, CENTER_WINDOW_H - 25, 50, 50))
  116.         pygame.draw.rect(screen, LIGHT_GREEN, pygame.Rect((640 / 4) * 2 - 25, CENTER_WINDOW_H - 25, 50, 50))
  117.         pygame.draw.rect(screen, LIGHT_RED, pygame.Rect((640 / 4) * 3 - 25, CENTER_WINDOW_H - 25, 50, 50))
  118.       if list1[counter] == 2:
  119.         pygame.draw.rect(screen, LIGHT_BLUE, pygame.Rect(640 / 4 - 25, CENTER_WINDOW_H - 25, 50, 50))
  120.         pygame.draw.rect(screen, GREEN, pygame.Rect((640/4) * 2 - 25, CENTER_WINDOW_H - 25, 50, 50))
  121.         pygame.draw.rect(screen, LIGHT_RED, pygame.Rect((640 / 4) * 3 - 25, CENTER_WINDOW_H - 25, 50, 50))
  122.       if list1[counter] == 3:
  123.         pygame.draw.rect(screen, LIGHT_BLUE, pygame.Rect(640 / 4 - 25, CENTER_WINDOW_H - 25, 50, 50))
  124.         pygame.draw.rect(screen, LIGHT_GREEN, pygame.Rect((640 / 4) * 2 - 25, CENTER_WINDOW_H - 25, 50, 50))
  125.         pygame.draw.rect(screen, RED, pygame.Rect((640 / 4) * 3 - 25, CENTER_WINDOW_H - 25, 50, 50))
  126.     if timeA - timeB > 1999:
  127.       counter += 1
  128.       if counter == limit:
  129.         random_num = random.randint(1, 3)
  130.         list1.append(random_num)
  131.       timeB = timeA
  132.       # print(counter)
  133.     if counter > limit:
  134.       print(list1)
  135.       stage = 3
  136.       click = True
  137.       counter = 0
  138.       limit += 1
  139.  
  140.   # UPDATE AND PRINT GOOD ANSWERS
  141.   good_text = font1.render("Good Answers: " + str(total_good), True, BLACK, GREEN)
  142.   screen.blit(good_text, good_text_rect)
  143.  
  144.   # UPDATE AND PRINT BAD ANSWERS
  145.   bad_text = font1.render("Bad Answers: " + str(total_bad), True, BLACK, RED)
  146.   screen.blit(bad_text, bad_text_rect)
  147.  
  148.   if stage == 1:
  149.     screen.blit(start_text, start_text_rect)
  150.     timeB = timeA
  151.  
  152.   # PLAYER'S TURN
  153.   if stage == 1 or stage == 3:
  154.     if x > 640/4 - 25 and x < 640/4 + 25 and y > CENTER_WINDOW_H-25 and y < CENTER_WINDOW_H + 25 and click == True:
  155.       pygame.draw.rect(screen, BLUE, pygame.Rect(640/4 - 25, CENTER_WINDOW_H-25, 50, 50))
  156.     else:
  157.       pygame.draw.rect(screen, LIGHT_BLUE, pygame.Rect(640 / 4 - 25, CENTER_WINDOW_H - 25, 50, 50))
  158.  
  159.     if x > (640/4)*2 - 25 and x < (640/4)*2 + 25 and y > CENTER_WINDOW_H - 25 and y < CENTER_WINDOW_H + 25 and click == True:
  160.       pygame.draw.rect(screen, GREEN, pygame.Rect((640/4) * 2 - 25, CENTER_WINDOW_H-25, 50, 50))
  161.     else:
  162.       pygame.draw.rect(screen, LIGHT_GREEN, pygame.Rect((640 / 4) * 2 - 25, CENTER_WINDOW_H - 25, 50, 50))
  163.  
  164.     if x > (640/4)*3 - 25 and x < (640/4)*3 + 25 and y > CENTER_WINDOW_H - 25 and y < CENTER_WINDOW_H + 25 and click == True:
  165.       pygame.draw.rect(screen, RED, pygame.Rect((640/4) * 3 - 25, CENTER_WINDOW_H-25, 50, 50))
  166.     else:
  167.       pygame.draw.rect(screen, LIGHT_RED, pygame.Rect((640 / 4) * 3 - 25, CENTER_WINDOW_H - 25, 50, 50))
  168.  
  169.   pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement