Showing posts with label picker. Show all posts
Showing posts with label picker. Show all posts

Monday, September 24, 2012

Petit Computer Journal #8


Petit Computer Journal #8





The Apple Picker Game? We're going to finish it up proper! Actually, we're not going to really make it professional quality. But we'll put on some fancy dressing on it to make it really enjoyable!

Adding color:
It was later in the development cycle (fancy words for repeatedly finding and fixing numerous bugs) that I find myself squinting at the screen looking for that last apple among the many snakes. Adding colors solves this problem. I colored the apples pink because I find red is too strong among the snake green.

Adding music:
It makes a great game. Looking at the help menu, I see a list of ready made music. Use BGMPLAY N, where N is a number. BGMSTOP to stop the music. Remember that N is a number. If you do this: BGMPLAY FANCY. That means you're playing music as defined by variable FANCY, which would be zero if you haven't set it to anything.

I purposely did not add music for level playing. I found it to be too noisy. But if you want it, add this to @NEWLEVEL
BGMPLAY LEVEL+ADJ
This will launch a new background music with every new level, with ADJ as offset.

Adding Sound Effect:
Oh, this is a good one! I added scream, hit, and coin sounds immediately. Easy sound effects! It took me awhile to add steps sound, but once I did, I never want to go back! This is how you know what you did is good!

Adding walls:
This was a doozy! The program kept hanging up (a polite way to say unintended infinite loop) and I didn't know why! Obviously, I needed to work out the math on paper, and then do exhaustive analysis upon it. I did neither. I did hack-and-slash programming, so even now, I'm not sure the solution is correct.

I did test out this form:
FOR I=0 TO 0:T=RND(5)+5):I=ABS(MY-T)-1:NEXT:WX[0]=T
You probably wonder why I bothered to put the FOR loop in there. I use it as REPEAT loop, just so you know. Notice that I set the value of I everytime. When MY==T, the loop will repeat. That is, T of any value EXCEPT WY.

The walls will erase snakes. That's fine. I also have to make sure that the walls will never form an enclosed space where the player cannot enter/exit. The easy way is to create some levels with array or DATA statements. I decided to create it dynamically, which isn't the easiest in the world, but I hate putting in DATA too much and results in repeat levels, making them boring.

Adding Lives:
I added this as I realize that the later levels may feature apples surrounded by snakes. I don't want the game to end immediately. Like any good design decision, once I put it in, I don't want to take it out.

Adding Time:
This comes in last. After a while, I started to add challenges. Can I finish 10 levels in 5 minutes? 20 levels in 10 minutes? Yes, I can! You can, too. Look at the levels and time completed. Major bragging rights, there!

Adding Alternative control:
It's not hard to do button controls. I simply extended the button readouts, and that's it! Noteworthy to mention is that I didn't put fast button where pushing L or R button results in faster movement. It's not hard to do, but I can pull the feature out after I put it in. Not a good design decision.

Adding moving snake:
I decided not to. It's not that kind of game. The game evolves from taking apples (Path Finding/Travelling Salesman) problem, to maze game, to Least Cost finding. Along the way, you have to be able to control the person very well to avoid running into snakes.

Adding customized graphic:
I decided not to. The game benefits from seeing the whole level all at once. If I use same size sprites, the size of the game would have expanded radically, for little returns. If I use double size sprites, the game will look great, but play suffers. If I use smaller playfield, the game becomes boring really quickly. If I use big sprite on top screen, and normal size for bottom screen as map, then the player will constantly look at the bottom screen. In which case, why bother?

How to CHEAT:
Yes, you can cheat! You have the source code in front of you! Whenever you feel like you want some extra lives, hit BREAK, then type this:
LIVE=20:CONT
You'll continue no problem. Your live is now 20! You have to be careful to do it so the screen does not scroll, and no apple is overwritten. I recommend waiting until the last apple is on upper right corner.

You can also add more apples, by typing this:
SCORE=9:CONT
Since the level advances every 10 points, this will cause new apples to appear at the next apple taken.

Level select is no problem either. Can you guess how?
LEVEL=15:CONT
Yup. It's that easy!

Finally, you may want a puzzle game, instead of an action game. Not a problem! Do this at @GAMELOOP

From this: VSYNC 15:GOSUB @SETB2
To this: VSYNC 1:GOSUB @SETB3

And you got yourself a puzzle game!

Professional Quality?

This game is a good Hobbyist effort, but I wouldn't call it a professional quality. There are different things that I can do as a professional, but choose not to. I already mentioned the exhaustive testing of walls. Here are some other things that I need to do in order to be professionals:

1. Limit the number of lives to 30, as to not mess up the display.
2. Add option to set background music to the levels
3. Add faster speed button
4. Save/Load High scores
5. Refresh whole screen to prevent display corruption
6. Add variety of enemies. Maybe some will add apples, others fling you randomly to another part of the screen.
7. Add time limit, allowing for levels of difficulty.
8. Also adjust player speed according to level of difficulty.
9. Add Puzzle mode from Option
10. Add replay option, play as demo on splash screen.
11. Tune up the presentation, not necessarily fancy graphics, but I would experiment with different placements of elements.
12. Make sure adding apples, snakes will not hang the game.

