Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- a:link{
- color: white;
- }
- a:visited{
- color: white;
- }
- .bg-1{
- background-color: #882190;
- color: #ffffff;
- }
- .bg-2{
- width: 60%;
- height: 40%;
- margin: auto;
- background-color: #3882CB;
- color: #ffffff;
- }
- .container-canvas {
- margin-top: 25px;
- margin-bottom: 25px;
- margin-right: auto;
- margin-left: auto;
- width: 900px;
- }
- canvas{
- background: #A2C9F0;
- }
- </style>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
- </head>
- <body>
- <div class="container-fluid bg-1 text-center">
- <h1>JumpCubes in JavaScript</h1>
- <p>Made by Ben Bollinger</p>
- </div>
- <div class="container-canvas">
- <canvas id="ctx" tabindex=0 width=900 height=500 style="border:1px solid #000000;" onkeypress="movePlayer(event)" onkeyup="keyUp(event)"></canvas>
- </div>
- <div class="container-fluid bg-2 text-center">
- <a href="https://benbcompsci.wordpress.com/">My Main Website</a>
- </div>
- <script>
- var canvas = document.getElementById("ctx");
- var ctx = canvas.getContext("2d");
- canvas.setAttribute('tabindex', 0);
- canvas.focus();
- canvas.addEventListener("keydown", movePlayer);
- //Maybe I can get a class working?
- class Coin {
- constructor(x, y){
- this.xPos = x;
- this.yPos = y
- this.xSize = 10;
- this.initialX = x;
- this.initialY = y;
- }
- render() {
- ctx.save()
- ctx.fillStyle = "blue";
- ctx.fillRect(this.xPos, this.yPos, this.xSize, 10);
- ctx.restore()
- }
- get getX() {
- return this.xPos;
- }
- get getY() {
- return this.yPos;
- }
- get getxSize(){
- return this.xSize;
- }
- remove(){
- this.xPos = 10000;
- this.yPos = 10000;
- }
- recreate(){
- this.xPos = this.initialX;
- this.yPos = this.initialY;
- }
- }
- //Coins:
- coin1 = new Coin(800, 450);
- coin2 = new Coin(450, 260);
- var coinArray = [];
- class Platform {
- constructor(x, y, xS, yS, moveBool) {
- this.xPos = x;
- this.yPos = y;
- this.xSize = xS;
- this.ySize = yS;
- this.moveable = moveBool;
- }
- render() {
- ctx.save()
- ctx.fillStyle = "red";
- ctx.fillRect(this.xPos, this.yPos, this.xSize, this.ySize);
- ctx.restore()
- }
- get getX() {
- return this.xPos;
- }
- get getY() {
- return this.yPos;
- }
- get getxSize() {
- return this.xSize;
- }
- get getySize() {
- return this.ySize;
- }
- get getMoveable() {
- return this.moveable;
- }
- }
- //Platform array:
- var platformArray = [];
- //Vars:
- var x_new = 50;
- var y_new = 50;
- var isJumping = false;
- var isColliding = false;
- //Movement vars:
- var speed = 10;
- var jump = 0;
- var maxJump = 23;
- var shouldJump = true;
- var leftOrRight = "";
- //Moving platform vars:
- var plat1X = 0;
- var direction = '';
- var keys = {
- up: false,
- right: false,
- left: false
- };
- function movePlayer(event) {
- switch(event.keyCode){
- //Right key down:
- case 39:
- keys["right"] = true;
- leftOrRight = "right";
- break;
- //Left key down:
- case 37:
- keys["left"] = true;
- leftOrRight = "left";
- break;
- //Up key down:
- case 38:
- keys["up"] = true;
- isJumping = true;
- break;
- }
- }
- function keyUp(event){
- switch(event.keyCode){
- //Up key up:
- case 38:
- isJumping = false;
- keys["up"] = false;
- break;
- //Right key up:
- case 39:
- keys["right"] = false;
- break;
- //Left key up:
- case 37:
- keys["left"] = false;
- break;
- }
- }
- //Intersection function:
- function boundsIntersect(x1,y1,x2,y2, xSize){
- if(x1 > x2-50 && x1 < x2+xSize && y1 < y2 && y1 > y2-50){
- return true;
- }
- return false;
- }
- //Scoring:
- var score = 0;
- function resetGame(){
- //Reset Position:
- x_new = 5;
- y_new = 430;
- //Reset score and coins:
- score = 0;
- coinArray.forEach(function(coin){
- coin.recreate();
- });
- }
- var gravitySpeed = 10;
- function update(){
- //Draw player:
- window.requestAnimationFrame(update);
- ctx.clearRect(0,0,900,500);
- ctx.fillStyle = "black";
- ctx.beginPath();
- ctx.fillRect(x_new,y_new,50,50);
- //Draw ground:
- ctx.beginPath();
- ctx.rect(0,490,900,10);
- ctx.fillStyle = "green";
- ctx.fill();
- //PLayer movement:
- if(keys["up"] && !keys["right"] && !keys["left"] && shouldJump){
- y_new-=speed;
- jump++;
- leftOrRight = "";
- } else if(keys["right"] && !keys["up"]){
- x_new+=speed;
- } else if(keys["left"] && !keys["up"]){
- x_new-=speed;
- } else if(keys["up"] && keys["right"] && shouldJump){
- y_new-=speed;
- x_new+=speed;
- jump++;
- } else if(keys["up"] && keys["left"] && shouldJump){
- y_new-=speed;
- x_new-=speed;
- jump++;
- }
- //Gravity
- if(y_new < 440 && !isJumping && !isColliding){
- y_new+=gravitySpeed;
- }
- //If the player is jumping more than is allowed, make them fall:
- if(jump >= maxJump){
- shouldJump = false;
- isJumping = false;
- if(leftOrRight == "right"){
- x_new+=speed/2;
- } else if(leftOrRight == "left"){
- x_new-=speed/2;
- }
- }
- //If the player is on the ground, allow them to jump:
- if(y_new >= 440){
- jump = 0;
- shouldJump = true;
- }
- //Coins:
- coinArray = [];
- coinArray.push(coin1);
- coinArray.push(coin2);
- //Coin scoring:
- coinArray.forEach(function(coin){
- if(boundsIntersect(x_new, y_new, coin.getX, coin.getY, coin.getxSize)){
- coin.remove();
- score++;
- }
- });
- //Render score:
- ctx.font="20px Times New Roman";
- ctx.fillStyle = "black";
- ctx.fillText("Score: " + score, 10, 20);
- //Render coins:
- coinArray.forEach(function(coin){
- coin.render()
- });
- platformArray = [];
- //Platforms:
- platform1 = new Platform(plat1X,350,200,10, true);
- platformArray.push(platform1);
- platform2 = new Platform(300,200,200,10, false);
- platformArray.push(platform2);
- platform3 = new Platform(400,300,200,10, false);
- platformArray.push(platform3);
- //Move platforms:
- platformArray.forEach(function(platform) {
- if (platform.getMoveable) {
- if (platform.getX >= 400) {
- direction = 'left'
- } else if (platform.getX <= 0) {
- direction = 'right';
- plat1X += 1;
- }
- if(platform.getX > 0 && platform.getY < 400){
- if (direction == 'right') {
- plat1X += 10;
- } else if(direction == 'left'){
- plat1X -= 10;
- }
- }
- }
- });
- // render platforms
- platformArray.forEach(function (platform) {
- platform.render()
- });
- //Platform intersections:
- platformArray.forEach(function(platform){
- if(boundsIntersect(x_new,y_new,platform.getX, platform.getY, platform.getxSize) && !isJumping){
- isColliding = true;
- y_new -= 10;
- //Allow the player to jump:
- jump = 0;
- shouldJump = true;
- } else if(boundsIntersect(x_new,y_new,platform.getX, platform.getY, platform.getxSize) && isJumping){
- isJumping = false;
- resetGame();
- isColliding = true;
- } else {
- isColliding = false;
- }
- });
- //Keep player in bounds:
- if(x_new+50 > 900){
- x_new = 849;
- } else if(x_new < 0){
- x_new = 1;
- } else if(y_new < 0){
- y_new = 1;
- isJumping = false;
- }
- }
- window.requestAnimationFrame(update);
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement