• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
kaylors

Code Review : Pong Clone

2 posts in this topic

Pretty self-explanatory, really smile.png

I followed the advice I got here and coded a Pong clone. I had some knowledge of Java and thus decided to go with Processing.

This is the completed game's code. Please guide me on understanding everything that's wrong with it, because I know there must be plenty.

Thank you.

 

Here's a link where you can play the game: http://www.openprocessing.org/sketch/103532

int MODE;


MenuText back;


/* @pjs font="Arial.ttf"; */


PFont titleF;
PFont menuItemF;
String[] menuItemsNames={"START","INSTRUCTIONS","CREDITS"};  //Creates the array that will be passed to the Menu class for forming individual MenuText objects.
Menu menu;


Paddle paddleLeft;
Paddle paddleRight;


Ball pongBall;
Ball pongScreenBall;
Ball pongCreditsBall;


Score tpScore;


Field field;


void setup(){
  size(640,480);
  background(255);
  smooth();
  noStroke();
  
  titleF=createFont("Arial");
  menuItemF=createFont("Arial");
  menu=new Menu("PONG",menuItemsNames,titleF,menuItemF,0,255,color(183,152,50),0,color(50,155,61));
  
  back=new MenuText("BACK",menuItemF,width/2,height/1.5+100,24,color(255),color(183,152,50),color(0),color(50,155,61));
  
  paddleLeft=new Paddle(width/48,height/5,color(183,152,50),0);
  paddleRight=new Paddle(width/48,height/5,color(183,152,50),1);
  
  pongBall=new Ball(10,color(255),5);
  pongScreenBall=new Ball(10,color(255),4);
  pongCreditsBall=new Ball(10,color(255),10);
  
  tpScore=new Score(10,createFont("Arial"),32,0,255,0,40,height-40,width-40,height-40);
  
  field=new Field(color(255),color(50,155,61),color(255));
  
  MODE=0;
}


void draw(){
  background(50,155,61);
  
  
  
  if(MODE==0){
    pongScreenBall.update();
    pongScreenBall.render();
    pongScreenBall.wallCollision();
    
    menu.render();    
    
    switch(menu.whichItem()){
      case 0:
      MODE=1;
      menu.passTo(menu.whichItem());
      break;
      
      case 1:
      MODE=2;
      menu.passTo(menu.whichItem());
      break;
      
      case 2:
      MODE=3;
      menu.passTo(menu.whichItem());
      break;
      
      default:
      break;
    }
  }
  else  if(MODE==1){
    field.render();
    tpScore.render();
    
    paddleLeft.render();
    paddleRight.render();
    paddleLeft.update();
    paddleRight.update();
    
    pongBall.wallCollision(paddleLeft);
    pongBall.paddleCollision(paddleLeft);
    pongBall.paddleCollision(paddleRight);
    
    pongBall.update();
    pongBall.render();
    
    if(pongBall.getSideHit()!=2){
      tpScore.update(pongBall.getSideHit());
      pongBall.setSideHit(2);
      pongBall.reset();
      pongBall.updateV(5);
      if(tpScore.hasMaxed()!=0){
        MODE=4;        
      }
    }
    
  }
  else if(MODE==2){
    background(50,155,61);
    
    textFont(createFont("Arial"));
    textSize(75);
    fill(255);
    text("INSTRUCTIONS",width/2-textWidth("INSTRUCTIONS")/2,height/4);
    
    textFont(menuItemF);
    textSize(20);
    text("Welcome to Pong, a 2 Player, 2 Dimensional Tabble-Tennis Game",width/2-textWidth("Welcome to Pong, a 2 Player, 2 Dimensional Tabble-Tennis Game")/2,height/2-38);
    text("Press W and S to control the Left Paddle",width/2-textWidth("Press W and S to control the Left Paddle")/2,height/2-10);
    text("Press UP and DOWN to control the Right Paddle",width/2-textWidth("Press UP and DOWN to control the Right Paddle")/2,height/2+18);
    text("Winner decided when one player amasses 10 points",width/2-textWidth("Winner decided when one player amasses 10 points")/2,height/2+46);
    
    back.update();
    back.render();
    
    if(back.getClicked()){
      MODE=0;
      back.unClick();
    }
  }
  else if(MODE==4){
    pongScreenBall.update();
    pongScreenBall.render();
    pongScreenBall.wallCollision();
    
    background(50,155,61);
    
    textFont(titleF);
    textSize(95);
    fill(255);
    text("GAME OVER",width/2-textWidth("GAME OVER")/2,height/2-(104/3));
    
    textFont(createFont("Arial"));
    textSize(32);
    text("Player "+str(tpScore.hasMaxed())+" WON!",width/2-textWidth("Player "+str(tpScore.hasMaxed())+" WON!")/2,height/2+25);    
    
    textFont(menuItemF);
    textSize(48);
    text("Thank You For Playing...",width/2-textWidth("Thank You For Playing...")/2,height/2+85);
    
    back.update();
    back.render();
    
    if(back.getClicked()){
      MODE=0;
      back.unClick();
      tpScore.reset();
    }
    
  }
  else if(MODE==3){
    background(50,155,61);
    
    pongCreditsBall.update();
    pongCreditsBall.render();
    pongCreditsBall.wallCollision();
    
    fill(255);
    textFont(createFont("Arial"));
    textSize(32);
    text("coded by",width/2-textWidth("coded by")/2,height/3);
    
    textFont(menuItemF);
    textSize(45);
    text("Le Shaun",width/2-textWidth("Le Shaun")/2,height/2);
    
    back.update();
    back.render();
    
    if(back.getClicked()){
      MODE=0;
      back.unClick();
    }
  }
}


