patternMinor
Simon-says in AS3 - Prototyping functionality
Viewed 0 times
as3simonprototypingsaysfunctionality
Problem
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:
This means that, as you'll see:
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
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
Solution
Disclaimer: I'm not an Actionscript programmer, however, I think there are a few universal things/improvements that apply to your program:
-
Aligning large blocks of variable initialization might benefit readability. Making similar things look similar is a good idea. The first block on class
Now it is pretty clear to distinguish between the variable name, its type and default initialization.
-
This type of
Put each command in its own line. I also suggest always providing
-
You have quite a few hardcoded constants, like colors (
-
Overall, your code seems nice, though I'm finding it a little too tightly packed. I think some blank lines between functions would make it more pleasant to read.
-
Aligning large blocks of variable initialization might benefit readability. Making similar things look similar is a good idea. The first block on class
Main would probably benefit from this: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();Now it is pretty clear to distinguish between the variable name, its type and default initialization.
-
This type of
if/else setup is harder to read. I would advise against:public function Main(){
if(stage)init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}Put each command in its own line. I also suggest always providing
{ } even for single line statements, to avoid problems like accidental highjacking of lines and errors introduced by bad code indenting.-
You have quite a few hardcoded constants, like colors (
0x007700) and positioning of sprites (100, 25) and also blinkCounter, which is being compared against 20 (why 20?). Even if this is prototyping code, avoiding excessive hardcoding makes it easier to expand the prototype later. Hardcode the obvious stuff that won't change, like 4 for the number of squares in the game, but avoid hardcoding things that are very likely to change during the prototyping phase, such as the colors and sprite positions.-
Overall, your code seems nice, though I'm finding it a little too tightly packed. I think some blank lines between functions would make it more pleasant to read.
Code Snippets
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);
}Context
StackExchange Code Review Q#71781, answer score: 4
Revisions (0)
No revisions yet.