<commit_before>/*  MondriPong
    Version 1.1
    Copyright 2016 <NAME>
    http://www.kmhcreative.com
    
    GitHub Contributors:
    @kmhcreative
    @salty-horse
    
    Based on "Pong" Tutorial by Maison
    http://blog.mailson.org/2013/02/simple-pong-game-using-html5-and-canvas/
*/
function Game() {
    var canvas = document.getElementById("game");
    this.width = canvas.width;
    this.height = canvas.height;
    this.context = canvas.getContext("2d");
    this.context.fillStyle = "white";
    this.keys = new KeyListener();
    
    this.p1 = new Paddle(20, 0, "red");
    this.p1.y = this.height/2 - this.p1.height/2;
 	this.display1 = new Display(this.width/4, 25,"red");
    this.p2 = new Paddle(this.width - 74 - 2, 0, "blue");
    this.p2.y = this.height/2 - this.p2.height/2;
    this.display2 = new Display(this.width*3/4, 25,"blue");
    
    this.ball = new Ball(0, 0, "yellow");
    this.ball.x = this.width/2;
    this.ball.y = this.height/2;
    this.ball.vy = Math.floor(Math.random()*12 - 6);
    this.ball.vx = 7 - Math.abs(this.ball.vy);
}

Game.prototype.draw = function()
{
    this.context.clearRect(0, 0, this.width, this.height);
    
    this.ball.draw(this.context);
    
    this.p1.draw(this.context);
    this.p2.draw(this.context);
    
    this.display1.draw(this.context);
    this.display2.draw(this.context);
};
 
