2017-06-12 77 views
1

當前我正在創建一個Javascript遊戲。我已經成功創建了所有的遊戲作品,以及我的遊戲的基本功能應該如何工作。但是,我遇到了兩個我似乎無法解決的問題,並且我已經花了幾個星期的時間試圖弄清楚如何去做。我無法弄清楚如果我的球員被推下瀑布,如何讓比賽結果導致比賽。另外,只要玩家仍然「活着」,我不確定自己的得分能否持續增長。如果任何人都可以演示如何做這些事情,或者指導我如何做到這一點的教程或文章的方向,這將是非常有益的。視頻遊戲分數問題

(function() { 
 
    var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame; 
 
    window.requestAnimationFrame = requestAnimationFrame; 
 
})(); 
 

 

 
const seeded = (() => { 
 
    var seed = 1; 
 
    return { 
 
    max: 2576436549074795, 
 
    reseed(s) { 
 
     seed = s 
 
    }, 
 
    random() { 
 
     return seed = ((8765432352450986 * seed) + 8507698654323524) % this.max 
 
    }, 
 
    } 
 
})(); 
 
const randSeed = (seed) => seeded.reseed(seed); 
 
const randSI = (min, max = min + (min = 0)) => (seeded.random() % (max - min)) + min; 
 
const randS = (min, max = min + (min = 0)) => (seeded.random()/seeded.max) * (max - min) + min; 
 

 
randSeed(100); // seed the random generators 
 

 

 
function Player(color, keymap, x) { 
 
    this.x = (typeof x === 'undefined') ? 1 : x; 
 
    this.y = 7; 
 
    this.width = 15; 
 
    this.height = 15; 
 
    this.speed = 10; 
 
    this.velX = 0; 
 
    this.velY = 0; 
 
    this.jumping = false; 
 

 
    this.keymap = {} 
 
    for (let key in keymap) { 
 
    switch (keymap[key]) { 
 
     case 'jump': 
 
     this.keymap[key] = this.jump 
 
     break; 
 
     case 'left': 
 
     this.keymap[key] = this.moveLeft 
 
     break; 
 
     case 'right': 
 
     this.keymap[key] = this.moveRight 
 
     break; 
 
    } 
 
    } 
 
    this.color = color; 
 
} // Player() 
 

 
Player.prototype.jump = function() { 
 
    if (!this.jumping) { 
 
    this.jumping = true; 
 
    this.velY = -this.speed * 1.5; 
 
    } 
 
} 
 
Player.prototype.moveRight = function() { 
 
    if (this.velX < this.speed) { 
 
    this.velX++; 
 
    } 
 
} 
 
Player.prototype.moveLeft = function() { 
 
    if (this.velX > -this.speed) { 
 
    this.velX--; 
 
    } 
 
} 
 
// Globals 
 
var canvas = document.getElementById("canvas"), 
 
    ctx = canvas.getContext("2d"), 
 
    width = 700, 
 
    height = 600, 
 
    keys = [], 
 
    friction = .9, 
 
    gravity = .9; 
 

 
canvas.width = width; 
 
canvas.height = height; 
 

 
// Set up players 
 
var players = []; 
 
players.push(new Player('purple', { 
 
    32: 'jump', 
 
    37: 'left', 
 
    38: 'jump', 
 
    39: 'right' 
 
})) 
 
/*players.push(new Player('yellow', { 
 
    56: 'jump', 
 
    52: 'left', 
 
    54: 'right' 
 
}, width-25))*/ 
 
