Showing posts with label GETKEY. Show all posts
Showing posts with label GETKEY. Show all posts

Sunday, May 18, 2025

Casio fx-CG100: The getkey command in Python

Casio fx-CG100: The getkey command in Python


The getkey command: A Demonstration


The fx-CG100 adds a command to its Casioplot module, the beloved getkey command. The getkey returns the key code of the last key pressed from the command’s execution. The getkey allows for custom keyboard, movement of characters, or easy input of menu choices.


The getkey code that is assigned to each key on the fx-CG100 is thankfully fairly simple: it is a combination of the row number and a column number. For instance, the key code 21 is assigned to the [SETTINGS] key while the key code 95 is assigned to the [EXE] key.


Some key codes include:

Left arrow (←)

23

Up arrow (↑)

14

OK key

24

Down arrow (↓)

34

Right arrow (→)

25

EXE

95

1 Key

81

2 Key

82

3 Key

83

4 Key

71

5 Key

72

6 Key

73

7 Key

61

8 Key

62

9 Key

63

0 Key

91


Other key assignments can be found in the manual under the getkey() command in the Casioplot section: https://support.casio.com/global/en/calc/manual/fx-CG100_1AUGRAPH_en/BONDSYlkjdoxxc.html#BONDSYhxeuohnh


Note that the [ ON ] and [ AC ] buttons have no key assigned to them and can not be used with the getkey command.


Example: Calculating Horsepower


The script, hp.py calculates horsepower by one of three sets of inputs:


Press [ 1 ] to input weight (pounds) and time (seconds).

Press [ 2 ] to input weight (pounds) and speed (mi/hr).

Press [ 3 ] to input RPM (revolutions per minute) and torque (foot-pounds).


Using Getkey for Menus

To set up the getkey, I first make a “choice variable”, such as ch, and set it to 0. The while loop contains the menu and does not end until the user presses [ 1 ], [ 2 ], or [ 3 ] (or [ AC ] to terminate the program).   


from casioplot import *


# setup

ch=0


# title screen

while ch==0:

  clear_screen()

  draw_string(0,0,"**HORSEPOWRER CALCULATOR**")

  draw_string(0,20,"**     Inputs Menu     **")

  draw_string(0,40,"1) weight, time",(192,0,0))

  draw_string(0,60,"2) weight, speed",(0,0,192))

  draw_string(0,80,"3) RPM, torque",(0,128,0))

  show_screen()

  k=getkey()

  if k==81:

    ch=1

  elif k==82:

    ch=2

  elif k==83:

    ch=3

  


# calculation

if ch==1:

  w=float(input("weight in lbs:  "))

  t=float(input("time in sec:  "))

  hp=w*(t/5.825)**(-3)

elif ch==2:

  w=float(input("weight in lbs:  "))

  s=float(input("speed in mph:  "))

  hp=w*(s/234)**3

elif ch==3:

  r=float(input("RPM:  "))

  t=float(input("torque (ft-lbs): "))

  hp=(r*t)/5252


clear_screen()

txt="{0:.5f} hp".format(hp)

draw_string(0,0,"Horsepower=")

draw_string(0,20,txt,(192,0,0))

draw_string(0,100,"Press AC to exit.",(0,128,128))

show_screen()


Notes: The getkey is meant to work with the Casioplot commands. Instead of the built-in print command, we use draw_string and show_screen commands to display output. The plus here is that we can have text in color. The show_screen is displayed until either the [ EXE ] or the back buttons is pressed.


The input command returns us to the Shell and is not part of the Casioplot commands.


Source for the Formulas Used: 


Inch Calculator “Engine Horsepower Calculator”. Calc Hub, LLC. 2025. https://www.inchcalculator.com/engine-horsepower-calculator/ Retrieved May 5, 2025.


Eddie


All original content copyright, © 2011-2025. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.


Sunday, May 1, 2022

TI-84 Plus CE: All In Chance Game

TI-84 Plus CE:  All In Chance Game


The All In Chance Game


The objective is to get a high score by stopping the 2 x 2 board like this:


A   B

C  D


You score the sum of all four values.   Watch for negative values and zeroes.


Spots B, C, and D range from -2 to 5, while A has four possible values: -5, 0, 5, 10.


