문제
왼쪽에서 날아오는 총알을 피해 살아남아야 하는 게임입니다 이를 해결하는 프로그램의 다음 실행상태에 대해 빈칸을 채우세요
프로그램의 실행순서 및 실행상태
①
②
1 public static void main( String [] args )
4 new CollidableObject( imagePath+"character.png", WIDTH-CollidableObject.IMGSIZE, HEIGHT/2, WIDTH, HEIGHT ), imagePath+"bullet.png" );
C1b public CollidableObject( final String image, int x, int y, int width, int height ) {
C11 super( image, x, y, 0, 40, width, height );
K1b public ObjectByKey( String image, int x, int y, int minX, int minY, int maxX, int maxY ) {
K1e }
C1e }
3 new BulletDodgePanel(
P1b public BulletDodgePanel( CollidableObject character, final String imageBomb ) {
P11 super( character, imageBomb );
B1b public BombDodgePanel( CollidableObject character, final String imageBomb ) {
B11 super( character );
V1b public ViewPanel( ObjectByKey character ){
V1e }
B1e }
P1e }
2 BulletDodgePanel panel =
5 }
③
E001 // 이벤트 대기중
E002 // ↓ 키 입력
④
K2b public void keyPressed( KeyEvent event ) {
K22 directionY = DOWN;
K2e }
E001 // 이벤트 대기중
E003 // 1초마다 타이머 활성화
⑤
V2b public void actionPerformed( ActionEvent event ) {
V21 update();
P2b protected void update() {
P21 bomb.move();
K4b public void move() {
K41 move( directionX, directionY );
C3b public void move( int directionX, int directionY ) {
C31 super.move( directionX * SPEED, directionY * SPEED );
K5b public void move( int directionX, int directionY ) {
K5e }
C3e }
K4e }
P22 character.move( CollidableObject.STOP, character.directionY() );
C3b public void move( int directionX, int directionY ) {
C31 super.move( directionX * SPEED, directionY * SPEED );
K5b public void move( int directionX, int directionY ) {
K5e }
C3e }
⑥
P23 updateHP();
B3b protected void updateHP() {
B31 if ( ( bomb.collide( character )
C4b public boolean collide( ObjectByKey that ) {
C4e }
B32 == true ) ) {
B3e }
P2e }
V22 repaint();
B4b public void paint( Graphics g ) {
B41 character.paint( g );
K6b public void paint( Graphics g ) {
K6e }
⑦
B42 bomb.paint( g );
K6b public void paint( Graphics g ) {
K6e }
⑧
B4e }
V2e }
E001 // 이벤트 대기중
프로그램 코드
E001 // 이벤트 대기중
E002 // ↓ 키 입력
E003 // 1초마다 타이머 활성화
import javax.swing.*;
import move.*;
public class BulletDodgeGameMain
{
1 public static void main( String [] args )
{
final String imagePath = "C:\\Users\\user\\Downloads\\JAVA-main\\src\\move\\image\\";
final int WIDTH = 600;
final int HEIGHT= 300;
2 BulletDodgePanel panel =
3 new BulletDodgePanel(
4 new CollidableObject( imagePath+"character.png", WIDTH-CollidableObject.IMGSIZE, HEIGHT/2, WIDTH, HEIGHT ), imagePath+"bullet.png" );
JFrame frame = new JFrame ( "총알 피하기 게임" );
frame.getContentPane().add( panel );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
5 }
}
package move;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ViewPanel extends JPanel
{
protected ObjectByKey character;
protected Timer timer;
V1b public ViewPanel( ObjectByKey character ){
this.character = character;
addKeyListener( character );
setFocusable( true );
requestFocus();
setPreferredSize( new Dimension( character.backgroundWidth(), character.backgroundHeight() ) );
timer = new Timer( 100, new ActionListener() {
@Override
V2b public void actionPerformed( ActionEvent event ) {
V21 update();
V22 repaint();
V2e }
});
timer.start();
V1e }
V3b protected void update() {
V31 character.move();
V3e }
@Override
V4b public void paint( Graphics g ){
super.paint( g );
V41 character.paint( g );
V4e }
}
package move;
import java.awt.*;
import java.util.ArrayList;
public class BombDodgePanel extends ViewPanel
{
protected ArrayList<CollidableObject> bombs;
protected int characterHP, time, width, height;
protected String imageBomb;
B1b public BombDodgePanel( CollidableObject character, final String imageBomb ) {
B11 super( character );
this.characterHP = 5;
this.bombs = new ArrayList<CollidableObject>();
this.imageBomb = imageBomb;
this.width = character.backgroundWidth();
this.height= character.backgroundHeight();
this.time = 300;
setBackground( Color.white );
setPreferredSize( new Dimension( width + CollidableObject.IMGSIZE, height ) );
B1e }
@Override
B2b protected void update() {
for ( CollidableObject bomb : bombs )
B21 bomb.move();
if ( ( ( time-- ) % 8 ) == 0 ) {
bombs.add( new CollidableObject( CollidableObject.STOP, CollidableObject.DOWN, imageBomb, width, height ) );
}
B22 character.move( character.directionX(), CollidableObject.STOP );
B23 updateHP();
B2e }
B3b protected void updateHP() {
for ( int i = 0; i < bombs.size(); i++ ) {
CollidableObject bomb = bombs.get(i);
B31 if ( ( bomb.collide( character )
B32 == true ) ) {
characterHP--;
bombs.remove( bomb );
}
else if ( ( bomb.collide() == true ) ) {
bombs.remove( bomb );
}
}
B3e }
@Override
B4b public void paint( Graphics g ) {
super.paint( g );
if ( this.time <= 0 ) {
g.setColor( Color.black );
g.setFont( new Font( "Arial", Font.BOLD, 40 ) );
g.drawString( "Success!", width/2-80, height/2 );
timer.stop();
}
else if ( this.characterHP <= 0 ) {
g.setColor( Color.black );
g.setFont( new Font( "Arial", Font.BOLD, 40 ) );
g.drawString( "Game Over!", width/2-100, height/2 );
timer.stop();
}
else {
g.setColor( Color.black );
g.setFont( new Font( "Arial", Font.BOLD, 20 ) );
g.drawString( "HP : " + hp(), 10, 30 );
B41 character.paint( g );
for ( CollidableObject bomb : bombs )
B42 bomb.paint( g );
}
B4e }
public String hp() {
switch( this.characterHP ) {
case 5:
return "● ● ● ● ●";
case 4:
return "● ● ● ● ○";
case 3:
return "● ● ● ○ ○";
case 2:
return "● ● ○ ○ ○";
case 1:
return "● ○ ○ ○ ○";
default:
return "○ ○ ○ ○ ○";
}
}
}
package move;
import java.awt.*;
public class BulletDodgePanel extends BombDodgePanel
{
P1b public BulletDodgePanel( CollidableObject character, final String imageBomb ) {
P11 super( character, imageBomb );
setPreferredSize( new Dimension( width, height + CollidableObject.IMGSIZE ) );
P1e }
@Override
P2b protected void update() {
for ( CollidableObject bomb : bombs )
P21 bomb.move();
if ( ( ( time-- ) % 8 ) == 0 ) {
bombs.add( new CollidableObject( CollidableObject.RIGHT, CollidableObject.STOP, imageBomb, width, height ) );
}
P22 character.move( CollidableObject.STOP, character.directionY() );
P23 updateHP();
P2e }
}
package move;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ObjectByKey extends KeyAdapter
{
private Image image;
protected int x, y, directionX, directionY;
protected int minX, minY, maxX, maxY;
public static final int LEFT = -1, RIGHT = 1, UP = -1, DOWN = 1, STOP = 0, IMGSIZE = 40;
K1b public ObjectByKey( String image, int x, int y, int minX, int minY, int maxX, int maxY ) {
this.image = new ImageIcon( image ).getImage();
this.x = x;
this.y = y;
this.minX = minX;
this.minY = minY;
this.maxX = maxX;
this.maxY = maxY;
this.directionX = STOP;
this.directionY = STOP;
K1e }
@Override
K2b public void keyPressed( KeyEvent event ) {
switch( event.getKeyCode() ) {
case KeyEvent.VK_ESCAPE:
System.exit(0);
break;
case KeyEvent.VK_LEFT: case 'A': case 'a':
directionX = LEFT;
break;
case KeyEvent.VK_RIGHT: case 'D': case 'd':
K21 directionX = RIGHT;
break;
case KeyEvent.VK_UP: case 'W': case 'w':
directionY = UP;
break;
case KeyEvent.VK_DOWN: case 'S': case 's':
K22 directionY = DOWN;
break;
}
K2e }
@Override
K3b public void keyReleased( KeyEvent event ) {
switch( event.getKeyCode() ) {
case KeyEvent.VK_LEFT: case 'A': case 'a':
case KeyEvent.VK_RIGHT: case 'D': case 'd':
K31 directionX = STOP;
break;
case KeyEvent.VK_UP: case 'W': case 'w':
case KeyEvent.VK_DOWN: case 'S': case 's':
directionY = STOP;
break;
}
K3e }
K4b public void move() {
K41 move( directionX, directionY );
K4e }
K5b public void move( int directionX, int directionY ) {
this.x += directionX;
this.y += directionY;
this.x = ( this.x <= minX ) ? minX : this.x;
this.y = ( this.y <= minY ) ? minY : this.y;
this.x = ( this.x >= maxX ) ? maxX : this.x;
this.y = ( this.y >= maxY ) ? maxY : this.y;
K5e }
K6b public void paint( Graphics g ) {
g.drawImage( image, x, y, IMGSIZE, IMGSIZE, null );
K6e }
public int directionX() {
return directionX;
}
public int directionY() {
return directionY;
}
public int backgroundWidth(){
return maxX;
}
public int backgroundHeight(){
return maxY;
}
}
package move;
public class CollidableObject extends ObjectByKey
{
C1b public CollidableObject( final String image, int x, int y, int width, int height ) {
C11 super( image, x, y, 0, 40, width, height );
C1e }
C2b public CollidableObject( int directionX, int directionY, final String image, int width, int height ) {
C21 this( image, 0, 40, width, height );
this.directionX = directionX;
this.directionY = directionY;
if ( ( directionX == STOP ) && ( directionY == DOWN ) ) {
this.x = (int)( Math.random() * this.maxX );
}
else if ( ( directionX == RIGHT ) && ( directionY == STOP ) ) {
this.y = IMGSIZE + (int)( Math.random() * ( this.maxY - IMGSIZE ) );
}
C2e }
@Override
C3b public void move( int directionX, int directionY ) {
final int SPEED = 20;
C31 super.move( directionX * SPEED, directionY * SPEED );
C3e }
C4b public boolean collide( ObjectByKey that ) {
return ( Math.abs( this.x - that.x ) < IMGSIZE ) && ( Math.abs( this.y - that.y ) < IMGSIZE );
C4e }
public boolean collide() {
return ( this.x < minX ) || ( maxX < this.x ) || ( this.y < minY ) || ( maxY < this.y );
}
}