Sunday, February 16, 2014

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

The fourth part of this mini-series will introduce MOVEM6.  In this edition, we will put a condition that is required to be met: collect ten dollar signs ($), called coins in this game, in order to unlock a barrier at the top of the screen.  Once the barrier is unlocked, the player can get to the goal.

The bottom of the screen will show the number of dollar signs left to collect.

Once again, you are controlling the π character.   Press the plus key to quickly exit the game.


Program:

EXPORT MOVEM6()
BEGIN
// Version 6
// 2014-02-16
// Collect 10 coins to unlock the goal

// A status screen is added on the bottom
// Bottom row is not avialable for play

// Game introduction
MSGBOX("Collect all 10 coins to unlock
the goal!");

// Clear the screen
RECT();

// Cursor set up
LOCAL A,B,C,D;
A:=150;
B:=216;
C:=150;
D:=216;
TEXTOUT_P("π",A,B,2);

// Draw Coins
LOCAL costr,L2,L3,cx,cy,I,coins;
coins:=10; // counter
L2:=RANDINT(10,0,31)*10;
L3:=RANDINT(10,1,18)*12;
costr:=CHAR(36);  // $ sign
FOR I FROM 1 TO 10 DO
cx:=L2(I); cy:=L3(I);
TEXTOUT_P(costr,cx,cy,2,#FF00h);
END;

// House setup
LOCAL hx,hy,hstr;
hx:=RANDINT(0,31)*10;
hy:=0;
hstr:=CHAR(9820); // castle
TEXTOUT_P(hstr,hx,hy,2,#964B00h);

// Lock setup
LOCAL lock:=1;

// Movement
REPEAT
K:=GETKEY;

// move
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<216 THEN
C:=A; D:=B+12;
END;
IF K==2 AND B>0 THEN
C:=A; D:=B-12;
END;

// Bump in the boundary?
LOCAL collide:=0;
IF D==hy AND lock==1 THEN
collide:=1;
// No BREAK command because there is no
// FOR loop to break out of
END;

IF collide==1 THEN
C:=A; D:=B;
END;


// Collect the coin?
FOR I FROM 1 TO 10 DO
IF C==L2(I)  AND D==L3(I) THEN
L2(I):=-1;
L3(I):=-1;
// Remove coin from play
coins:=coins-1;
IF coins==0 THEN lock:=0 END;
BREAK;
END;
END;


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

// Draw a boundary
IF coins≠0 THEN
LINE_P(0,12,319,12,#FF0000h);
ELSE
// Unlock boundary, all coins collected
LINE_P(0,12,319,12,#FF00h);
END;

// Draw Static Objects
TEXTOUT_P(hstr,hx,hy,2,#964B00h);

FOR I FROM 1 TO 10 DO
cx:=L2(I); cy:=L3(I);
// Don't draw at -1
IF L2(I)≠−1 AND L3(I)≠-1 THEN
TEXTOUT_P(costr,cx,cy,2,#FF00h);
END;

END;

// Draw Status Screen
RECT_P(0,217,319,239);
TEXTOUT_P(STRING(IP(coins)),0,228,2);

// Prepare
 for the next move
A:=C; B:=D;
// Arrived home?
UNTIL K==50  OR (C==hx AND D==hy);
MSGBOX("Home! :)");
END;



Foes, coins, and trees will be combined in the next game.

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 ...