Sunday, February 9, 2014

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

In this and in the next two blog entries will show some of the basics on how to design a simple game.  

The first program is called MOVEM.  This really isn't a game, but it is to introduce the GETKEY function on the HP Prime.  The GETKEY gets the last key pressed, while the programmer decides what happens with each key stroke.  Each key will return a key code.

The key codes used are:
2:  up
7:  left
8:  right
12: down
50: plus key

Other common key codes include 30 for Enter.

 The HP Prime pixel screen is 320 x 240, with row pixels 0-319 going right, and column pixels 0-239 going down.

The size of the text that is used in MOVEM series is size 2, which means that the characters are 10 pixels long by 12 pixels high.  

Comments in the program are followed by two forward slashes.  They don't have to be typed into the program, but they will provide helpful notes.

To run MOVEM, call up MOVEM and move the "M" around the screen using the arrow keys on the arrow pad.  Exit by pressing the Plus (+) button.  Note the code after each arrow movement - your character ("M") will not move beyond the screen's borders.

Code:
EXPORT MOVEM()
BEGIN



// clear the screen
RECT();


// set up the character
A:=100;
B:=60;
TEXTOUT_P("M",A,B,2);




// Start the main loop
REPEAT
K:=GETKEY;

// Move Left
IF K==7  AND A>0 THEN
RECT_P(A,B,A+10,B+12);
A:=A-10;
END;

// Move Right
IF K==8  AND A<320 THEN
RECT_P(A,B,A+10,B+12);
A:=A+10;
END;

// Move Down
IF K==12  AND B<240 THEN
RECT_P(A,B,A+10,B+12);
B:=B+12;
END;

// Move Up
IF K==2 AND B>0 THEN
RECT_P(A,B,A+10,B+12);
B:=B-12;
END;
TEXTOUT_P("M",A,B,2);
// Exit using the Plus Key
UNTIL K==50;

END;






The next blog entry will show how to put a goal and immovable solid objects.


This blog is property of Edward Shore - 2014.

No comments:

Post a Comment

HP Prime and Casio fx-CG50: Trapezoid Midsegment, Height, Area

HP Prime and Casio fx-CG50:  Trapezoid Midsegment, Height, Area The program TRAPEZ calculates the following: Midsegment leng...