Advertisement
shh_algo_PY

Full Clicker Paint

Jul 16th, 2022
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.66 KB | None | 0 0
  1. import pygame
  2. import time
  3. pygame.init()
  4.  
  5. from random import randint
  6.  
  7. back = (200, 255, 255) #background color
  8. mw = pygame.display.set_mode((500, 500)) #main window
  9. mw.fill(back)
  10. clock = pygame.time.Clock()
  11.  
  12. class Area():
  13.     def __init__(self, x=0, y=0, width=10, height=10, color=None):
  14.         self.rect = pygame.Rect(x, y, width, height) #rectangle
  15.         self.fill_color = color
  16.     def color(self, new_color):
  17.         self.fill_color = new_color
  18.     def fill(self):
  19.         pygame.draw.rect(mw, self.fill_color, self.rect)
  20.     def outline(self, frame_color, thickness): #outline of an existing rectangle
  21.         pygame.draw.rect(mw, frame_color, self.rect, thickness)  
  22.     def collidepoint(self, x, y):
  23.         return self.rect.collidepoint(x, y)      
  24.  
  25. class Label(Area):
  26.     def set_text(self, text, fsize=12, text_color=(0, 0, 0)):
  27.         self.image = pygame.font.SysFont('verdana', fsize).render(text, True, text_color)
  28.     def draw(self, shift_x=0, shift_y=0):
  29.         self.fill()
  30.         mw.blit(self.image, (self.rect.x + shift_x, self.rect.y + shift_y))
  31.  
  32. RED = (255, 0, 0)
  33. GREEN = (0, 255, 51)
  34.  
  35. YELLOW = (255, 255, 0)
  36. DARK_BLUE = (0, 0, 100)
  37. BLUE = (80, 80, 255)
  38.  
  39. LIGHT_GREEN = (200, 255, 200)
  40. LIGHT_RED = (250, 128, 114)
  41.  
  42. cards = []
  43. num_cards = 4
  44. x = 70
  45.  
  46. start_time = time.time()
  47. current_time = start_time
  48.  
  49. time_text = Label(0,0,50,50,back)
  50. time_text.set_text('Time:',40, DARK_BLUE)
  51. time_text.draw(20, 20)
  52.  
  53. timer = Label(50,55,50,40,back)
  54. timer.set_text('0', 40, DARK_BLUE)
  55. timer.draw(0,0)
  56.  
  57. score_text = Label(380,0,50,50,back)
  58. score_text.set_text('Count:',45, DARK_BLUE)
  59. score_text.draw(20,20)
  60.  
  61. score = Label(430,55,50,40,back)
  62. score.set_text('0', 40, DARK_BLUE)
  63. score.draw(0,0)
  64.  
  65. for i in range(num_cards):
  66.     new_card = Label(x, 170, 70, 100, YELLOW)
  67.     new_card.outline(BLUE, 10)
  68.     new_card.set_text('CLICK', 26)
  69.     cards.append(new_card)
  70.     x = x + 100
  71.  
  72. wait = 0
  73. points = 0
  74.  
  75. while True:
  76.     if wait == 0:
  77.         wait = 20
  78.         click = randint(1, num_cards)
  79.         for i in range(num_cards):
  80.             cards[i].color(YELLOW)
  81.             if (i + 1) == click:
  82.                 cards[i].draw(10, 40)
  83.             else:
  84.                 cards[i].fill()
  85.     else:
  86.         wait -= 1
  87.    
  88.     for event in pygame.event.get():
  89.         if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
  90.             x, y = event.pos
  91.             for i in range(num_cards):
  92.                 if cards[i].collidepoint(x,y):
  93.                     if i + 1 == click:
  94.                         cards[i].color(GREEN)
  95.                         points += 1
  96.                     else:
  97.                         cards[i].color(RED)
  98.                         points -= 1
  99.                     cards[i].fill()
  100.                     score.set_text(str(points),40, DARK_BLUE)
  101.                     score.draw(0,0)
  102.  
  103.     new_time = time.time()
  104.    
  105.     if new_time - start_time  >= 11:
  106.         win = Label(0, 0, 500, 500, LIGHT_RED)
  107.         win.set_text("Time's up!!!", 60, DARK_BLUE)
  108.         win.draw(140, 180)
  109.         break
  110.    
  111.     if int(new_time) - int(current_time) == 1:
  112.         timer.set_text(str(int(new_time - start_time)),40, DARK_BLUE)
  113.         timer.draw(0,0)
  114.         current_time = new_time
  115.    
  116.     if points >= 5:
  117.         win = Label(0, 0, 500, 500, LIGHT_GREEN)
  118.         win.set_text("You won!!!", 60, DARK_BLUE)
  119.         win.draw(140, 180)
  120.         result_time = Label(90, 230, 250, 250, LIGHT_GREEN)
  121.         result_time.set_text("Completion time: " + str (int(new_time - start_time)) + " sec", 40, DARK_BLUE)
  122.    
  123.         result_time.draw(0, 0)
  124.    
  125.         break
  126.  
  127.     pygame.display.update()
  128.     clock.tick(40)
  129.  
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement