package net.nivangerow.pong;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class Game extends JPanel implements ActionListener{
Timer time;
public Bat b3;
public BatR b4;
public Ball ball;
Image sccl;
Image logo;
int scoreL,scoreR;
boolean pause=true;
public Gui gui;
boolean ai=false;
Thread aiThread;
int aiIntelligence = 8;
public GuiPause guipause;
public GuiOp guiop;
public GuiError guierror;
public Game()
{
b3=new Bat();
b4=new BatR();
ball=new Ball();
setVisible(true);
setSize(640, 480);
setBackground(Color.BLACK);
setFocusable(true);
time = new Timer(5, this);
time.start();
addKeyListener(new AL());
scoreL=scoreR=0;
ImageIcon i = new ImageIcon(getClass().getResource("sccl.png"));
sccl=i.getImage();
ImageIcon logo2 = new ImageIcon(getClass().getResource("logo.png"));
logo = logo2.getImage();
gui = new Gui();
guipause=new GuiPause();
guiop=new GuiOp();
guierror=new GuiError();
aiThread = new Thread(new AI(), "aiThread");
}
public void actionPerformed(ActionEvent e)
{
if(!pause)
{
if(ball.x>630&&ball.px==+1){ball.px=-1; scoreR++;}
if(ball.x<0&&ball.px==-1){ball.px=+1; scoreL++;}
if(ball.y>450&&ball.py==+1)ball.py=-1;
if(ball.y<0&&ball.py==-1)ball.py=+1;
b3.move();
b4.move();
ball.move();
checkCollisions();
}
if(ai)
{
b3.move();
}
repaint();
}
public void aiInit()
{
int target=aiGo()-100/2;
if(target >= 480 - 100){target = 480 - 100;}
if(b3.y > target + 1){b3.y -= 1;}
if(b3.y < target - 1){b3.y += 1;}
}
public int aiGo(){
int tmpx = ball.x;
int tmpy = ball.y;
if(ball.px> 0){return (480)/2-15;}
return(tmpy);
}
public void checkCollisions()
{
Rectangle ballC = ball.getBounds();
Rectangle b3C = b3.getBounds();
Rectangle b4C = b4.getBounds();
if(ballC.intersects(b3C))
{
ball.x=10;
ball.px=+1;
}
if(ballC.intersects(b4C))
{
ball.x=620;
ball.px=-1;
}
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(sccl, 320, 0, null);
Font fontL = new Font("Arial", Font.PLAIN, 30);
g2d.setFont(fontL);
g2d.setColor(Color.WHITE);
g2d.drawString(scoreR+" "+scoreL, 215, 100);
if(scoreL>=9){g2d.setColor(Color.RED); g2d.drawString("The right side has won!", 160, 200); pause=true; aiThread.stop();}
if(scoreR>=9){g2d.setColor(Color.RED); g2d.drawString("The left side has won!", 160, 200); pause=true; aiThread.stop();}
g2d.drawImage(b3.bat, 0, b3.y, null);
g2d.drawImage(b4.bat, 630, b4.y, null);
g2d.drawImage(ball.ball, ball.x, ball.y, null);
if(gui.visible){
g2d.drawImage(gui.background, 0, 0, null);
g2d.drawImage(ball.ball, 200, gui.y, null);
g2d.drawImage(logo, 120, 10, null);
g2d.drawString("1 Player",260, 215);
g2d.drawString("2 Player",260, 315);
g2d.drawString("Options",260, 415);
}
if(guipause.visible&&pause){
g2d.drawImage(gui.background, 0, 0, null);
g2d.drawImage(ball.ball, 200, guipause.y, null);
g2d.drawString("GAME PAUSED",210, 115);
g2d.drawString("Continue",254, 215);
g2d.drawString("Abort Game",250, 315);
}
if(guiop.visible&&pause){
g2d.drawImage(gui.background, 0, 0, null);
g2d.drawImage(ball.ball, 200, guiop.y, null);
g2d.drawString("GAME OPTIONS",210, 115);
g2d.drawString("AI intelligence: A < "+aiIntelligence+" > D",220, 215);
g2d.drawString("Back",285, 315);
}
if(guierror.visible&&pause){
gui.visible=false;
guiop.visible=false;
guipause.visible=false;
g2d.drawImage(gui.background, 0, 0, null);
g2d.drawString("FATAL AI ERROR",210, 115);
g2d.drawString("A fatal error occoured with the AI!",110, 215);
g2d.drawString("Setting the AI intelligence under 8",110, 265);
g2d.drawString("can cause crashes. Please restart game.",50, 315);
}
}
public class AL extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if(!pause){
if(key==KeyEvent.VK_W&&!ai)
{
b3.py=-1;
}
if(key==KeyEvent.VK_S&&!ai)
{
b3.py=+1;
}
if(key==KeyEvent.VK_I)
{
b4.py=-1;
}
if(key==KeyEvent.VK_K)
{
b4.py=+1;
}
}
if(gui.visible&&pause)
{
if(key==KeyEvent.VK_W)
{
gui.y-=100;
if(gui.y<=200)gui.y=200;
}
if(key==KeyEvent.VK_S)
{
gui.y+=100;
if(gui.y>=400)gui.y=400;
}
if(key==KeyEvent.VK_ENTER)
{
if(gui.y==200)
{
gui.visible=false;
ai=true;
pause=false;
aiThread.start();
}
if(gui.y==300)
{
ai=false;
gui.visible=false;
pause=false;
}
if(gui.y==400)
{
//WHY DOES THIS ONLY WORK ONCE?
gui.visible=false;
guiop.visible=true;
}
}
}
if(guipause.visible&&pause)
{
if(key==KeyEvent.VK_W)
{
guipause.y-=100;
if(guipause.y<=200)guipause.y=200;
}
if(key==KeyEvent.VK_S)
{
guipause.y+=100;
if(guipause.y>=300)guipause.y=300;
}
if(key==KeyEvent.VK_ENTER)
{
if(guipause.y==200)
{
aiIntelligence = tempaiIntelligence;
pause=false;
guipause.visible=false;
}
if(guipause.y==300)
{
aiIntelligence = tempaiIntelligence;
pause=true;
guipause.visible=false;
gui.visible=true;
ball.x=ball.y=100;
ball.py=+1;
ball.px=+1;
b3.y=b4.y=180;
scoreL=scoreR=0;
}
}
}
if(guiop.visible&&pause)
{
if(key==KeyEvent.VK_W)
{
guiop.y-=100;
if(guiop.y<=200)guiop.y=200;
}
if(key==KeyEvent.VK_S)
{
guiop.y+=100;
if(guiop.y>=300)guiop.y=300;
}
if(guiop.y==200)
{
if(key==KeyEvent.VK_A)aiIntelligence--;
if(key==KeyEvent.VK_D)aiIntelligence++;
}
if(key==KeyEvent.VK_ENTER)
{
if(guiop.y==300)
{
guiop.visible=false;
gui.visible=true;
}
}
}
if(key==KeyEvent.VK_ESCAPE&&!pause&&gui.visible==false)
{
tempaiIntelligence = aiIntelligence;
aiIntelligence = 1000;
pause=true;
guipause.visible=true;
}
}
int tempaiIntelligence;
public void keyReleased(KeyEvent e)
{
int key = e.getKeyCode();
if(!pause){
if(key==KeyEvent.VK_W&&!ai)
{
b3.py=0;
}
if(key==KeyEvent.VK_S&&!ai)
{
b3.py=0;
}
if(key==KeyEvent.VK_I)
{
b4.py=0;
}
if(key==KeyEvent.VK_K)
{
b4.py=0;
}
}
}
}
public class AI extends Thread {
AI(){}
AI(String threadName) {
super(threadName);
System.out.println(this);
start();
}
public void run() {
try{
if(ai)aiInit();
aiThread.currentThread().sleep(aiIntelligence);
run();
}catch(StackOverflowError ex){guierror.visible=true; pause=true;} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}