Advertisement
shh_algo_PY

Clicker Game - Part 2

Jul 16th, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 KB | None | 0 0
  1. import pygame
  2. import time
  3. pygame.init()
  4.  
  5. from random import randint
  6.  
  7. '''create programme window'''
  8.  
  9. back = (200, 255, 255) #background color
  10. mw = pygame.display.set_mode((500, 500)) #main window
  11. mw.fill(back)
  12. clock = pygame.time.Clock()
  13.  
  14. '''rectangle class'''
  15.  
  16. class Area():
  17.     def __init__(self, x=0, y=0, width=10, height=10, color=None):
  18.         self.rect = pygame.Rect(x, y, width, height) #rectangle
  19.         self.fill_color = color
  20.  
  21.     def color(self, new_color):
  22.         self.fill_color = new_color
  23.  
  24.     def fill(self):
  25.         pygame.draw.rect(mw, self.fill_color, self.rect)
  26.  
  27.     def outline(self, frame_color, thickness): #outline of an existing rectangle
  28.         pygame.draw.rect(mw, frame_color, self.rect, thickness)    
  29.  
  30.     #---------------------------------------#
  31.     def collidepoint(self, x, y):
  32.         return self.rect.collidepoint(x,y)
  33.     #---------------------------------------#
  34.  
  35. '''class label'''
  36.  
  37. class Label(Area):
  38.     def set_text(self, text, fsize=12, text_color=(0, 0, 0)):
  39.         self.image = pygame.font.SysFont('comic sans ms', fsize).render(text, True, text_color)
  40.  
  41.     def draw(self, shift_x=0, shift_y=0):
  42.         self.fill()
  43.         mw.blit(self.image, (self.rect.x + shift_x, self.rect.y + shift_y))
  44.  
  45. #---------------------------------------#
  46. RED = (255, 0, 0)
  47. GREEN = (0, 255, 51)
  48. #---------------------------------------#
  49.  
  50. YELLOW = (255, 255, 0)
  51. DARK_BLUE = (0, 0, 100)
  52. BLUE = (80, 80, 255)
  53.  
  54. cards = []
  55. num_cards = 4
  56.  
  57. x = 70
  58.  
  59. for i in range(num_cards):
  60.     new_card = Label(x, 200, 70, 100, YELLOW) # x, y, width, height, colour
  61.     new_card.outline(BLUE, 10)
  62.     new_card.set_text('CLICK', 26)
  63.     cards.append(new_card)
  64.     x = x + 100 # Spacing between each card
  65.  
  66. #---------------------------------------#
  67. wait=0    #ADD THIS!
  68. #---------------------------------------#
  69.  
  70. while True:
  71.     if wait == 0:
  72.         wait = 20       # How long the word click will stay on-screen
  73.        
  74.         click = randint(1, num_cards)   # YOU HAVE THIS!
  75.         for i in range(num_cards):
  76.             cards[i].color(YELLOW)
  77.             if (i + 1) == click:
  78.                 cards[i].draw(10, 40)
  79.             else:
  80.                 cards[i].fill()
  81.  
  82.     else:               # ADD THIS ELSE!
  83.         wait -= 1
  84.  
  85.     #checking the click on each tick:
  86.     for event in pygame.event.get():
  87.         if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
  88.             x, y = event.pos
  89.             for i in range(num_cards):
  90.                 if cards[i].collidepoint(x,y):
  91.                     if i + 1 == click:
  92.                         cards[i].color(GREEN)
  93.                     else:
  94.                         cards[i].color(RED)
  95.  
  96.                     cards[i].fill()
  97.  
  98.     pygame.display.update()
  99.     clock.tick(40)                
  100.  
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement