Sunday, February 9, 2014

HP Prime: Programming a (Very) Simple Game Part 2

In Part 2, we will actually input some elements that would go into a tile based game.





In this version, called MOVEM4 (I had two beta programs to test and learn things), you are now the Pi (π) character, and your job is to navigate through the forest to the brown castle.  
 You will not be able to move through the green trees that are scattered through the forest.

There is a blue cloud that buzzes by - that is just for art to make it look like a sunny day.

Like MOVEM, to operate in MOVEM4, use the arrow keys to control your character.  You can always exit by pressing the Plus key.

Here is the code, once again, comments are provided so for those of you who are learning programming, hopefully this helps.  I had a lot of fun doing this.

EXPORT MOVEM4()
BEGIN
// Version 4
// 2014-02-09
// Clear the screen
RECT();

// Cursor set up
// I use two sets of coordinates for the cursor
// A and B for the original and C and D for updated position
LOCAL A,B,C,D;
A:=100;
B:=60;
C:=100;
D:=60;
TEXTOUT_P("π",A,B,2);

// Trees Setup - trees are solid, stopping our player in its tracks
LOCAL tstr;
LOCAL L0,L1,S;
// Random population of a forest
S:=RANDINT(50,100);
L0:=RANDINT(S,0,31)*10;
L1:=RANDINT(S,0,19)*12;
tstr:=CHAR(8857); // tree

LOCAL tx,ty,I;
// Draw the forest
//  #8000h is forest green (RGB(0,128,0))
FOR I FROM 1 TO S DO
tx:=L0(I); ty:=L1(I);
TEXTOUT_P(tstr,tx,ty,2,#8000h);
END;

// House setup - the house is the goal
// Once you get to the house, you win.
//  #964B00h is brown (RGB(150,75,0))
LOCAL hx,hy,hstr;
hx:=RANDINT(0,31)*10;
hy:=RANDINT(0,19)*12;
hstr:=CHAR(9820); // castle
TEXTOUT_P(hstr,hx,hy,2,#964B00h);

// Cloud
// #1E90FF is Dodger Blue (RGB(30,144,255))
LOCAL bx1,by1,bx2,by2,bstr;
bx1:=0;
by1:=RANDINT(0,19)*12;
bstr:=CHAR({10042,10042,10042,
10042,10042}); // cloud
TEXTOUT_P(bstr,bx1,by1,2,#1E90FF);

// Main Loop
REPEAT
// Get a key stroke
K:=GETKEY;

// Update C and D - potential next position
IF K==7  AND A>0 THEN
C:=A-10; D:=B;
END;
IF K==8  AND A<310 THEN
C:=A+10; D:=B;
END;
IF K==12  AND B<228 THEN
C:=A; D:=B+12;
END;
IF K==2 AND B>0 THEN
C:=A; D:=B-12;
END;

// Collision with any of the trees?
LOCAL collide:=0;
FOR I FROM 1 TO S DO
IF C==L0(I)  AND D==L1(I) THEN
collide:=1;
BREAK;
END;
END;

// If yes, you will not move.  Otherwise, you will.
IF collide==1 THEN
C:=A; D:=B;
END;

// Anminate the moving cloud
// In the emulator, I used bx2:==bx1+0.01;
bx2:=bx1+1; by2:=by1;
// If the cloud disappears to the right of the screen,
// it starts over on the left side of the screen
IF bx2>310 THEN
bx2:=0;
by2:=RANDINT(0,19)*12;
END;
RECT_P(bx1,by1,bx1+10,by1+12);
TEXTOUT_P(bstr,bx2,by2,2,#1E90FFh);
bx1:=bx2; by1:=by2;

// Move your Player
RECT_P(A,B,A+10,B+12);
TEXTOUT_P("π",C,D,2);

// Draw Static Objects 
// The House
TEXTOUT_P(hstr,hx,hy,2,#964B00h);
// The Trees
LOCAL tx,ty,I;
FOR I FROM 1 TO S DO
tx:=L0(I); ty:=L1(I);
TEXTOUT_P(tstr,tx,ty,2,#8000h);
END;
// Prepare for the next keystroke
A:=C; B:=D;
// Loop exits if you arrived home or pressed the Plus Key
UNTIL K==50  OR (C==hx AND D==hy);
MSGBOX("Home! :)");
END;

This may not be the most sufficient way of doing things - but I stress, I am also learning how to do this and thought I share.  Hope this helps, and next time, we face some enemies!

Eddie

This blog is property of Edward Shore - 2014.


TI 84 Plus CE: Consolidated Debts

TI 84 Plus CE: Consolidated Debts   Disclaimer: This blog is for informational and academic purposes only. Financial decisions are your ...