-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.jack
78 lines (74 loc) · 1.76 KB
/
Game.jack
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
class Game
{
field SnakeBody head;
field Fruit fruit;
field int dir, count;
constructor Game new()
{
let count = 0;
let dir = 0;
let head = SnakeBody.new(254, 126);
let fruit = Fruit.new();
return this;
}
method void showStats()
{
var String str;
let str = "Score: ";
do Screen.clearScreen();
do Output.printString(str);
do Output.printInt(count);
return;
}
method void dispose()
{
do head.dispose();
do fruit.dispose();
do Memory.deAlloc(this);
return;
}
method void start()
{
var boolean skip;
var char key;
var int lastDir;
let skip = false;
while(~skip)
{
while ((key = 0) & (~skip))
{
let key = Keyboard.keyPressed();
if(fruit.resetPosition(head.getX(), head.getY()))
{
do head.incrementTailCount();
let count = count + 1;
}
let skip = head.moveBody(dir);
do Sys.wait(10);
}
if (key = 81)
{
let skip = true;
}
if ((key = 131) & (~(lastDir = 2)))
{
let dir = 1;
}
if ((key = 133) & (~(lastDir = 1)))
{
let dir = 2;
}
if ((key = 130) & (~(lastDir = 4)))
{
let dir = 3;
}
if ((key = 132) & (~(lastDir = 3)))
{
let dir = 4;
}
let lastDir = dir;
let key = 0;
}
return;
}
}