players.push(new Player('blue', { 
 
    87: 'jump', 
 
    65: 'left', 
 
    68: 'right' 
 
}, (width-25)/2)) 
 

 
function update() { 
 
    ctx.clearRect(0, 0, width, height); 
 
    addPlatformsToBottom(); // will add platforms if needed 
 
    drawPlatforms(); 
 
    players.forEach(player => { 
 
    // check player-specific keys 
 
    for (let i in player.keymap) { 
 
     if (keys[i] && typeof player.keymap[i] === 'function') 
 
     player.keymap[i].bind(player)(); 
 
    } 
 

 
    player.velX *= friction; 
 

 
    player.velY += gravity; 
 

 
    player.x += player.velX; 
 
    player.y += player.velY; 
 

 
    if (player.x >= width - player.width) { 
 
     player.x = width - player.width; 
 
    } else if (player.x <= 0) { 
 
     player.x = 0; 
 
    } 
 

 
    if (player.y >= height - player.height) { 
 
     player.y = height - player.height; 
 
     player.jumping = false; 
 
     player.velY = 0; 
 
    } 
 
    testPlayerForPlatforms(player); 
 

 
    ctx.fillStyle = player.color; 
 
    ctx.fillRect(player.x, player.y, player.width, player.height); 
 
    }) // player.forEach 
 
    requestAnimationFrame(update); 
 
} 
 

 
document.body.addEventListener("keydown", function(e) { 
 
    // console.log(e.keyCode); 
 
    keys[e.keyCode] = true; 
 
}); 
 

 
document.body.addEventListener("keyup", function(e) { 
 
    keys[e.keyCode] = false; 
 
}); 
 

 
window.addEventListener("load", function() { 
 
    update(); 
 
}); 
 

 
function testPlayerForPlatforms(player) { 
 
    player.hitPlatform = false; // reset platform hit flag 
 
    for (var i = 0; i < platforms.length; i++) { 
 
    var p = platforms[i]; 
 
    if (p.active) { 
 
     testPlayer(player, p); 
 
     if (player.hitPlatform) { 
 
     break; // stop search as player has hit a platform 
 
     } 
 
    } 
 
    } 
 
} 
 

 

 
function drawPlatforms() { // draws all platforms and move up 
 
    platformInfo.lastPlatformY += platformInfo.speed; 
 
    for (var i = 0; i < platforms.length; i++) { 
 
    var p = platforms[i]; 
 
    if (p.active) { 
 
     p.yPos += platformInfo.speed; 
 
     if (p.yPos + p.height < 0) { // platform above top 
 
     p.active = false; // turn it off 
 
     } else { 
 
     p.draw(); 
 
     } 
 
    } 
 
    } 
 
} 
 

 
function addPlatformsToBottom() { 
 
    while (platformInfo.lastPlatformY < ctx.canvas.height) { 
 
    generateLevel(); 
 
    } 
 
} 
 

 
// some constants and vars to control random generation of platforms 
 
const platformInfo = { 
 
    speed: -2.5, 
 
    height: 8, // platform height 
 
    minLength: 100, // in pixels 
 
    maxLength: 300, 
 
    vertSpacing: 100, // distance between platforms 
 
    minHorSpacing: 50, // should be larger than player 
 
    maxHorSpacing: 80, 
 
    lastPlatformY: 100, // y position of last platform created 
 
    maxHoleCount: 3, 
 
    color: "#FFF", 
 
} 
 
// array object holds platforms 
 
const platforms = []; 
 

 
// a platform template object that will be used to create platforms from 
 
const platform = { 
 
    left: 0, 
 
    right: 0, 
 
    yPos: 0, 
 
    height: 0, // thickness 
 
    active: false, // true if platform in use 
 
    color: "#F84", 
 
    draw() { // function to draw the platform 
 
    ctx.fillStyle = this.color; 
 
    ctx.fillRect(this.left, this.yPos, this.right - this.left, this.height); 
 
    }, 
 
    init(left, right, yPos) { // function to initialize 
 
    // alias to save typing. 
 
    const pI = platformInfo 
 
    this.yPos = yPos; 
 
    this.left = left; 
 
    this.right = right; 
 
    this.height = pI.height; 
 
    this.color = pI.color; 
 
    this.active = true; 
 
    }, 
 
} 
 
// function adds platforms to array. If no inactive platforms a 
 
// new one is created 
 
function addPlatform() { 
 
    var platform; 
 
    for (var i = 0; i < platforms.length; i++) { 
 
    if (!platforms[i].active) { // is the platform inactive 
 
     platform = platforms[i]; 
 
     break; // stop searching 
 
    } 
 
    } 
 
    if (!platform) { // if no inactive platform then create a new one 
 
    platform = createPlatform(); 
 
    platforms.push(platform); 
 
    } 
 
    return platform; 
 
} 
 

 
// a function to create a platform object 
 
