- import java.applet.*;
- public class Game extends Applet implements Runnable {
- Image dbI;
- Graphics dbg;
- Random rnd = new Random();
- Button start;
- Button pause;
- Button play;
- Font f = new Font("Mistral", Font.BOLD, 32);
- Font f2 = new Font("Serif", Font.BOLD, 16);
- AudioClip bounce;
- AudioClip score;
- //FIELD
- int size_x = 800; //length of x-axis (width of field)
- int size_y = (size_x / 10) * 7; //length of y-axis (height of field)
- int middle_x = size_x / 2; //middle of x-axis
- int middle_y = size_y / 2; //middle of y-axis
- //PONG-BALL
- int radius = 7; //radius for ball
- int ball_pos_x = middle_x; //position on x-axis
- int ball_pos_y = middle_y; //position on y-axis
- double ball_speed_x = 0; //speed of position on x-axis | pos:right - neg:left
- double ball_speed_y = 0; //speed of position on y-axis | pos:down - neg:up
- double speed = 10; //speed at the beginning
- double temp_x; //for saving the x-coordinate of the ball when pausing the game
- double temp_y; //for saving the y-coordinate of the ball when pausing the game
- //GLOBAL PLAYERS
- int player_height = size_y / 8;
- int player_width = (int) (size_x * 0.02);
- double ptenth = player_height / 10; //a tenth of the player's height
- double ptenth2 = ptenth * 2;
- double ptenth3 = ptenth * 3;
- double ptenth4 = ptenth * 4;
- double ptenth5 = ptenth * 5;
- double ptenth6 = ptenth * 6;
- double ptenth7 = ptenth * 7;
- double ptenth8 = ptenth * 8;
- double ptenth9 = ptenth * 9;
- //LEFT PLAYER
- int player_l_pos_x = size_x / 100 * 2;
- int player_l_pos_y = middle_y - player_height / 2;
- int player_l_midpos_y;
- int mouse_pos_y;
- int player_l_score = 0;
- //RIGHT PLAYER
- int player_r_pos_x = size_x - size_x / 100 * 5;
- int player_r_pos_y = middle_y - player_height / 2;
- int player_r_midpos_y;
- int player_r_score = 0;
- int player_r_speed = 5;
- public void init() {
- this.setLayout(null);
- start = new Button("Spiel starten");
- pause = new Button("Pause");
- play = new Button("Weiter");
- start.setBounds((int) middle_x - 100, (int) middle_y - 10, 80, 20);
- pause.setBounds((int) middle_x - 40, (int) size_y, 80, 20);
- play.setBounds((int) middle_x - 40, (int) size_y, 80, 20);
- pause.setVisible(false);
- play.setVisible(false);
- add(start);
- add(pause);
- add(play);
- setFont(f);
- start.setFont(f2);
- play.setFont(f2);
- pause.setFont(f2);
- bounce = getAudioClip (getCodeBase(), "drip.au");
- score = getAudioClip (getCodeBase(), "game_win.au");
- bounce.play();
- bounce.stop();
- score.play();
- score.stop();
- }
- public boolean action(Event e, Object args) {
- if (e.target == start) {
- pause.setVisible(true);
- start.setVisible(false);
- ball_speed_x = speed * -1;
- ball_speed_y = 0;
- }
- if (e.target == pause) {
- play.setVisible(true);
- pause.setVisible(false);
- temp_x = ball_speed_x;
- temp_y = ball_speed_y;
- ball_speed_x = 0;
- ball_speed_y = 0;
- }
- if (e.target == play) {
- pause.setVisible(true);
- play.setVisible(false);
- ball_speed_x = temp_x;
- ball_speed_y = temp_y;
- }
- return false;
- }
- public void start() {
- Thread th = new Thread(this);
- th.start();
- }
- public void stop() {
- }
- public void destroy() {
- }
- public boolean mouseMove(Event e, int x, int y) {
- mouse_pos_y = y;
- return true;
- }
- public void run() {
- Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
- while (true) {
- //-----------------------------------------------------------------------------------------
- //----------------------------------------PONG-BALL----------------------------------------
- //-----------------------------------------------------------------------------------------
- //TOP BOUND
- if (ball_pos_y >= size_y - radius) {
- ball_speed_y *= -1;
- bounce.play();
- }
- //BOTTOM BOUND
- if (ball_pos_y <= radius) {
- ball_speed_y *= -1;
- bounce.play();
- }
- //MOVEMENT
- ball_pos_x += ball_speed_x;
- ball_pos_y += ball_speed_y;
- //MAX-SPEED
- if(ball_speed_x >= 30) {
- ball_speed_x--;
- }
- //-------------------------------------------------------------------------------------------
- //----------------------------------------LEFT PLAYER----------------------------------------
- //-------------------------------------------------------------------------------------------
- //MOVEMENT (OVER MOUSE)
- player_l_pos_y = mouse_pos_y - player_height/2;
- player_l_midpos_y = player_l_pos_y + player_height/2;
- //TOP BOUND
- if (player_l_pos_y <= 0) {
- player_l_pos_y = 0;
- }
- //BOTTOM BOUND
- if (player_l_pos_y >= size_y - player_height) {
- player_l_pos_y = size_y - player_height;
- }
- //PLAYER SCORES
- if (ball_pos_x > player_r_pos_x + radius * 2) {
- score.play();
- player_l_score++;
- start.setVisible(true);
- pause.setVisible(false);
- ball_speed_x = 0;
- ball_speed_y = 0;
- ball_pos_x = middle_x;
- ball_pos_y = middle_y;
- }
- //PONG-BOUNCE
- if ( ball_pos_y + radius >= player_l_pos_y &&
- ball_pos_y - radius <= player_l_pos_y + player_height &&
- ball_pos_x - radius <= player_width + player_l_pos_x) {
- bounce.play();
- ball_speed_x *= -1;
- ball_speed_x += 1;
- //UPPERPART
- if (ball_pos_y < player_l_midpos_y) {
- if (ball_pos_y + radius > player_l_pos_y &&
- ball_pos_y < player_l_pos_y + ptenth) {
- ball_speed_y = ptenth5 / 4 * -1;} else
- if (ball_pos_y > player_l_pos_y + ptenth &&
- ball_pos_y < player_l_pos_y + ptenth2) {
- ball_speed_y = ptenth4 / 4 * -1;} else
- if (ball_pos_y > player_l_pos_y + ptenth2 &&
- ball_pos_y < player_l_pos_y + ptenth3) {
- ball_speed_y = ptenth3 / 4 * -1;} else
- if (ball_pos_y > player_l_pos_y + ptenth3 &&
- ball_pos_y < player_l_pos_y + ptenth4) {
- ball_speed_y = ptenth2 / 4 * -1;} else
- if (ball_pos_y > player_l_pos_y + ptenth4 &&
- ball_pos_y < player_l_pos_y + ptenth5) {
- ball_speed_y = ptenth / 4 *-1;} else {
- if (ball_speed_y > 0) {
- ball_speed_y++;
- } else if (ball_speed_y < 0) {
- ball_speed_y--;
- } else if(ball_speed_y == 0) {
- ball_speed_y = rnd.nextInt((int) speed);
- }
- }
- }
- //UNDERPART
- if (ball_pos_y > player_l_midpos_y) {
- if (ball_pos_y > player_l_pos_y + ptenth5 &&
- ball_pos_y < player_l_pos_y + ptenth6) {
- ball_speed_y = ptenth / 4;} else
- if (ball_pos_y > player_l_pos_y + ptenth6 &&
- ball_pos_y < player_l_pos_y + ptenth7) {
- ball_speed_y = ptenth2 / 4;} else
- if (ball_pos_y > player_l_pos_y + ptenth7 &&
- ball_pos_y < player_l_pos_y + ptenth8) {
- ball_speed_y = ptenth3 / 4;} else
- if (ball_pos_y > player_l_pos_y + ptenth8 &&
- ball_pos_y < player_l_pos_y + ptenth9) {
- ball_speed_y = ptenth4 / 4;} else
- if (ball_pos_y - radius> player_l_pos_y + ptenth9 &&
- ball_pos_y < player_l_pos_y + player_height) {
- ball_speed_y = ptenth5 / 4;} else {
- if (ball_speed_y > 0) {
- ball_speed_y++;
- } else if (ball_speed_y < 0) {
- ball_speed_y--;
- } else if(ball_speed_y == 0) {
- ball_speed_y = rnd.nextInt((int) speed);
- }
- }
- }
- }
- //--------------------------------------------------------------------------------------------
- //----------------------------------------RIGHT PLAYER----------------------------------------
- //--------------------------------------------------------------------------------------------
- player_r_midpos_y = player_r_pos_y + player_height/2;
- player_r_speed = player_r_midpos_y / 50;
- //TOP BOUND
- if (player_r_pos_y <= 0) {
- player_r_speed = 0;
- player_r_pos_y = 0;
- }
- //BOTTOM BOUND
- if (player_r_pos_y >= size_y - player_height) {
- player_r_speed = 0;
- player_r_pos_y = size_y - player_height;
- }
- //PLAYER SCORES
- if (ball_pos_x < player_l_pos_x - radius * 2) {
- score.play();
- player_r_score++;
- start.setVisible(true);
- pause.setVisible(false);
- ball_speed_x = 0;
- ball_speed_y = 0;
- ball_pos_x = middle_x;
- ball_pos_y = middle_y;
- }
- //PONG-BOUNCE
- if ( ball_pos_y + radius >= player_r_pos_y &&
- ball_pos_y - radius <= player_r_pos_y + player_height &&
- ball_pos_x + radius >= player_r_pos_x) {
- bounce.play();
- ball_speed_x *= -1;
- ball_speed_x -= 1;
- //UPPERPART
- if (ball_pos_y < player_r_midpos_y) {
- if (ball_pos_y + radius> player_r_pos_y &&
- ball_pos_y < player_r_pos_y + ptenth) {
- ball_speed_y = ptenth5 / 4 * -1;} else
- if (ball_pos_y > player_r_pos_y + ptenth &&
- ball_pos_y < player_r_pos_y + ptenth2) {
- ball_speed_y = ptenth4 / 4 * -1;} else
- if (ball_pos_y > player_r_pos_y + ptenth2 &&
- ball_pos_y < player_r_pos_y + ptenth3) {
- ball_speed_y = ptenth3 / 4 * -1;} else
- if (ball_pos_y > player_r_pos_y + ptenth3 &&
- ball_pos_y < player_r_pos_y + ptenth4) {
- ball_speed_y = ptenth2 / 4 * -1;} else
- if (ball_pos_y > player_r_pos_y + ptenth4 &&
- ball_pos_y < player_r_pos_y + ptenth5) {
- ball_speed_y = ptenth / 4 *-1;} else {
- if (ball_speed_y > 0) {
- ball_speed_y++;
- } else if (ball_speed_y < 0) {
- ball_speed_y--;
- } else if(ball_speed_y == 0) {
- ball_speed_y = rnd.nextInt((int) speed);
- }
- }
- }
- //UNDERPART
- if (ball_pos_y > player_l_midpos_y) {
- if (ball_pos_y > player_r_pos_y + ptenth5 &&
- ball_pos_y < player_r_pos_y + ptenth6) {
- ball_speed_y = ptenth / 4;} else
- if (ball_pos_y > player_r_pos_y + ptenth6 &&
- ball_pos_y < player_r_pos_y + ptenth7) {
- ball_speed_y = ptenth2 / 4;} else
- if (ball_pos_y > player_r_pos_y + ptenth7 &&
- ball_pos_y < player_r_pos_y + ptenth8) {
- ball_speed_y = ptenth3 / 4;} else
- if (ball_pos_y > player_r_pos_y + ptenth8 &&
- ball_pos_y < player_r_pos_y + ptenth9) {
- ball_speed_y = ptenth4 / 4;} else
- if (ball_pos_y - radius > player_r_pos_y + ptenth9 &&
- ball_pos_y < player_r_pos_y + player_height) {
- ball_speed_y = ptenth5 / 4;} else {
- if (ball_speed_y > 0) {
- ball_speed_y++;
- } else if (ball_speed_y < 0) {
- ball_speed_y--;
- } else if(ball_speed_y == 0) {
- ball_speed_y = rnd.nextInt((int) speed);
- }
- }
- }
- }
- // ----------------------------------------ARTIFICIAL INTELLIGENCE----------------------------------------
- // ||
- // O<< || PONG FLY AWAY FROM PLAYER
- // ||
- if (ball_speed_x < 0) {
- if (player_r_midpos_y < middle_y + 10 && player_r_midpos_y > middle_y - 10) {
- player_r_pos_y = middle_y - player_height/2;
- player_r_speed = 0;
- }
- if (player_r_midpos_y < middle_y) {
- player_r_pos_y += player_r_speed;
- } else if (player_r_midpos_y > middle_y) {
- player_r_pos_y -= player_r_speed;
- }
- } else
- // ||
- // >>O || PONG FLY TOWARDS PLAYER
- // ||
- if (ball_speed_x > 0) {
- if (player_r_speed >= 1) {
- if(player_r_pos_x - ball_pos_x <= 10) {
- player_r_speed -= 1;
- }
- }
- //PLAYER'S SPEED ADJUSTMENT
- if ( player_r_pos_y - ball_pos_y < size_y * 0.05 && player_r_pos_y - ball_pos_y > -size_y * 0.05) {
- player_r_speed = ball_pos_x / 80;
- } else
- if ( player_r_pos_y - ball_pos_y < size_y * 0.1 && player_r_pos_y - ball_pos_y > size_y * 0.05 ||
- player_r_pos_y - ball_pos_y > -size_y * 0.1 && player_r_pos_y - ball_pos_y < -size_y * 0.05) {
- player_r_speed = ball_pos_x / 70;
- } else
- if ( player_r_pos_y - ball_pos_y < size_y * 0.2 && player_r_pos_y - ball_pos_y > size_y * 0.1 ||
- player_r_pos_y - ball_pos_y > -size_y * 0.2 && player_r_pos_y - ball_pos_y < -size_y * 0.1) {
- player_r_speed = ball_pos_x / 60;
- } else
- if ( player_r_pos_y - ball_pos_y < size_y * 0.3 && player_r_pos_y - ball_pos_y > size_y * 0.2 ||
- player_r_pos_y - ball_pos_y > -size_y * 0.3 && player_r_pos_y - ball_pos_y < -size_y * 0.2) {
- player_r_speed = ball_pos_x / 50;
- } else
- if ( player_r_pos_y - ball_pos_y < size_y * 0.4 && player_r_pos_y - ball_pos_y > size_y * 0.3 ||
- player_r_pos_y - ball_pos_y > -size_y * 0.4 && player_r_pos_y - ball_pos_y < -size_y * 0.3) {
- player_r_speed = ball_pos_x / 40;
- } else
- if ( player_r_pos_y - ball_pos_y < size_y * 0.5 && player_r_pos_y - ball_pos_y > size_y * 0.4 ||
- player_r_pos_y - ball_pos_y > -size_y * 0.5 && player_r_pos_y - ball_pos_y < -size_y * 0.4) {
- player_r_speed = ball_pos_x / 30;
- } else
- if ( player_r_pos_y - ball_pos_y < size_y * 0.6 && player_r_pos_y - ball_pos_y > size_y * 0.5 ||
- player_r_pos_y - ball_pos_y > -size_y * 0.6 && player_r_pos_y - ball_pos_y < -size_y * 0.5) {
- player_r_speed = ball_pos_x / 25;
- } else
- if ( player_r_pos_y - ball_pos_y < size_y * 0.7 && player_r_pos_y - ball_pos_y > size_y * 0.6 ||
- player_r_pos_y - ball_pos_y > -size_y * 0.7 && player_r_pos_y - ball_pos_y < -size_y * 0.6) {
- player_r_speed = ball_pos_x / 20;
- } else
- if ( player_r_pos_y - ball_pos_y < size_y * 0.8 && player_r_pos_y - ball_pos_y > size_y * 0.7 ||
- player_r_pos_y - ball_pos_y > -size_y * 0.8 && player_r_pos_y - ball_pos_y < -size_y * 0.7) {
- player_r_speed = ball_pos_x / 15;
- } else
- if ( player_r_pos_y - ball_pos_y < size_y * 0.9 && player_r_pos_y - ball_pos_y > size_y * 0.8 ||
- player_r_pos_y - ball_pos_y > -size_y * 0.9 && player_r_pos_y - ball_pos_y < -size_y * 0.8) {
- player_r_speed = ball_pos_x / 10;
- } else
- if ( player_r_pos_y - ball_pos_y < size_y && player_r_pos_y - ball_pos_y > size_y * 0.9 ||
- player_r_pos_y - ball_pos_y > -size_y && player_r_pos_y - ball_pos_y < -size_y * 0.9) {
- player_r_speed = ball_pos_x / 5;
- }
- //PLAYER'S ACUITY
- //1ST FIFTH OF FIELD (LEFT BOUND)
- if (ball_pos_x < size_x * 0.2) {
- if (player_r_pos_y != ball_pos_y - rnd.nextInt((int) (size_x * 0.8) / 2) + size_x * 0.8 / 4) {
- if (ball_pos_y - rnd.nextInt((int) (size_x * 0.8) / 2) + (size_x * 0.8) / 4 < player_r_pos_y + player_height/2) {player_r_pos_y -= player_r_speed;}
- else if (ball_pos_y - rnd.nextInt((int) (size_x * 0.8) / 2) + (size_x * 0.8) / 4 > player_r_pos_y + player_height/2) {player_r_pos_y += player_r_speed;}
- }
- }
- //2ND FIFTH OF FIELD
- else if (ball_pos_x > size_x * 0.2 && ball_pos_x < size_x * 0.4) {
- if (player_r_pos_y != ball_pos_y - rnd.nextInt((int) (size_x * 0.6) / 2) + size_x * 0.6 / 4) {
- if (ball_pos_y - rnd.nextInt((int) (size_x * 0.6) / 2) + (size_x * 0.6) / 4 < player_r_pos_y + player_height/2) {player_r_pos_y -= player_r_speed;}
- else if (ball_pos_y - rnd.nextInt((int) (size_x * 0.6) / 2) + (size_x * 0.6) / 4 > player_r_pos_y + player_height/2) {player_r_pos_y += player_r_speed;}
- }
- }
- //3RD FIFTH OF FIELD
- else if (ball_pos_x > size_x * 0.4 && ball_pos_x < size_x * 0.6) {
- if (player_r_pos_y != ball_pos_y - rnd.nextInt((int) (size_x * 0.4) / 2) + size_x * 0.4 / 4) {
- if (ball_pos_y - rnd.nextInt((int) (size_x * 0.4) / 2) + (size_x * 0.4) / 4 < player_r_pos_y + player_height/2) {player_r_pos_y -= player_r_speed;}
- else if (ball_pos_y - rnd.nextInt((int) (size_x * 0.4) / 2) + (size_x * 0.4) / 4 > player_r_pos_y + player_height/2) {player_r_pos_y += player_r_speed;}
- }
- }
- //4TH FIFTH OF FIELD
- else if (ball_pos_x > size_x * 0.6 && ball_pos_x < size_x * 0.8) {
- if (player_r_pos_y != ball_pos_y - rnd.nextInt((int) (size_x * 0.2) / 2) + size_x * 0.4 / 4) {
- if (ball_pos_y - rnd.nextInt((int) (size_x * 0.2) / 2) + (size_x * 0.2) / 4 < player_r_pos_y + player_height/2) {player_r_pos_y -= player_r_speed;}
- else if (ball_pos_y - rnd.nextInt((int) (size_x * 0.2) / 2) + (size_x * 0.2) / 4 > player_r_pos_y + player_height/2) {player_r_pos_y += player_r_speed;}
- }
- }
- //5TH FIFTH OF FIELD (RIGHT BOUND)
- else if (ball_pos_x > size_x * 0.8 && ball_pos_x < size_x) {
- if (player_r_pos_y != ball_pos_y) {
- if (ball_pos_y < player_r_pos_y + player_height/2) {player_r_pos_y -= player_r_speed;}
- else if (ball_pos_y > player_r_pos_y + player_height/2) {player_r_pos_y += player_r_speed;}
- }
- }
- }
- // ----------------------------------------AI END----------------------------------------
- repaint();
- try {Thread.sleep(20);} catch (InterruptedException ex) {}
- Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
- }
- }
- public void paint(Graphics g) {
- this.setBackground(Color.black);
- ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //FONT & SHAPE SMOOTHING
- g.setColor(Color.white); //OBJECT & FONT COLOR
- g.fillOval((int) ball_pos_x - (int) radius, (int) ball_pos_y - (int) radius, 2 * (int) radius, 2 * (int) radius); //PONG-BALL
- g.fillRect((int) player_l_pos_x, (int) player_l_pos_y, (int) player_width, (int) player_height); //LEFT PLAYER
- g.fillRect((int) player_r_pos_x, (int) player_r_pos_y, (int) player_width, (int) player_height); //RIGHT PLAYER
- g.drawString((int) player_l_score + "", (int) middle_x / 2, 30); //LEFT PLAYER'S SCOREBOARD
- g.drawString(player_r_score + " ", (int) middle_x / 2 * 3, 30); //RIGHT PLAYER'S SCOREBOARD
- g.fillRect(0, (int) size_y, (int) size_x, 2); //BOTTOM-LINE
- g.fillRect((int) middle_x, 0, 2, (int) size_y); //CENTERLINE
- this.setSize((int) size_x, (int) size_y + 60); // APPLET-SIZE
- }
- //DOUBLE-BUFFERING
- public void update(Graphics g) {
- if (dbI == null) {
- dbI = createImage(this.getSize().width, this.getSize().height);
- dbg = dbI.getGraphics();
- }
- dbg.setColor(getBackground());
- dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
- dbg.setColor(getForeground());
- paint(dbg);
- g.drawImage(dbI, 0, 0, this);
- }
- }
Undefined
By: Guest | Date: Jul 13 2010 10:00 | Format: None | Expires: never | Size: 18.25 KB | Hits: 916
Latest pastes
1 hours ago
11 hours ago
1 days ago
2 days ago
2 days ago