How many spins you get?  The game has a spinner from 2 to 7 spins.  


Free spins can be earned.  One way is to stop the board when A = 10.   There are two other ways, can you discover them?  



TI-84 Plus CE Program: ALLIN

(605 bytes)


Full

ClrHome

Output(1,1,"ALL IN GAME")

Output(2,1,"EWS 2022")

Wait 0.5

Output(4,1,"HOW MANY")

Output(5,1,"SPINS?")

Wait 0.5


0

Repeat Ans

randInt(2,7)→S

ClrHome

Output(1,3,"PRESS ANY KEY")

Output(3,3,"SPINS:")

Output(3,11,S)

Wait 0.25

getKey

End


ClrHome

Output(4,3,"YOU GOT")

Output(5,3,S)

Output(5,5,"SPINS!")

Output(7,3,"GOOD LUCK!")

Wait 0.8


0→T

0→M

While S>0

0

Repeat Ans

augment(5*randInt(­1,2,1),randInt(­2,5,3))→L6

ClrHome

Output(1,1,"SPINS LEFT: "+toString(S))

Output(3,4,L6(1))

Output(3,8,L6(2))

Output(6,4,L6(3))

Output(6,8,L6(4))

Output(8,1,"PRESS ANY KEY")

Wait 0.2

getKey

End

sum(L6)→P

T+P→T

If P>M:P→M

If L6(1)=L6(4) or L6(1)=10

Then

Output(1,1,"****FREE SPIN!****")

S+1→S

End

Output(8,1,"YOU GOT "+toString(P)+"         ")

Output(9,1,"TOTAL: "+toString(T))

Output(10,1,"PRESS ENTER")

Pause 

S-1→S

End


ClrHome

Disp "FINAL SCORE: "+toString(T)

Disp "BEST SPIN: "+toString(M)


Notes:


On the line Output(8,1,"YOU GOT "+toString(P)+"         "), extra spaces are needed to "erase" the PRESS ANY KEY message.


0 is entered before the Repeat Ans loop to make Ans "false", allowing for the loop to happen and the player to press any key to exit the loop. See the TI-Basic Developer's page for more details:  http://tibasicdev.wikidot.com/getkey


Source:


"The getKey Command"  TI-BASIC DEVELOPER: The TI-BASIC INFORMATION REPOSITORY.  Retrieved February 6, 2022.  http://tibasicdev.wikidot.com/getkey


This program is made with OS 5.7, but it should work on O.S versions 5.5 and 5.6.  Earlier OS versions may not have the Wait and toString commands.  


L6 refers to List 6, [ 2nd ]  [ 6  ].


Download the file here: 

https://drive.google.com/file/d/1QkrXnt6dR1KgEy-2d9JQxE7jmQS93Drk/view?usp=sharing

HP 32S and HP 32SII Week:  May 2, 2022 - May 6, 2022


Good luck!


Eddie



All original content copyright, © 2011-2022.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Saturday, July 11, 2020

HP 42S/DM42/Free42: GETKEY Demonstration

HP 42S/DM42/Free42:  GETKEY Demonstration

Good day everyone, hope everything is well with you today!

How GETKEY Works

To find the GETKEY command:  PGM.FCN menu ( [ (shift) ] [  3  ] ), [ ↑ ], (GETK)

GETKEY puts the HP 42S on hold and awaits for you to press a key.  Whatever key you press returns a key code.   Shift+Key combinations are allowed.  Key codes start at the upper left hand corner [ Σ+ ] with key code 1, going right then down, to the bottom right hand corner [ + ] with key code 37.

Hence:
[ Σ+ ] has code 1
[ 1/x ] has code 2
[ √x ] has code 3
[ LOG ] has code 4
and so on.

Shift+Key combinations add 37 to the key code.
[ (shift) ] [ Σ+ ] has code 38
[ (shift) ] [ 1/x ] has code 39
[ (shift) ] [ √x ] has code 40
[ (shift) ] [ LOG ] has code 41
and so on.

There is no key code for the shift key alone.

The key code is returned to the X stack, which can be stored and analyzed.  Pretty simple.

HP 42S/DM42/Free 42 Program:  TABU 

Raw file:  tabulate.raw
Download:  tabulate.raw

