Zum Inhalt springen

Creating Button Combination Sequences in Scratch (Konami Code Style)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

💡 Want to add secret codes and advanced input systems to your games? 🚀 Get Advanced Help

IM

InputMaster_Ryan

Posted on January 25, 2024 • Intermediate

🎮 Need Help with Button Combination Sequences

Hey everyone! I’m trying to implement a button combination system like the famous Konami Code in my game. The idea is:

  • ⬆️ Player presses UP arrow
  • ⏱️ System waits for next input (with timeout)
  • ⬆️ Player presses UP arrow again
  • ⏱️ Wait for next input…
  • 🎉 If sequence is completed correctly, trigger special effect!

I want to create a system where:

  • 🔄 Each button press advances to the next step in the sequence
  • ⏰ There’s a timeout period for each input
  • ❌ Wrong input resets the sequence
  • ✅ Completing the sequence triggers a cheat code or special feature

How do I implement this kind of sequential input detection? Any help would be amazing! 🙏

SC

SequenceCoder_Maya

Replied 1 hour later • ⭐ Best Answer

Awesome question @InputMaster_Ryan! Button combination sequences are super fun to implement. Let me show you how to build a robust system that can handle any sequence you want:

🏗️ System Architecture

Here’s how a proper sequence detection system works:

flowchart TD A[🎮 Key Pressed] --> B{Is Expected Key?} B -->|Yes| C[Advance Sequence] B -->|No| D[Reset Sequence] C --> E{Sequence Complete?} E -->|Yes| F[🎉 Trigger Effect] E -->|No| G[Wait for Next Input] G --> H{Timeout?} H -->|Yes| D H -->|No| A D --> I[Start Over] I --> A F --> J[Reset for Next Use] J --> I style A fill:#e1f5fe style F fill:#e8f5e8 style D fill:#ffebee style C fill:#f3e5f5

🎯 Step 1: Setup Variables and Sequence

First, let’s set up the core system variables:

    // Initialize the sequence system
when flag clicked
set [Sequence Step v] to [1]
set [Input Timer v] to [0]
set [Max Wait Time v] to [180]  // 3 seconds at 60 FPS
set [Sequence Active v] to [true]

// Define the Konami Code sequence
set [Step 1 v] to [up arrow]
set [Step 2 v] to [up arrow]
set [Step 3 v] to [down arrow]
set [Step 4 v] to [down arrow]
set [Step 5 v] to [left arrow]
set [Step 6 v] to [right arrow]
set [Step 7 v] to [left arrow]
set [Step 8 v] to [right arrow]
set [Total Steps v] to [8]
  

⌨️ Step 2: Input Detection System

Create a system that detects key presses and validates them:

    // Main input detection loop
when flag clicked
forever
if <(Sequence Active) = [true]> then
// Check for the expected key
set [Expected Key v] to (item (Sequence Step) of [Sequence v])

if <key (Expected Key) pressed?> then
// Correct key pressed!
change [Sequence Step v] by [1]
set [Input Timer v] to [0]

// Visual feedback
play sound [beep v]
say (join [Step ] (join ((Sequence Step) - [1]) [ correct!])) for (0.5) seconds

// Check if sequence is complete
if <(Sequence Step) > (Total Steps)> then
broadcast [sequence complete v]
set [Sequence Step v] to [1]
set [Sequence Active v] to [false]
wait (2) seconds
set [Sequence Active v] to [true]
end
else
// Check for wrong key press
if <<key [up arrow v] pressed?> or <<key [down arrow v] pressed?> or <<key [left arrow v] pressed?> or <key [right arrow v] pressed?>>>> then
// Wrong key pressed - reset sequence
set [Sequence Step v] to [1]
set [Input Timer v] to [0]
play sound [buzz v]
say [Wrong key! Sequence reset.] for (1) seconds
end
end

// Handle timeout
change [Input Timer v] by [1]
if <(Input Timer) > (Max Wait Time)> then
set [Sequence Step v] to [1]
set [Input Timer v] to [0]
say [Timeout! Sequence reset.] for (1) seconds
end
end
end
  

🎉 Step 3: Sequence Completion Handler

Handle what happens when the sequence is successfully completed:

    // Handle sequence completion
when I receive [sequence complete v]
play sound [success v]
say [KONAMI CODE ACTIVATED!] for (2) seconds

// Add your cheat code effects here:
change [Lives v] by [30]  // Extra lives
change [Score v] by [10000]  // Bonus points
set [Invincible v] to [true]  // Temporary invincibility
wait (10) seconds
set [Invincible v] to [false]

// Visual effects
repeat (10)
change [color v] effect by (25)
wait (0.1) seconds
end
clear graphic effects
  

