Solucionado (ver solução)
Solucionado
(ver solução)
3
respostas

My final code - Game Pong

Hey guys,

I'd like to show you my project, it isn't the final way I hoped yet, but this is my first try. If you see something that can help me, improving my code, please, let me know. My code is in English, because I'm living out, and I need to learn and improve my programmer English. I'm so happy with my result!!

Below my entire code and the game that you can play. Enjoy!

Ei pessoal,

Gostaria de mostrar a vocês meu projeto, ainda não é a forma final que eu esperava, mas esta é minha primeira tentativa. Se você ver algo que possa me ajudar, melhorando meu código, por favor, me avise. Meu código está em inglês, pois estou morando fora e preciso aprender e melhorar meu inglês de programador. Estou muito feliz com meu resultado!!

Abaixo meu código inteiro e o jogo que você pode jogar. Aproveitar!

//Ball variables
let xBall = 300;
let yBall = 200;
let dBall = 20;
let rBall = dBall / 2;
let position = [50,100,150,200,250,300,350]

//Speed variables
let speedxBall = 10;
let speedyBall = speedxBall;
let direction = [speedxBall,speedyBall*-1];

//Racket variables
let xRacket = 5
let yRacket = 150
let wRacket = 10
let hRacket = 90

//Racket IA variables
let xRacketIA = 585;
let yRacketIA = 150;
let wRacketIA = 10;
let hRacketIA = 90;
let speedRacketIA;
let ErrorChanceIA = [speedxBall+1,speedxBall,speedyBall-1]

//Score Game
let myscore = 0;
let oppscore = 0;

//Music Game
let RacketM;
let ScoreM;
let ThemeM;
let TableM;

function preload(){
  ThemeM = loadSound("theme.mp3");
  ScoreM = loadSound("score.mp3"); 
  RacketM = loadSound("racket.mp3");
  TableM = loadSound("table.mp3");
}

function setup() {
  createCanvas(600, 400);
  begin();
  ThemeM.loop();
}

function draw() {
  background(47,79,79);
  stroke(255);
  strokeWeight(2);
  line(300,0,300,400);
  showBall();
  moveBall();
  boardBall();
  showRacket(xRacket,yRacket);
  showRacket(xRacketIA,yRacketIA);
  moveRacket();
  moveRacketIA();
  touchRacket();
  touchRacketIA();
  score();
  getscore();

}

function begin(){
  xBall = 300;
  //left random begin direction and position
  yBall = random(position);
  speedxBall = random(direction);
  speedyBall = random(direction);

}

function showBall(){ circle(xBall, yBall, dBall)

}

function moveBall (){
  xBall += speedxBall;
  yBall += speedyBall;
}

function boardBall(){
  //recognize the board table game
  if (xBall + rBall > width || xBall - rBall < 0){
    speedxBall *= -1;
  }
  if (yBall + rBall > height || yBall - rBall < 0){
    speedyBall *= -1;
    TableM.play();

  }
}

function showRacket(x,y){
  rect(x, y, wRacket, hRacket)
}


function moveRacket(){
 // if (keyIsDown(UP_ARROW))
   // yRacket -= 10;

  //if (keyIsDown(DOWN_ARROW))
    //yRacket += 10;
 yRacket = mouseY;
}




function moveRacketIA(){
  //makes RacketIA move just the ball through the line 
  if (xBall > 300 && speedxBall > 0){
    //makes the racket move realistically and with variable speed to the ball
    if (yRacketIA != yBall){
      if (yRacketIA < yBall){
        yRacketIA += random(ErrorChanceIA);
      }
      else{
        yRacketIA += random(ErrorChanceIA) * -1;
      }
    }
  }

}


function touchRacket(){
  //recognize the touch
  if (xBall - rBall < xRacket + wRacket && yBall - rBall < yRacket + hRacket && yBall + rBall > yRacket){
    speedxBall *= -1;
    RacketM.play();
    }
}

function touchRacketIA() {
  //recognize the touch
  if (xBall + rBall > xRacketIA && yBall + rBall < yRacketIA + hRacketIA && yBall + rBall >   yRacketIA - wRacketIA){
    speedxBall *= -1; 
    RacketM.play();
  }
}


function score(){
  stroke(255);
  textAlign(CENTER);
  textSize(18);
  fill(color(255,140,0));
  square(125, 4, 48, 5);
  fill(255);
  text(myscore, 150,35);
  fill(color(255,140,0));
  square(425, 4, 50, 5);
  fill(255);
  text(oppscore, 450,35);
}

function getscore(){
  if (xBall > 590){
    myscore += 1;
    begin();
    ScoreM.play();
  }
  if (xBall < 10){
    oppscore += 1;
    begin();
    ScoreM.play();
  }
}


function dontlockball(){
    if (xBall - rBall < 0){
    xBall = 23
    }
}




https://editor.p5js.org/DevZilio/full/zKChTT6Py - Game

https://editor.p5js.org/DevZilio/sketches/zKChTT6Py - Code

3 respostas
solução!

Hello, Paulo! How are you? (vou treinar meu english também hihih)

Olha, só preciso dizer que você arrasou no desenvolvimento do jogo(YOU ROCK!)!

Sua customização está fantástica, pois as cores, o tamanho da tela, o placar, tudo está bastante harmônico! A jogabilidade também ficou deveras interessante e o uso do mouse para manipular a raquete foi uma boa sacada! Devo destacar também a escrita do código em inglês!

Continue estudando e mantenha sempre essa dedicação, pois vejo um caminho brilhante na sua jornada como desenvolvedor! Parabéns!

Hello, I'm so glad that you liked!!

I'm keeping my studies and soon, I'll complete the first degree on Programming beginner.

If possible, I'd like you check my code and help me to finish, I create a bottom to back main, but when the game finish, the bottom doesn't work.

https://editor.p5js.org/DevZilio/sketches/0tumxbYbX

Now my code is more complete...

Thank You!

Apos realizar os cursos de programação em java e finalizar o projeto Pong, decidi criar minha própria versão aprimorando o código e trazendo mais propriedades que busquei na internet.

Criei um menu interativo, modalidades de jogo e novo design.

Porem estou com um problema para finalizar meu projeto, quando o jogo termina, chega a pontuação desejada, o jogo finaliza e criei um botão para voltar ao menu principal e iniciar novamente o jogo ou alterar o modo. O botão funciona antes de o jogo atingir a pontuação, porem quando termina, e o jogo para, o botão não funciona mais...

Se alguém puder conferir o código e me ajudar a solucionar esse problema, ficarei muito grato...

Segue o link do código no JS5

https://editor.p5js.org/DevZilio/sketches/0tumxbYbX

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software