Docs - james-bern/CS136 GitHub Wiki
🐮 Cow.java
Cow.java is a simple Java dialect and game library designed to prepare you to program in C
🆎 primitive types
- Use
boolean
for truth values,double
for real numbers,int
for (signed) integers, andchar
for characters- NOTE: do NOT use
byte
,short
,long
, orfloat
- NOTE: do NOT use
PRINT(thing);
is likeSystem.out.println(thing);
but can actually prints arrays, printschar
's like'A'
, and prints the 0 char like'\0'
📏 math
Number MIN(Number a, Number b);
returns the lesser of the two arguments (can beint
ordouble
)Number MAX(Number a, Number b);
returns the greater of the two arguments (can beint
ordouble
)Number ABS(Number a);
returns $|a|$ (can beint
ordouble
)double SQRT(double a);
returns $\sqrt{a}$- NOTE: to square a number use
a * a
int MODULO(int a, int b);
is likea % b
but works for negative numbers (like Python's%
🐍)int ROUND(double a);
rounds $a$ to the nearest integerboolean ARE_EQUAL(double a, double b);
checks whether $a \approx b$ (allows for "floating point error"); returns $(|a - b| < \varepsilon)$ for a small $\varepsilon$
🐄 app
-
void beginFrame();
can be used to create a game/app with the classic setup-loop structureclass ExampleApp extends Cow { public static void main(String[] arguments) { // setup double time = 0.0; while (beginFrame()) { // loop time += 0.033; drawCircle(mouseX, mouseY, 32.0, colorRainbowSwirl(time)); } } }
🎹 keyboard
boolean keyPressed(char key);
returns whetherkey
was pressed on your keyboard during the previous framekeyPressed('A')
returns whether the key labeledA
was just pressed on your keyboard- NOTE: do NOT use
keyPressed('a')
(there is no key labeleda
on your keyboard) - NOTE: for uppercase and lowercase use
(keyHeld(SHIFT) && keyPressed('A'))
and((!keyHeld(SHIFT)) && keyPressed('A'))
- NOTE: do NOT use
boolean keyHeld(char key);
returns whetherkey
is currently held on your keyboardboolean keyReleased(char key);
boolean keyToggled(char key);
is useful for creating "toggle switches"- NOTE:
keyToggled('A')
will befalse
ifA
has never been pressed,true
if it has been pressed once, `false if it has been pressed twice, ...
- NOTE:
🔑 keys
For most keys, you can use a char
, such as 'A'
for "the A key" and ' '
for "the space bar." For other special keys, you can use these constants.
char ENTER, TAB;
char BACKSPACE, DELETE;
char CONTROL, COMMAND, ALT, SHIFT;
char LEFT_ARROW, RIGHT_ARROW, UP_ARROW, DOWN_ARROW;
🐭 mouse
double mouseX;
is the x-coordinate of the current mouse position- NOTE: $x$ increases as we move from left to right
double mouseY;
is the y-coordinate of the current mouse position- NOTE: $y$ increases as we move from bottom to top
boolean mousePressed;
is whether your left mouse button was pressed during the previous frameboolean mouseHeld;
is whether your left mouse button is currently heldboolean mouseReleased;
✏️ draw
Simple drawing functions.
void drawLine(double x1, double y1, double x2, double y2, Color color, double thickness);
draws a line connecting $(x_1, y_1)$ and $(x_2, y_2)$;thickness
is in something like pixelsvoid drawCircle(double x, double y, double radius, Color color);
draws a circle with center $(x, y)$ and radius $r$void drawRectangle(double x1, double y1, double x2, double y2, Color color);
draws a rectangle with corners $(x_1, y_1)$ and $(x_2, y_2)$- NOTE: The style arguments
color
andthickness
are optional (they default to BLACK and a reasonable thickness).
🎨 color
My simple color class. We will just use the constants it provides in our calls to the drawing functions.
Color BLACK, BLUE, BROWN, CYAN, GREEN, MAGENTA, ORANGE, PURPLE, RED, YELLOW, WHITE;
Color colorRainbowSwirl(double time);
is a cyclic rainbow color map.
🖼️ canvas
canvasConfig(double left, double bottom, double right, double top, Color color, int maxDimensionInPixels)
; can be used to set up the canvas coordinate system and size- NOTE: the left-bottom corner of the canvas is $(x_\text{left}, y_\text{bottom})$ in "world units"
- NOTE: the right-top corner of the canvas is $(x_\text{right}, y_\text{top})$ in "world units"
- NOTE: the canvas is cleared to
color
eachbeginFrame()
- NOTE: the longer side of the canvas is
maxDimensionInPixels
pixels long on your screen - NOTE: the
color
andmaxDimensionInPixels
arguments are optional (they default toWHITE
and512
) - NOTE: calling this function at all is optional (the other arguments default to
-256
,256
,-256
,256
)- NOTE: but if you're going to call this function, put it just above
while (beginFrame()) { ... }
- NOTE: but if you're going to call this function, put it just above