ball=newPlayerBall(GAME_WIDTH/2,GAME_HEIGHT/2);//create a player controlled ball, set start location to middle of screen
this.setFocusable(true);//make everything in this class appear on the screen
this.addKeyListener(this);//start listening for keyboard input
//add the MousePressed method from the MouseAdapter - by doing this we can listen for mouse input. We do this differently from the KeyListener because MouseAdapter has SEVEN mandatory methods - we only need one of them, and we don't want to make 6 empty methods
//make this class run at the same time as other classes (without this each class would "pause" while another class runs). By using threading we can remove lag, and also allows us to do features like display timers in real time!
gameThread=newThread(this);
gameThread.start();
}
//paint is a method in java.awt library that we are overriding. It is a special method - it is called automatically in the background in order to update what appears in the window. You NEVER call paint() yourself
publicvoidpaint(Graphicsg){
//we are using "double buffering here" - if we draw images directly onto the screen, it takes time and the human eye can actually notice flashes of lag as each pixel on the screen is drawn one at a time. Instead, we are going to draw images OFF the screen, then simply move the image on screen as needed.
image=createImage(GAME_WIDTH,GAME_HEIGHT);//draw off screen
graphics=image.getGraphics();
draw(graphics);//update the positions of everything on the screen
g.drawImage(image,0,0,this);//move the image on the screen
}
//call the draw methods in each class to update positions as things move
publicvoiddraw(Graphicsg){
ball.draw(g);
}
//call the move methods in other classes to update positions
//this method is constantly called from run(). By doing this, movements appear fluid and natural. If we take this out the movements appear sluggish and laggy
publicvoidmove(){
ball.move();
}
//handles all collision detection and responds accordingly
//run() method is what makes the game continue running without end. It calls other methods to move objects, check for collision, and update the screen
publicvoidrun(){
//the CPU runs our game code too quickly - we need to slow it down! The following lines of code "force" the computer to get stuck in a loop for short intervals between calling other methods to update the screen.
longlastTime=System.nanoTime();
doubleamountOfTicks=60;
doublens=1000000000/amountOfTicks;
doubledelta=0;
longnow;
while(true){//this is the infinite game loop
now=System.nanoTime();
delta=delta+(now-lastTime)/ns;
lastTime=now;
//only move objects around and update screen if enough time has passed