HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
canvas#buffhammer
let c,
ctx,
fps = 60,
backGround,
player = {
ship: {
color: '#fff',
x: 0,
y: 0,
speed: {
x: 5,
y: 5,
rotate: 5,
},
width: 10,
height: 10,
rotation: 180,
},
score: 0,
lives: 10,
},
keysDown = {};
// I've made player an object with ship instead it because I would think lots of other things go in there too.
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / fps);
};
})();
window.onload = () => {
c = document.getElementById('buffhammer');
c.width = window.innerWidth;
c.height = window.innerHeight;
ctx = c.getContext('2d');
backGround = {
color: '#000',
x: 0,
y: 0,
width: c.width,
height: c.height
};
// we now know the size so lets put the ship in the middle
player.ship.x = (c.width/2) - (player.ship.width/2);
player.ship.y = (c.height/2) - (player.ship.height/2);
// Handle keyboard controls
addEventListener("keydown", function (e) {
e.preventDefault(); // this stops screen wiggle
keysDown[e.keyCode] = true;
}, false);
addEventListener("keyup", function (e) {
e.preventDefault(); // this stops screen wiggle
delete keysDown[e.keyCode];
}, false);
animationLoop();
}
let animationLoop = () => {
requestAnimFrame(animationLoop);
render();
}
let render = () => {
// all of your render code goes here
drawRect(backGround);
movePlayer();
drawShip(player.ship, player.ship.rotation);
//drawRect(player.ship, player.ship.rotation);
// write the score
let score = {
message: `Score: ${player.score}`,
x: 50,
y: 20
}
drawText(score);
// write the health
let health = {
message: `Lives Remaining: ${player.lives}`,
x: (c.width - 200),
y: 20
}
drawText(health);
}
// I'm changing it so you have to run so only up & down will let you move. This means the if's for wall collision need to be rewritten
let movePlayer = () => {
if (38 in keysDown) { // Player holding up
let ySpeed = player.ship.speed.y / (player.ship.rotation/180);// doing crazy things at this point
console.log(ySpeed);
player.ship.y = -ySpeed + player.ship.y;
player.ship.x += (player.ship.speed.y - ySpeed); // 5 is your max move so whatever didn't get used by y goes to x?
console.log(player.ship.speed.y - ySpeed);
// going to check if they've hit the top, if they have, put them on the bottom
if (player.ship.y <= 0) {
player.ship.y = c.height;
}
}
if (40 in keysDown) { // Player holding down
player.ship.y += player.ship.speed.y;
// going to check if they've hit the top, if they have, put them on the bottom
if (player.ship.y >= c.height) {
player.ship.y = 0;
}
}
if (37 in keysDown) { // Player holding left
//player.ship.x += -(player.ship.speed.x);
if (player.ship.x <= 0) {
player.ship.x = c.width;
}
player.ship.rotation += -player.ship.speed.rotate; // left is a negative degree
if (player.ship.rotation == 0) {
player.ship.rotation = 360; // i think 0 breaks everything
}
console.log(player.ship);
}
if (39 in keysDown) { // Player holding right
//player.ship.x += player.ship.speed.x;
if (player.ship.x >= c.width) {
player.ship.x = 0;
}
player.ship.rotation += player.ship.speed.rotate; // right is a positive degree
if (player.ship.rotation == 0) {
player.ship.rotation = 360; // i think 0 breaks everything
}
console.log(player.ship.rotation);
}
}
// if you want to draw lots of circles you'll use this function
let drawCircle = (circle) => {
ctx.fillStyle = circle.color;
ctx.beginPath();
ctx.arc(circle.x, circle.y, circle.radius, 0, Math.PI*2, true);
ctx.fill();
}
// if you want to draw lots of rectangles you'll use this function
let drawRect = (rectangle, rotation = 0) => {
ctx.fillStyle = rectangle.color;
ctx.rotate(rotation * Math.PI / 180); // this is used to rotate the object
ctx.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
ctx.setTransform(1, 0, 0, 1, 0, 0); // honestly no idea that this is: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/rotate
};
let drawShip = (rectangle, rotation = 0) => {
ctx.save();
ctx.beginPath();
ctx.translate((rectangle.x + rectangle.width / 2), (rectangle.y + rectangle.height / 2)); // this is supposed to rotate it around the center point
ctx.rotate(rotation * Math.PI / 180); // this is used to rotate the object
ctx.rect(-rectangle.width/2, -rectangle.height/2, rectangle.width, rectangle.height);
ctx.fillStyle = rectangle.color;
ctx.fill();
//ctx.fillRect(-rectangle.x/2, -rectangle.y/2, rectangle.width, rectangle.height);
//ctx.setTransform(1, 0, 0, 1, 0, 0); // honestly no idea that this is: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/rotate
ctx.restore();
};
// if you want to draw some text on the screen use this function
let drawText = (text) => {
ctx.fillStyle = text.color | '#fff';
ctx.font = '20px Helvetica';
ctx.fillText(text.message, text.x, text.y);
};
Also see: Tab Triggers