import java.awt.*; import java.applet.*; import java.util.*; //************************************************************************************* //************************************************************************************* //*************************** The Applet ********************************************* //************************************************************************************* //************************************************************************************* public class TwoPlayerPong extends Applet implements Runnable { OurCanvas table_canvas; Graphics g_buffer; Graphics g_applet; Graphics g_canvas; Thread animate; AudioClip hit_sound; AudioClip bounce_sound; AudioClip miss_sound; MediaTracker mt; Image paddle_img; Image paddle_mask_img; Image ball_img; Image ball_mask_img; Image img_buffer; private Label P1_score_label; private Label P2_score_label; private CheckboxGroup radio_ball_speed; private Checkbox radio_slow, radio_medium, radio_fast; private Panel p1; private Label time_label; private Label l1; BorderLayout p1bordermanager; long start_time; //Save the millisecond we started at long current_millisec; //Current milli-second; Date timer_date; String timer_string; int P1_score = 0; int P2_score = 0; Ball puck; Paddle P1_paddle; Paddle P2_paddle; int table_width = 400; int table_height = 300; int table_top_edge = 0; int table_bottom_edge = table_height; int table_right_edge = table_width; int table_left_edge = 0; int last_mouse_y = 0; boolean in_play = true; //************************ Start ************************************* public void init() { super.init(); this.resize(table_width, table_height); //setLayout( new BorderLayout() ); table_canvas = new OurCanvas(); table_canvas.resize(table_width,table_height); add(table_canvas); Panel scores_p = new Panel(); BorderLayout bordermanager_1 = new BorderLayout(); Label place_holder_label; scores_p.setLayout (bordermanager_1); scores_p.add( "West", P1_score_label = new Label("0")); scores_p.add( "Center", place_holder_label = new Label(" ")); scores_p.add( "East", P2_score_label = new Label("0")); add(scores_p); Panel p1 = new Panel(); p1bordermanager = new BorderLayout(); p1.setLayout ( p1bordermanager); Panel p2 = new Panel(); radio_ball_speed = new CheckboxGroup(); p2.add( l1 = new Label("Ball Speed:")); p2.add( radio_slow = new Checkbox ("Slow", radio_ball_speed, true) ); p2.add( radio_medium = new Checkbox ("Med", radio_ball_speed, false) ); p2.add( radio_fast = new Checkbox ("Fast", radio_ball_speed, false) ); p1.add( "West", p2); timer_string = new String(" Time = 0:00 "); p1.add( "East", time_label = new Label(timer_string) ); add(p1); g_applet = this.getGraphics(); g_canvas= table_canvas.getGraphics(); img_buffer = createImage( table_width, table_height); g_buffer = img_buffer.getGraphics(); g_buffer.setColor(Color.black); g_buffer.fillRect(0,0,table_width,table_height); bounce_sound = getAudioClip( getDocumentBase(), "bounce.au"); hit_sound = getAudioClip( getDocumentBase(), "hit.au"); miss_sound = getAudioClip( getDocumentBase(), "miss.au"); paddle_img = getPicImage("paddle.gif"); paddle_mask_img = getPicImage("paddle_mask.gif"); ball_img = getPicImage("ball.gif"); ball_mask_img = getPicImage("ball_mask.gif"); P1_paddle = new Paddle(paddle_img.getWidth(null), paddle_img.getHeight(null), 0, table_height); P2_paddle = new Paddle(paddle_img.getWidth(null), paddle_img.getHeight(null), 0, table_height); puck = new Ball(ball_img.getWidth(null), ball_img.getHeight(null)); timer_date = new Date(); if (animate == null) { animate = new Thread (this); animate.start(); } requestFocus(); P1_paddle.jump_to(10, 0); P1_paddle.center(); P1_paddle.set_speed(2); P1_paddle.set_paddle_direction(0); P2_paddle.jump_to(table_width - 10, 0); P2_paddle.center(); P2_paddle.set_speed(2); P2_paddle.set_paddle_direction(0); puck.set_speed(1); start_volley(); } //********************************************************************************* //Run- this method is called by the thread "animate" and starts an endless loop *** public void run() { int currentSecond =0; int lastSecond =0; int remainderSeconds = 0; int minutes = 0; StringBuffer displayMminutes = new StringBuffer(""); StringBuffer seconds = new StringBuffer(""); StringBuffer timerString = new StringBuffer(""); for (;;) { //If we rolled around to another second update Timer on screen current_millisec = System.currentTimeMillis() - start_time; currentSecond = (int)current_millisec/1000; if ( currentSecond != lastSecond) { timerString = new StringBuffer(""); timerString.append(" Time = "); minutes = currentSecond / 60; remainderSeconds = currentSecond - (minutes * 60); timerString.append(minutes); timerString.append(":"); if (remainderSeconds < 10) timerString.append("0"); timerString.append(remainderSeconds); timerString.append(" "); time_label.setText(timerString.toString()); lastSecond = currentSecond; } puck.move(); P1_paddle.move(); P2_paddle.move(); collision_detect_walls(); off_table_detect(); if (in_play) collision_detect_paddles(); table_paint(); try { animate.sleep(6); } catch (InterruptedException e) {}; } } //********************** off_table_detect ****************************** public void off_table_detect() { String score; if (puck.right_edge < 0 ) { P2_score++; P2_score_label.setText(String.valueOf(P2_score)); end_volley(); } if (puck.left_edge > table_width ) { P1_score++; P1_score_label.setText(String.valueOf(P1_score)); end_volley(); } } //********************** end_volley ****************************** private void end_volley() { in_play = false; miss_sound.play(); try { Thread.sleep(1000); } catch(InterruptedException e) { return; }; start_volley(); } //********************** start_volley ****************************** public void start_volley() { start_time = System.currentTimeMillis(); puck.flip_horz_direction(); puck.jump_to(table_width/2, table_height/2); in_play = true; } //********************** collision_detect_paddle ********************** public void collision_detect_paddles() { int diff_x; if ( (P1_paddle.top_edge <= puck.bottom_edge) && (P1_paddle.bottom_edge >= puck.top_edge)) { diff_x = P1_paddle.right_edge - puck.left_edge; if (diff_x > 0) { hit_sound.play(); if (diff_x > 3) { puck.flip_vert_direction(); in_play = false; } else puck.flip_horz_direction(); } } if ( (P2_paddle.top_edge <= puck.bottom_edge) && (P2_paddle.bottom_edge >= puck.top_edge)) { diff_x = puck.right_edge - P2_paddle.left_edge; if (diff_x > 0) { hit_sound.play(); if (diff_x > 3) { puck.flip_vert_direction(); in_play = false; } else puck.flip_horz_direction(); } } } //********************** collision_detect_walls ********************** public void collision_detect_walls() { //****** Detect collision between ball and table edges if ( puck.top_edge < table_top_edge ) { bounce_sound.play(); puck.flip_vert_direction(); } if ( puck.bottom_edge > table_bottom_edge ) { bounce_sound.play(); puck.flip_vert_direction(); } } //************************ Stop ************************************* public void stop() { if (animate != null) { animate.stop(); animate=null; } } //************************ Update ************************************* public void update(Graphics g) { paint(g); } //************************ Paint ************************************* public void table_paint() { g_buffer.drawImage(paddle_mask_img, P1_paddle.last_pos_x, P1_paddle.last_pos_y, null); g_buffer.drawImage(paddle_img, P1_paddle.pos_x, P1_paddle.pos_y, null); g_buffer.drawImage(paddle_mask_img, P2_paddle.last_pos_x, P2_paddle.last_pos_y, null); g_buffer.drawImage(paddle_img, P2_paddle.pos_x, P2_paddle.pos_y, null); g_buffer.drawImage(ball_mask_img, puck.last_pos_x, puck.last_pos_y, null); g_buffer.drawImage(ball_img, puck.pos_x, puck.pos_y, null); g_canvas.drawImage(img_buffer, 0, 0, null); } //************************ KeyDown ************************************* public boolean keyDown(Event evt, int key) { if ( key == 97 ) { P1_paddle.set_paddle_direction(1); } if ( key == 113 ) { P1_paddle.set_paddle_direction(-1); } if ( key == Event.DOWN ) { P2_paddle.set_paddle_direction(1); } if ( key == Event.UP ) { P2_paddle.set_paddle_direction(-1); } return true; } //************************ KeyUp ************************************* public boolean keyUp(Event evt, int key) { if (evt.id == Event.KEY_ACTION_RELEASE) { P2_paddle.set_paddle_direction(0); } else { P1_paddle.set_paddle_direction(0); } return true; } //************************ action ************************************* public boolean action( Event e, Object o) { if (e.target instanceof Checkbox) { if (radio_slow.getState() == true) puck.set_speed(1); if (radio_medium.getState() == true) puck.set_speed(2); if (radio_fast.getState() == true) puck.set_speed(3); } return true; } //*************** getPicImage ************************************** // Use MediaTracker to insure the images are fully loaded before // we try to use them private Image getPicImage(String image_file_name) { Image img_work = null; mt = new MediaTracker(this); try { img_work = getImage(getDocumentBase(),image_file_name); } catch(Exception e1) { System.out.println(e1); } mt.addImage(img_work, 0); try { showStatus("Loading image " + image_file_name ); mt.checkID(0, true); mt.waitForID(0); } catch(InterruptedException e2) { System.out.println(e2); } return img_work; } } //---- End of Applet