BrainBlitz.org

June 24, 2008

Handy Dandy PHP/Form/MySQL Utility

Filed under: Programming — Tags: , , , , — Anthony @ 5:52 am

Always looking for excuses to write little utilities, I have ignored the fact that similiar or equivalent functionality may already be a part of my web development software (DW), and created this nifty little tool.

 Creating and handling forms has always been a particularily annoying task to me, and so I present the cleverly named…

PHPFormUtility!

http://www.brainblitz.org/Flash/PHPFormUtil.html

It’ll save me a good 10 minutes a day. 

  • Print this article!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Yahoo! Bookmarks

June 22, 2008

Tetris Guts (a tutorial on Flash game programming (AS3))

Filed under: Programming — Tags: , , , — Anthony @ 9:03 pm

I’m pretty new to flash/actionscript – and this tutorial is more of a “learning log” than well developed material. Just keep that in mind!

***

SOURCE DOWNLOAD

This portion of the tutorial is a general overview. The exact details can be found in the (heavily commented) program files.

 This is a slightly simplified version of the game actually posted on this website, in that I removed music, the splash screen and PHP interfacing to 1.) keep file size down, 2.) not divulge the specifics of my leaderboard implementation, but mainly 3) to keep things relatively simple.

This tutorial covers:

- Fast graphics via copyPixels (from a sprite sheet). Not that fast graphics are necessary for this game, but it’s a convenient way to do it with sprite sheets (since no rotation or other tranforms of the graphics are required).
- Handling game logic (scoring, level, game over, pause, etc)
- Basic keyboard input
- Object Orientation in ActionScript 3.

Overview

The general program flow is this:

  • Set up drawing drawing surfaces (Bitmap and BitmapData objects attached to MovieClip objects).
  • Initiate game logic.
  • Initiate game loop when user starts game:
    • Apply user input to game objects
    • Update game objects (“step the world”)
    • Check for states in which actions need to occur (i.e. collision, row delete).
    • Draw screen
  • Set game to “over” state when a “losing” condition has been detected.
  • Wait for user to restart game -> Clear game over flag and re-initiate game logic


Objects

Tetris Screen ShotThere are 3 classes defined in the code.

  • The main application (Tetris.as) – created when the swf loads. All game logic, control and display is handled here.
  • The Body’s (Body.as). There are always 2 Body’s in existence. When a Body is created, it creates 4 Block objects in a certain pattern, making one of the seven standard Tetris pieces.
    • The current body is under the control of the keyboard
    • The next body is waiting in line to become the current body. It is necessary to have this body defined ahead of time so it can be shown in the preview pane.
  • The Block’s (Block.as). The Body’s are constructed of blocks, and when a Body “dies” (reaches the “ground”), it is converted to stand-alone Block’s – no longer part of a Body.


Collision Handling

When the current Body’s position updates, via the game or user input (pressing the up/down arrow), the game checks for 3 types of collisions.

  • Horizontal Collisions – if a Block within a Body is found to be overlapping another Block, then the code rewinds the Body to it’s previous position, moving it back into a valid state. The user never sees the overlap, because the collision is detected and rewound BEFORE anything gets drawn to the screen. No further action is taken.
  • Rotational Collisions – are handled seperately because it’s more complex to rewind, and you don’t want to waste time looking for a rotate collision unless the user had actually rotated. So while a horizontal collision check takes place on every frame, the rotation collision check takes place ONLY IF the user has pressed the up arrow.
  • Vertical Collisions – this is where the fun really starts. If there has been a vertical collision, then
    • The Body needs to be rewound
    • We need to check the position of the Body – if it hasn’t moved, then previous Body deaths must have stacked all the way to the top, and the game is over. Game ends here.
    • If the game hasn’t ended: Since this was vertical, the block has reached the ground, and must “die.” The Body’s 4 Blocks are copied into an array of Blocks in the main application (they are no longer within a Body).
    • The pointer to the current Body is re-pointed to the next Body.
    • The next Body is assigned to a new Body.
    • Now we have to check to see if the Body death resulted in a filled row, which would need to be removed/score incremented, etc.
      • When a Body dies and the Blocks are copied to the main application, for each Block that is copied, a variable holding the sum of the blocks in the row is incremented.
      • If that sum reaches 10, then the row has been completely filled.
      • If the row is completely filled then:
        • Delete that row of blocks (via an array splice)
        • Push a new row to the top of row array.
        • Shift every block above that row down.
        • Increment the score.

Drawing

Drawing is achieved with copyPixel.

  • When the swf loads, a bitmap object is created from the library MovieClip “BlockMap”, which looks like this:BlockMap
  • Each Body type has an ID, 0 to 6 (for the 7 Tetris shapes). Each Block knows what type of Body it belongs to. When it comes time to draw a block to the screen, the code takes the blocks type, multiplies it by the blocks width, and gets a number of the value 0, 30, 60, 90, 120, 150, or 180. Since each block in the BlockMap is drawn at block width, that list of numbers corresponds to the x-value of that blocks sprite (lost yet?). For example, if we want to draw a block of type 3, the function called is: gameScreen.copyPixels(blockMap, new Rectangle(3 * 30, 0, 30, 30), new Point(x,  y));This is simplified so you don’t get lost in the details at this point. Basically it’s saying: Copy the portion of the bitmap blockMap within the 30 x 30 square whose upper left corner is at 90. Place it on the game screen @ (x, y). That would draw an orange block to the point x, y.


Final Notes