Running TABU:

You will see a running total, starting with 0.   Press [ 1 ] to add 1,  [ 2 ] to add 2, or [ 3 ]  to add 3 to the total.   Any other key will exit the program. 

Key codes used:  [ 1 ]:  29,  [ 2 ]:  30,  [ 3 ]:  31

00 { 62-Byte Prgm }
01▸LBL "TABU"
02 0
03 STO 00
04▸LBL 10
05 GETKEY
06 STO 01
07 32
08 X<Y?
09 GTO 11
10 R↓
11 28
12 X>=Y?
13 GTO 11
14 R↓
15 28
16 -
17 STO+ 00
18 CLA
19 "TOTAL: "
20 ARCL 00
21 AVIEW
22 GTO 10
23▸LBL 11
24 CLA
25 "FINAL: "
26 ARCL 00
27 AVIEW
28 .END.

HP 42S/DM42/Free 42 Program:  BUILD

Raw file:  build.raw
Download:  build.raw

Running BUILD:   A mathematical statement is generated with two random integers between -100 and 100.   The RAN command uses the range 0 ≤ n < 1 with n ≠ 1 for all results.   To get the range wanted, I use the subroutine RAN 201 * 100 - IP  (integer(201*RAN) - 100) to get the range -100 ≤ n < 101. 

 Key codes used:  [ + ]:  37,  [ - ]:  32,  [ × ]:  27

Any other key repeats the cycle until one of the acceptable keys are pressed.

00 { 150-Byte Prgm }
01▸LBL "BUILD"
02 ALL
03 XEQ 00
04 STO 01
05 XEQ 00
06 STO 02
07 GTO 01
08▸LBL 00
09 RAN
10 201
11 ×
12 100
13 -
14 IP
15 RTN
16▸LBL 01
17 RCL 01
18 RCL+ 02
19 STO 03
20 RCL 01
21 RCL- 02
22 STO 04
23 RCL 01
24 RCL× 02
25 STO 05
26▸LBL 02
27 CLA
28 "+, -, ×?"
29 AVIEW
30 GETKEY
31 37
32 X=Y?
33 GTO 03
34 R↓
35 32
36 X=Y?
37 GTO 04
38 R↓
39 27
40 X=Y?
41 GTO 05
42 GTO 02
43▸LBL 03
44 CLA
45 ARCL 01
46 ├" + "
47 ARCL 02
48 ├" = "
49 ARCL 03
50 GTO 06
51▸LBL 04
52 CLA
53 ARCL 01
54 ├" - "
55 ARCL 02
56 ├" = "
57 ARCL 04
58 GTO 06
59▸LBL 05
60 CLA
61 ARCL 01
62 ├" × "
63 ARCL 02
64 ├" = "
65 ARCL 05
66 GTO 06
67▸LBL 06
68 AVIEW
69 END

Example results (results will vary):

-53 + -32 = -85
93 - 19 = 74
41 × -15 = -615

And that concludes the demonstration on GETKEY. 

Eddie

All original content copyright, © 2011-2020.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

Saturday, July 20, 2019

TI Nspire CX II and TI Nspire CX II CAS: User Input in Graphics Mode with getKey

TI Nspire CX II  and TI Nspire CX II CAS:  User Input in Graphics Mode with getKey

Introduction

In graphics mode, the TI Nspire CX II cannot use the Text, Request, or RequestStr.  However, with the use of getKey command, we can have the user interact with the program. 

TI Nspire CX II Program keydemo2

This program is a game where the user stops a spinner in hopes to win money or a car.  Good luck!

Define keydemo2()=
Prgm
:Clear 
:Local l,str,k,n
:l:={"$200","$300","$500","$1000","CAR","Nope."}
:While getKey(0)≠"enter"
:  Clear 
:  UseBuffer 
:© get key continous execution
:  n:=randInt(1,6)
:  SetColor 0,0,0
:  DrawText 50,25,"PRESS enter TO STOP"
:  If n≤5 Then
:    SetColor 0,128,128
:  Else
:    SetColor 255,0,0
:  EndIf
:© use square brackets for elements 
:© we can clear only dynamic areas
:© but it does not look good
:  DrawText 50,50,l[n]
:  PaintBuffer 
:© usebuffer and paintbuffer allows all objects to be displayed at once, ensuring a smooth transition, can also use wait
:EndWhile
:EndPrgm