function createPlatform(customProps = {}) { // custom props can be used to modify the 
 
    // platform in future. For now it just defaults to empty 
 
    return Object.assign({}, platform, customProps); 
 
} 
 
// creates a set of platforms for a single level 
 
function generateLevel() { 
 
    var numHoles = randSI(1, platformInfo.maxHoleCount); 
 
    var spacing = ctx.canvas.width/(numHoles); // get spacing 
 
    var ypos = platformInfo.lastPlatformY; 
 
    platformInfo.lastPlatformY += platformInfo.vertSpacing; 
 
    var left = 0; // the starting left edge 
 
    for (var i = 1; i <= numHoles; i++) { // create numHoles 
 
    var platform = addPlatform(); 
 
    var holeOffset = randSI(-spacing, 0); 
 
    platform.init(left, spacing * i + holeOffset, ypos); 
 
    left = spacing * i + holeOffset + randSI(platformInfo.minHorSpacing, platformInfo.maxHorSpacing); 
 
    } 
 
    // add the last platform 
 
    platform = addPlatform(); 
 
    platform.init(left, ctx.canvas.width, ypos); 
 
} 
 

 

 

 
function testPlayer(player, platform) { 
 
    var p, pl; // p for player, pl for platform 
 
    p = player; 
 
    pl = platform; 
 
    // is the player above or below platform 
 
    if (!(p.x + p.width < pl.left || p.x > pl.right)) { // yes 
 
    if (p.velY > 0 && p.y < pl.yPos) { // is player moving down and above platform 
 
     if (p.y + p.height > pl.yPos) { //is bottom of player below top of platform 
 
     // must have hit platform 
 
     p.jumping = false; 
 
     p.y = pl.yPos - p.height; // move player so that it is on the platform 
 
     p.velY = 0; 
 
     p.hitPlatform = true; // flag a platform has been hit 
 
     } 
 
    } else if (p.y + p.height > pl.yPos + pl.height) { // player must be moving up so check if below platform 
 
     if (p.y < pl.yPos + pl.height) { // is top of player above bottom of platform 
 
     // must have hit head on platform 
 
     p.velY = 0; 
 
     p.y = pl.yPos + pl.height; 
 
     p.jumping = false; 
 
     p.hitPlatform = true; // flag a platform has been hit 
 
     } 
 
    } 
 
    } 
 
}
#score { 
 
    color:white; 
 
    font-size:35px; 
 
    
 
}
0<html> 
 

 
<head> 
 
    <title>Square Stairs™</title> 
 
</head> 
 

 
<body bgcolor="#000"> 
 
    <div id="score">SCORE:</div> 
 
    <br><br><br> <!-- line breaks to move canvas away from SO title bar that gets in the way when switching to full page mode --> 
 
    <canvas id="canvas" style="border:3px solid #fff"></canvas> 
 
</body> 
 

 
</html>

這是我的代碼,因爲它目前爲。我在網站CodePen上提出,這是我的原始代碼。我與Unity或任何其他編碼平臺無關,所以如果您有任何關於如何解決此問題的信息,它必須能夠在CodePen網站上運行。謝謝您的幫助。

回答

0

我已經添加代碼,你的代碼,尋找////////////////////

添加以下到你的源代碼:

  1. 新增功能checkPlayerBounds(player)addToScore(x)

    • checkPlayerBounds(player)檢查player是否已經被按下/從屏幕上掉下來。

      • 注:這裏需要添加更多的功能,擁有遊戲在做你想做的。
    • addToScore(x)增加score變量和分數 HTML元素如果scoreShouldUpdate是真實的。

  2. 新增的全局變量updatesscorescoreShouldUpdate

    • updates記錄update()函數被調用的次數。
    • score記錄遊戲得分。
  3. update()函數中添加代碼。

    • 第一位不斷增加分數,但只有在遊戲更新了指定的時間後。
    • 接下來的一點是每個玩家都會調用checkPlayerBounds函數。

