Advertisement
Daniel_leinaD

123456789

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