Game.prototype.update = function() 
{
    if (this.paused)
        return;
    
    this.ball.update();
    this.display1.value = this.p1.score;
    this.display2.value = this.p2.score;
 
    // To which Y direction the paddle is moving
    if (this.keys.isPressed(90)) { // DOWN
        this.p1.y = Math.min(this.height - this.p1.height, this.p1.y + 4);
    } else if (this.keys.isPressed(65)) { // UP
        this.p1.y = Math.max(0, this.p1.y - 4);
    }
 
    if (this.keys.isPressed(40)) { // DOWN
        this.p2.y = Math.min(this.height - this.p2.height, this.p2.y + 4);
    } else if (this.keys.isPressed(38)) { // UP
        this.p2.y = Math.max(0, this.p2.y - 4);
    }
 
    if (this.ball.vx > 0) {
        if (this.p2.x <= this.ball.x + this.ball.width &&
                this.p2.x > this.ball.x - this.ball.vx + this.ball.width) {
            var collisionDiff = this.ball.x + this.ball.width - this.p2.x;
            var k = collisionDiff/this.ball.vx;
            var y = this.ball.vy*k + (this.ball.y - this.ball.vy);
            if (y >= this.p2.y && y + this.ball.height <= this.p2.y + this.p2.height) {
                // collides with right paddle
                this.ball.x = this.p2.x - this.ball.width;
                this.ball.y = Math.floor(this.ball.y - this.ball.vy + this.ball.vy*k);
                this.ball.vx = -this.ball.vx;
            }
        }
    } else {
        if (this.p1.x + this.p1.width >= this.ball.x) {
            var collisionDiff = this.p1.x + this.p1.width - this.ball.x;
            var k = collisionDiff/-this.ball.vx;
            var y = this.ball.vy*k + (this.ball.y - this.ball.vy);
            if (y >= this.p1.y && y + this.ball.height <= this.p1.y + this.p1.height) {
                // collides with the left paddle
                this.ball.x = this.p1.x + this.p1.width;
                this.ball.y = Math.floor(this.ball.y - this.ball.vy + this.ball.vy*k);
                this.ball.vx = -this.ball.vx;
            }
        }
    }
 
    // Top and bottom collision
    if ((this.ball.vy < 0 && this.ball.y < 0) ||
            (this.ball.vy > 0 && this.ball.y + this.ball.height > this.height)) {
        this.ball.vy = -this.ball.vy;
    }
    <commit_msg>Fixed collision detection<commit_after>/*  MondriPong
    Version 1.1
    Copyright 2016 <NAME>
    http://www.kmhcreative.com
    
    GitHub Contributors:
    @kmhcreative
    @salty-horse
    
    Based on "Pong" Tutorial by Maison
    http://blog.mailson.org/2013/02/simple-pong-game-using-html5-and-canvas/
*/
function Game() {
    var canvas = document.getElementById("game");
    this.width = canvas.width;
    this.height = canvas.height;
    this.context = canvas.getContext("2d");
    this.context.fillStyle = "white";
    this.keys = new KeyListener();
    
    this.p1 = new Paddle(20, 0, "red");
    this.p1.y = this.height/2 - this.p1.height/2;
 	this.display1 = new Display(this.width/4, 25,"red");
    this.p2 = new Paddle(this.width - 74 - 2, 0, "blue");
    this.p2.y = this.height/2 - this.p2.height/2;
    this.display2 = new Display(this.width*3/4, 25,"blue");
    
    this.ball = new Ball(0, 0, "yellow");
    this.ball.x = this.width/2;
    this.ball.y = this.height/2;
    this.ball.vy = Math.floor(Math.random()*12 - 6);
    this.ball.vx = 7 - Math.abs(this.ball.vy);
}

Game.prototype.draw = function()
{
    this.context.clearRect(0, 0, this.width, this.height);
    
    this.ball.draw(this.context);
    
    this.p1.draw(this.context);
    this.p2.draw(this.context);
    
    this.display1.draw(this.context);
    this.display2.draw(this.context);
};
 
Game.prototype.update = function() 
{
    if (this.paused)
        return;
    
    this.ball.update();
    this.display1.value = this.p1.score;
    this.display2.value = this.p2.score;
 
    // To which Y direction the paddle is moving
    if (this.keys.isPressed(90)) { // DOWN
        this.p1.y = Math.min(this.height - this.p1.height, this.p1.y + 4);
    } else if (this.keys.isPressed(65)) { // UP
        this.p1.y = Math.max(0, this.p1.y - 4);
    }
 
    if (this.keys.isPressed(40)) { // DOWN
        this.p2.y = Math.min(this.height - this.p2.height, this.p2.y + 4);
    } else if (this.keys.isPressed(38)) { // UP
        this.p2.y = Math.max(0, this.p2.y - 4);
    }
 
    if (this.ball.vx > 0) {
		//If I overshot
        if (this.p2.x <= this.ball.x + this.ball.width &&
				//And had not overshot last frame (i.e. only test once)
                this.p2.x > this.ball.x - this.ball.vx + this.ball.width) {

			//X-distance by which I overshot
            var collisionDiff = this.ball.x + this.ball.width - this.p2.x;

			//Percentage of timestep by which I overshot
            var k = collisionDiff/this.ball.vx;

			var inv_k = 1 - k; //percentage of timestep I should have travelled

			//y-distance by which I overshot 
			var last_frame_y = this.ball.y - this.ball.vy

			//y when I collided
			//start of last frame + distance I should have travelled
            var collide_y = last_frame_y  + (this.ball.vy * inv_k);

			var collided = collide_y + this.ball.height >= this.p2.y &&  //below top of paddle
			               collide_y  <= this.p2.y + this.p2.height;     //above bottom of paddle

			console.log(collide_y, collided);

            if (collided) {
                // collides with right paddle
                this.ball.x = this.p2.x - this.ball.width;
                this.ball.y = Math.floor(collide_y);
                this.ball.vx = -this.ball.vx;
            }
        }
    } else {
		//If I overshot
        if (this.p1.x + this.p1.width >= this.ball.x &&
				//And had not overshot last frame (i.e. only test once)
                this.p1.x + this.p1.width < this.ball.x - this.ball.vx) {

			//X-distance by which I overshot
            var collisionDiff = (this.p1.x + this.p1.width) - this.ball.x;

			//Percentage of timestep by which I overshot
            var k = collisionDiff/this.ball.vx;

			var inv_k = 1 - k; //percentage of timestep I should have travelled

			//y-distance by which I overshot 
			var last_frame_y = this.ball.y - this.ball.vy

			//y when I collided
			//start of last frame + distance I should have travelled
            var collide_y = last_frame_y  + (this.ball.vy * inv_k);

			var collided = collide_y + this.ball.height >= this.p1.y &&  //below top of paddle
			               collide_y  <= this.p1.y + this.p1.height;     //above bottom of paddle

            if (collided) {
                // collides with right paddle
                this.ball.x = this.p1.x + this.p1.width;
                this.ball.y = Math.floor(collide_y);
                this.ball.vx = -this.ball.vx;
            }
        }
    }
 
    // Top and bottom collision
    if ((this.ball.vy < 0 && this.ball.y < 0) ||
            (this.ball.vy > 0 && this.ball.y + this.ball.height > this.height)) {
        this.ball.vy = -this.ball.vy;
    }
    
{'fraction_non_alphanumeric': 0.15365087040618955, 'fraction_numerical': 0.025145067698259187, 'mean_word_length': 1.8755648244699339, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3894559', 'n_tokens_mistral': 3196, 'n_tokens_neox': 2975, 'n_words': 613}