Anda di halaman 1dari 24

import java.awt.*; import java.applet.*; import java.awt.event.

*;

public class bricks extends Applet { bricks_struct brick[]; final int NO_OF_BRICKS final int WINNER_SCORE = 64; = 640; // no of bricks * 10

final int NO_OF_BRICKS_PER_ROW = 16; final int LIVE final int DEAD final int LIFE final int LEFT final int RIGHT final int SCREEN_X final int SCREEN_Y final int BRICKS_WIDTH final int BRICKS_HEIGHT final int TIME_DELAY = 1; = -1; = 3; = 2; = -2; = 640; = 410; = 40; = 20; = 45; = 10; = 40; = 8;

final int HANDLE_INCREMENT final int HANDLE_WIDTH final int ALTERNATE_RADIUS final float RADIAN int RAD

= 3.14159f; = 8;

int i,x_pos,y_pos,key_dir,ball_x,ball_y,angle,score,width,height;

boolean first_time,bricks_hit,game_over,restart; int no_of_players_still_alive = LIFE;

Image ball; MediaTracker ball_track; Demoframe f; Menu menu; MenuBar bar;

public void init() { f = new Demoframe("Help Window for Bricks");

f.setLayout(new FlowLayout()); f.setBackground(new Color(200,156,135));

brick = new bricks_struct [NO_OF_BRICKS]; setBackground(Color.black); postEvent( new Event(this,Event.MOUSE_ENTER, "")); for(i=0;i<NO_OF_BRICKS;i++) { brick[i] brick[i].x brick[i].y = new bricks_struct(); = ( (int) ( i % NO_OF_BRICKS_PER_ROW ) ) * BRICKS_WIDTH;

= ( (int) ( i / NO_OF_BRICKS_PER_ROW ) + 1 ) * BRICKS_HEIGHT;

brick[i].score = 10; brick[i].live = LIVE;

brick[i].color = new Color( (int) (i + 100) % 255, (int) (i + 50) % 255, (int) (i % 255) );

} initialize_game(); no_of_players_still_alive = LIFE; score = 0;

} public void initialize_game() { x_pos y_pos key_dir ball_x ball_y angle = 300; = 350; = 0; = 300; = 330; = 225;

first_time = true; bricks_hit = true; game_over = false; restart = false;

ball_track = new MediaTracker(this);

ball //ball

= getImage(getDocumentBase(),"ball.gif"); = getImage(bricks.class.getResource("ball.gif"));

ball_track.addImage(ball,0); try { ball_track.waitForID(0); } catch (InterruptedException e ) { showStatus("Error in loading Graphics"); }

width height

= ball.getWidth(this); = ball.getHeight(this);

if(width < 0 || height < 0) { width RAD = ALTERNATE_RADIUS*2; = ALTERNATE_RADIUS;

height = ALTERNATE_RADIUS*2; } else RAD = width*3/8;

} public void stop() { showStatus("Press F1 for Help !!!");

bricks_hit = true; first_time = true;

public void start() { showStatus("Press F1 for Help !!!"); bricks_hit = true; first_time = true; repaint(); }

public void destroy() { if(ball != null) ball bar menu ball_track f = null; = null; = null; = null;

= null;

for(i=0;i<NO_OF_BRICKS;i++) brick[i] = null;

} public boolean handleEvent (Event e)

{ if(game_over == false) switch (e.key) { case Event.LEFT: if( x_pos >= HANDLE_INCREMENT ) { x_pos -= HANDLE_INCREMENT; key_dir = LEFT; } repaint(); return true; case Event.RIGHT: if(x_pos <= SCREEN_X - HANDLE_WIDTH - HANDLE_INCREMENT) { x_pos += HANDLE_INCREMENT; key_dir = RIGHT; } repaint(); return true; } switch (e.key) {

case Event.DOWN: bricks_hit = true;

first_time = true; repaint(); return true;

case Event.UP: restart = true; repaint(); return true;

case Event.F1: bricks_hit = true; first_time = true; repaint(); bar = new MenuBar(); menu = new Menu("Developed by Poonum"); bar.add(menu); f.setMenuBar(bar);

f.show(); return true;

} return true; }

public void paint(Graphics g)

{ if(restart) { g.setColor(Color.black); g.fill3DRect( (SCREEN_X-200)/2, (SCREEN_Y-150)/2, 200, 150, true);

erase_handle(g); erase_ball(g); restart_game(); repaint(); return; } if(game_over) return; if(bricks_hit) { redraw_wall(g); display_score(g); } if(first_time) { create_handle(g); // redraw the walls // & display score // if one of the bricks is hit // if game is over, don't repaint.

create_ball(g); showStatus("Press F1 for Help !!!"); } else if( (no_of_players_still_alive != 0 ) && (game_over == false) ) move_ball(g);

if((no_of_players_still_alive == 0 ) || (score == WINNER_SCORE)) { erase_ball(g); erase_handle(g); display_score(g); show_final_message(g); redraw_wall(g); // destroy(); game_over = true;

} public void update(Graphics g) { if( (key_dir == LEFT) || (key_dir == RIGHT) ) { move_handle_accordingly(g);

first_time = false; }

paint(g); } public void move_handle_accordingly(Graphics g) {

erase_handle(g); create_handle(g); key_dir = 0; } public void create_handle(Graphics g) { g.setColor(new Color(128,128,128)); g.fill3DRect( x_pos, y_pos, HANDLE_WIDTH, 5, true); }

public void erase_handle(Graphics g) { g.setColor(Color.black); g.drawLine(0,y_pos,SCREEN_X,y_pos); // erase the whole line for handle

g.drawLine(0,y_pos+1,SCREEN_X,y_pos+1); // erase the whole line for handle g.drawLine(0,y_pos+2,SCREEN_X,y_pos+2); // erase the whole line for handle g.drawLine(0,y_pos+3,SCREEN_X,y_pos+3); // erase the whole line for handle g.drawLine(0,y_pos+4,SCREEN_X,y_pos+4); // erase the whole line for handle } public void redraw_wall(Graphics graph) { for(i=0;i<NO_OF_BRICKS;i++) if(brick[i].live == LIVE) {

graph.setColor ( brick[i].color); graph.fill3DRect( brick[i].x, brick[i].y, BRICKS_WIDTH, BRICKS_HEIGHT, true ); } else if ( ( brick[i].live == DEAD ) && ( brick[i].color != Color.black ) ) { graph.setColor(Color.black); brick[i].color = Color.black; graph.fillRect ( brick[i].x, brick[i].y,

BRICKS_WIDTH, BRICKS_HEIGHT);

} }

public void move_ball(Graphics graph) { bricks_hit = false; erase_ball(graph); if(ball_x <= 0 && ball_y != y_pos - 2*RAD) angle = 360 - angle; else if(ball_x >= SCREEN_X - 2*RAD - 2 && ball_y != y_pos - 2*RAD) angle = 360 - angle; else if(ball_y <= 0) angle = 540 - angle; else if( ( ball_x + RAD >= x_pos ) && ( ball_x + RAD <= x_pos + HANDLE_WIDTH ) && ( ball_y >= y_pos-2*RAD ) ) { if(ball_x + RAD <= x_pos + 10) angle = 145; else

if( (ball_x + RAD > x_pos + 10 )&&(ball_x + RAD <= x_pos + 30 )) angle = 540 - angle; else if( (ball_x + RAD > x_pos + 30 )&&(ball_x + RAD <= x_pos + 40 )) angle = 215; create_handle(graph); } else if(ball_y >= y_pos) {

-- no_of_players_still_alive; ball = null;

erase_ball(graph); erase_handle(graph); if(no_of_players_still_alive > 0) { initialize_game(); create_ball(graph); repaint(); return; } } else { check_brick_collision();

} ball_x -= 7*sine(angle); ball_y += 7*cosine(angle);

if(ball_x < 0 ) ball_x = 0; if(ball_y < 0 ) ball_y = 0;

create_ball(graph);

try { Thread.sleep(TIME_DELAY - score/20); } catch ( InterruptedException e) { showStatus( e.toString()); }

repaint(); }

public void erase_ball(Graphics graph) { graph.setColor(Color.black); if(width > ALTERNATE_RADIUS*2 &&

height > ALTERNATE_RADIUS*2 ) graph.fillRect( ball_x, ball_y, (int) width * 3/4, (int) height * 3/4); else graph.fillRect( ball_x, ball_y, (int) width+ 1, // FIX FOR NETSCAPE GOLD 3.0 (int) height+1);

public void create_ball(Graphics graph) {

if(width > ALTERNATE_RADIUS*2 && height > ALTERNATE_RADIUS*2 ) graph.drawImage(ball,ball_x,ball_y,(int) width*3/4,(int)height*3/4,this); else for(int k=0;k<ALTERNATE_RADIUS;k++) {

graph.setColor( new Color( (int) ((k*10 + 100) % 255),

(int) ((k*10 + 55) % 255), (int) ( k )); % 255)

graph.fillOval( ball_x+k, ball_y+k, ALTERNATE_RADIUS*2 - k*2, ALTERNATE_RADIUS*2 - k*2); }

public void check_brick_collision() { int count;

for(count=0;count<NO_OF_BRICKS;count++) { if ( brick[count].x <= ball_x + RAD && brick[count].x + BRICKS_WIDTH >= ball_x + RAD && brick[count].y - 2*RAD <= ball_y && ball_y <= brick[count].y - RAD && brick[count].live == LIVE )

{ angle = 540 - angle; score+=brick[count].score; brick[count].live = DEAD; bricks_hit = true; break; }

if (

brick[count].x <= ball_x + RAD && brick[count].x + BRICKS_WIDTH >=ball_x + RAD && brick[count].y + BRICKS_HEIGHT <= ball_y + RAD && ball_y <= brick[count].y + BRICKS_HEIGHT && brick[count].live == LIVE

) { angle = 540 - angle; score+=brick[count].score; brick[count].live = DEAD; bricks_hit = true; break; }

if (

brick[count].x + BRICKS_WIDTH <= ball_x + RAD && brick[count].x + BRICKS_WIDTH >= ball_x brick[count].y <= ball_y + RAD && &&

ball_y + RAD <=brick[count].y + BRICKS_HEIGHT && brick[count].live == LIVE

) { angle = 360 - angle; score+=brick[count].score; brick[count].live = DEAD; bricks_hit = true; break; }

if (

brick[count].x <= ball_x + (2*RAD) && brick[count].x >= ball_x + RAD brick[count].y <= ball_y + RAD ball_y + RAD && &&

<= brick[count].y + BRICKS_HEIGHT &&

brick[count].live == LIVE

) { angle = 360 - angle; score+=brick[count].score; brick[count].live = DEAD; bricks_hit = true; break; }

} }

public void display_score(Graphics g) { g.setColor(Color.lightGray); g.fill3DRect( SCREEN_X/2-60, SCREEN_Y-50, 120, 30, true); g.setColor(Color.black); g.fill3DRect( SCREEN_X/2-50, SCREEN_Y-45, 100, 20, true); g.setColor(Color.yellow); g.drawString(" Life: " +

no_of_players_still_alive + " Score: " score, SCREEN_X/2-50, SCREEN_Y-30); +

} public void show_final_message(Graphics graph) { int msg_length = 200; int msg_width = 150;

for(int k=0;k<ALTERNATE_RADIUS;k++) {

graph.setColor( new Color( (int) ((k*10 + 100) % 255), (int) ((k*10 + 5) % 255), (int) ( k )); graph.fill3DRect( (SCREEN_X-200)/2+k, (SCREEN_Y-150)/2+k, msg_length-2*k, msg_width-2*k, true); } % 255)

graph.setFont(new Font("TimesRoman",Font.BOLD,30)); graph.setColor(Color.black); graph.drawString("Game Over",(SCREEN_X-200)/2+30, (SCREEN_Y-150)/2+60);

graph.setFont(new Font("TimesRoman",Font.BOLD,25)); if(score == WINNER_SCORE) graph.drawString("You Won !!!", (SCREEN_X-170)/2+30, (SCREEN_Y-100)/2+60); else graph.drawString("You Lost!!!", (SCREEN_X-170)/2+30, (SCREEN_Y-100)/2+60);

void restart_game() { destroy(); init(); start(); } double sine(int angle) { return Math.sin(angle*RADIAN/180); }

double cosine(int angle) {

return Math.cos(angle*RADIAN/180); }

}; /*Next File */ import java.awt.*; import java.applet.*;

public class bricks_struct { public int x; public int y; public int score; public int live; public Color color; };

/* next file */ import java.awt.*; class Demoframe extends Frame { private Label l1,l2,l3,l4,l5; Menu menu; MenuBar bar;

public Demoframe (String s) { super(s); this.setFont(new Font("TimesRoman",Font.BOLD,20)); l1 = new Label("Press Left arrow for moving the handle to left"); l2 = new Label("Press Right arrow for moving the handle to right"); l3 = new Label("Press Up arrow to restart the game"); l4 = new Label("Press Down arrow to pause the game"); l5 = new Label("Press F1 key to display this help message"); add(l1); add(l2); add(l3); add(l4); add(l5); this.reshape(150,100,500,300); this.setResizable(false);

public boolean handleEvent(Event e) { if(e.id == Event.WINDOW_DESTROY) { hide(); dispose(); return true; } return super.handleEvent(e); }