Untitled

 avatar
unknown
plain_text
10 months ago
3.6 kB
20
Indexable
import pygame
import time
from random import randint
pygame.init()

back = (200, 255, 255) #RGB
mw = pygame.display.set_mode((500, 500))

RED = (255, 0, 0)
GREEN = (0, 255, 51)
BlUE = (0, 0, 255)
ORANGE = (255, 123, 0)
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
LIGHT_GREEN = (200, 255, 200)
LIGHT_RED = (250, 128, 114)
BLACK = (0, 0, 0)
DARK_BLUE = (0, 0, 100)
LIGHT_BLUE = (80, 80, 255)
#
clock = pygame.time.Clock()

class Area():
    def __init__(self, x, y, width, height, new_color):
        self.rect = pygame.Rect( x, y, width, height)
        self.fill_color = new_color

    def color(self, new_color):
        self.fill_color = new_color

    def fill(self):
        pygame.draw.rect(mw, self.fill_color, self.rect)
    
    def outline(self, frame_color = DARK_BLUE, thickness = 5):
        pygame.draw.rect(mw, frame_color, self.rect, thickness )

    def collidepoint(self, x, y):
        return self.rect.collidepoint(x, y)
    
class Label(Area):
    def set_text(self, text, fsize = 40, text_color = (0, 0, 0)):
        self.image = pygame.font.SysFont("Impact", fsize).render(text, True, text_color)
    def draw(self, shift_x, shift_y):
        self.fill()
        mw.blit(self.image, (self.rect.x + shift_x, self.rect.y+ shift_y))



CARD_WIDTH = 70
CARD_HEIGHT = 100


cards = []
num_cards = 5

x = 70
mw.fill(back)
#починаємо заповнення списку карток
for i in range (num_cards):
    card = Label(x, 180, CARD_WIDTH, CARD_HEIGHT, YELLOW)
    card.set_text("click")
    card.outline()
    cards.append(card)
    x = x + CARD_WIDTH + 15


wait = 0

#таймер

start_time = time.time() # скільки зараз секунд пройшло взагалом
cur_time = start_time # яка секунда програми
new_time = 0 # чи пройшла 1 секунда

timer_text = Label(420, 2, 120, 30, back)
timer_text.set_text("TIMER:")

timer = Label(420, 40, 120, 50, back)
timer.set_text("0")

# рахунок
points = 0

score_text = Label(0, 2, 120, 30, back)
score_text.set_text("SCORE:")

score = Label(0, 40, 120, 30 , back)
score.set_text('0')

while True:

    if wait == 0:
        wait = 70
        
        click = randint(0, num_cards - 1)
        for i in range (num_cards):
            cards[i].color(YELLOW)
            if i == click: 
                cards[i].draw(15,40)
            else:
                cards[i].fill()
    else:
        wait -= 1

    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            x, y = event.pos
            for i in range (num_cards):
                if cards[i].collidepoint(x, y):
                    if i == click:
                        cards[i].color(GREEN)
                        points += 1
                        score.set_text(str(points))
                    else:
                        cards[i].color(RED)
                        points -= 1
                        score.set_text(str(points))

                    
                    cards[i].fill()
                    if i == click:
                        cards[i].draw(15,40)
     
    new_time = time.time()
    if int(new_time) - int(cur_time) >= 1:
        timer.set_text(str(int(new_time - start_time)))
        cur_time = new_time

    timer_text.draw(0,0)
    timer.draw(0,0)

    score.draw(0,0)
    score_text.draw(0,0)

    pygame.display.update()
    clock.tick(60)
Editor is loading...
Leave a Comment