Since the level could be, in principle quite large, it didn't make sense to draw the entire level each "tick," but to at the very least draw only what is visible on the screen at the time. Given that the blocks from the level file are already sorted by x-position, and since the levels are long but not tall, this wasn't too hard. I just searched through the array of "Squobs" until I got to the first one which had an x-component greater than the leftmost screen point minus the block width and marked that as the starting point. From there I kept going through the list until I got to the first brick which had an x-coordinate greater than the rightmost screen-point and marked that as the ending point and drew all of the blocks in between.
The next task was to draw the player as an animated sprite. Normally sprites are a collection of images which animate in sequence to make the appearance of moving. Since you don't necessarily want to make one animation frame per clock cycle, you can incorporate a frame rate by counting up each clock cycle to a certain number before flipping frames. For the purposes of making a character look like he's walking, I counted distance traveled instead of clock cycles. This makes it look a little (but not too much) more natural and keeps the character from walking on the spot when his velocity is little or zero.
In order to get the player to face the right way and to look like he's jumping when he's jumping, I gave the Sprite class a set of states:
- s: Standing and facing right
- S: Standing and facing left
- j: Jumping and facing right
- J: Jumping and facing left
- r: Running and facing right
- R: Running and facing left
... and each is sorted out in the code via a switch(state) statement.
Here is a screenshot of the code in action:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXzm42f8zNa609hSUXi-ClaNoovbrJ5bpvJf4ujVfPZ2N-VfB7vjqhZiW6dpH9oeJzYi2u1O64h5sDxXOY-xBcWprYMmIa-GcRFdTHk-bZVz_2cSNbC3C7f1Mbr8p_76ggZzwkaF9m6wlY/s400/Screen+shot+2010-09-11+at+10.35.20+PM.png)
In order to circumvent Java's (admittedly necessary) security measures, all the files are loaded as URLs from the server where they reside. As a result, loading the level takes a really long time. However I couldn't convince Java to read files from a browser so until I find a better way, I'm stuck doing it this way.
You can play the game here (all you need is a little patience.)
No comments:
Post a Comment