Skip to content
Tin học trung học phổ thông
    Menu
    • KHỐI 10
    • KHỐI 11
    • KHỐI 12
    • LẬP TRÌNH
    • ÔN THI TỐT NGHIỆP
    • ĐỀ THI THỬ TỐT NGHIỆP
    Menu

    Hướng dẫn lập trình Python – Đồng hồ đếm ngược

    Posted on Tháng 9 24, 2024

    Hướng dẫn chi tiết cách tạo phần mềm ứng dụng đếm ngược thời gian bằng ngôn ngữ lập trình Python.

    Link tải audio sử dụng trong chương trình: Click here

    Sau đây là code chương trình:

    import pygame

    import time

    import math

    pygame.init()

    screen=pygame.display.set_mode((500,600))

    pygame.display.set_caption(“Countdown”)

    GRAY=(169,169,169)

    AQUA=(0,255,255)

    BLACK=(0,0,0)

    RED=(255,0,0)

    font=pygame.font.SysFont(‘sans’,50)

    font1=pygame.font.SysFont(‘sans’,100)

    b1=font.render(‘+’,True,BLACK)

    b2=font.render(‘-‘,True,BLACK)

    b3=font.render(‘Min’,True,BLACK)

    b4=font.render(‘Sec’,True,BLACK)

    b5=font.render(‘Start’,True,BLACK)

    b6=font.render(‘Reset’,True,BLACK)

    clock=pygame.time.Clock()

    mins=0

    secs=0

    total_secs=0

    total=0

    start=False

    sound1=pygame.mixer.Sound(‘tick.wav’)

    sound2=pygame.mixer.Sound(‘timeout.wav’)

    running=True

    while running:

                clock.tick(60)

                screen.fill(GRAY)

                mouse_x,mouse_y=pygame.mouse.get_pos()

                # Vẽ các hình chữ nhật

                pygame.draw.rect(screen,AQUA,(100,50,50,50))

                pygame.draw.rect(screen,AQUA,(100,130,50,50))

                pygame.draw.rect(screen,AQUA,(350,50,50,50))

                pygame.draw.rect(screen,AQUA,(350,130,50,50))

                pygame.draw.rect(screen,AQUA,(50,200,170,50))

                pygame.draw.rect(screen,AQUA,(280,200,170,50))

                pygame.draw.rect(screen,BLACK,(45,495,410,60))

                pygame.draw.rect(screen,AQUA,(50,500,400,50))

                #Vẽ Min

                screen.blit(b3,(10,45))

                screen.blit(b3,(420,45))

                #Vẽ Sec

                screen.blit(b4,(10,125))

                screen.blit(b4,(420,125))

                #Vẽ dấu cộng

                screen.blit(b1,(110,45))

                screen.blit(b1,(110,125))

                #Vẽ dấu trừ

                screen.blit(b2,(365,45))

                screen.blit(b2,(365,125))

                # Vẽ nút Start, Reset

                screen.blit(b5,(90,200))

                screen.blit(b6,(320,200))

                # Vẽ đồng hồ

                pygame.draw.circle(screen,BLACK,(250,380),100)

                pygame.draw.circle(screen,AQUA,(250,380),98)

                pygame.draw.circle(screen,BLACK,(250,380),5)

                for event in pygame.event.get():

                            if event.type==pygame.QUIT:

                                        running=False

                            if event.type==pygame.MOUSEBUTTONDOWN:

                                        if event.button==1:

                                                    if (100<mouse_x<150) and (50<mouse_y<100):

                                                                total_secs+=60

                                                                total=total_secs

                                                    if (100<mouse_x<150) and (130<mouse_y<180):

                                                                total_secs+=1

                                                                total=total_secs

                                                    if (350<mouse_x<400) and (50<mouse_y<100):

                                                                total_secs-=60

                                                                total=total_secs

                                                    if (350<mouse_x<400) and (130<mouse_y<180):

                                                                total_secs-=1

                                                                total=total_secs

                                                    if (50<mouse_x<220) and (200<mouse_y<250):

                                                                total=total_secs

                                                                start=True

                                                    if (280<mouse_x<450) and (200<mouse_y<250):

                                                                total_secs=0

                #Ghi lệnh cho nút start

                if start:

                            if total_secs>0:

                                        pygame.mixer.Sound.play(sound1)

                                        total_secs-=1

                                        time.sleep(1)

                            else:

                                        pygame.mixer.Sound.play(sound2)

                                        start=False

                #Vẽ Thời gian hiện tại

                mins=total_secs//60

                secs=total_secs-(mins*60)

                text_time=font1.render(str(mins)+’:’+str(secs),True,RED)

                screen.blit(text_time,(180,50))

                # Vẽ kim giây chạy

                x_sec=250+90*math.sin(6*secs*math.pi/180)

                y_sec=380-90*math.cos(6*secs*math.pi/180)

                pygame.draw.line(screen,RED,(250,380),(int(x_sec),int(y_sec)))

                # Vẽ kim phút chạy

                x_min=250+50*math.sin(6*mins*math.pi/180)

                y_min=380-50*math.cos(6*mins*math.pi/180)

                pygame.draw.line(screen,BLACK,(250,380),(int(x_min),int(y_min)))

               # Vẽ hình chữ nhật đỏ để giảm dần theo thời gian

                if total!=0:

                            pygame.draw.rect(screen,RED,(50,500,int(400*(total_secs/total)),50))

                pygame.display.flip()

    pygame.quit()

    1 thought on “Hướng dẫn lập trình Python – Đồng hồ đếm ngược”

    1. Linh viết:
      Tháng 10 26, 2024 lúc 2:29 sáng

      Cái này hay nè

      Bình luận

    Để lại một bình luận Hủy

    Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

    Thống kê

    • 0
    • 20
    • 17.097

    Bài viết gần đây

    • Đề thi thử tốt nghiệp THPT môn Tin học số 4.
    • Đề thi thử tốt nghiệp THPT môn Tin học số 3
    • Đề thi thử tốt nghiệp THPT môn Tin học số 2
    • Đề thi thử tốt nghiệp THPT môn Tin học số 1
    • Chủ đề ICT 2. Sử dụng phần mềm tạo trang web.

    Bình luận gần đây

    1. Bình An trong Hướng dẫn lập trình Python – Game Flappy bird
    2. Hùng trong Hướng dẫn lập trình Python – Game Hứng Táo
    3. Linh trong Hướng dẫn lập trình Python – Đồng hồ đếm ngược

    Thời gian của mỗi chúng ta là có hạn. Vì thế đừng dùng nó để sống cuộc đời của một ai khác - Steve Jobs

    Công nghệ là thứ gần gũi nhất với ma thuật còn tồn tại trên thế giới này - Elon Musk

    Liên hệ sdt 0374624871
    ltnga.c3dahuoai@gmail.com

    • YouTube
    • Facebook

    Không bao giờ là quá muộn để bạn bắt đầu giấc mơ của mình.

      ©2025 Tin học trung học phổ thông | Design: Newspaperly WordPress Theme