void mouseClicked(){
  if(MODE==0){
    menu.passTo(mouseX,mouseY);
  }
  else if(MODE==3 || MODE==2 || MODE==4){
    back.mClicked(mouseX,mouseY);
  }
}


void keyReleased(){
  if(MODE==1){
    if(key==CODED){
      if(keyCode==UP){
        paddleRight.updateMve(0);
      }
      else if(keyCode==DOWN){
        paddleRight.updateMve(0);
      }
    }
    else{
      if(key=='W' || key=='w'){
        paddleLeft.updateMve(0);
      }
      else if(key=='S' || key=='s'){
        paddleLeft.updateMve(0);
      }
    }
  }
}
    


void keyPressed(){
  if(MODE==1){
    if(key==CODED){
      if(keyCode==UP){
        paddleRight.updateMve(-2);
      }
      else if(keyCode==DOWN){
        paddleRight.updateMve(2);
      }
    }
    else{
      if(key=='W' || key=='w'){
        paddleLeft.updateMve(-2);
      }
      else if(key=='S' || key=='s'){
        paddleLeft.updateMve(2);
      }
    }
    if(key=='p' || key=='P'){
      noLoop();
    }
    if(key=='r' || key=='R'){
      loop();
    }
  }
}


class Ball
{
  float radius;
  float bX;
  float bY;
  
  float vBase;
  float vX;
  float vY;


  color bColor;
  int sideHit;
  
  Ball(float radius,color bColor,float vBase){
    this.radius=radius;
    this.bColor=bColor;
    this.vBase=vBase;
    
    bX=width/2;
    bY=height/2;
    
    float rnd=random(0,3);
    if(rnd<0){vX=-sqrt(sq(vBase)/2); vY=sqrt(sq(vBase)/2);}
    else if(rnd>=0 && rnd<1){vX=-sqrt((vBase*vBase)/2); vY=-sqrt(sq(vBase)/2);}
    else if(rnd>=1 && rnd<2){vX=sqrt(sq(vBase)/2); vY=sqrt(sq(vBase)/2);}
    else{vX=sqrt(sq(vBase)/2); vY=-sqrt(sq(vBase)/2);}
    
    sideHit=2;
  }
  
  void render(){
    stroke(0);
    fill(bColor);
    
    ellipse(bX,bY,radius*2,radius*2);
  }
  
  void update(){
    bX+=vX;
    bY+=vY;
  }
  
  void reset(){
    bX=width/2;
    bY=height/2;
    vX=sqrt(sq(vBase)/2); 
    vY=sqrt(sq(vBase)/2);
  }
  
  void updateV(float V){
    vBase=V;
  }
  
  void accelerate(float V){
    vBase+=V;
  }
  
  int getSideHit(){
    return sideHit;
  }
  
  void setSideHit(int hit){
    sideHit=hit;
  }
  
  void wallCollision(Paddle p){
    if(bX>=(width-radius-p.getWidth()/2)){
      sideHit=1;         
    }
    else if(bX<=radius+p.getWidth()/2){
      sideHit=0;
    }
    
    if(bY>=(height-radius) || bY<=radius){
      vY*=-1;
    }
  }
  
  void wallCollision(){
    if(bX>=(width-radius) || bX<=radius){
      vX*=-1;
    } 
    if(bY>=(height-radius) || bY<=radius){
      vY*=-1;
    }
  }
  
