Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 乒乓球比赛我的球一直打到底线以上_Javascript_Canvas - Fatal编程技术网

Javascript 乒乓球比赛我的球一直打到底线以上

Javascript 乒乓球比赛我的球一直打到底线以上,javascript,canvas,Javascript,Canvas,我有一个简单的乒乓球游戏,在这个游戏中,我可以射出球来检查是否有伤口。我在X和Y坐标上画了一些斜线,但由于我在球的轨迹上加了角度,所以在X坐标上的斜线有时会失败,不知道为什么。这就是我目前所拥有的,colion检查由ballMovementHandler函数处理 var canvas; var ctx; var player; var ball; var gameStarted; var flagY; var flagX; var angle; function init() { ca

我有一个简单的乒乓球游戏,在这个游戏中,我可以射出球来检查是否有伤口。我在X和Y坐标上画了一些斜线,但由于我在球的轨迹上加了角度,所以在X坐标上的斜线有时会失败,不知道为什么。这就是我目前所拥有的,colion检查由ballMovementHandler函数处理

var canvas;
var ctx;
var player;
var ball;
var gameStarted;
var flagY;
var flagX;
var angle;

function init() {
    canvas = document.getElementById('myCanvas')
    if (canvas.getContext) {
        ctx = canvas.getContext('2d')
        setCanvasSize(ctx)
        player = new Player()
        ball = new Ball()
        attachKeyboardListeners()   
        reset();
        animate();  
    }
}

function reset() {
    flagX = -1;
    flagY = -1;
    angle = 90;
    gameStarted = false
    player = new Player();
    ball = new Ball();
}

function animate () {
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    drawIce()    
    player.drawPlayer();
    ball.drawBall();
    playerMovementeHandler()

    if(gameStarted) {
        ballMovementHandler();            
    }

    window.requestAnimationFrame(animate);    
}

function playerMovementeHandler() {
    if(player.left === true) {
        if(player.x > 0) {
            player.movePlayer(-SPEED);
            if(!gameStarted) {
                ball.moveBallWithPlayer(-SPEED);
            }            
        }
    }

    if(player.right === true) {
        if(player.x + player.width < ctx.canvas.width) {
            player.movePlayer(SPEED);            
            if(!gameStarted) {
                ball.moveBallWithPlayer(SPEED);
            }      
        }
    }
}

function ballMovementHandler() {
    if(ball.y - ball.radius <= 0) {
        flagY = 1;
    }
    if(ball.y + ball.radius === player.y) {
        if(ball.x + ball.radius >= player.x && ball.x < player.x + player.width) {
            angle = playerAngleHandler()
            flagY = -1;            
        }
        else {
            reset();
        }
    }
    if(ball.x - ball.radius <= 0) {
        flagX = 1;
    }
    if(ball.x + ball.radius >= ctx.canvas.width) {
        flagX = -1;
    }
    radians = angle * Math.PI/ 180;
    ball.moveBallY(Math.sin(radians) * ball.speed * flagY);
    ball.moveBallX(Math.cos(radians) * ball.speed * flagX);  
}

function playerAngleHandler() {
    var angle = 90;
    if(ball.x + ball.radius <= player.x + 25) {
        angle = 35;
    } else if(ball.x + ball.radius >= player.x + player.width - 25) {
        angle = 135;
    } else if(ball.x + ball.radius >= player.x + player.width - 50) {
        angle = 120
    } else if(ball.x + ball.radius <= player.x + 50 ) {
        angle = 50;
    } else if(ball.x + ball.radius <= player.x + 75) {
        angle = 75;
    } else if( ball.x + ball.radius >= player.x + player.width - 75 ) {
        angle = 100;
    }
    return angle;
}

function drawIce() {
    ctx.fillStyle = 'rgb(134,214,216)'
    ctx.fillRect(0,ctx.canvas.height - Y_OFFSET + player.height + 10, ctx.canvas.width, Y_OFFSET)
}

function setCanvasSize() {
    ctx.canvas.width = window.innerWidth;
    ctx.canvas.height = window.innerHeight;
}


function keyboardEvent(keyCode, keyStatus) {
    switch(keyCode) {
        case 37: 
            player.left = keyStatus;
            break;
        case 39: 
            player.right = keyStatus;
            break;
        case 32:
            gameStarted = true;
            break;
    }
}

function attachKeyboardListeners() {
    document.addEventListener('keydown', function(e) {
        keyboardEvent(e.keyCode, true)
    })  
    document.addEventListener('keyup', function(e) {
        keyboardEvent(e.keyCode, false)   
    }) 
}

  init();
player.js

// defines player configuration behaviour
const BALL_POSITION_Y = 100;
const RADIUS = 12;
const BALL_SPEED = 10;

function Ball(x = ctx.canvas.width/2, y = ctx.canvas.height - Y_OFFSET - RADIUS, radius = RADIUS, color = 'rgb(100,149,237)', speed = BALL_SPEED) {
    this.x = x;
    this.y = y;
    this.radius = radius;
    this.color = color;
    this.speed = speed;

    this.drawBall = function() {
        ctx.fillStyle = this.color;        
        ctx.beginPath();
        ctx.arc(this.x,this.y,this.radius,0,2*Math.PI);
        ctx.fill();
    }

    // for inital game started
    this.moveBallWithPlayer = function(deltaX) {
        this.x += deltaX;
    }

    this.moveBallY = function(flag) {
        this.y = this.y + flag;
    }

    this.moveBallX = function(flag) {
        this.x = this.x + flag;
    }
}
// defines player configuration behaviour
const PLAYER_WIDTH = 200;
const Y_OFFSET = 100;
const PLAYER_HEIGHT = 30;
const SPEED = 6;

function Player(x = ctx.canvas.width/2 - PLAYER_WIDTH/2, y = ctx.canvas.height - Y_OFFSET, width = PLAYER_WIDTH, height = PLAYER_HEIGHT, color = 'rgba(0,0,0)') {
    this.left = false;
    this.right = false;
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.color = color;

    this.movePlayer = function(deltaX) {
        this.x += deltaX;
    }

    this.drawPlayer = function() {
        ctx.fillStyle = this.color;        
        ctx.fillRect(this.x, this.y, this.width, this.height);        
    }
}

缺少一些代码。你能提供球和球员的构造器吗?编辑了这个问题,抱歉忘了:)你说碰撞失败是什么意思?您是否尝试监视flagX变量?它是否永远不会改变,或者碰撞可能会触发多次?我的意思是,有时它会失败,有时不会