Jump to content

I need help closing my score in a Pong game


program_newbie
 Share

Recommended Posts

I"m making a pong game for a school project and i can't figure out how to make the game stop and display an image at a certain score, i would like to stop the game and display a image when a player reach a score.

here's the section of the js file in update function:

if (Point1 === 2 || Point2 === 2)

{ Balle.body.velocity.setTo(0, 0);

introText.text = 'Game Over!';

introText.visible = true;

jeu.image("Game over", "images/2.jpg"); }

Pong.js

Link to comment
Share on other sites

  • 2 weeks later...

What exactly is not working in your current program? I don't think you need to check the win-condition in the update() function. If you do it in the update() then it will be checked for every frame, you only need to check if there is a winner on the moment that a point is scored.

I would add an extra variable that remembers if the game is still in progress or if it's gameover and there is a winner. See the "iswinner" variable in code below.

/*global Phaser,*/
var clavier, Joueur2, Joueur1, Balle, iswinner, texte //etc.
var etat = {
	//..
    create: function () {
		iswinner = 0; // 0=no winner, 1=player 1 wins, 2=player 2 wins
		this.winimage = this.game.add.sprite(200, 100, "winimage");
		this.winimage.visible = false;
		//etc.
	},

    update: function () {
		//..
		if (iswinner == 0) {
			if (Balle.body.blocked.right) {
				Point1 += 1;
				jeu.sound.play("Point1!");
				this.checkWinner(1);
			} else if (Balle.body.blocked.left) {
				Point2 += 1;
				jeu.sound.play("Point2!");
				this.checkWinner(2);
			} else if //.. etc.
		}
	},
    checkWinner: function (plr) {
		if (Point1 >= 2 || Point2 >= 2) {
			// remember which winner
			iswinner = (Point1 >= 2 ? 1 : 2); // 1 or 2
			// set text
			Balle.body.velocity.setTo(0, 0);
			texte.text = 'Game Over! Player ' + iswinner + ' wins!';
			texte.visible = true;
			this.winimage.visible = true;
		} else {
			// continue game, reset ball
			Balle.x = 240; // center
			Balle.body.velocity.setTo(400, 450);
		}
	}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...