-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
141 lines (138 loc) · 4.96 KB
/
main.ts
File metadata and controls
141 lines (138 loc) · 4.96 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
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
140
141
// Create itemPos variable and player object with default values
let itemPos: number[];
let player = {
posX: 2,
posY: 2,
length: 1,
direction: 'up',
};
// Function to spawn item
function spawnItem() {
// Checks if item already exists
if (itemPos) {
console.log('Item exists');
// Removes item
led.unplot(itemPos[0], itemPos[1])
console.log(`Unplotted item at position (${itemPos[0]}, ${itemPos[1]})`);
}
console.log('Item does not exist');
// Generates random X and Y values
const itemPosX: number = Math.floor(Math.random() * 5);
const itemPosY: number = Math.floor(Math.random() * 5);
// Activates LEDs at coordinates
led.plot(itemPosX, itemPosY);
// Records the item's position in the itemPos variable
itemPos = [itemPosX, itemPosY];
console.log(`Plotted item at position (${itemPos[0]}, ${itemPos[1]})`);
}
// Function to spawn player
function spawnPlayer() {
if (player.length == 1) {
led.plot(player.posX, player.posY);
} else {
for (let i = 0; i < player.length; i++) {
//
}
}
}
// Spawns initial item
spawnItem();
console.log('Spawned first item');
// Runs game loop
basic.forever(function () {
// Checks if player position is the same as item position
if (player.posX == itemPos[0] && player.posY == itemPos[1]) {
console.log('Player on item position');
// Adds point/length to player object
player.length += 1;
console.log('Player got 1 point');
console.log(`Current points: ${player.length}`)
// Spawns new item
spawnItem();
}
// Activates LED at player coordinates
led.plot(player.posX, player.posY)
console.log(`Plotted player in position (${player.posX.toString()}, ${player.posY.toString()})`);
// Records original positions for player
const origPosX: number = player.posX;
const origPosY: number = player.posY;
// If/else statements to keep track of direction and next player position
if (player.direction == "up") {
if (player.posY == 0) {
player.posY = 4;
}
else {
player.posY -= 1;
}
} else if (player.direction == "left") {
if (player.posX == 0) {
player.posX = 4;
}
else {
player.posX -= 1;
}
} else if (player.direction == "down") {
if (player.posY == 4) {
player.posY = 0;
}
else {
player.posY += 1;
}
} else if (player.direction == "right") {
if (player.posX == 4) {
player.posX = 0;
}
else {
player.posX += 1;
}
}
// Runs when the A button is pressed, which changes the direction 90° to the left
input.onButtonPressed(Button.A, function () {
// Records the player's original direction
const origDir = player.direction;
// If/else statements to change player direction
if (player.direction == 'up') {
player.direction = 'left';
console.log(`Player direction changed from ${origDir} to ${player.direction}`);
}
else if (player.direction == 'left') {
player.direction = 'down';
console.log(`Player direction changed from ${origDir} to ${player.direction}`);
}
else if (player.direction == 'down') {
player.direction = 'right';
console.log(`Player direction changed from ${origDir} to ${player.direction}`);
}
else if (player.direction == 'right') {
player.direction = 'up';
console.log(`Player direction changed from ${origDir} to ${player.direction}`);
}
})
// Runs when the B button is pressed, which changes the direction 90° to the right
input.onButtonPressed(Button.B, function () {
// Records the player's original direction
const origDir = player.direction;
// If/else statements to change player direction
if (player.direction == 'up') {
player.direction = 'right';
console.log(`Player direction changed from ${origDir} to ${player.direction}`);
}
else if (player.direction == 'left') {
player.direction = 'up';
console.log(`Player direction changed from ${origDir} to ${player.direction}`);
}
else if (player.direction == 'down') {
player.direction = 'left';
console.log(`Player direction changed from ${origDir} to ${player.direction}`);
}
else if (player.direction == 'right') {
player.direction = 'down';
console.log(`Player direction changed from ${origDir} to ${player.direction}`);
}
})
// Waits 1 second before continuing, crucial for being able to actually play
pause(1000);
// Deactivates LED at the player's original position
led.unplot(origPosX, origPosY);
console.log(`Unplotted player at position (${player.posX}, ${player.posY})`);
})