-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.jack
executable file
·125 lines (93 loc) · 2.31 KB
/
Main.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
class Main {
function char getDirection(char direction) {
var char tmp;
let tmp = Keyboard.keyPressed();
if ( (tmp = 87) | (tmp = 65) | (tmp = 83) | (tmp = 68) ) {
//Make sure snake can't go backwards on itself
if ( (direction = 87) & (tmp = 83) ) {
return 0;
}
if ( (direction = 83) & (tmp = 87) ) {
return 0;
}
if ( (direction = 65) & (tmp = 68) ) {
return 0;
}
if ( (direction = 68) & (tmp = 65) ) {
return 0;
} else {
return tmp;
}
}
return 0;
}
function Segment generateFood(Snake snake) {
var bool collide;
var Segment nfood;
let collide = -1;
while (collide) {
let nfood = Segment.new(Random.randRange(63),Random.randRange(31), null);
let collide = snake.overlap(nfood);
}
return nfood;
}
function void welcome() {
do Output.moveCursor(10, 24);
do Output.printString("Welcome to Snake!");
do Output.moveCursor(12, 18);
do Output.printString("Press W, A, S, or D to start");
return;
}
function void main() {
var int counter;
var char direction;
var Snake snake;
var Segment food;
var char tmp;
var bool ateFood;
var int score;
while(-1) {
let score = 0;
let direction = 0;
do Main.welcome();
while(direction = 0) {
let counter = counter + 1;
let direction = Main.getDirection(direction);
}
let snake = Snake.new(direction);
do Random.setSeed(counter);
let food = Main.generateFood(snake);
while( snake.isAlive() ) {
let tmp = Main.getDirection(direction);
if ( ~(tmp = 0) ) {
let direction = tmp;
}
// Move the snake and draw the snake
do Screen.clearScreen();
do snake.move(direction, food);
if ( snake.hasEaten() ) {
let score = score + 1;
do food.dispose();
let food = Main.generateFood(snake);
}
do Screen.drawCircle(food.getX()*8 + 4, food.getY()*8 + 4, 3);
do snake.drawSnake();
do Sys.wait(100);
}
do snake.dispose();
if ( ~(food = null)){
do food.dispose();
}
do Screen.clearScreen();
do Output.moveCursor(10, 18);
do Output.printString("Game over! Score: ");
do Output.printInt(score);
do Output.moveCursor(12, 18);
do Output.printString("Press space to continue.");
while( ~(Keyboard.keyPressed() = 32) ) {
}
do Screen.clearScreen();
}
return;
}
}