If I want to put this out for sale, I will add these:
13. Multiplayer option. 2 players. One is using Dpad. The other, buttons. Photo Dojo style
14. Computer player, single, double. With good path finding algorithm.
15. Selectable number of player
16. Sound/Music selection. Volume adjustable individually.
17. Optional level editor. It's not that hard.
18. Optional sprite editor. It's not that hard.
19. Optional music editor. It's not that hard.
20. Optional 3D graphic. This one is hard, and not at all useful. It's great marketing tool, though.


Monday, September 10, 2012

Petit Computer Journal #6


Petit Computer Journal #6

I don't know about you, but I'm itching for a game. I know I'm bucking the convention here, but I want a real game, none of this Hi-Lo stuff that some people call game. I actually have 2 choices: A word guessing game (Like Wheel of Fortune) or a Snake game. Let's do Snake game first. It's probably a good idea to set the difficulty in linear manner, but I think we can handle otherwise.

We'll lower the difficulty by making The Apple Picker Game. What kind of game is that? It's an Apple Eating Snake game without the Snake. Not that implementing a snake is difficult, but I want to explain how strings work before I do that. The reasonable approach is to do the other game first. We'll do this game first because, hey, sometimes it's good to live dangerously!

Haha, joking aside, I think we have all that we need in order to build it. Show-and-Tell time! What makes an Apple Picker game?

1. A Character who picks apples
2. Apples
3. Area
4. Obstacles (optional)
5. Time Limit (optional)
6. Score (optional)

The gameplay area is the console 32x24 character map. We'll use character for graphics. The movement will be once per second (VSYNC 60). A character will keep moving forward until it runs into an obstacle (wall). Do we need time limit? How about ending the game after all the apples are gone? Do we need score?

Let's keep things simple for now. We do a character, put some apples, and build wall along the perimeter. We'll split the program into 3 sections: Init, GameLoop, and Ending

Init:
1. Clear the screen CLS
2. Put character on location 10,20
3. Draw Wall along perimeter
4. Put 10 apples randomly

GameLoop:
1. Wait 1 second VSYNC 60
2. Read BUTTON(0) via SETB2
3. Depending on UDLR, we check the next space
3a. Space - move character there
3b. Apple - Increase score by 1, and move character there
3c. Wall  - Set character movement to STOP.
4. If score is 10 then GOTO Ending

Ending:
1. Display "GAME OVER"
2. Wait 2 seconds
3. Wait for button press SETB3
4. End Program.

That's not too bad. It's not even one page. How hard can it be? Notice how we reuse our old BUTTON reading code. That makes thing simple.

@INIT :'Program Initialization
'1. Clear the screen 
CLS
'2. Put character on location 10,20
LOCATE 10,20:?CHR$(244):'MAN
'3. Draw Wall along perimeter
LOCATE 0,1:?CHR$(3)*32
LOCATE 0,21:?CHR$(3)*32
FOR Y=1 TO 21
LOCATE 0,Y:?CHR$(3)
LOCATE 31,Y:?CHR$(3)
NEXT
'4. Put 10 apples randomly
GOSUB @PUTAPPLE


@ENDING :'Program Ending
'1. Display "GAME OVER"
LOCATE 10,22:?"GAME OVER";
'2. Wait 2 seconds
WAIT 120
'3. Wait for button press SETB3
GOSUB @SETB3
'4. End Program.
END

We need to work on @GameLoop. We also need to work on @PUTAPPLE. Otherwise, the program is half done. Here is PUTAPPLE:

@PUTAPPLE :'CHR$(233) IS APPLE
FOR I=1 TO 10
X=RND(31-1)+1:Y=RND(20-2)+2:C=CHKCHR(X,Y)
IF C==0 THEN LOCATE X,Y:?CHR$(233) ELSE I=I-1
NEXT
RETURN

The thing we want to watch out for is that we only put apple on bare ground. We also make sure that there are EXACTLY 10 apples. Ah, that was easy. We run it, and see if it works. Save it as APPLE.

Now for the main game loop. Wait 1 second. Read Input. Move pieces. Repeat until done. What can be the problem? Oh, wait. We forgot to store our man's location. Put this on Init: "MX=10:MY=20". We also forgot the score: "SCORE=0"


@GameLoop
'1. Wait 1 second 
'2. Read BUTTON(0) via SETB2
VSYNC 60:GOSUB @SETB2

'3. Depending on UDLR, we check the next space
GOSUB @NEXTSPACE:'SETS NX,NY
GOSUB @DOMOVE

'4. If score is 10 then GOTO Ending
IF SCORE==10 GOTO @ENDING
GOTO @GAMELOOP