  void paddleCollision(Paddle p){
    if(p.getOrient()==0){
      if(bY>=(p.getY()-p.getHeight()/2) && bY<=(p.getY()+p.getHeight()/2)){
        if((bX-radius)<=p.getWidth()){
          float varier=map(p.getY()-bY,0,p.getHeight()/2,0.1,1);
          
          vY=sqrt(sq(vBase)/2)*varier*-1;
          vX=sqrt(sq(vBase)/2)*sqrt(2-sq(varier))*(-1*(vX/abs(vX)));
          
          accelerate(0.2);
          
          //println(vX+" "+vY+" "+" "+sqrt(sq(vBase)/2)+" "+varier);
        }
      }
    }
    else if(p.getOrient()==1){
      if(bY>=(p.getY()-p.getHeight()/2) && bY<=(p.getY()+p.getHeight()/2)){
        if((bX+radius)>=width-p.getWidth()){
          float varier=map(p.getY()-bY,0,p.getHeight()/2,0.1,1);
          
          vY=sqrt(sq(vBase)/2)*varier*-1;
          vX=sqrt(sq(vBase)/2)*sqrt(2-sq(varier))*(-1*(vX/abs(vX)));
          
          accelerate(0.2);
          
          //println(vX+" "+vY+" "+sqrt(sq(vBase)/2)+" "+varier);
        }
      }
    }
  }
}


class Field
{
  color fEdge;
  color fFloor;
  color fNet;
  
  Field(color fEdge,color fFloor,color fNet){
    this.fEdge=fEdge;
    this.fFloor=fFloor;
    this.fNet=fNet;
  }
  
  void render(){
    rectMode(CENTER);
    
    fill(fFloor);
    noStroke();
    rect(width/2,height/2,width,height);
    
    noFill();
    stroke(fEdge);
    rect(width/2,height/2,width-10,height-10);
    
    stroke(fNet);
    for(int i=9;i<=height-10;i+=(height-10)/100){
      ellipse(width/2,i,1,1);
    }
  }
}
//Menu class uses the objects from the MenuText and forms a menu with a title and a list of MenuText objects.


class Menu{
  String titleT;
  
  PFont titleF;
  PFont menuItem;
  
  color titleC;
  
  float spacer;    //This is used to define the space between successive MenuText objects.
  float iniY=height/2.5;
  
  MenuText[] menuItems;
  
  Menu(String titleT,String[] menuItemsNames,PFont titleF,PFont menuItemF,color titleC,color menuItemC,color menuBackC,color itemHoverC,color backHoverC){
    this.titleT=titleT;
    this.titleF=titleF;
    this.titleC=titleC;
    
    menuItems=new MenuText[menuItemsNames.length];  //Initializes the MenuText objects depending on the array passed to it. This makes the menu system very flexible.
    spacer=48;
    for(int i=0;i<menuItemsNames.length;i++){      
      menuItems[i]=new MenuText(menuItemsNames[i],menuItemF,width/2,iniY+(spacer*i),24,menuItemC,menuBackC,itemHoverC,backHoverC);
    }
  }


  void render(){
    textFont(titleF);
    textSize(92);
    fill(titleC);
    text(titleT,width/2-(textWidth(titleT)/2),height/3.8);
    
    for(int i=0;i<menuItems.length;i++){
      menuItems[i].update();
      menuItems[i].render();
    }
  }
  
  void passTo(float mX,float mY){    //This accepts the X,Y mouse coords when the mouse is clicked and passes it to the relevant MenuText object to check if the click occurs on that object.
    for(int i=0;i<menuItems.length;i++){
      menuItems[i].mClicked(mX,mY);
    }
  }
  
  void passTo(int item){
    menuItems[item].unClick();
  }
  
  int whichItem(){  //Checks each time if the clicked state of any MenuText object is true. If it is, returns the array position of the relevant object.
    for(int i=0;i<menuItems.length;i++){
      if(menuItems[i].getClicked()){
        return i;
      }
    }
    return menuItems.length;
  }
}
//MenuText holds the attributes and methods relating to each single item on the menu. Thus each item is treated as a separate object.
//Each MenuText object comprises mainly of a foreground text and a background object. 


class MenuText{
  
  String menuItem;
  PFont menuFont;
  float itemX;
  float itemY;
  float itemSize;
  color itemColor;
  color backColor;
  color pressedColor;
  color pressedBack;
  
  color presentItem;
  color presentBack;
  
  float tWidth;
  
  boolean clicked=false;  //This vairable is used to check the clicked state of the menu item. If the mouse is clicked over the menu item, this variable becomes true.
  
  MenuText(String menuItem,PFont menuFont,float itemX,float itemY,float itemSize,color itemColor,color backColor,color pressedColor,color pressedBack){
    this.menuItem=menuItem;
    this.menuFont=menuFont;
    this.itemX=itemX;
    this.itemY=itemY;
    this.itemSize=itemSize;
    this.itemColor=itemColor;
    this.backColor=backColor;
    this.pressedColor=pressedColor;
    this.pressedBack=pressedBack;
  }
  
  void render(){
    textFont(menuFont);
    textSize(itemSize);
    tWidth=textWidth(menuItem);
    
    stroke(0);
    fill(presentBack);
    rectMode(CENTER);
    rect(itemX,itemY,tWidth*1.3,itemSize*1.4,50);
    
    fill(presentItem);
    text(menuItem,itemX-tWidth/2,itemY+itemSize*.3);
  }
  
