Javascript Ball不会反弹

时间:2016-11-01 07:02:45

标签: javascript html css

我正在完成一项学校作业,你必须使用Javascript,HTML和CSS制作游戏,而不是Jquery。我正在做Pong,但我不能让我的球反弹。 这是弹跳位:

function ballBounce()
{

    if(ball.y == -200)
    {
        ball.speedY = 1;
        ball.speedX = 1;
        ball.y += 1;
        console.log("Bounce 1");
        ball.y == -1;
    }
    else if(ball.y == 200)
    {
        ball.y - 1;
        ball.speedY = -1;
        ball.speedX = -1;
        ball.y += -1;
        console.log("Bounce 2");
        ball.y == 1;
    }
    else if(ball.x == -200)
    {
        ball.y + 1;
        ball.speedY = 1;
        ball.speedX = 1;
        ball.x += 1;
        console.log("Bounce 3");
        ball.x == -1;

    }
    else if(ball.x == 200)
    {
        ball.y - 1;
        ball.speedY = -1;
        ball.speedX = -1;
        ball.y += -1;
        console.log("Bounce 4");
        ball.x == 1;
    }
}

这就是我的所有代码:

var paddle1;
var paddle2;
var line;
var ball;
var W = 0;
var H = 0;

function startGame()
{
    myGameArea.start();
    paddle1 = new component(10, 50, "white", 10, 325);
    paddle2 = new component(10, 50, "white", 980, 325);
    line = new component(1, 700, "white", 500, 0);
    ball = new component(12, 12, "white", 495, 325);

}

var myGameArea = {
    canvas: document.createElement("canvas"),
    start: function() {
        this.canvas.width = 1000;
        this.canvas.height = 690;
        this.context = this.canvas.getContext("2d");
        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
        this.interval = setInterval(updateGameArea, 20);
        window.addEventListener('keydown', function(e) {
            myGameArea.key = e.keyCode;
        })
        window.addEventListener('keyup', function(e) {
            myGameArea.key = false;
        })
    },
    clear: function() {
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
    }
}

function component(width, height, color, x, y, type)
{
    this.type = type;
    this.gamearea = myGameArea;
    this.width = width;
    this.height = height;
    this.speedX = 0;
    this.speedY = 0;
    this.x = x;
    this.y = y;
    this.gravity = 0.1;
    this.gravitySpeed = 0;
    this.bounce = 0.6;
    this.update = function() {
        ctx = myGameArea.context;
        ctx.fillStyle = color;
        ctx.fillRect(this.x, this.y, this.width, this.height);
    }
    this.newPos = function() {
        this.x += this.speedX;
        this.y += this.speedY;
    }
    this.hitBottom = function() {
        var rockbottom = myGameArea.canvas.height - this.height;
        if (this.by > rockbottom) {
            this.by = rockbottom;
            this.gravitySpeed = -(this.gravitySpeed * this.bounce);
        }
    }
}

function updateGameArea()
{
    myGameArea.clear();
    paddle1.speedX = 0;
    paddle1.speedY = 0;
    ballBounce();
    console.log(ball.y + " + " + ball.x);
    ball.y += 5;
    ball.x += 5;
    ball.speedY = 1;
    ball.speedX = -1;

    if(myGameArea.key == 38)
    {
        paddle1.speedY = -5;
        paddle2.y += 5;
        H = H + 1;
        score1 = document.getElementById("score").innerHTML = H;
        if (H >= 68) {
            paddle1.speedY = 0;
            H = 68;
            score1 = document.getElementById("score").innerHTML = H;
        }
    }

    if(myGameArea.key == 40)
    {
        paddle1.speedY = 5;
        paddle2.y -= 5;
        H = H - 1;
        score2 = document.getElementById("score").innerHTML = H;
        if (H <= -68) {
            paddle1.speedY = 0;
            H = -68;
            score2 = document.getElementById("score").innerHTML = H;
        }
    }

    paddle1.newPos();
    paddle1.update();
    ball.newPos();
    ball.update();
    paddle2.newPos();
    paddle2.update();
    line.update();

    function ballBounce()
    {
        if (ball.y == -200) {
            ball.speedY = 1;
            ball.speedX = 1;
            ball.y += 1;
            console.log("Bounce 1");
            ball.y == -1;
        } else if (ball.y == 200) {
            ball.y - 1;
            ball.speedY = -1;
            ball.speedX = -1;
            ball.y += -1;
            console.log("Bounce 2");
            ball.y == 1;
        } else if (ball.x == -200) {
            ball.y + 1;
            ball.speedY = 1;
            ball.speedX = 1;
            ball.x += 1;
            console.log("Bounce 3");
            ball.x == -1;
        } else if (ball.x == 200) {
            ball.y - 1;
            ball.speedY = -1;
            ball.speedX = -1;
            ball.y += -1;
            console.log("Bounce 4");
            ball.x == 1;
        }
    }
}

整件事:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style>
	
body {
 
	position: fixed;
    
    padding: 0;
    
    background-color: blue;
    
    font-family: Pong;
    
    
  }
	
@font-face {
 
    font-family: Pong;
    
    src: url(Pong.ttf);
    
}
	
canvas {
	
    border: 1px solid grey;
    
    background-color: black;
    
    margin: auto;
    
    padding-left: 0;
    
    padding-right: 0;
    
    margin-left: auto;
    
    margin-right: auto;
    
    display: block;
    
}
	
