Advertisement
ZEdKasat

Memory Game

Feb 12th, 2022
233
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.     def show_number(self):
  14.         self.image = self.image2
  15.     def hide_number(self):
  16.         self.image = self.image1
  17.        
  18. pygame.init()
  19. window = pygame.display.set_mode((800, 600))
  20. font = pygame.font.Font('freesansbold.ttf', 50)
  21. run = True
  22.  
  23. clock = pygame.time.Clock()
  24. block_grp = pygame.sprite.Group()
  25. j = []
  26. limit = 3
  27. timer = 0
  28. score = 0
  29. speed = 0
  30.  
  31. for x in range(limit):
  32.         block = number(x+1)
  33.         block_grp.add(block)
  34.  
  35. temp = True
  36. while temp:
  37.     clock.tick(30)
  38.    
  39.     window.fill((255, 255, 255))
  40.    
  41.     for event in pygame.event.get():
  42.         if event.type == pygame.QUIT:
  43.             run = False
  44.             print("Quit")
  45.             temp = False
  46.         if event.type == pygame.MOUSEBUTTONDOWN:
  47.             print("MBDOWN")
  48.             mouseposition = pygame.mouse.get_pos()
  49.             for d in block_grp:
  50.                 if d.rect.collidepoint(mouseposition):
  51.                     speed = d.number
  52.                     print("Clicked Number")
  53.                     temp = False
  54.                     block_grp.empty()
  55.     window.blit(font.render(f'Select speed', True, (255, 150, 200)), (370, 10))
  56.     block_grp.update()
  57.     block_grp.draw(window)
  58.     pygame.display.update()
  59.  
  60. while run:
  61.     window.fill((255,255,255))
  62.     for x in range(limit):
  63.         block = number(x+1)
  64.         block_grp.add(block)
  65.     response = True
  66.     current_number = 1
  67.     while response:
  68.         clock.tick(30)
  69.         timer += 1
  70.         window.fill((255, 255, 255))
  71.         if timer > speed*30:
  72.             for x in block_grp:
  73.                 x.hide_number()
  74.         for event in pygame.event.get():
  75.             if event.type == pygame.QUIT:
  76.                 run = False
  77.                 response = False
  78.             if event.type == pygame.MOUSEBUTTONDOWN and timer > 150:
  79.                 mouseposition = pygame.mouse.get_pos()
  80.                 for d in block_grp:
  81.                     if d.rect.collidepoint(mouseposition):
  82.                         if d.number == current_number:
  83.                             d.kill()
  84.                             block_grp.remove(d)
  85.                             print(1)
  86.                             current_number += 1
  87.                         else:
  88.                             run = False
  89.                             response = False
  90.         if len(block_grp) == 0:
  91.             limit += 1
  92.             response = False
  93.             timer = 0
  94.             score += 1
  95.            
  96.         window.blit(font.render(f'{score}', True, (255, 150, 200)), (370, 10))
  97.         block_grp.update()
  98.         block_grp.draw(window)
  99.         pygame.display.update()
  100. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement