Advertisement
ZEdKasat

Memory Game

Dec 5th, 2021
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 KB | None | 0 0
  1. import pygame, random
  2.  
  3. class number(pygame.sprite.Sprite):
  4.     def __init__(self, numeral):
  5.         super().__init__()
  6.         self.image1 = pygame.Surface((50, 50))
  7.         self.image1.fill((255, 255, 100))
  8.         self.image2 = font.render(f'{numeral}', True, (200, 200, 200))
  9.         self.image = self.image2
  10.         self.rect = self.image.get_rect()
  11.         self.number = numeral
  12.         self.rect.center = (random.randrange(50 ,750), random.randrange(50 ,550))
  13.  
  14.     def show_number(self):
  15.         self.image = self.image2
  16.  
  17.     def hide_number(self):
  18.         self.image = self.image1
  19.        
  20. pygame.init()
  21. window = pygame.display.set_mode((800, 600))
  22. font = pygame.font.Font('freesansbold.ttf', 50)
  23. run = True
  24.  
  25. clock = pygame.time.Clock()
  26. block_grp = pygame.sprite.Group()
  27. limit = 3
  28. timer = 0
  29. score = 0
  30. speed = 0
  31.  
  32. for x in range(limit):
  33.     block = number(x+1)
  34.     block_grp.add(block)
  35.  
  36. temp = True
  37. while temp:
  38.     clock.tick(30)
  39.    
  40.     window.fill((255, 255, 255))
  41.    
  42.     for event in pygame.event.get():
  43.         if event.type == pygame.QUIT:
  44.             run = False
  45.             print("Quit")
  46.             temp = False
  47.    
  48.         if event.type == pygame.MOUSEBUTTONDOWN:
  49.             print("MBDOWN")
  50.             mouseposition = pygame.mouse.get_pos()
  51.             for d in block_grp:
  52.                 if d.rect.collidepoint(mouseposition):
  53.                     speed = d.number
  54.                     print("Clicked Number")
  55.                     temp = False
  56.                     block_grp.empty()
  57.  
  58.     window.blit(font.render(f'Select speed', True, (255, 150, 200)), (370, 10))
  59.     block_grp.update()
  60.     block_grp.draw(window)
  61.     pygame.display.update()
  62.  
  63. while run:
  64.     window.fill((255,255,255))
  65.     for x in range(limit):
  66.         block = number(x+1)
  67.         block_grp.add(block)
  68.  
  69.     response = True
  70.     current_number = 1
  71.     while response:
  72.         clock.tick(30)
  73.         timer += 1
  74.         window.fill((255, 255, 255))
  75.         if timer > speed*30:
  76.             for x in block_grp:
  77.                 x.hide_number()
  78.         for event in pygame.event.get():
  79.             if event.type == pygame.QUIT:
  80.                 run = False
  81.                 response = False
  82.             if event.type == pygame.MOUSEBUTTONDOWN and timer > 150:
  83.                 mouseposition = pygame.mouse.get_pos()
  84.                 for d in block_grp:
  85.                     if d.rect.collidepoint(mouseposition):
  86.                         if d.number == current_number:
  87.                             d.kill()
  88.                             block_grp.remove(d)
  89.                             print(1)
  90.                             current_number += 1
  91.                         else:
  92.                             run = False
  93.                             response = False
  94.         if len(block_grp) == 0:
  95.             limit += 1
  96.             response = False
  97.             timer = 0
  98.             score += 1
  99.            
  100.         window.blit(font.render(f'{score}', True, (255, 150, 200)), (370, 10))
  101.         block_grp.update()
  102.         block_grp.draw(window)
  103.         pygame.display.update()
  104. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement