-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
132 lines (112 loc) · 3.19 KB
/
Copy pathmain.js
File metadata and controls
132 lines (112 loc) · 3.19 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
let game;
let imgs = [];
let frameData = [];
let spriteSheet;
let spriteMeta;
function preload() {
// Load sprite sheet image and metadata JSON
spriteSheet = loadImage("spritesheet.webp"); // Replace with your sprite sheet path
spriteMeta = loadJSON("spritesheet.json"); // Replace with your metadata path
}
function setup() {
// Extract frame data from JSON
for (let i = 0; i < spriteMeta.frames.length; i++) {
let frame = spriteMeta.frames[i].frame;
frameData.push({
filename: spriteMeta.frames[i].filename,
x: frame.x,
y: frame.y,
w: frame.w,
h: frame.h,
});
}
// Load each image (frame) from the sprite sheet
for (let i = 0; i < frameData.length; i++) {
let frame = frameData[i];
imgs.push(spriteSheet.get(frame.x, frame.y, frame.w, frame.h));
}
let canvas = createCanvas(800, 800);
canvas.parent("sketch");
game = new Game();
startMs = millis();
textSize(100);
textAlign(CENTER, CENTER);
let dSeconds = localStorage.getItem("MAX_SECONDS") || 60;
changeTimeMode(dSeconds);
}
function draw() {
background("#70C1B3");
game.drawCards(); // Assuming your game logic uses this function
text(seconds, width / 2, 100);
text(game.score, width / 2, height - 100);
if (!game.gameOn) return;
let currentMs = millis();
if (currentMs - startMs > 1000) {
startMs = currentMs;
seconds--;
if (seconds <= 0) {
seconds = 0;
game.scores.push([
round(new Date().getTime() / 1000),
MAX_SECONDS,
game.rounds,
game.score,
]);
updateScoreDiv();
game.stopGame();
}
}
let cursorOverElement = false;
for (let i = 0; i < game.card1Locs.length; i++) {
let thisLoc = game.card1Locs[i];
if (mouseInside(mouseX, mouseY, thisLoc.x, thisLoc.y, 50, 50)) {
cursorOverElement = true;
break;
}
}
for (let i = 0; i < game.card2Locs.length; i++) {
let thisLoc = game.card2Locs[i];
if (mouseInside(mouseX, mouseY, thisLoc.x, thisLoc.y, 50, 50)) {
cursorOverElement = true;
break;
}
}
// Update the cursor based on whether it's over an element
if (cursorOverElement) {
cursor("pointer"); // Set the cursor to a pointer when over an element
} else {
cursor("default"); // Set the cursor back to default if not over an element
}
}
function mouseClicked() {
console.log(mouseX, mouseY);
game.isSymbolClicked(mouseX, mouseY);
}
function startGame() {
seconds = MAX_SECONDS;
game.startGame();
}
function updateScoreDiv() {
let scoresDiv = document.getElementById("scores");
scoresDiv.innerHTML = "";
for (let s of game.scores) {
let span = document.createElement("span");
span.innerHTML = s.join(",");
scoresDiv.appendChild(span);
scoresDiv.appendChild(document.createElement("br"));
}
}
function changeTimeMode(mode) {
console.log("mode", mode);
let modesDiv = document.getElementsByClassName("timeMode");
for (let i = 0; i < modesDiv.length; i++) {
if (modesDiv[i].innerHTML == mode) {
modesDiv[i].classList.add("active");
} else {
modesDiv[i].classList.remove("active");
}
}
MAX_SECONDS = mode;
seconds = MAX_SECONDS;
localStorage.setItem("MAX_SECONDS", mode);
}