コンテンツにスキップ

How to create a timer in Scratch

このコンテンツはまだ日本語訳がありません。

💡 Having trouble with Scratch block assembly? Don’t know how to implement code logic? 🚀 Get Help Now

TM

TimerMaster_Dev

Posted on August 5, 2024 • Beginner

⏰ Need help creating a timer

Hey everyone! I’m working on a project and I need to add a timer but I can’t remember how to make one properly. Could someone help me out?

I want to create a timer that can count down from a specific time and also maybe a stopwatch that counts up. Any help would be really appreciated! 😊

SC

ScratchCoder_Pro

Replied 1 hour later • ⭐ Best Answer

Great question @TimerMaster_Dev! Timers are super useful in many projects. Here are several different types of timers you can create:

⏰ Timer System Architecture

Here’s how different types of timers work in Scratch:

flowchart TD A[🚩 Game Start] --> B[📊 Choose Timer Type] B --> C{⏰ Timer Type?} C -->|Countdown| D[⏳ Countdown Timer] C -->|Stopwatch| E[⏱️ Stopwatch Timer] C -->|Game Timer| F[🎮 Game Timer] D --> G[📊 Set Start Time] G --> H[🔄 Countdown Loop] H --> I[⏱️ Wait 1 Second] I --> J[📉 Decrease Timer] J --> K{⏰ Timer = 0?} K -->|No| L[📺 Update Display] K -->|Yes| M[🚨 Timer Finished] L --> H M --> N[🔊 Play Alert Sound] N --> O[🎯 Trigger Event] E --> P[📊 Set Timer to 0] P --> Q[🔄 Stopwatch Loop] Q --> R[⏱️ Wait 1 Second] R --> S[📈 Increase Timer] S --> T[📺 Update Display] T --> U{⏹️ Stop Pressed?} U -->|No| Q U -->|Yes| V[⏸️ Pause Timer] F --> W[📊 Set Game Duration] W --> X[🔄 Game Timer Loop] X --> Y[⏱️ Wait 1 Second] Y --> Z[📉 Decrease Time] Z --> AA[📺 Update Display] AA --> BB{⏰ Time Up?} BB -->|No| CC{🎮 Game Over?} BB -->|Yes| DD[🏁 End Game] CC -->|No| X CC -->|Yes| EE[🛑 Stop Timer] subgraph "Timer Variables" FF["⏰ Timer Value"] GG["⏱️ Start Time"] HH["🔄 Is Running"] II["📺 Display Format"] JJ["🎯 Timer Events"] end subgraph "Timer Types" KK["⏳ Countdown (60→0)"] LL["⏱️ Stopwatch (0→∞)"] MM["🎮 Game Timer"] NN["⏰ Real-time Clock"] end style A fill:#e1f5fe style M fill:#ffcdd2 style V fill:#fff3e0 style DD fill:#f3e5f5 style O fill:#c8e6c9

⏱️ Method 1: Simple Countdown Timer

This is the most basic timer that counts down from a starting value:

    when flag clicked
set [Timer v] to [60] // Start with 60 seconds
repeat until <(Timer) = [0]>
wait [1] seconds
change [Timer v] by [-1]
end
broadcast [Time Up! v]
  

🕐 Method 2: Stopwatch (Count Up Timer)

This timer counts up from zero:

    when flag clicked
set [Stopwatch v] to [0]
forever
wait [1] seconds
change [Stopwatch v] by [1]
end
  

⏰ Method 3: Formatted Timer Display (MM:SS)

This creates a nice-looking timer in minutes and seconds format:

    when flag clicked
set [Total Seconds v] to [300] // 5 minutes
forever
set [Minutes v] to ([floor v] of ((Total Seconds) / [60]))
set [Seconds v] to ((Total Seconds) mod [60])
if <(Seconds) < [10]> then
set [Timer Display v] to (join (join (Minutes) [:0]) (Seconds))
else
set [Timer Display v] to (join (join (Minutes) [:]) (Seconds))
end
wait [1] seconds
if <(Total Seconds) > [0]> then
change [Total Seconds v] by [-1]
end
end
  

🎮 Method 4: Game Timer with Pause Function

This timer can be paused and resumed:

    when flag clicked
set [Timer v] to [120]
set [Timer Running v] to [true]
forever
if <(Timer Running) = [true]> then
wait [1] seconds
change [Timer v] by [-1]
if <(Timer) = [0]> then
broadcast [Game Over v]
set [Timer Running v] to [false]
end
end
end

// To pause/unpause the timer
when [space v] key pressed
if <(Timer Running) = [true]> then
set [Timer Running v] to [false]
else
set [Timer Running v] to [true]
end
  

🚀 Method 5: Precise Timer (Using Built-in Timer)

For more accuracy, you can use Scratch’s built-in timer:

    when flag clicked
reset timer
set [Start Time v] to [60] // 60 seconds
forever
set [Time Left v] to ((Start Time) - (timer))
if <(Time Left) <= [0]> then
set [Time Left v] to [0]
broadcast [Timer Finished v]
stop [this script v]
end
end
  

Hope this helps! Each method has its own advantages depending on what you need. Let me know if you want me to explain any part in more detail! 😊

TM

TimerMaster_Dev

Replied 30 minutes later

@ScratchCoder_Pro This is exactly what I needed! Thank you so much! 🎉

The formatted timer display is perfect for my project. One quick question - how can I make the timer display look more visually appealing on the stage?

UI

UIDesigner_Sarah

Replied 45 minutes later

@TimerMaster_Dev Great question! Here are some ways to make your timer look more professional:

    // Create a timer display sprite with custom costumes
when flag clicked
forever
switch costume to (Timer Display)
set size to [150] %
go to x: [0] y: [150]
// Add shadow effect
create clone of [myself v]
end

when I start as a clone
change [ghost v] effect by [50]
change x by [3]
change y by [-3]
go to [back v] layer
  

You can also create custom number sprites with different fonts and colors to make it look even better! ✨

VB

Vibelf_Community

Pinned Message • Moderator

🚀 Want to Master Advanced Timer Techniques?

Excellent discussion everyone! For those looking to create even more sophisticated timing systems, our community can help you implement:

  • ⏰ Multiple simultaneous timers
  • 🎯 Event-triggered timers
  • 📊 Performance timing and optimization
  • 🎮 Complex game timing mechanics

📚 Related Discussions

Ready to take your timing skills to the next level? Get personalized guidance from our expert tutors in the Vibelf app!