-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPA5movedot.java
More file actions
80 lines (63 loc) · 2.17 KB
/
Copy pathPA5movedot.java
File metadata and controls
80 lines (63 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* PA5movedot.java
*
* Local vars keep track of where dot is so can move it around with DPad.
* Also have a local var that stores a tone value and then uses that
* to call toneStart when a button is pressed.
*
* MMS, 2/2/11
*/
import meggy.Meggy;
class PA5movedot {
public static void main(String[] whatever){
new Dot().run();
}
}
class Dot {
byte curr_x;
byte curr_y;
Meggy.Color dotcolor;
public void run() {
Meggy.Tone localvar;
localvar = Meggy.Tone.C3;
// initialize color
dotcolor = Meggy.Color.ORANGE;
// initialize the x and y coordinates of the dot
curr_x = (byte)3;
curr_y = (byte)6;
// and then let the user move it around with the Dpad
while (true) {
if (Meggy.checkButton(Meggy.Button.Up)) {
this.movedot(curr_x, (byte)(curr_y+(byte)1));
Meggy.toneStart(localvar, 50);
} else {}
if (Meggy.checkButton(Meggy.Button.Down)) {
this.movedot(curr_x, (byte)(curr_y-(byte)1));
Meggy.toneStart(localvar, 50);
} else {}
if (Meggy.checkButton(Meggy.Button.Left)) {
this.movedot((byte)(curr_x-(byte)1), curr_y);
Meggy.toneStart(localvar, 50);
} else {}
if (Meggy.checkButton(Meggy.Button.Right)) {
this.movedot((byte)(curr_x+(byte)1), curr_y);
Meggy.toneStart(localvar, 50);
} else {}
Meggy.delay(100);
}
}
public void movedot(byte x, byte y) {
if (this.inBounds(x, y)) {
// darken the current location of the dot
Meggy.setPixel(curr_x, curr_y, Meggy.Color.DARK);
// light up the new location
Meggy.setPixel(x, y, dotcolor);
// update the dot's current location
curr_x = x;
curr_y = y;
} else {}
}
public boolean inBounds(byte x, byte y) {
return ((byte)(0-1) < x) && (x < (byte)8) && ((byte)(0-1) < y) && (y < (byte)8);
}
}