#codevember 3 - CharControl

Developer
Size
2,809 Kb
Views
18,216

How do I make an #codevember 3 - charcontrol?

Character controller with keyboard.. What is a #codevember 3 - charcontrol? How do you make a #codevember 3 - charcontrol? This script and codes were developed by Pedro Cacique on 20 November 2022, Sunday.

#codevember 3 - CharControl Previews

#codevember 3 - CharControl - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>#codevember 3 - CharControl</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <h1>Codevember 3</h1>
<section> <p><b>Instructions:</b></p> <p>Click at the canvas to start. Use left or right arrow to move the character.</p>
</section>
<canvas id="canvas"></canvas>
<footer> <p>Made by <a href="http://www.pedrocacique.com">Cacique</a> <a href="http://codevember.xyz/about">#codevember</a> <a href="http://codevember.xyz/day/3">#day3</a></p>
</footer> <script src="js/index.js"></script>
</body>
</html>

#codevember 3 - CharControl - Script Codes CSS Codes

html,body{ height: 100%; width: 100%; background-color: #f0f0f0; margin:0; font-family: Helvetica, Arial;
}
body{ display: flex; flex-direction: column; align-items: center; justify-content: center;
}
canvas{ background-color: #fff;
}
section{ width: 80%;
}
footer{ background-color: #202020; width: 80%; color: #fff; text-align: right; font-size: 0.8em;
}
footer a{ color: #d0d0d0; margin-right: 20px;
}
h1{ font-size: 1.2em; color: #5ba2d8; width: 80%;
}

#codevember 3 - CharControl - Script Codes JS Codes

var canvas, ctx, img, cw, ch;
var LEFT = 0, RIGHT = 1, IDLE = 0, RUN = 1;
var posX = 0, posY = 10, speed = 20, scale = 0.25;
var FPS = 1000 / 12;
var cont = 0, tick = 0, topY = 0;
var player_dir = RIGHT, player_state = IDLE, isRight = false, isLeft = false;
var w1 = 113, h1 = 308, w2 = 261, h2 = 289;
var interval;
window.onload = function(){ canvas = document.getElementById("canvas"); ctx = canvas.getContext("2d"); cw = window.innerWidth * 0.8; ch = 100; canvas.width = cw; canvas.height = ch; img = new Image(); img.src = "http://pedrocacique.com/game/img/spritesheet.png"; img.onload = function(){ init(); }
}
function init(){ interval = setInterval(gameloop, FPS); document.addEventListener('keydown', onKeyDown, false); document.addEventListener('keyup', onKeyUp, false);
}
function gameloop(){ update(); drawImage();
}
function drawImage(){ ctx.clearRect(0,0,cw,ch); switch(player_state){ case IDLE: topY = (player_dir==RIGHT)?0:h1; posY = ch - h1*scale; ctx.drawImage(img, cont*w1,topY,w1,h1, posX, posY, w1*scale, h1*scale); tick++; if(tick==10) { cont++; if(cont==2) cont=0; tick = 0; } break; case RUN: topY = (player_dir==RIGHT)?(2*h1):(2*h1+h2); ctx.drawImage(img, cont*w2, topY, w2, h2, posX, posY, w2*scale, h2*scale); cont++; if(cont==8) cont=0; break; }
}
function update(){ if(isRight) { posX+=speed; if(posX > cw - w2*scale) posX = cw - w2*scale; } if(isLeft) { posX-=speed; if(posX<0) posX = 0; }
}
function onKeyDown(event){ var codigo = event.keyCode; switch(codigo){ case 37: isLeft = true; player_dir = LEFT; switchState(RUN); break; case 39: player_dir = RIGHT; isRight = true; switchState(RUN); break; }
}
function onKeyUp(event){ var codigo = event.keyCode; switch(codigo) { case 37: isLeft = false; switchState(IDLE); break; case 39: isRight = false; switchState(IDLE); break; }
}
function switchState(state){ if(player_state != state){ if(state == IDLE && (isLeft || isRight) ){ player_state = RUN; player_dir = isLeft ? LEFT : RIGHT; } else { player_state = state; } cont = 0; }
}
#codevember 3 - CharControl - Script Codes
#codevember 3 - CharControl - Script Codes
Home Page Home
Developer Pedro Cacique
Username phcacique
Uploaded November 20, 2022
Rating 4
Size 2,809 Kb
Views 18,216
Do you need developer help for #codevember 3 - CharControl?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Pedro Cacique (phcacique) Script Codes
Create amazing blog posts with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!