這一切工作,唯一的問題是,播放器開始關閉/右屏幕的邊緣,這樣的比分將不會更新,直到在你的項目中發生變化。

(function() { 
 
    var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame; 
 
    window.requestAnimationFrame = requestAnimationFrame; 
 
})(); 
 

 

 
const seeded = (() => { 
 
    var seed = 1; 
 
    return { 
 
    max: 2576436549074795, 
 
    reseed(s) { 
 
     seed = s 
 
    }, 
 
    random() { 
 
     return seed = ((8765432352450986 * seed) + 8507698654323524) % this.max 
 
    }, 
 
    } 
 
})(); 
 
const randSeed = (seed) => seeded.reseed(seed); 
 
const randSI = (min, max = min + (min = 0)) => (seeded.random() % (max - min)) + min; 
 
const randS = (min, max = min + (min = 0)) => (seeded.random()/seeded.max) * (max - min) + min; 
 

 
randSeed(100); // seed the random generators 
 

 
/////////////////////////// 
 
// This function increases the score by 'x' amount. 
 
/////////////////////////// 
 
function addToScore(x){ 
 
    if(scoreShouldUpdate){ 
 
     score += x; 
 
     var scoreElement = document.getElementById("score"); 
 
     scoreElement.innerHTML = "Score: " + score; 
 
    } 
 
} 
 

 
function checkPlayerBounds(player){ 
 
    if(player.y >= 0){ 
 
     scoreShouldUpdate = false; 
 
     // Insert game over stuff here. 
 
    } 
 
     
 
} 
 

 
function Player(color, keymap, x) { 
 
    this.x = (typeof x === 'undefined') ? 1 : x; 
 
    this.y = 7; 
 
    this.width = 15; 
 
    this.height = 15; 
 
    this.speed = 10; 
 
    this.velX = 0; 
 
    this.velY = 0; 
 
    this.jumping = false; 
 

 
    this.keymap = {} 
 
    for (let key in keymap) { 
 
    switch (keymap[key]) { 
 
     case 'jump': 
 
     this.keymap[key] = this.jump 
 
     break; 
 
     case 'left': 
 
     this.keymap[key] = this.moveLeft 
 
     break; 
 
     case 'right': 
 
     this.keymap[key] = this.moveRight 
 
     break; 
 
    } 
 
    } 
 
    this.color = color; 
 
} // Player() 
 

 
Player.prototype.jump = function() { 
 
    if (!this.jumping) { 
 
    this.jumping = true; 
 
    this.velY = -this.speed * 1.5; 
 
    } 
 
} 
 
Player.prototype.moveRight = function() { 
 
    if (this.velX < this.speed) { 
 
    this.velX++; 
 
    } 
 
} 
 
Player.prototype.moveLeft = function() { 
 
    if (this.velX > -this.speed) { 
 
    this.velX--; 
 
    } 
 
} 
 
// Globals 
 
var canvas = document.getElementById("canvas"), 
 
    ctx = canvas.getContext("2d"), 
 
    width = 700, 
 
    height = 600, 
 
    keys = [], 
 
    friction = .9, 
 
    gravity = .9; 
 

 
////////////////////////////// 
 
//New variables for score increase 
 
////////////////////////////// 
 
var updates = 0; 
 
var score = 0; 
 
var scoreShouldUpdate = true; 
 

 
canvas.width = width; 
 
canvas.height = height; 
 

 
// Set up players 
 
var players = []; 
 
players.push(new Player('purple', { 
 
    32: 'jump', 
 
    37: 'left', 
 
    38: 'jump', 
 
    39: 'right' 
 
})) 
 
/*players.push(new Player('yellow', { 
 
    56: 'jump', 
 
    52: 'left', 
 
    54: 'right' 
 
}, width-25))*/ 
 