This implmentation could be vastly improved. For one thing, the entire game pane is cleared on every frame. That is horribly wasteful, as the only thing  changing is the one block that’s moving – so really, only it’s bounding box needs to be cleared. I’m sure there are lots of other little things, but due to 1.) my limited knowledge of flash/actionscript, 2.) my desire to keep this tutorial relatively simple, but still produce a perfectly functional application, and 3) my laziness, those details are left as an excercise for the reader. ; )

One detail that is sure to cause confusion is how to get the coordinates at which a block must be drawn. In Body.as, each Tetris shape is defined by an array of x values, and an array of y values. For a Line shape, the coordinates would be:

x = {0, 0, 0, 0}
y = {2, 1, 0, -1}

So each block has it’s own coordinate within the body. And then you have the Body’s position on the screen. So to get the position of an individual block on the screen, you have to add the Block’s local position to the Body’s world position:

BlocksTruePosition = BodysPositionOnScreen + BlocksPositionInBody

Again, the code is heavily commented, so if you understand the theory, the rest is just syntax.

SOURCE DOWNLOAD

  • Print this article!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Yahoo! Bookmarks

New Flash Projects

Filed under: Programming — Anthony @ 4:11 pm

A couple of projects I’ve been using to learn flash:

BoxCAD

Tetris / Scores

At least for the tetris one, I’ll be posting the code in a tutorial format shortly.

  • Print this article!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Yahoo! Bookmarks

June 19, 2008

Adderall – Just over a Year

Filed under: Adderall — Tags: , , , , , — Anthony @ 3:16 am

I can’t believe it, but it’s the truth. Was it really that long ago that I wrote my “Day 1″ post?!

Anyway, I was looking at webstats for the first time today, going back to the beginning of this blog. I thought it would be fun to address a few search terms that come up frequently in Google searches.

1. Provigil or/vs/with Adderall
My experience with Provigil was very brief (1 week) – keep that in mind. Provigil was far, far more subtle than Adderall. I never noticed any effect with Provigil, I simply didn’t get sleepy when I usually do. Adderall gives you a very noticable pleasant, motivated feeling. But restricting this to only the effects on sleepiness, Adderall seems to be more intense, but shorter in duration. Of course you could take lots of tiny Adderall doses to equalize that difference, but that’s just annoying. The critical difference was in side effects. Provigil didn’t really have any. Adderall basically smacks you in the head with a baseball bat at the end of the day. Last but not least – price. You can get a prescription for Provigil only if you have tested positive for narcolepsy. I haven’t. So for me the difference is between $10 and $200 a month.

2. Adderall and sore eyes / eye pain
During The Crash, my eyes feel sunken in and strained, but no differently than when I’m very tired.

3. How long does Adderall take to stabilize in the body?
It is different for every effect. Some effects increase over time, some decrease. The “anti-sleep” diminished over a couple of months on what seemed like a log scale – a lot over the first month, a little the second month, a tiny bit the third month, etc. I don’t notice any change over time at this point (1 year). For me, the “cold hands” effect slowly increased, until the 8 month mark or so. I didn’t notice it at first. Now my hands and feet are freezing when I take it, unless the ambient temperature is pretty warm. Then they’re just “cold.” All of the “recreational” effect diminish rapidly over the first week. That includes talkativeness, euphoria, etc. I’ve read that those effects return with a week-or-so break, but I haven’t noticed it. I’ve never re-experienced my “first day.”

4. Adderall Nausea
If I take 10 mg or more at once, I will sometimes feel a very slight nausea, from 20 minutes to 40 minutes or so. It’s very minimal, and hasn’t really bothered me.

5. Adderall numb arms and face
Same cause as the cold limbs; Adderall constricts your blood vessels. I’ve never been numb, but I have had noticably blue-ish limbs, with sharp pains upon fast movement, on several occasions. It generally happens in my ankles when I take a little extra to meet a late night deadline, then get up from my desk.

6. How to combat the tiredness when taking a break from adderall
I wish I knew! If you’re desperate, they have 200 mg caffein pills (I’ve only seen them at Fred Myers), but of course – caffein is pretty disappointing after Adderall! It IS enough to keep you functional though. But really, plan on sleeping and being depressed. :)

7. Avoiding Adderall Crash
Interestingly, I’ve noticed that even if I crash rather hard at the end of the day, if I bludgeon my way through it, it actually dissipates after an hour or so. But the only time I really crash hard is when my last dose was too large. You can minimize the crash by tapering it off slowly. I usually take the largest dose at 12:30, knowing that I am hit hardest to sleep around 1:30. Then I take a medium dose around 3:30, and a small dose around 5:30.

Other assorted:

ADDERALL
IS
ADDICTIVE!!!

Sure, maybe YOU didn’t get addicted, but that doesn’t mean it’s not addictive. I would consider myself addicted, and that means everything. This doesn’t make it “bad” – it just necessitates certain measures.

I leave my Adderall in my wife’s safekeeping, knowing that when I start to feel the crash, the desire “to get back up” will be overwhelming. Once the crash is over though, I find I don’t care so much anymore. Nothing motivates the consumption of adderall like… the consumption of adderall.

Taking antacid prior to Adderall (I’ve read from “an hour before” to “at the same time”) sometimes seems to help, but not worth the chewing of those awful tablets. It is worth noting that the literature from the manufacturer warns against taking Antacids and Adderall together.

A lot of people struggle with insomnia on Adderall – and some of them find Melatonin effect in getting to sleep. It’s available over the counter as a supplement all over the place for $5 or so. I’ve tried it, and it does help me go to sleep, but I always wake up 3 to 4 hours later, and can’t get back to sleep for some time, so I gave up on it. I don’t have much of an issue with insomnia these days anyway.

  • Print this article!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Yahoo! Bookmarks

Powered by WordPress