/**** * Classes ****/ // Class for Pokéball var Pokeball = Container.expand(function () { var self = Container.call(this); var pokeballGraphics = self.attachAsset('pokeball', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -5; self.update = function () { self.y += self.speed; }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Class for Pokémon var Pokemon = Container.expand(function () { var self = Container.call(this); var pokemonGraphics = self.attachAsset('pokemon', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 2; self.update = function () { self.x += self.speed; if (self.x > 2048 || self.x < 0) { self.speed *= -1; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Init game with sky blue background }); /**** * Game Code ****/ // Initialize variables var pokemons = []; var pokeballs = []; var score = 0; // Create score display var scoreTxt = new Text2('Score: 0', { size: 100, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Function to update score function updateScore() { scoreTxt.setText('Score: ' + score); } // Function to spawn a Pokémon function spawnPokemon() { var newPokemon = new Pokemon(); newPokemon.x = Math.random() * 2048; newPokemon.y = Math.random() * 1000 + 500; pokemons.push(newPokemon); game.addChild(newPokemon); } // Function to throw a Pokéball function throwPokeball(x, y) { var newPokeball = new Pokeball(); newPokeball.x = x; newPokeball.y = y; pokeballs.push(newPokeball); game.addChild(newPokeball); } // Handle game touch events game.down = function (x, y, obj) { throwPokeball(x, y); }; // Update game state game.update = function () { // Update Pokémon positions for (var i = pokemons.length - 1; i >= 0; i--) { pokemons[i].update(); } // Update Pokéball positions and check for collisions for (var j = pokeballs.length - 1; j >= 0; j--) { pokeballs[j].update(); for (var k = pokemons.length - 1; k >= 0; k--) { if (pokeballs[j].intersects(pokemons[k])) { score += 10; updateScore(); pokemons[k].destroy(); pokeballs[j].destroy(); pokemons.splice(k, 1); pokeballs.splice(j, 1); break; } } if (pokeballs[j] && pokeballs[j].y < -50) { pokeballs[j].destroy(); pokeballs.splice(j, 1); } } // Spawn new Pokémon every 120 ticks if (LK.ticks % 120 == 0) { spawnPokemon(); } };
/****
* Classes
****/
// Class for Pokéball
var Pokeball = Container.expand(function () {
var self = Container.call(this);
var pokeballGraphics = self.attachAsset('pokeball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -5;
self.update = function () {
self.y += self.speed;
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Class for Pokémon
var Pokemon = Container.expand(function () {
var self = Container.call(this);
var pokemonGraphics = self.attachAsset('pokemon', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.update = function () {
self.x += self.speed;
if (self.x > 2048 || self.x < 0) {
self.speed *= -1;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Init game with sky blue background
});
/****
* Game Code
****/
// Initialize variables
var pokemons = [];
var pokeballs = [];
var score = 0;
// Create score display
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to update score
function updateScore() {
scoreTxt.setText('Score: ' + score);
}
// Function to spawn a Pokémon
function spawnPokemon() {
var newPokemon = new Pokemon();
newPokemon.x = Math.random() * 2048;
newPokemon.y = Math.random() * 1000 + 500;
pokemons.push(newPokemon);
game.addChild(newPokemon);
}
// Function to throw a Pokéball
function throwPokeball(x, y) {
var newPokeball = new Pokeball();
newPokeball.x = x;
newPokeball.y = y;
pokeballs.push(newPokeball);
game.addChild(newPokeball);
}
// Handle game touch events
game.down = function (x, y, obj) {
throwPokeball(x, y);
};
// Update game state
game.update = function () {
// Update Pokémon positions
for (var i = pokemons.length - 1; i >= 0; i--) {
pokemons[i].update();
}
// Update Pokéball positions and check for collisions
for (var j = pokeballs.length - 1; j >= 0; j--) {
pokeballs[j].update();
for (var k = pokemons.length - 1; k >= 0; k--) {
if (pokeballs[j].intersects(pokemons[k])) {
score += 10;
updateScore();
pokemons[k].destroy();
pokeballs[j].destroy();
pokemons.splice(k, 1);
pokeballs.splice(j, 1);
break;
}
}
if (pokeballs[j] && pokeballs[j].y < -50) {
pokeballs[j].destroy();
pokeballs.splice(j, 1);
}
}
// Spawn new Pokémon every 120 ticks
if (LK.ticks % 120 == 0) {
spawnPokemon();
}
};