players.push(new Player('blue', { 
 
    87: 'jump', 
 
    65: 'left', 
 
    68: 'right' 
 
}, (width-25)/2)) 
 

 
function update() { 
 

 
    ///////////////////////////////////// 
 
    // You can change the '5' to something else to change the rate of which score is increased. 
 
    ///////////////////////////////////// 
 
    if(updates == 5){ 
 
     updates = 0; 
 
     addToScore(1); 
 
    } 
 
    updates++; 
 

 
    ctx.clearRect(0, 0, width, height); 
 
    addPlatformsToBottom(); // will add platforms if needed 
 
    drawPlatforms(); 
 
    players.forEach(player => { 
 
    // check player-specific keys 
 
    for (let i in player.keymap) { 
 
     if (keys[i] && typeof player.keymap[i] === 'function') 
 
     player.keymap[i].bind(player)(); 
 
    } 
 

 
    player.velX *= friction; 
 

 
    player.velY += gravity; 
 

 
    player.x += player.velX; 
 
    player.y += player.velY; 
 

 
    if (player.x >= width - player.width) { 
 
     player.x = width - player.width; 
 
    } else if (player.x <= 0) { 
 
     player.x = 0; 
 
    } 
 

 
    if (player.y >= height - player.height) { 
 
     player.y = height - player.height; 
 
     player.jumping = false; 
 
     player.velY = 0; 
 
    } 
 
    testPlayerForPlatforms(player); 
 

 
    ctx.fillStyle = player.color; 
 
    ctx.fillRect(player.x, player.y, player.width, player.height); 
 

 
    //My code 
 
    checkPlayerBounds(player); 
 

 
    }) // player.forEach 
 
    requestAnimationFrame(update); 
 
} 
 

 
document.body.addEventListener("keydown", function(e) { 
 
    // console.log(e.keyCode); 
 
    keys[e.keyCode] = true; 
 
}); 
 

 
document.body.addEventListener("keyup", function(e) { 
 
    keys[e.keyCode] = false; 
 
}); 
 

 
window.addEventListener("load", function() { 
 
    update(); 
 
}); 
 

 
function testPlayerForPlatforms(player) { 
 
    player.hitPlatform = false; // reset platform hit flag 
 
    for (var i = 0; i < platforms.length; i++) { 
 
    var p = platforms[i]; 
 
    if (p.active) { 
 
     testPlayer(player, p); 
 
     if (player.hitPlatform) { 
 
     break; // stop search as player has hit a platform 
 
     } 
 
    } 
 
    } 
 
} 
 

 

 
function drawPlatforms() { // draws all platforms and move up 
 
    platformInfo.lastPlatformY += platformInfo.speed; 
 
    for (var i = 0; i < platforms.length; i++) { 
 
    var p = platforms[i]; 
 
    if (p.active) { 
 
     p.yPos += platformInfo.speed; 
 
     if (p.yPos + p.height < 0) { // platform above top 
 
     p.active = false; // turn it off 
 
     } else { 
 
     p.draw(); 
 
     } 
 
    } 
 
    } 
 
} 
 

 
function addPlatformsToBottom() { 
 
    while (platformInfo.lastPlatformY < ctx.canvas.height) { 
 
    generateLevel(); 
 
    } 
 
} 
 

 
// some constants and vars to control random generation of platforms 
 
const platformInfo = { 
 
    speed: -2.5, 
 
    height: 8, // platform height 
 
    minLength: 100, // in pixels 
 
    maxLength: 300, 
 
    vertSpacing: 100, // distance between platforms 
 
    minHorSpacing: 50, // should be larger than player 
 
    maxHorSpacing: 80, 
 
    lastPlatformY: 100, // y position of last platform created 
 
    maxHoleCount: 3, 
 
    color: "#FFF", 
 
} 
 
// array object holds platforms 
 
const platforms = []; 
 

 
// a platform template object that will be used to create platforms from 
 
