-
Notifications
You must be signed in to change notification settings - Fork 1
/
Snake.jack
executable file
·139 lines (112 loc) · 2.69 KB
/
Snake.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
class Snake {
field Segment snakeHead;
field Segment snakeTail;
field bool isDead;
field bool ateFood;
/** Constructs a new snake */
constructor Snake new(char direction) {
var int i;
// Create snake head
let snakeHead = Segment.new(30, 16, null);
let snakeTail = snakeHead;
let isDead = 0;
let i = 1;
// Create the rest of the snake
while(i < 5) {
do move(direction, null);
let i = i + 1;
}
return this;
}
// Sees if snake ate food last time it moved
method bool hasEaten() {
return ateFood;
}
// Checks if snake is still alive
method bool isAlive() {
return ~isDead;
}
// Makes sure newly generated food is not already on top of snake
method bool overlap(Segment food) {
var bool ol;
var Segment currentSeg;
let ol = 0;
let currentSeg = snakeHead;
while(~(currentSeg = null)) {
let ol = ol | currentSeg.collide(food);
let currentSeg = currentSeg.next();
}
return ol;
}
// Draws snake and checks if snake has hit itself
method void drawSnake() {
var Segment currentSeg;
do snakeHead.draw();
let currentSeg = snakeHead.next();
while(~(currentSeg = null)) {
let isDead = isDead | currentSeg.collide(snakeHead);
do currentSeg.draw();
let currentSeg = currentSeg.next();
}
return;
}
// Moves snake in specified direction, growing it if it eats food
method void move(char direction, Segment food) {
var int x;
var int y;
var Segment tmp;
let x = 0;
let y = 0;
let ateFood = 0;
let isDead = 0;
//Change based on direction
if (direction = 87) { //w
let y = -1;
} else {
if(direction = 65) { //a
let x = -1;
} else {
if(direction = 83) { //s
let y = 1;
} else { // d
let x = 1;
}
}
}
let tmp = Segment.new(snakeHead.getX() + x, snakeHead.getY() + y, null);
//Check if snake is trying to move off the screen and kills if is
if ( (tmp.getX() > -1) & (tmp.getY() > -1) & (tmp.getX() < 64) & (tmp.getY() < 32)) {
// adds TMP to front of snake
do tmp.add(snakeHead);
let snakeHead = tmp;
} else {
let isDead = -1;
do tmp.dispose();
}
if ((food = null) | tmp.collide(food)) {
//Leaves tail if food is eaten
let ateFood = -1;
} else {
// Deletes tail if no food eaten
let tmp = snakeTail.previous();
do tmp.setNext(null);
do snakeTail.dispose();
let snakeTail = tmp;
}
return;
}
// Deletes snake and all segments corresponding to snake
method void dispose() {
var Segment currentSeg;
var Segment tmp;
let currentSeg = snakeHead;
while(~(currentSeg = null)) {
// Deletes all segments
let tmp = currentSeg;
let currentSeg = currentSeg.next();
do tmp.dispose();
}
do Memory.deAlloc(this);
return;
}
}