'3a. Space - move character there
'3b. Apple - Increase score by 1, and move character there
'3c. Wall  - Set character movement to STOP.
@DOMOVE
C=CHKCHR(NX,NY)
IF C==0 THEN GOSUB @MANMOVE:RETURN
IF C==233 THEN GOSUB @EATAPPLE:GOSUB @MANMOVE:RETURN
IF C==3 THEN GOSUB @HITWALL:RETURN
RETURN

@MANMOVE
LOCATE MX,MY:?CHR$(0);
LOCATE NX,NY:?CHR$(244);
MX=NX:MY=NY
RETURN

@EATAPPLE
BEEP 7:'COIN
SCORE=SCORE+1
LOCATE 0,22:?"SCORE ";SCORE;
RETURN

@HITWALL
BEEP 11:'DAMAGE
RETURN

@NEXTSPACE
IF INBUTTON$=="U" THEN NX=MX:NY=MY-1:RETURN
IF INBUTTON$=="D" THEN NX=MX:NY=MY+1:RETURN
IF INBUTTON$=="L" THEN NX=MX-1:NY=MY:RETURN
IF INBUTTON$=="R" THEN NX=MX+1:NY=MY:RETURN
IF INBUTTON$=="" THEN NX=MX:NY=MY:RETURN
RETURN

If you look at @NEXTSPACE structure, you may wonder why I put in so many RETURNs in there. Actually, I'm using it as ELSE-IF ladder. Since SmileBASIC doesn't allow multi-line ELSE-IF ladder, I'm using Subroutine as substitute. It works fine. The many RETURNs can be skipped, at the cost of performance. Since this structure can be difficult to discover on your own, I figure I'll highlight it here.

It took me about one hour to do everything. Not bad at all. Save the game and pat yourself on the back. You have successfully completed a whole new game.

How about some improvements? Once per second is too slow. 3 per second seems about right. Change to VSYNC 20.

How about changing the movement so that once you move, you cannot stop?

How about putting in some other obstacle? Snakes? Who says we can't have snakes in there? Have the program ends when you hit a snake. Otherwise, keep going.

@PUTSNAKE :'CHR$(27) IS SNAKE
LEVEL=LEVEL+1
FOR I=1 TO LEVEL
X=RND(31-1)+1:Y=RND(20-2)+2:C=CHKCHR(X,Y)
IF C==0 THEN LOCATE X,Y:?CHR$(27) ELSE I=I-1
NEXT
LOCATE 21,22:?"LEVEL ";LEVEL;
RETURN



What Have We Learned?
1. We know multiplying a character repeats that character.
2. CHKCHR(X,Y) returns the value of the character on that console location.
3. CLS fills the console with CHR$(0).
4. BEEP makes easy sound effects.
5. Computer Programming is easy. Good Design is hard!

Tuesday, September 4, 2012

Lotto Number Picker


There is something to be said for a well written plan. It's easy to follow through. OTOH, there is more than one way to skin a cat, and there's more than one way to code a program.

This Petit Computer program was done while docked at King Sooper, waiting to be unloaded. It's very easy to do, and it is proof that computer programming can be convenient, fun, and with potential of great reward!

Look at the algorithm and see if you can follow the code! Feel free to modify to suit!



'LOTTO NUMBER PICKER
'THE ALGORITHM:
'1. TITLE AND INITIALIZATIONS
'2. FILL BALL ARRAY 1 TO MAXNUM
'3. RANDOMIZE BALL ARRAY
'4. FOR I=1 TO NUMBER OF BALLS
'4.1 IF BALL NUMBER < 10, ADD LEADING ZERO
'4.2 APPEND TO TICKET ARRAY STRING
'5. REPEAT STEPS 3-4 UNTIL NUMBER OF TICKETS
'6. FOR I=1 TO NUMBER OF TICKETS
'6.1 PRINT TICKET$ ARRAY. ONE PER LINE
'7. PRESS ANY KEY TO CONTINUE. BACK TO TOP

@STEP1
CLS:CLEAR:COLOR 2
?"LOTTO GENERATOR"
?"(C) 2012 Harry M. Hardjono"
MN=42:'MAXNUM 1-42
NB=6:'NUMBER OF BALLS
NT=20:'NUMBER OF TICKETS

@STEP2
@STEP3
@STEP4
@STEP5
REM THEN A MIRACLE OCCURS...

@STEP6
FOR I=0 TO NB*NT-1:C$=CHR$(RND(MN)+101)
IF !(I%NB) THEN LOCATE 0,I/NB+3:S$="":COLOR I/NB/3%2+4
IF 0>INSTR(S$,C$) THEN ?RIGHT$(STR$(ASC(C$)),2);" ";:S$=S$+C$ ELSE I=I-1
NEXT

@STEP7
FOR I=0 TO 1:I=TCHST OR BUTTON(0):NEXT
GOTO @STEP1