Code edit (12 edits merged)
Please save this source code
User prompt
in handleHitImpact when KO call game over
Code edit (1 edits merged)
Please save this source code
Code edit (6 edits merged)
Please save this source code
User prompt
add a new class HealthBar
Code edit (1 edits merged)
Please save this source code
User prompt
in punch function, increase self.currentPunchEnergy when punchCount is high, restore it to self.basePunchEnergy when its low
Code edit (2 edits merged)
Please save this source code
User prompt
in punch, count the number of punches per second
Code edit (18 edits merged)
Please save this source code
User prompt
when Athlete punched, reduce his energy by 2
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: self.bg is undefined' in or related to this line: 'self.fill = self.attachAsset('energyBarFill', {' Line Number: 355
Code edit (3 edits merged)
Please save this source code
User prompt
in EnergyBar class, add a energyBarBackground asset
Code edit (7 edits merged)
Please save this source code
User prompt
in EnergyBar class, add a lightning icon above
Code edit (1 edits merged)
Please save this source code
User prompt
in gameInitialize, Add the energy bar on the left
Code edit (1 edits merged)
Please save this source code
User prompt
add a class energyBar to handle energy level. use the asset energyBar and another asset energyBarFill
User prompt
add a vertical energyBAr on the right
Code edit (1 edits merged)
Please save this source code
/****
* Classes
****/
/****************************************************************************************** */
/************************************* ATHLETE CLASS ************************************** */
/****************************************************************************************** */
var Athlete = Container.expand(function (isPlayer) {
var self = Container.call(this);
self.isPlayer = isPlayer;
self.isPunching = false; // Indicates if the athlete is currently punching
self.isFighting = false;
self.isHit = false;
self.isTouched = false;
self.body = new Container();
game.addChild(self.body);
var punchDistance = 60; // Define punch distance for punching animation
var attackDistance = 500;
self.targetX = self.targetX || 1024; // Default target X
self.targetY = self.targetY || 1366; // Default target Y
// Left Arm
self.leftForearm = self.body.attachAsset('forearm', {
anchorX: 0.5,
anchorY: 0.5,
x: -110,
y: -125,
width: 80,
rotation: -Math.PI * 0.2
});
self.leftHand = self.body.attachAsset('hand', {
anchorX: 0.5,
anchorY: 0.5,
x: -100,
y: -250,
width: 90,
height: 180,
rotation: -Math.PI * 0.05,
tint: isPlayer ? 0xFFFFFF : 0x33CCFF
});
self.leftArm = self.body.attachAsset('arm', {
anchorX: 0.5,
anchorY: 0,
x: -120,
y: -120
});
// Right Arm
self.rightForearm = self.body.attachAsset('forearm', {
anchorX: 0.5,
anchorY: 0.5,
x: 110,
y: -125,
scaleX: -1,
width: 80,
rotation: Math.PI * 0.2
});
self.rightHand = self.body.attachAsset('hand', {
anchorX: 0.5,
anchorY: 0.5,
x: 100,
y: -250,
scaleX: -1,
width: 90,
height: 180,
rotation: Math.PI * 0.05,
tint: isPlayer ? 0xFFFFFF : 0x33CCFF
});
self.rightArm = self.body.attachAsset('arm', {
anchorX: 0.5,
anchorY: 0,
x: 120,
y: -120,
scaleX: -1
});
self.torso = self.body.attachAsset('body', {
anchorX: 0.5,
anchorY: 0.5
});
self.head = self.body.attachAsset(isPlayer ? 'head' : 'head2', {
anchorX: 0.5,
anchorY: 0.5,
x: 0,
y: -40
});
// Player movement speed
self.speed = 5;
// Punch function to handle punching action
self.punch = function (isLeft) {
log("self.punch: " + isLeft);
if (self.isPunching) {
return;
}
var arm = isLeft ? self.leftArm : self.rightArm;
var forearm = isLeft ? self.leftForearm : self.rightForearm;
var hand = isLeft ? self.leftHand : self.rightHand;
// Refactored punch animation logic using deltas method
self.isPunching = true;
self.setPosture(idlePosture);
var deltas = {
arm: {
height: punchDistance,
x: 15 * (isLeft ? 1 : -1),
y: -punchDistance
},
forearm: {
height: punchDistance,
x: 22 * (isLeft ? 1 : -1),
y: -punchDistance * 1.5,
rotation: isLeft ? 0.6 : -0.6
},
hand: {
x: 80 * (isLeft ? 1 : -1),
y: -punchDistance * 1.5,
rotation: isLeft ? 0.4 : -0.4
},
head: {
x: isLeft ? 15 : -15,
width: -0.05,
height: 0.05,
rotation: isLeft ? 0.1 : -0.1
}
};
// Apply deltas for punch animation
(function (arm, forearm, hand, head) {
LK.setTimeout(function () {
applyDeltas(arm, deltas.arm);
applyDeltas(forearm, deltas.forearm);
applyDeltas(hand, deltas.hand);
applyDeltas(head, deltas.head);
}, 10);
})(arm, forearm, hand, self.head);
(function (arm, forearm, hand, head) {
LK.setTimeout(function () {
applyDeltas(arm, reverseDeltas(deltas.arm));
applyDeltas(forearm, reverseDeltas(deltas.forearm));
applyDeltas(hand, reverseDeltas(deltas.hand));
applyDeltas(head, reverseDeltas(deltas.head));
self.isPunching = false;
}, 200);
})(arm, forearm, hand, self.head);
function applyDeltas(element, deltas) {
for (var key in deltas) {
element[key] += deltas[key];
}
}
function reverseDeltas(deltas) {
var reversed = {};
for (var key in deltas) {
reversed[key] = -deltas[key];
}
return reversed;
}
};
// Guard function to handle guarding action
self.guard = function (setGuard) {
log("self.guard: " + setGuard);
if (self.isGuarding === setGuard) {
return;
}
self.isGuarding = setGuard;
if (setGuard) {
self.setPosture(idlePosture);
self.head.height *= 0.95;
}
// Simplified adjustments for guarding using symmetrical elements
var adjustPosition = function adjustPosition(elementPair, deltaX, deltaY, deltaW, deltaH, deltaR, setGuard) {
elementPair.forEach(function (element, index) {
var directionH = (index === 0 ? 1 : -1) * (setGuard ? 1 : -1);
var directionV = setGuard ? 1 : -1;
element.x += deltaX * directionH;
element.y += deltaY * directionV; // Add vertical adjustment
element.rotation += deltaR * directionH;
element.width += deltaW * directionV; // Add width adjustment
element.height += deltaH * directionV; // Add height adjustment
});
};
// Pair elements for symmetrical adjustments
var elementPairs = [[self.leftHand, self.rightHand], [self.leftForearm, self.rightForearm], [self.leftArm, self.rightArm]];
// Adjustments for Hands, Forearms and Arms
var adjustments = [{
deltaX: 65,
deltaY: 90,
deltaW: 15,
deltaH: 15,
deltaR: 0.35
}, {
deltaX: 30,
deltaY: 60,
deltaW: 0,
deltaH: 0,
deltaR: 0.2
}, {
deltaX: 50,
deltaY: 10,
deltaW: 0,
deltaH: 0,
deltaR: 0.4
}];
adjustments.forEach(function (adj, index) {
return adjustPosition(elementPairs[index], adj.deltaX, adj.deltaY, adj.deltaW, adj.deltaH, adj.deltaR, setGuard);
});
if (!setGuard) {
self.setPosture(idlePosture);
}
};
// Update player position based on input
self.update = function () {
if (isPlaying) {
self[self.isPlayer ? 'mainPlayerMove' : 'mainAiMove']();
}
// Player is idle
self.miniMove();
// Update body position and rotation
self.body.x = self.x;
self.body.y = self.y;
// Calculate angle to face the opponent
var target = self.isPlayer ? opponent : player; // Determine target based on whether self is Player or Opponent
var angleToOpponent = Math.atan2(target.y - self.y, target.x - self.x) + Math.PI * 0.5;
self.body.rotation = angleToOpponent;
};
self.mainPlayerMove = function () {
var target = opponent;
};
self.mainAiMove = function () {
// AI Move
var target = player;
self.targetX = target.x;
self.targetY = target.y;
// Check if athlete has reached the target
var distanceToTarget = Math.sqrt(Math.pow(self.targetX - self.x, 2) + Math.pow(self.targetY - self.y, 2));
if (distanceToTarget < attackDistance) {
self.isInAttackRange = true;
//self.guard(false);
self.aiFight();
} else {
// Progressively move to the target
var moveX = (self.targetX - self.x) / distanceToTarget * self.speed;
var moveY = (self.targetY - self.y) / distanceToTarget * self.speed;
self.x += moveX;
self.y += moveY;
//self.guard(true);
}
};
self.aiFight = function () {
if (LK.ticks % 33 == 0 && !self.isPunching && !self.isFighting) {
self.punch(Math.random() < 0.5);
// Punch during 2sec
LK.setTimeout(function () {
self.isFighting = true;
// Stop durring 2 sec
LK.setTimeout(function () {
self.isFighting = false;
}, 4000 - 2000 * Math.random());
}, 4000 - 2000 * Math.random());
}
};
self.miniMove = function () {
if (self.isPunching || self.isGuarding || self.isHit) {
return;
}
// Mini head movement with added randomness
self.head.y = -40 + Math.sin(LK.ticks / 10 + Math.random() * 0.2) * 2;
// Mini arm and forearm movement with added randomness
self.leftArm.rotation += Math.sin(LK.ticks / 15 + Math.random() * 0.2) * 0.005;
self.leftForearm.rotation += Math.sin(LK.ticks / 15 + Math.random() * 0.2) * 0.005; // Add forearm movement
self.leftHand.x += Math.sin(LK.ticks / 15 + Math.random() * 0.2) * 0.5; // Add hand movement
self.leftHand.rotation += Math.sin(LK.ticks / 15 + Math.random() * 0.2) * 0.005; // Add hand movement
self.rightArm.rotation -= Math.sin(LK.ticks / 15 + Math.random() * 0.2) * 0.005;
self.rightForearm.rotation -= Math.sin(LK.ticks / 15 + Math.random() * 0.2) * 0.005; // Add forearm movement
self.rightHand.x -= Math.sin(LK.ticks / 15 + Math.random() * 0.2) * 0.5; // Add hand movement
self.rightHand.rotation -= Math.sin(LK.ticks / 15 + Math.random() * 0.2) * 0.005; // Add hand movement
};
self.setPosture = function (targetPosture) {
if (targetPosture == null) {
return;
}
log("self.setPosture: " + targetPosture.name);
// Torso
self.torso.x = targetPosture.torso.x;
self.torso.y = targetPosture.torso.y;
self.torso.width = targetPosture.torso.w;
self.torso.height = targetPosture.torso.h;
self.torso.rotation = targetPosture.torso.r;
// Head
self.head.x = targetPosture.head.x;
self.head.y = targetPosture.head.y;
self.head.width = targetPosture.head.w;
self.head.height = targetPosture.head.h;
self.head.rotation = targetPosture.head.r;
// Left Arm
self.leftArm.x = targetPosture.leftArm.x;
self.leftArm.y = targetPosture.leftArm.y;
self.leftArm.width = targetPosture.leftArm.w;
self.leftArm.height = targetPosture.leftArm.h;
self.leftArm.rotation = targetPosture.leftArm.r;
// Right Arm
self.rightArm.x = targetPosture.rightArm.x;
self.rightArm.y = targetPosture.rightArm.y;
self.rightArm.width = targetPosture.rightArm.w;
self.rightArm.height = targetPosture.rightArm.h;
self.rightArm.rotation = targetPosture.rightArm.r;
// Left Forearm
self.leftForearm.x = targetPosture.leftForearm.x;
self.leftForearm.y = targetPosture.leftForearm.y;
self.leftForearm.width = targetPosture.leftForearm.w;
self.leftForearm.height = targetPosture.leftForearm.h;
self.leftForearm.rotation = targetPosture.leftForearm.r;
// Right Forearm
self.rightForearm.x = targetPosture.rightForearm.x;
self.rightForearm.y = targetPosture.rightForearm.y;
self.rightForearm.width = targetPosture.rightForearm.w;
self.rightForearm.height = targetPosture.rightForearm.h;
self.rightForearm.rotation = targetPosture.rightForearm.r;
// Left Hand
self.leftHand.x = targetPosture.leftHand.x;
self.leftHand.y = targetPosture.leftHand.y;
self.leftHand.width = targetPosture.leftHand.w;
self.leftHand.height = targetPosture.leftHand.h;
self.leftHand.rotation = targetPosture.leftHand.r;
// Right Hand
self.rightHand.x = targetPosture.rightHand.x;
self.rightHand.y = targetPosture.rightHand.y;
self.rightHand.width = targetPosture.rightHand.w;
self.rightHand.height = targetPosture.rightHand.h;
self.rightHand.rotation = targetPosture.rightHand.r;
};
});
/****************************************************************************************** */
/************************************* GUARD BUTTON CLASS ********************************** */
/****************************************************************************************** */
var GuardButton = Container.expand(function () {
var self = Container.call(this);
self.buttonAsset = self.attachAsset('guardButton', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.8
});
self.x = 400; // Position button in the bottom center
self.y = game.height - 350;
self.down = function (x, y, obj) {
player.guard(true); // Activate guard state for player
self.width *= 0.9;
self.height *= 0.9;
};
self.up = function (x, y, obj) {
player.guard(false);
self.width = 600;
self.height = 600;
};
});
/****************************************************************************************** */
/************************************* PUNCH BUTTON CLASS ********************************** */
/****************************************************************************************** */
var PunchButton = Container.expand(function () {
var self = Container.call(this);
self.buttonAsset = self.attachAsset('punchButton', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.8
});
self.x = game.width - 400; // Position button on the bottom right
self.y = game.height - 350;
self.down = function (x, y, obj) {
// Simulate button press by triggering punch with left arm
player.punch(Math.random() < 0.5); // Randomly choose left or right arm for punching
self.width *= 0.9;
self.height *= 0.9;
};
self.up = function (x, y, obj) {
// Simulate button release by triggering punch with right arm
self.width = 600;
self.height = 600;
};
});
/****************************************************************************************** */
/**************************************** RING CLASS ************************************** */
/****************************************************************************************** */
var Ring = Container.expand(function () {
var self = Container.call(this);
self.leftBorder = 300;
self.rightBorder = 1800;
self.topBorder = 200;
self.bottomBorder = 1750;
self.ringAsset = self.attachAsset('ring', {
anchorX: 0.5,
anchorY: 0.5
});
self.x = 1024; // Center of the screen horizontally
self.y = 1366 - self.height / 6; // Center of the screen vertically
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
/****************************************************************************************** */
/************************************** GLOBAL VARIABLES ********************************** */
/****************************************************************************************** */
var isPlaying = false;
var ring;
var punchButton;
var guardButton;
var player;
var opponent;
var touchPosition = null;
var swipeStart = null;
var swipeEnd = null;
var isDebug = true;
var debugMarker;
/****************************************************************************************** */
/************************************** POSTURES ****************************************** */
/****************************************************************************************** */
var idlePosture = {
name: "idlePosture",
// Left Arm
leftForearm: {
x: -110,
y: -125,
w: 80,
h: 120,
r: -Math.PI * 0.2
},
leftHand: {
x: -110,
y: -250,
w: 80,
h: 180,
r: -Math.PI * 0.05
},
leftArm: {
x: -120,
y: -120,
w: 80,
h: 150,
r: 0
},
// Right Arm
rightForearm: {
x: 110,
y: -125,
w: 80,
h: 120,
r: Math.PI * 0.2
},
rightHand: {
x: 110,
y: -250,
w: 90,
h: 180,
r: Math.PI * 0.05
},
rightArm: {
x: 120,
y: -120,
w: 80,
h: 150,
r: 0
},
torso: {
x: 0,
y: 0,
w: 300,
h: 150,
r: 0
},
head: {
x: 0,
y: -40,
w: 150,
h: 190,
r: 0
}
};
/****************************************************************************************** */
/*********************************** UTILITY FUNCTIONS ************************************ */
/****************************************************************************************** */
function log() {
if (isDebug) {
var _console;
(_console = console).log.apply(_console, arguments);
}
}
/****************************************************************************************** */
/************************************** INPUT HANDLERS ************************************ */
/****************************************************************************************** */
game.down = function (x, y, obj) {
if (!isPlaying) {
isPlaying = true;
}
};
game.move = function (x, y, obj) {};
game.up = function (x, y, obj) {};
/****************************************************************************************** */
/************************************ GAME INITIALIZE ************************************* */
/****************************************************************************************** */
function gameInitialize() {
log("Game initialize...");
// Initialize Ring
ring = game.addChild(new Ring());
punchButton = game.addChild(new PunchButton());
guardButton = game.addChild(new GuardButton());
// Initialize Player
player = game.addChild(new Athlete(true));
player.x = 1024; // Center horizontally
player.y = game.height * 0.55; // Position towards the bottom
// Initialize Opponent
opponent = game.addChild(new Athlete());
opponent.x = 1024; // Center horizontally
opponent.y = game.height * 0.2; // Center vertically in the middle of the ring
opponent.rotation = Math.PI * 0.5;
if (isDebug) {
// Debug Marker
debugMarker = LK.getAsset('debugMarker', {
anchorX: 0.5,
anchorY: 0.5
});
game.addChild(debugMarker);
debugMarker.x = 1800;
debugMarker.y = 1750;
}
}
/****************************************************************************************** */
/************************************* GAME FUNCTIONS ************************************* */
/****************************************************************************************** */
function handleHit(attacker, defender) {
defender.isHit = true;
defender.isTouched = !defender.isGuarding;
var backDelta = defender.isGuarding ? 2 : 50;
var backHeadDelta = defender.isGuarding ? 1 : 4;
// Player hit reaction: head goes back, flash screen red
// Calculate defender's current direction and move back accordingly
var defenderDirection = Math.atan2(defender.y - attacker.y, defender.x - attacker.x);
// Move defender back
var newX = defender.x + Math.cos(defenderDirection) * backDelta;
var newY = defender.y + Math.sin(defenderDirection) * backDelta;
// Check if newX and newY are within ring's borders
var withinLimits = newX >= ring.leftBorder && newX <= ring.rightBorder && newY >= ring.topBorder && newY <= ring.bottomBorder;
log("Is in ring ?", withinLimits);
if (withinLimits) {
defender.x = newX; // Move defender back in the x direction of current movement
defender.y = newY; // Move defender back in the y direction of current movement
} else {
// If out of limits, move attacker back instead
/*
var attackerDirection = Math.atan2(attacker.y - defender.y, attacker.x - defender.x);
attacker.x += Math.cos(attackerDirection) * backDelta;
attacker.y += Math.sin(attackerDirection) * backDelta;
*/
}
var flashColor = defender.isGuarding ? 0xcccccc : 0xFF0000;
if (defender.isGuarding) {
LK.effects.flashObject(defender.leftHand, flashColor, 100); // Flash the defender
LK.effects.flashObject(defender.rightHand, flashColor, 100); // Flash the defender
} else {
LK.effects.flashObject(defender.head, flashColor, 100); // Flash the defender
LK.effects.flashObject(defender.torso, flashColor, 100); // Flash the defender
}
LK.effects.flashObject(defender.leftArm, flashColor, 100); // Flash the defender
LK.effects.flashObject(defender.leftForearm, flashColor, 100); // Flash the defender
LK.effects.flashObject(defender.rightArm, flashColor, 100); // Flash the defender
LK.effects.flashObject(defender.rightForearm, flashColor, 100); // Flash the defender
// Head back movement
defender.head.x += 2.5 * Math.cos(defenderDirection) * backHeadDelta; // Head returns to normal position
defender.head.y += 2.5 * Math.sin(defenderDirection) * backHeadDelta; // Head returns to normal position
defender.torso.x += Math.cos(defenderDirection) * backHeadDelta; // Head returns to normal position
defender.torso.y += Math.sin(defenderDirection) * backHeadDelta; // Head returns to normal position
(function (defender, defenderDirection, backHeadDelta) {
LK.setTimeout(function () {
defender.head.x -= 2.5 * Math.cos(defenderDirection) * backHeadDelta; // Head returns to normal position
defender.head.y -= 2.5 * Math.sin(defenderDirection) * backHeadDelta; // Head returns to normal position
defender.torso.x -= Math.cos(defenderDirection) * backHeadDelta; // Head returns to normal position
defender.torso.y -= Math.sin(defenderDirection) * backHeadDelta; // Head returns to normal position
defender.isHit = false;
if (!defender.isGuarding) {
defender.setPosture(idlePosture);
}
}, 500);
})(defender, defenderDirection, backHeadDelta);
}
/****************************************************************************************** */
/************************************** MAIN GAME LOOP ************************************ */
/****************************************************************************************** */
// Game update function
game.update = function () {
// This section has been removed to prevent redundant player movement handling.
// Check for collision between player's hand and opponent's head
if (player.isPunching && (player.leftHand.intersects(opponent.torso) || player.rightHand.intersects(opponent.torso))) {
handleHit(player, opponent);
/*
// Player hit reaction: head goes back, flash screen red
// Calculate opponent's current direction and move back accordingly
var opponentDirection = Math.atan2(opponent.y - player.y, opponent.x - player.x);
opponent.x += Math.cos(opponentDirection) * 50; // Move opponent back in the x direction of current movement
opponent.y += Math.sin(opponentDirection) * 50; // Move opponent back in the y direction of current movement
opponent.head.y -= 20; // Head returns to normal position
LK.effects.flashScreen(0xFFFFFF, 100); // Flash the whole screen red for 0.5 seconds
LK.setTimeout(function () {
opponent.head.y += 20; // Head returns to normal position
}, 500);
*/
}
if (opponent.isPunching && (opponent.leftHand.intersects(player.torso) || opponent.rightHand.intersects(player.torso))) {
handleHit(opponent, player);
}
};
gameInitialize();
clear
basic light gray convex round button with a red boxing glove icon. UI
Un gant de boxe bleu vu de dessus. video game
basic light round convex gray button with a raised blue shield icon.. UI
un éclair. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
remove
a basic white heart.. game icon
A boxer has lost the match..
man boxer with red gloves is KO on the ring..