My non-serious entry for Simon Says.
Based on literal interpretation of the rules:
Create a UI with four colored buttons that light up in a random pattern. After displaying the pattern, the player must repeat the pattern by clicking the buttons in proper order. The pattern gets longer each time the player completes the pattern. If the player presses a wrong button, the game ends.
Basically:
- Must have UI
- 4 colored buttons
- that light up
- in a random pattern
- player repeats the pattern
- input: clicking
- when player completes pattern, pattern is made longer
- when player pushes wrong button, game ends.
This means that, as you'll see:
- I didn't add different colors
- There's no player feedback for clicking
- The game ends by having the buttons disappear
Perfect for prototyping.
I restrained myself from programming a version that did not re-display the pattern, since it's not stated in the requirements (But it would make a rather boring game if I did).
I wrote this quickly. I wrote this without an IDE.
My goal:
Learning how to prototype better. I'm interested in finding ways to test game mechanics faster. That means I don't want to write much code and I'm not interested in polishing it up nice. I just want it to work so I can test whether it's any fun. If it is, I can rebuild a clean version. For the answers, this means I'm looking for how I could alter the way I write code from the start, rather than refining it pass by pass.
The code:
package
{
import flash.display.Sprite;
import flash.events.*;
public class Main extends Sprite {
public var playerTurn:Boolean = false;
private var pattern:Array = new Array();
private var patternIndex:uint = 0;
private var playerPattern:Array = new Array();
private var blinkCounter:uint = 0;
private var buttons:Array = new Array();
private var graphicSprite:Sprite = new Sprite();
public function Main(){
if(stage)init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
for(var i:uint = 0; i < 4; i++){
buttons[i] = new SimonButton(this, i);
}
addChild(graphicSprite);
addPattern();
}
public function addPattern():void {
playerTurn = false;
pattern.push(Math.floor(Math.random()*4));
patternIndex = 0;
addEventListener(Event.ENTER_FRAME, displayPattern);
}
public function displayPattern(e:Event = null):void {
graphicSprite.graphics.clear();
graphicSprite.graphics.beginFill(0xFFFFFF, 0.9);
graphicSprite.graphics.drawRect(pattern[patternIndex]*110, 0, 100, 25);
graphicSprite.graphics.endFill();
blinkCounter++;
if(blinkCounter >= 20){
patternIndex++;
if(patternIndex == pattern.length){
removeEventListener(Event.ENTER_FRAME, displayPattern);
playerTurn = true;
graphicSprite.graphics.clear();
patternIndex = 0;
}
blinkCounter = 0;
}
}
public function addPlayerPattern(choice:uint):void {
playerPattern.push(choice);
for(var i:uint = 0; i < playerPattern.length; i++){
if(playerPattern[i] != pattern[i]){
for(var j:uint = 0; j < buttons.length; j++){
removeChild(buttons[j]);
}
return;
}
}
if(playerPattern.length == pattern.length){
playerPattern = new Array();
addPattern();
}
}
}
}
import flash.display.Sprite;
import flash.events.*;
class SimonButton extends Sprite {
private var simon:Main = null;
private var index:uint = 0;
public function SimonButton(game:Main, buttonIndex:uint){
simon = game;
index = buttonIndex;
simon.addChild(this);
this.x = index*110;
this.graphics.beginFill(0x007700);
this.graphics.drawRect(0, 0, 100, 25);
this.graphics.endFill();
addEventListener(MouseEvent.CLICK, clickBtn);
}
public function clickBtn(e:Event = null):void{
if(simon.playerTurn){
simon.addPlayerPattern(index);
}
}
}
Running version here:
var foo:Boolean = false
, usevar foo:Boolean
. Same rules say that you shouldn't usenew Array()
, unless you give it length argument. Use[]
instead. Binary operators require space on both sides. Single letter variable names are bad (except in few cases, like loop iterators). Put empty line between definitions. Separate keywords from the rest of the text with a space. \$\endgroup\$