#score {

	
    position: absolute;
    
    left: 400px;
    
    top: 0px;
    
    font-size: 50px;
    
    color: white;
}
	
#score2 {

    position: absolute;
    
    right: 400px;
    
    top: 0px;
    
    font-size: 50px;
    
    color: white;
}
	

</style>
</head>
	
<body onload="startGame()">
	
	<p id="score">0</p>
	<p id="score2">0</p>
	
	
<script>

var paddle1;
var paddle2;
var line;
var ball;
var W = 0;
var H = 0;

	
function startGame() {
    myGameArea.start();
    paddle1 = new component(10, 50, "white", 10, 325);
    paddle2 = new component(10, 50, "white", 980, 325);
    line = new component(1, 700, "white", 500, 0);
    ball = new component(12, 12, "white", 495, 325);
	
}


	
var myGameArea = {
    canvas : document.createElement("canvas"),
    start : function() {
        this.canvas.width = 1000;
        this.canvas.height = 690;
		
        this.context = this.canvas.getContext("2d");
        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
        this.interval = setInterval(updateGameArea, 20);
        window.addEventListener('keydown', function (e) {
            myGameArea.key = e.keyCode;
        })
        window.addEventListener('keyup', function (e) {
            myGameArea.key = false;
        })
    },
    clear : function(){
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
    }
}

function component(width, height, color, x, y, type) {
	this.type = type;
    this.gamearea = myGameArea;
    this.width = width;
    this.height = height;
    this.speedX = 0;
    this.speedY = 0;
    this.x = x;
    this.y = y;
	this.gravity = 0.1;
    this.gravitySpeed = 0;
    this.bounce = 0.6;
    this.update = function() {
        ctx = myGameArea.context;
        ctx.fillStyle = color;
        ctx.fillRect(this.x, this.y, this.width, this.height);
    }
    this.newPos = function() {
        this.x += this.speedX;
        this.y += this.speedY;
    }
	this.hitBottom = function() {
        var rockbottom = myGameArea.canvas.height - this.height;
        if (this.by > rockbottom) {
            this.by = rockbottom;
            this.gravitySpeed = -(this.gravitySpeed * this.bounce);
        }
    }
}
	

function updateGameArea() {
    myGameArea.clear();
	//BallDir();
    paddle1.speedX = 0;
    paddle1.speedY = 0;
	ballBounce();
	//console.log(ball.y + " + " + ball.x);
	ball.y += 5;
		ball.x += 5;
		ball.speedY = 1;
		ball.speedX = -1;
		//console.log("Ball.y: " + ball.y + " Ball.x: " + ball.x); 

	
   if (myGameArea.key == 38) {
	 
    paddle1.speedY = -5;
    
    paddle2.y += 5;

    
    H = H + 1;
    
    score1 = document.getElementById("score").innerHTML = H;

    if (H >= 68) {
      
      paddle1.speedY = 0;
      
      H = 68;
      
      score1 = document.getElementById("score").innerHTML = H;
  
      
    }
    
    
    
  }
  
  
  
  if (myGameArea.key == 40) {
    
    
    
    paddle1.speedY = 5;
    
    paddle2.y -= 5;
    
    
    H = H - 1;
    
    score2 = document.getElementById("score").innerHTML = H;
    
    
    
    if (H <= -68) {
      
      
      
      paddle1.speedY = 0;
      
      H = -68;
      
      score2 = document.getElementById("score").innerHTML = H;
 
      
      
    }
    
    
	
    

  }
	
		
		
	//if (ball.H > 500) {
	//	ball.x -= 1;
	//} else {
	//	ball.x += 1;
	//}

    paddle1.newPos();
    paddle1.update();
    ball.newPos();
    ball.update();
	paddle2.newPos();
    paddle2.update();
    line.update();
	
	function ballBounce() {
	
	if( ball.y < -200) {
		ball.y += 1;
		ball.speedY = 1;
		console.log("Bounce 1");
	}
		
	if (ball.y > 200) {
		ball.y -= 1;
		ball.speedY = -1;

		console.log("Bounce 2");
	}
		
	if( ball.x < -200) {
		ball.x += 1;
		ball.speedX = 1;

		console.log("Bounce 3");
		
	}
		
	if (ball.x > 200) {
		ball.x -= 1;
		ball.speedX = -1;
		console.log("Bounce 4");
	}
			}

}
	
	
	/*if (ball.x == paddle2.x || ball.y == paddle2.y ) {
		ball.speedX = 10;
		ball.speedY = 10;
		ball.x += 10;
		ball.y += 10;
	} */
	

	
	
	
	
/*function BallDir() {
	  var x = Math.floor((Math.random() * 4) + 1);
	  //console.log("Ball.y: " + ball.y)
	 // console.log("X: " + x)

	if (x == 1) {
		ball.y += 5;
		ball.x += 5;
		ball.speedY = 1;
		ball.speedX = -1;
	} else if (x == 2) {
		ball.y -= 5;
		ball.x -= 5;
		ball.speedY = -1;
		ball.speedX = 1;
	} else if (x == 3) {
		ball.y += 5;
		ball.x -= 5;
		ball.speedY = 1;
		ball.speedX = 1;
	} else {
		ball.y -= 5;
		ball.x += 5;
		ball.speedY = -1;
		ball.speedX = -1;	
	}
}*/
	


	
	



 

   
</script>
	
</body>
	
</html>

提前致谢

0 个答案:

没有答案