2017-10-17 25 views
0

我試圖用PixiJS重寫一個香草JavaScript Pong遊戲,主要是因爲我調整了問題以調整畫布的響應度。在PixiJS的幫助下重寫了香草JS Pong

調整大小正常工作與PixiJS,但所有的對象開始有尾隨的路徑:Fiddle1

我想這是因爲在更新添加了新的實例,而不是隻更新位置,所以我想從頭返工再次。球員的動作到目前爲止,但我無法設法讓球的工作......我有點失落,並且願意學習。非常感謝幫助...非常感謝!

Fiddle2

import 'pixi.js'; 

// define gamne variables 
const appWidth = window.innerWidth; 
const appHeight = window.innerHeight; 
const paddleWidth = 150; 
const paddleHeight = 30; 
const ballSize = 15; 
const halfBall = ballSize/2; 
const appWidthHalf = appWidth/2; 
const appHeightHalf = appHeight/2; 
const paddleWidthHalf = paddleWidth/2; 
const pongColor = 0x57dfbf; 
const bgColor = 0x282625; 
const computerPositionX = appWidthHalf - paddleWidthHalf; 
const computerPositionY = 50; 
const playerPositionX = computerPositionX; 
const playerPositionY = appHeight - computerPositionY - paddleHeight; 
const ballPositionX = appWidthHalf - halfBall; 
const ballPositionY = appHeightHalf - halfBall; 
const playerSpeed = 4; 
const computerSpeed = 4; 
const ballSpeed = 3; 

// Setup the ticker and the root stage PIXI.Container. 
const app = new PIXI.Application(appWidth, appHeight, { 
    antialias: false, 
    backgroundColor: bgColor, 
    transparent: false, 
    resolution: 1, 
}); 

// append app to body 
document.body.appendChild(app.view); 

// create graphic elements 
const player = new PIXI.Graphics(); 
const computer = new PIXI.Graphics(); 
const ball = new PIXI.Graphics(); 

// Player 
player 
    .beginFill(pongColor) 
    .drawRect(playerPositionX, playerPositionY, paddleWidth, paddleHeight) 
    .endFill(); 

// Computer 
computer 
    .beginFill(pongColor) 
    .drawRect(computerPositionX, computerPositionY, paddleWidth, paddleHeight) 
    .endFill(); 

// Ball 
ball 
    .beginFill(pongColor) 
    .drawRect(ballPositionX, ballPositionY, ballSize, ballSize) 
    .endFill(); 

// Player Movement 
player.update = function() { 
    for (const key in keysDown) { 
    const value = Number(key); 
    if (value === 37) { 
     player.move(-playerSpeed, 0); 
    } else if (value === 39) { 
     player.move(playerSpeed, 0); 
    } else { 
     player.move(0, 0); 
    } 
    } 
}; 

player.move = function (x, y) { 
    this.x += x; 
    this.y += y; 
    this.x_speed = x; 
    this.y_speed = y; 
    if (this.x < -appWidthHalf + paddleWidthHalf) { 
    this.x = -appWidthHalf + paddleWidthHalf; 
    this.x_speed = 0; 
    } else if (this.x + this.width - paddleWidthHalf > appWidthHalf) { 
    this.x = appWidthHalf - this.width + paddleWidthHalf; 
    this.x_speed = 0; 
    } 
}; 

// computer Movement 
// eslint-disable-next-line 
computer.update = function(ball) { 
    const x_pos = ball.x; 
    let diff = -(computer.x + paddleWidthHalf - x_pos); 
    if (diff < 0 && diff < -computerSpeed) { 
    diff = -ballSize; 
    } else if (diff > 0 && diff > computerSpeed) { 
    diff = ballSize; 
    } 
    computer.position.set(diff, 0); 
    if (computer.x < 0) { 
    computer.x = 0; 
    } else if (computer.x + paddleWidthHalf > appWidth) { 
    computer.x = appWidth - paddleWidthHalf; 
    } 
}; 

// Ball Movement 
ball.update = function (paddle1, paddle2) { 
    this.x += this.x_speed; 
    this.y += this.y_speed; 
    const top_x = this.x - ballSize; 
    const top_y = this.y - ballSize; 
    const bottom_x = this.x + ballSize; 
    const bottom_y = this.y + ballSize; 
    if (this.x - ballSize < 0) { 
    this.x = ballSize; 
    this.x_speed = -this.x_speed; 
    } else if (this.x + ballSize > appWidth) { 
    this.x = appWidth - ballSize; 
    this.x_speed = -this.x_speed; 
    } 

    if (this.y < 0 || this.y > appHeight) { 
    this.x_speed = 0; 
    this.y_speed = ballSpeed; 
    this.x = appWidthHalf; 
    this.y = appHeightHalf; 
    } 

    if (top_y > appHeightHalf) { 
    if (
     top_y < paddle1.y + paddle1.height && 
     bottom_y > paddle1.y && 
     top_x < paddle1.x + paddle1.width && 
     bottom_x > paddle1.x 
    ) { 
     this.y_speed = -ballSpeed; 
     this.x_speed += paddle1.x_speed/2; 
     this.y += this.y_speed; 
    } 
    } else if (
    top_y < paddle2.y + paddle2.height && 
    bottom_y > paddle2.y && 
    top_x < paddle2.x + paddle2.width && 
    bottom_x > paddle2.x 
) { 
    this.y_speed = ballSpeed; 
    this.x_speed += paddle2.x_speed/2; 
    this.y += this.y_speed; 
    } 
}; 

// controls 
const keysDown = {}; 

window.addEventListener('keydown', (event) => { 
    keysDown[event.keyCode] = true; 
}); 

window.addEventListener('keyup', (event) => { 
    delete keysDown[event.keyCode]; 
}); 

// update function 
function update() { 
    player.update(); 
    computer.update(ball); 
    ball.update(player, computer); 
} 

// append childs to app 
app.stage.addChild(player); 
app.stage.addChild(computer); 
app.stage.addChild(ball); 

// game loop 
app.ticker.add(update); 

回答

0

小提琴1肯定有問題,因爲你不改變ball.x和y有,但不是改變對象的圖形,在fiddle2看起來,你在做 抓好它。代碼中只有一些邏輯錯誤,例如設置x座標和y座標以及球移動。

因此,如果您在fiddle2的ball.update函數中添加console.log(this.x,this.y),您將注意到NaN值。

來看球的移動,如果你設置的初始值:

ball.x_speed = 0; 
ball.y_speed = 0; 

然後

paddle.x_speed 
paddle.y_speed 

paddle.x 
paddle.y 

你看到球至少移動(儘管碰撞邏輯不起作用),(在下面的小提琴中修改示例) https://jsfiddle.net/4L6zbd01/