<html>
<head>
<link href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="//calebevans.me/projects/jcanvas/resources/jcanvas/jcanvas.js"></script>
<meta charset="utf-8">
<title>Tough Mudder Lunch and Learn</title>
</head>
<body>
<h1>Bobby Bananza</h1>
<canvas width="400" height="400"></canvas>
<div>
<button id="button-jump">jump</button>
</div>
<span id="debug"></span>
</body>
</html>
// Ideas:
// 1. Play with variables:
// characterX, characterY, characterYVelocity, ouchText
// 2. Finish increaseScore function
// 3. Create jump function
// a. alert function
// b. make the character jump
var characterX = 70;
var characterY = 400;
var characterYVelocity = 0;
var score = 0;
var ouchText = "Ouch";
var msLast = getMilliseconds();
var msSinceLast = 0;
var wallpng = "https://cdn0.iconfinder.com/data/icons/super-mono-reflection/red/wall_red.png";
var wallX;
var $canvas;
var showOuch = false;
var requestAnimationFrame =
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame;
// Main drawing function. Called every frame.
function draw(){
drawBackground();
drawCharacter();
drawScore();
drawWall();
increaseScore();
updateCoordinates();
// This is to redraw the next frame
requestAnimationFrame(draw);
}
// We need a jump function to let our character jump!
function increaseScore(){
// What to do here?
}
function drawBackground(){
$canvas.clearCanvas();
}
function drawCharacter(){
var feetY = characterY - 10;
var hipY = characterY - 60;
var armY = characterY - 90;
var neckY = characterY - 120;
// Draw head
$canvas.drawImage({
source: 'http://thomasongeri.com/landl/img/bob.gif',
x: characterX, y: neckY - 35,
width: 50, height: 50
});
// Draw legs
drawLine(characterX - 20, feetY, characterX, hipY);
drawLine(characterX + 20, feetY, characterX, hipY);
// Draw torso
drawLine(characterX, hipY, characterX, neckY);
// Draw arms
drawLine(characterX - 20, armY, characterX + 20, armY);
// Draw ouch?
if (showOuch){
$canvas.drawText({
strokeStyle: '#25a',
x: characterX + 40, y: characterY - 150,
fontSize: 25,
text: ouchText,
fromCenter: false
});
window.setTimeout(function(){ showOuch = false; }, 500);
}
}
function drawWall(){
if (wallX){
$canvas.drawImage({
source: wallpng,
x: wallX, y: 380,
fromCenter: true
});
// Update wall coordinates
if (wallX <= 0){
wallX = null;
} else{
wallX = wallX - 2;
}
if (wallX == characterX && characterY > 350){
// Character hit the wall :(
// Reset!
showOuch = true;
score = 0;
wallX = null;
}
} else{
if (Math.floor(Math.random() * 200) === 0){
wallX = 400;
}
}
}
function drawScore(){
$canvas.drawText({
fillStyle: 'blue',
x: 30, y: 10,
fontSize: 12,
text: 'Score: ' + score,
fromCenter: false
});
}
function drawLine(x1, y1, x2, y2){
$canvas.drawLine({
strokeStyle: "black",
strokeWidth: 2,
x1: x1, y1: y1, x2: x2, y2: y2
});
}
function updateCoordinates(){
var msNow = getMilliseconds();
msSinceLast = msNow - msLast;
msLast = msNow;
// Apply gravity
characterYVelocity = characterYVelocity - msSinceLast/100;
// Subtract the velocity so positive velocity means going up
characterY = characterY - characterYVelocity;
if (characterY > 400){
characterYVelocity = 0;
characterY = 400;
}
}
function getMilliseconds(){
var ms = (new Date()).getTime();
return ms;
}
$(document).ready(function(){
$canvas = $("canvas");
$("#button-jump").click(function(){
// jump button was clicked!
try{
jump();
} catch(e){}
});
requestAnimationFrame(draw);
});
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |