Sunday, February 9, 2014

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

The third (and maybe final? TBD) part of this mini-series will introduce MOVEM5.  Unlike stationary trees with no threat in MOVEM4, there are enemies in MOVEM5.  The enemies move up and down.  In this simple game, there are no weapons, you just have to avoid the enemies as you try to head home.  All the enemies are red.  Once touch, and it's game over, so be careful. 

Once again, you are controlling the π character.  


I left in the "quick exit" key, just press the Plus key to exit at any time.  

Code:

EXPORT MOVEM5()
BEGIN
// Version 5
// 2014-02-09
// Get home avoid foes (4)

// Set up the screen
RECT();

// Cursor set up - player
// Two sets of coordinates, original (A,B) and updated (C,D) are 

// used
LOCAL A,B,C,D;
A:=0;
B:=60;
C:=0;
D:=60;
TEXTOUT_P("π",A,B,2);

// Ememy Setup
LOCAL estr;
LOCAL ey1A,ey2A,ey3A,ey4A;
LOCAL ex1A,ex2A,ex3A,ex4A;
LOCAL ey1B,ey2B,ey3B,ey4B;
// Set delay paramenters

// This is compensate because the HP Prime acts so fast
// In the emulator I have 4000, 3000, 2000, and 1000 for
// d1, d2, d3, and d4, respectively.
LOCAL d1:=200,d2:=150,d3:=100,d4:=50;

// Initialize position of the enemies
ex1A:=80;
ex2A:=RANDINT(11,17)*10;
ex3A:=RANDINT(20,26)*10;
ex4A:=280;
ey1A:=0;
ey2A:=228;
ey3A:=RANDINT(0,19)*12;
ey4A:=RANDINT(0,19)*12;
estr:=CHAR(9991); //enemy
TEXTOUT_P(estr,ex1A,ey1A,2,#FF0000);
TEXTOUT_P(estr,ex2A,ey2A,2,#FF1000);
TEXTOUT_P(estr,ex3A,ey3A,2,#FF2000);
TEXTOUT_P(estr,ex4A,ey4A,2,#FF3000);

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

// Main loop
REPEAT

// Count down the delay timers
d1:=d1-1;
d2:=d2-1;
d3:=d3-1;
d4:=d4-1;

// Get keystroke from the player
K:=GETKEY;

// Potentially update C and D
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;

// Animate the enemies
// Enemy 1: moves up. Wraps around the screen
IF d1==0 THEN

// refresh delay rate - 4000 in the emulator
d1:=200;

// move the enemy
ey1B:=ey1A+12;
IF ey1B>240 THEN
ey1B:=0;
END;
END;

// Draw a blank box of the previous position
RECT_P(ex1A,ey1A,ex1A+10,ex1A+12);

// Draw the enemy at its new location
TEXTOUT_P(estr,ex1A,ey1B,2,#FF0000h);
ey1A:=ey1B;

// Enemy 2 - moving down
IF d2==0 THEN

// Delay rate of 3000 is used in the emulator
d2:=150;
ey2B:=ey2A-12;
IF ey2B<0 THEN
ey2B:=240;
END;
END;
RECT_P(ex2A,ey2A,ex2A+10,ey2A+12);
TEXTOUT_P(estr,ex2A,ey2B,2,#FF0000h);
ey2A:=ey2B;

// Enemy 3 - moving up
IF d3==0 THEN

// Delay rate of 2000 used in the emulator
d3:=100;
ey3B:=ey3A+12;
IF ey3B>240 THEN
ey3B:=0;
END;
END;
RECT_P(ex3A,ey3A,ex3A+10,ex3A+12);
TEXTOUT_P(estr,ex3A,ey3B,2,#FF0000h);
ey3A:=ey3B;

// Enemy 4 - moving down
IF d4==0 THEN

// Delay rate of 1000 used in the emulator
d4:=50;
ey4B:=ey4A-12;
IF ey4B<0 THEN
ey4B:=240;
END;
END;
RECT_P(ex4A,ey4A,ex4A+10,ey4A+12);
TEXTOUT_P(estr,ex4A,ey4B,2,#FF0000h);
ey4A:=ey4B;


// Did we hit the enemy?
LOCAL collide:=0;
IF (C==ex1A) AND (D==ey1B) THEN
collide:=1
END;
IF (C==ex2A) AND (D==ey2B) THEN
collide:=1
END;
IF (C==ex3A) AND (D==ey3B) THEN
collide:=1
END;
IF (C==ex4A) AND (D==ey4B) THEN
collide:=1
END;

// We hit the enemy - not good
IF collide==1 THEN
MSGBOX("OUCH! :(");
KILL;
END;


// Move the 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);


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


Until next time,


Eddie


This blog is property of Edward Shore.  2014 

Spotlight: Sharp EL-5200

  Spotlight: Sharp EL-5200 As we come on the 13 th (April 16) anniversary of this blog, I want to thank you. Blogging about mathematic...