TI Nspire CX II Program enterdemo

With the use of getKey, the user enters a number in graphics mode.  This can be used as a template. 

Define enterdemo()=
Prgm
:© goal: develop input in graphics mode
:© use float mode
:setMode(1,1)
:Local str,num,k
:str:=""
:© use initial text
:Clear 
:  SetColor 0,0,0
:  DrawText 0,50,"Press [enter] to stop."
:
:
:© main loop: enter the numbers
:Loop
:  Clear 
:  UseBuffer 
:  SetColor 0,0,0
:  DrawText 0,50,"Press [enter] to stop."
:
:  k:=getKey(1)
:  If k="." and inString(str,".")=0 Then
:    str:=str&"."
:  ElseIf k="0" Then
:    str:=str&"0"
:  ElseIf k="1" Then
:    str:=str&"1"
:  ElseIf k="2" Then
:    str:=str&"2"
:  ElseIf k="3" Then
:    str:=str&"3"
:  ElseIf k="4" Then
:    str:=str&"4"
:  ElseIf k="5" Then
:    str:=str&"5"
:  ElseIf k="6" Then
:    str:=str&"6"
:  ElseIf k="7" Then
:    str:=str&"7"
:  ElseIf k="8" Then
:    str:=str&"8"
:  ElseIf k="9" Then
:    str:=str&"9"
:  ElseIf k="−" Then
:    str:=string(−1*expr(str))
:  ElseIf k="del" and dim(str)>0 Then
:    str:=left(str,dim(str)-1)
:
:  ElseIf k="enter" Then
:© "lock" the number and leave
:  SetColor 0,0,0
:  DrawText 0,100,str
:  PaintBuffer 
:    Exit
:  EndIf
:
:SetColor 0,0,255
:DrawText 0,100,str
:PaintBuffer 
:
:EndLoop
:© return number to home
:num:=expr(str)
:Disp num
:EndPrgm



There are two ways the getKey command can be used in graphics mode.

Eddie

All original content copyright, © 2011-2019.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

Thursday, May 10, 2018

HP Prime: Pentcolor Demonstration





HP Prime: Pentcolor Demonstration

Introduction

The HP Prime program PENTCOLOR allows the cycler to through colors with the touch of the following buttons:

Key [ 1 ]:  Red
Key [ 2 ]:  Green
Key [ 3 ]:  Blue

Each color setting cycles through the values of 0, 64, 128, 192, and 255.  Exit the program with the Escape key ( [ Esc ] ).  This program allows the user to cycle through different colors using RGB values.

 I use two subroutines in PENTCOLOR.  The first draws the text every time the key is selected, which allows for crisp text.   The second is for value calculation.  In subroutines on the HP Prime, all variables that are used must be local.  Global variables are passed through the subroutine arguments.  





HP Prime Program:  PENTCOLOR

SBR1(); // sub routine text
// allows for crisp text

SBR2(); // sub routine calc
// saves space in main program

// main program
EXPORT PENTCOLOR()
BEGIN
// EWS 2018-05-07

LOCAL K,R,G,B;
// R,G,B are set at 0
RECT();