  void update(){             //Constatnly checks for the state of the object. If the mouse is over it a certain style is show and otherwise another style is shown.
    if(mouseX<(itemX+(tWidth*1.3)/2) && mouseX>(itemX-(tWidth*1.3)/2) && mouseY<(itemY+(itemSize*1.4)/2) && mouseY>(itemY-(itemSize*1.4)/2)){
     presentItem=pressedColor;
     presentBack=pressedBack;
    }
    else{
     presentItem=itemColor;
     presentBack=backColor;
    }
  }
  
  boolean getClicked(){    //Returns the clicked state of the object.
    return clicked;
  }
  
  void unClick(){
    clicked=false;
  }
  
  void mClicked(float mX,float mY){  //Changes the clicked state of the object depending on the position of the mouse as inputs.
    if(mX<(itemX+(tWidth*1.3)/2) && mX>(itemX-(tWidth*1.3)/2) && mY<(itemY+(itemSize*1.4)/2) && mY>(itemY-(itemSize*1.4)/2)){
      clicked=true;
      //println(menuItem);
    }
  }
}
class Paddle {


  float pWidth;
  float pHeight;
  color pColor;


  float pX;
  float pY;
  float pV;
  int mve;
  
  int pOrient;


  Paddle(float pWidth, float pHeight, color pColor, int pOrient){
    this.pWidth=pWidth;
    this.pHeight=pHeight;
    this.pColor=pColor;
    this.pOrient=pOrient;
    
    if (pOrient==0) {
      this.pX=(pWidth/2);
    }
    else {
      this.pX=width-(pWidth/2);
    }
    
    this.pY=height/2;
    this.pV=7;
    this.mve=0;
  }
  
  //Le Getters
  int getOrient(){
    return pOrient;
  }
  
  float getX(){
    return pX;
  }
  
  float getY(){
    return pY;
  }
  
  float getDiag(){
    return (float)(sqrt(pWidth*pWidth-pHeight*pHeight));
  }
  
  float getWidth(){
    return pWidth;
  }
  
  float getHeight(){
    return pHeight;
  }
  
  //Le Rendered & Updater
  void render() {
    rectMode(CENTER);
    fill(pColor);
    stroke(0);


    rect(pX, pY, pWidth, pHeight,pHeight/10);
  }
  
  void updateMve(int mve){
    this.mve=mve;
  }
  
  void update(){
    this.pY=constrain(this.pY+pV*mve, pHeight/2+10, height-(pHeight/2)-10);
  }
  
}
class Score{
  int p1;
  int p2;
  int maxScore;
  
  PFont pFont;
  int pSize;
  color pText;
  color pBack;
  color pStroke;
  
  float p1X;
  float p1Y;
  
  float p2X;
  float p2Y;
   
  Score(int maxScore,PFont pFont,int pSize,color pText,color pBack,color pStroke,float p1X,float p1Y,float p2X,float p2Y){
    p1=0;
    p2=0;
    this.maxScore=maxScore;
    this.pSize=pSize;
    
    this.pFont=pFont;
    this.pText=pText;
    this.pBack=pBack;
    this.pStroke=pStroke;
    
    this.p1X=p1X;
    this.p2X=p2X;
    
    this.p1Y=p1Y;
    this.p2Y=p2Y;
  }
  
  void render(){
    textFont(pFont);
    textSize(pSize);
    
    fill(pBack);
    stroke(pStroke);
    
    rectMode(CENTER);
    
    rect(p1X,p1Y,textWidth(str(p1))*1.3,pSize);
    rect(p2X,p2Y,textWidth(str(p2))*1.3,pSize);
    
    fill(pText);
    text(str(p1),p1X-(textWidth(str(p1))/2),p1Y+pSize*0.3);
    text(str(p2),p2X-(textWidth(str(p2))/2),p2Y+pSize*0.3);
  }
  
  void update(int p){
    if(p==0){
      p2+=1;
    }
    else if(p==1){
      p1+=1;
    }
  }
  
  void reset(){
    p1=0;
    p2=0;
  }
  
  int hasMaxed(){
    if(p1>=maxScore){
      return 1;
    }
    else if(p2>=maxScore){
      return 2;
    }
    else{
      return 0;
    }
  }}
2

Share this post


Link to post
Share on other sites

Thank you for your reply! ^.^

This actually gives me a lot to think about. I'm a very new coder and I'm still just learning and not only have you taught me something new, you might just have helped me understand some things that I hadn't even thought of trying to understand. Implementing the scenes in that way actually helps me in another way which I will reveal later if I can work it out, lol. I'm so gonna refactor this thing.

 

Thank you, once again!

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0