🔧 Step 4: Advanced Features

Multiple Sequences:

    // Support for multiple different sequences
define check sequence (sequence name)
if <(sequence name) = [konami]> then
set [Active Sequence v] to [konami]
set [Total Steps v] to [8]
end
if <(sequence name) = [debug]> then
set [Active Sequence v] to [debug]
set [Total Steps v] to [4]
// Debug sequence: up, down, left, right
end
if <(sequence name) = [powerup]> then
set [Active Sequence v] to [powerup]
set [Total Steps v] to [6]
// Power-up sequence: left, right, left, right, up, down
end
  

Visual Progress Indicator:

    // Show sequence progress
when flag clicked
forever
if <(Sequence Active) = [true]> then
set [Progress v] to (join [Sequence: ] (join ((Sequence Step) - [1]) (join [/] (Total Steps))))

// Show expected next key
if <(Sequence Step) ≤ (Total Steps)> then
set [Next Key v] to (item (Sequence Step) of [Sequence v])
set [Hint v] to (join [Next: ] (Next Key))
end
else
set [Progress v] to []
set [Hint v] to []
end
end
  

Difficulty Levels:

    // Adjustable difficulty
define set difficulty (level)
if <(level) = [easy]> then
set [Max Wait Time v] to [300]  // 5 seconds
end
if <(level) = [normal]> then
set [Max Wait Time v] to [180]  // 3 seconds
end
if <(level) = [hard]> then
set [Max Wait Time v] to [120]  // 2 seconds
end
if <(level) = [expert]> then
set [Max Wait Time v] to [60]   // 1 second
end
  

🎮 Step 5: Custom Sequence Builder

Allow players to create their own sequences:

    // Custom sequence recorder
when [r v] key pressed
if <(Recording Mode) = [true]> then
set [Recording Mode v] to [false]
say [Recording stopped. Sequence saved!] for (2) seconds
else
set [Recording Mode v] to [true]
delete all of [Custom Sequence v]
say [Recording sequence... Press R again to stop.] for (2) seconds
end

// Record key presses
when flag clicked
forever
if <(Recording Mode) = [true]> then
if <key [up arrow v] pressed?> then
add [up arrow] to [Custom Sequence v]
wait until <not <key [up arrow v] pressed?>>
end
if <key [down arrow v] pressed?> then
add [down arrow] to [Custom Sequence v]
wait until <not <key [down arrow v] pressed?>>
end
// Add more keys as needed...
end
end
  

🛠️ Implementation Tips

  1. 🎯 Start Simple: Begin with a short 3-4 key sequence
  2. Timing is Key: Adjust timeout based on your game’s pace
  3. 🔊 Feedback: Always provide audio/visual feedback for each step
  4. 🧪 Test Thoroughly: Try different timing and wrong key scenarios
  5. 🎨 Polish: Add particle effects and satisfying completion rewards

Pro Tips:

  • 💡 Use lists to store sequences for easy modification
  • 💡 Consider allowing partial resets (wrong key doesn’t reset completely)
  • 💡 Add easter eggs with different sequences for different effects
  • 💡 Make sequences discoverable through hints or documentation

The key insight is treating the sequence as a state machine where each correct input advances the state! 🎯

IM

InputMaster_Ryan

Replied 2 hours later

@SequenceCoder_Maya This is absolutely perfect! 🤩 Thank you so much!

I love how you broke it down into a state machine - that makes so much sense! The timeout system and visual feedback are exactly what I needed. I’m implementing this right now and it’s working beautifully!

Quick question: How would I modify this to work with mouse clicks or custom sprites instead of just arrow keys?

SC

SequenceCoder_Maya

Replied 30 minutes later

@InputMaster_Ryan Great question! For mouse clicks and custom sprites, you can adapt the system like this:

    // For clickable sprites (create one for each button)
when this sprite clicked
if <(Expected Input) = [button1]> then
broadcast [correct input v]
else
broadcast [wrong input v]
end

// For mouse position-based sequences
when flag clicked
forever
if <(Expected Input) = [top area]> then
if <<mouse down?> and <(mouse y) > [100]>> then
broadcast [correct input v]
end
end
end
  

You can even create gesture-based sequences by tracking mouse movement patterns! 🎨

VB

Vibelf_Community

Pinned Message • Moderator

🚀 Master Advanced Input Systems!

Button combinations are just the beginning! Our expert tutors can help you create:

  • 🎮 Complex gesture recognition systems
  • 🎯 Multi-input combo systems for fighting games
  • 🔐 Secure password and unlock mechanisms
  • ⚡ Real-time input processing and optimization

📚 Related Topics

Ready to create professional input systems? Get personalized guidance from our programming experts!