TEXTOUT_P("Ready! 1. Red,
2. Green, 3. Blue",0,20,4);

REPEAT
K:=GETKEY;

// Key 1, red
IF K==42 THEN
RECT();
R:=SBR2(R);
SBR1(R,G,B);
END;

// Key 2, green
IF K==43 THEN
RECT();
G:=SBR2(G);
SBR1(R,G,B);
END;

// Key 3, blue
IF K==44 THEN
RECT();
B:=SBR2(B);
SBR1(R,G,B);
END;

UNTIL K==4; // ESC key

END;

// sub routines
// all subroutines have
// all local variables
SBR1(x,y,z)
BEGIN
TEXTOUT_P("[ 1 ]: Red: "+x,
0,20,4,#FF0000h);
TEXTOUT_P("[ 2 ]: Green: "+y,
0,40,4,#00FF00h);
TEXTOUT_P("[ 3 ]: Blue: "+z,
0,60,4,#0000FFh);
TEXTOUT_P("[Esc]: Exit",
0,120,4);
// draw box
FILLPOLY_P({(160,20),(310,20),
(310,170),(160,170)},RGB(x,y,z));
END;

SBR2(t)
BEGIN
t:=t+64;
IF t==256 THEN t:=255; END;
IF t==319 THEN t:=0; END;
RETURN t;
END;

Eddie

All original content copyright, © 2011-2018.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.  Please contact the author if you have questions.

Wednesday, September 21, 2016

TI-84 Plus CE and HP Prime: Pitch versus Angle

TI-84 Plus CE and HP Prime:  Pitch versus Angle

The program PITCH calculates the pitch and angle of various roof heights.  The pitch of a roof is the number of inches of vertical rise per 12 inches.  For example: a roof of 3/12 is pitch is a roof that rises 3 inches for every 12 horizontal inches. 

This program demonstrates the angle computer given different roof pitches.  You control the rise, anywhere for 0.5 ft to 12 ft.   Use the up and down arrow keys to control the rise. 

To exit the program:

TI-84 Plus CE:  Press [CLEAR]. 
HP Prime:  Press [Esc].

TI-84 Plus CE: PITCH



"9/8/2016 EWS"
FnOff
ZoomSto
ZStandard

AxesOff
6→H:0→K
Degree
ClrDraw
Repeat K=45
getKey→K

TextColor(BLUE)
Text(­1,0,10,"PITCH = ")
Text(­1,0,110,H)
Text(­1,0,160,"/12")

Line(­4,­4,4,­4,GREEN)
Line(4,­4,4,­4+H,ORANGE)
Line(­4,­4,4,­4+H,GREEN)

TextColor(BLACK)
Text(­1,130,10,"ANGLE = ")
Text(­1,130,115,tan(H/12)

If K=25 and H≠12
Then
ClrDraw
H+.5→H
End
If K=34 and H≠0.5
Then
H-.5→H
ClrDraw
End

End

AxesOn
ZoomRcl
Disp "DONE."


HP Prime:  PITCH



EXPORT PITCH()
BEGIN
// HP Prime, EWS 2061-09-21

Xmin:=−10;Xmax:=10;
Ymin:=−10;Ymax:=10;

LOCAL h,k,a,t,s1,s2;
k:=0; h:=6; a:=HAngle;
HAngle:=1;
RECT(0);
REPEAT

k:=GETKEY;

s1:="Pitch = "+h+"/12";
TEXTOUT(s1,−5,8,4,#7DF9FFh);

LINE(−4,−4,4,−4,#FF00h);
LINE(4,−4,4,−4+h,#EDC9AFh);
LINE(−4,−4,4,−4+h,#FF00h);

t:=ATAN(h/12);
s2:="Angle = "+t+"°";
TEXTOUT(s2,−5,−8,4,#7DF9FFh);

// UP
IF k==2 AND h≠12 THEN
RECT(0); h:=h+.5;
END;

// DOWN
IF k==12 AND h≠.5 THEN
RECT(0); h:=h-.5;
END

UNTIL k==4; // Esc

RETURN "Done.";

END;


Update & HHC 2016 Highlights to come!

I have returned from Colorado attending the HHC 2016 conferences.  The weekend went by way too fast!  Highlights are going to come later this week.


Eddie

This blog is property of Edward Shore, 2016.

Saturday, March 1, 2014

HP Prime/TI 84 Plus Color Silver Edition: Dynamic Programming: Orbit on a elliptic path.

This program orbits a θ character on a elliptical path. Use the arrow keys to change the size and the elliptical path. The program executes until ENTER is pressed.

Controls:
Left: Decrease of horizontal semi-axis
Right: Increase of horizontal semi-axis
Up: Increase of vertical semi-axis
Down: Decrease of vertical semi-axis
ENTER: Exits the program

HP Prime
EXPORT ORBIT( )
BEGIN
LOCAL A,B,K,I,X,Y;

// Initialize
A:=25; B:=25; I:=0;
HAngle:=0; // Radians
X:=160+A*COS(I);
Y:=120-B*SIN(I);
MSGBOX("Press Enter to exit.");
RECT( );

REPEAT
// clear old placements and text
RECT_P(X,Y,X+10,Y+12);
RECT_P(0,218,319,239);

// get input from keyboard
K:=GETKEY;

// Adjust A and B
IF K==7 AND A>1 THEN
A:=A+1; END;
IF K==8 AND A<50 THEN
A:=A-1; END;
IF K==2 AND B<50 THEN
B:=B+1; END;
IF K==12 AND B>1 THEN
B:=B-1; END;

// Move the satellite
I:=I+π/64;
X:=160+A*COS(I);
Y:=120-B*SIN(I);
TEXTOUT_P("θ",X,Y,2);
TEXTOUT_P("A = "+STRING(A)+", B = "+STRING(B),
0,2,#FFh);
WAIT(0.05);
UNTIL K==30;
END;


TI-84 Color Plus Silver Edition
Note: Pixels on the newer Color edition (size 250 × 150 approximately for the graph screen) opposed to the classic black and white TI-84+ (62 × 94)

Program: ORBIT
: ZStandard
: ZSquare
: FnOff
: Radian
: 5 → A
: 5 → B
: 0 → I
: ClrDraw
: AxesOff
: Repeat K=105
: Pt-Off(A cos(I), B sin(I), 2
: Text(150,0," " // 32 spaces - there is no box command
: GETKEY→ K
: If K=24 and A>1
: A-1 → A
: If K=26 and A<10
: A+1→ A
: If K=25 and B<10
: B+1→ B
: If K=34 and B>1
: B-1 → B
: I + π/16 → I
: Pt-On(A cos(I), B sin(I), 2
: Text(150,0,"A =",A,",B =",B
: End
: AxesOn

This blog is property of Edward Shore. 2014

Sunday, February 16, 2014

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

Here it is - a complete simple game.  The objective is to collect dollar signs (coins) to unlock the boundary at the top of the screen to allow access to the goal.  Trees block your progress.  Avoid the purple foes at all times, one touch and it's over!  One purple foe jumps around randomly while two fly horizontal.  

Once again, you are controlling the π character.   Press the plus key to quickly exit the game.  This comes in handy if the game ever becomes "unbeatable" (although I tried to knock out all the bugs). 





The game features a status bar at the bottom of the screen that shows two things:
*  The number of coins remaining
*  A "LOCKED" indicator that indicates that the boundary is active (red) 

This is a culmination of the Simple Game series.  The program is named MOVEM7.

Note: if you are using the emulator, let d1:=1200 and d2:=200.

EXPORT MOVEM7()
BEGIN
// Version 7
// 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
// This version puts it all together:
// Coins, trees, and enemies

// Game introduction
MSGBOX("Collect all 10 $ signs to unlock
the goal! Avoid the purple enemies!");

// 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;  // x coin
L3:=RANDINT(10,1,17)*12;  // y coin
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;

// Draw Trees
// Trees Setup
LOCAL tstr,L0,L1,S,tx,ty,J;
S:=10;
L0:=RANDINT(S,0,31)*10;   // x tree
L1:=RANDINT(S,1,17)*12;   // y tree
tstr:=CHAR(8857); // tree

// Make sure trees and coins do not
// appear in the same place.
// If there is a conflict, the coin wins.
// Middle
FOR I FROM 2 TO S-1 DO
FOR J FROM 1 TO 10 DO
IF (L0(I)==L2(J)) AND (L1(I)==L3(J)) THEN
// Use CONCAT and SUB
L0:=CONCAT(SUB(L0,1,I-1),SUB(L0,I+1,S));
L1:=CONCAT(SUB(L1,1,I-1),SUB(L1,I+1,S));
// Length of L0 and L1 decrease by 1
S:=S-1;
END;
END;
END;
// Left Side
FOR J FROM 1 TO 10 DO
IF (L0(1)==L2(J)) AND (L1(1)==L3(J)) THEN
L0:=SUB(L0,2,S); L1:=SUB(L1,2,S);
S:=S-1;
END;
END;
// Right Side
FOR J FROM 1 TO 10 DO
IF (L0(S)==L2(J)) AND (L1(S)==L3(J)) THEN
L0:=SUB(L0,1,S-1); L1:=SUB(L1,1,S-1);
S:=S-1;
END;
END;

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

// Ememy Setup
LOCAL estr,d1,d2;
LOCAL ey1A,ey2A,ey3A;
LOCAL ex1A,ex2A,ex3A;
LOCAL ey1B,ey2B,ey3B;
LOCAL ex1B,ex2B,ex3B;

// Delay
d1:=120;d2:=20;
// Enemy 1 jumps to random places
// Enemy 2 flies left to right
// Enemy 3 flies right to left

ex1A:=RANDINT(0,31)*10;
ey1A:=RANDINT(1,17)*12;

ex2A:=−10;
ey2A:=96;

ex3A:=330;
ey3A:=96;

estr:=CHAR(9991); //enemy
TEXTOUT_P(estr,ex1A,ey1A,2,#6000FFh);
TEXTOUT_P(estr,ex2A,ey2A,2,#8000FFh);
TEXTOUT_P(estr,ex3A,ey3A,2,#8000FFh);



// 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
// Delay Counter
d1:=d1-1; d2:=d2-1;

// Press an Arrow Key
K:=GETKEY;
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;

// Move the Enemies
IF d1==0 THEN
d1:=120;

// Enemy 1
ex1B:=RANDINT(0,31)*10;
ey1B:=RANDINT(2,17)*12;
END;

IF d2==0 THEN
d2:=20;

// Enemy 2
ex2B:=ex2A+10;
ey2B:=ey2A;
IF ex2B>330 THEN
ex2B:=−10;
ey2B:=RANDINT(1,17)*12;
END;

// Enemy 3
ex3B:=ex3A-10;
ey3B:=ey3A;
IF ex3B<−10 THEN
ex3B:=330;
ey3B:=RANDINT(1,17)*12;
END;

END;

// Clear previous enemy spot
// Fixed objects will be drawn later
RECT_P(ex1A,ey1A,ex1A+10,ey1A+12);
RECT_P(ex2A,ey2A,ex2A+10,ey2A+12);
RECT_P(ex3A,ey3A,ex3A+10,ey3A+12);

// Draw new enemy positions
TEXTOUT_P(estr,ex1B,ey1B,2,#6000FFh);
ex1A:=ex1B;
ey1A:=ey1B;
TEXTOUT_P(estr,ex2B,ey2B,2,#8000FFh);
ex2A:=ex2B;
ey2A:=ey2B;
TEXTOUT_P(estr,ex3B,ey3B,2,#8000FFh);
ex3A:=ex3B;
ey3A:=ey3B;

// 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;

// Ran into a tree?
FOR I FROM 1 TO S DO
IF C==L0(I)  AND D==L1(I) THEN
collide:=1;
BREAK;
// BREAK needed because we have a FOR loop
END;
END;

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

// Got hit by the enemy?
LOCAL death;
IF (C==ex1B) AND (D==ey1B) THEN
death:=1;
END;
IF (C==ex2B) AND (D==ey2B) THEN
death:=1;
END;
IF (C==ex3B) AND (D==ey3B) THEN
death:=1;
END;
IF death==1 THEN
MSGBOX("OUCH! :(");
BREAK;
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;
BREAK;
END;
END;
// Are all the coins collected?
IF coins==0 THEN lock:=0 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 the Goal
TEXTOUT_P(hstr,hx,hy,2,#964B00h);

// Draw the Forest
FOR I FROM 1 TO S DO
tx:=L0(I); ty:=L1(I);
TEXTOUT_P(tstr,tx,ty,2,#8000h);
END;

// Draw the Coins
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("Coins Left: "+
STRING(IP(coins)),0,228,2);
IF lock==1 THEN
TEXTOUT_P("LOCKED",260,228,2,#FFh);
END;

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

// Exit Conditions
// Plus Exit
IF K==50 THEN
MSGBOX("Program Terminated.");
END;
// Home Exit
IF C==hx AND D==hy THEN
MSGBOX("Home! :) "+CHAR(9829));
// CHAR(9829) produces a heart
END;

END;





Have fun, and hope you have learned something.  

Eddie

This blog is property of Edward Shore, 2014.

Earth's Radius by Latitude

Earth's Radius by Latitude Introduction: Calculating the Earth’s Radius In quick, general calculations, we assume that the...