const platform = { 
 
    left: 0, 
 
    right: 0, 
 
    yPos: 0, 
 
    height: 0, // thickness 
 
    active: false, // true if platform in use 
 
    color: "#F84", 
 
    draw() { // function to draw the platform 
 
    ctx.fillStyle = this.color; 
 
    ctx.fillRect(this.left, this.yPos, this.right - this.left, this.height); 
 
    }, 
 
    init(left, right, yPos) { // function to initialize 
 
    // alias to save typing. 
 
    const pI = platformInfo 
 
    this.yPos = yPos; 
 
    this.left = left; 
 
    this.right = right; 
 
    this.height = pI.height; 
 
    this.color = pI.color; 
 
    this.active = true; 
 
    }, 
 
} 
 
// function adds platforms to array. If no inactive platforms a 
 
// new one is created 
 
function addPlatform() { 
 
    var platform; 
 
    for (var i = 0; i < platforms.length; i++) { 
 
    if (!platforms[i].active) { // is the platform inactive 
 
     platform = platforms[i]; 
 
     break; // stop searching 
 
    } 
 
    } 
 
    if (!platform) { // if no inactive platform then create a new one 
 
    platform = createPlatform(); 
 
    platforms.push(platform); 
 
    } 
 
    return platform; 
 
} 
 

 
// a function to create a platform object 
 
function createPlatform(customProps = {}) { // custom props can be used to modify the 
 
    // platform in future. For now it just defaults to empty 
 
    return Object.assign({}, platform, customProps); 
 
} 
 
// creates a set of platforms for a single level 
 
function generateLevel() { 
 
    var numHoles = randSI(1, platformInfo.maxHoleCount); 
 
    var spacing = ctx.canvas.width/(numHoles); // get spacing 
 
    var ypos = platformInfo.lastPlatformY; 
 
    platformInfo.lastPlatformY += platformInfo.vertSpacing; 
 
    var left = 0; // the starting left edge 
 
    for (var i = 1; i <= numHoles; i++) { // create numHoles 
 
    var platform = addPlatform(); 
 
    var holeOffset = randSI(-spacing, 0); 
 
    platform.init(left, spacing * i + holeOffset, ypos); 
 
    left = spacing * i + holeOffset + randSI(platformInfo.minHorSpacing, platformInfo.maxHorSpacing); 
 
    } 
 
    // add the last platform 
 
    platform = addPlatform(); 
 
    platform.init(left, ctx.canvas.width, ypos); 
 
} 
 

 

 

 
function testPlayer(player, platform) { 
 
    var p, pl; // p for player, pl for platform 
 
    p = player; 
 
    pl = platform; 
 
    // is the player above or below platform 
 
    if (!(p.x + p.width < pl.left || p.x > pl.right)) { // yes 
 
    if (p.velY > 0 && p.y < pl.yPos) { // is player moving down and above platform 
 
     if (p.y + p.height > pl.yPos) { //is bottom of player below top of platform 
 
     // must have hit platform 
 
     p.jumping = false; 
 
     p.y = pl.yPos - p.height; // move player so that it is on the platform 
 
     p.velY = 0; 
 
     p.hitPlatform = true; // flag a platform has been hit 
 
     } 
 
    } else if (p.y + p.height > pl.yPos + pl.height) { // player must be moving up so check if below platform 
 
     if (p.y < pl.yPos + pl.height) { // is top of player above bottom of platform 
 
     // must have hit head on platform 
 
     p.velY = 0; 
 
     p.y = pl.yPos + pl.height; 
 
     p.jumping = false; 
 
     p.hitPlatform = true; // flag a platform has been hit 
 
     } 
 
    } 
 
    } 
 
}
#score { 
 
    color:white; 
 
    font-size:35px; 
 
    
 
}
<html> 
 

 
<head> 
 
    <title>Square Stairs™</title> 
 
</head> 
 

 
<body bgcolor="#000"> 
 
    <div id="score">SCORE:</div> 
 
    <br><br><br> <!-- line breaks to move canvas away from SO title bar that gets in the way when switching to full page mode --> 
 
    <canvas id="canvas" style="border:3px solid #fff"></canvas> 
 
</body> 
 

 
</html>

+0

你能解釋一下這更好的,我不知道如何實現它變成我的代碼 – MilkyBoii

+0

我實現了一個可能的版本,我也去到它上面的一些細節。如果您對此有任何疑問,請告訴我。謝謝。 –