문제
왼쪽에서 날아오는 총알을 피해 살아남아야 하는 게임입니다 이를 해결하는 다음 프로그램에 대해 빈칸을 채우세요
코드 빈칸 채우기
import javax.swing.*;
import move.*;
public BulletDodgeGameMain
{
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;
BulletDodgePanel panel =
new BulletDodgePanel(
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 );
}
}
package move;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public ViewPanel JPanel
{
protected ObjectByKey character;
protected Timer timer;
public ViewPanel( ObjectByKey character ){
.character = character;
( character );
setFocusable( true );
requestFocus();
setPreferredSize( new Dimension( character.backgroundWidth(), character.backgroundHeight() ) );
timer = new Timer( 100, new () {
@Override
public void ( event ) {
update();
();
}
});
timer.start();
}
protected void update() {
character.move();
}
@Override
public void ( Graphics g ){
.( g );
character.( g );
}
}
package move;
import java.awt.*;
import java.util.ArrayList;
public BombDodgePanel ViewPanel
{
protected ArrayList<CollidableObject> bombs;
protected int characterHP, time, width, height;
protected String imageBomb;
public BombDodgePanel( CollidableObject character, final String imageBomb ) {
( character );
.characterHP = 5;
.bombs = new ArrayList<CollidableObject>();
.imageBomb = imageBomb;
.width = character.backgroundWidth();
.height= character.backgroundHeight();
.time = 300;
setBackground( Color.white );
setPreferredSize( new Dimension( width + CollidableObject.IMGSIZE, height ) );
}
@Override
protected void update() {
for ( CollidableObject bomb : bombs )
bomb.move();
if ( ( ( time-- ) % 8 ) == 0 ) {
bombs.add( new CollidableObject( CollidableObject.STOP, CollidableObject.DOWN, imageBomb, width, height ) );
}
character.move( character.directionX(), CollidableObject.STOP );
updateHP();
}
protected void updateHP() {
for ( int i = 0; i < bombs.size(); i++ ) {
CollidableObject bomb = bombs.get(i);
if ( ( bomb.collide( character )
== true ) ) {
characterHP--;
bombs.remove( bomb );
}
else if ( ( bomb.collide() == true ) ) {
bombs.remove( bomb );
}
}
}
@Override
public void ( Graphics g ) {
.( g );
if ( .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 ( .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 );
character.( g );
for ( CollidableObject bomb : bombs )
bomb.( g );
}
}
public String hp() {
switch( .characterHP ) {
case 5:
return "● ● ● ● ●";
case 4:
return "● ● ● ● ○";
case 3:
return "● ● ● ○ ○";
case 2:
return "● ● ○ ○ ○";
case 1:
return "● ○ ○ ○ ○";
default:
return "○ ○ ○ ○ ○";
}
}
}
package move;
import java.awt.*;
public BulletDodgePanel BombDodgePanel
{
public BulletDodgePanel( CollidableObject character, final String imageBomb ) {
( character, imageBomb );
setPreferredSize( new Dimension( width, height + CollidableObject.IMGSIZE ) );
}
@Override
protected void update() {
for ( CollidableObject bomb : bombs )
bomb.move();
if ( ( ( time-- ) % 8 ) == 0 ) {
bombs.add( new CollidableObject( CollidableObject.RIGHT, CollidableObject.STOP, imageBomb, width, height ) );
}
character.move( CollidableObject.STOP, character.directionY() );
updateHP();
}
}
package move;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public ObjectByKey
{
private 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;
public ObjectByKey( String image, int x, int y, int minX, int minY, int maxX, int maxY ) {
.image = new ( image ).();
.x = x;
.y = y;
.minX = minX;
.minY = minY;
.maxX = maxX;
.maxY = maxY;
.directionX = STOP;
.directionY = STOP;
}
@Override
public void ( event ) {
switch( event.() ) {
case .VK_ESCAPE:
System.exit(0);
break;
case .VK_LEFT: case 'A': case 'a':
directionX = LEFT;
break;
case .VK_RIGHT: case 'D': case 'd':
directionX = RIGHT;
break;
case .VK_UP: case 'W': case 'w':
directionY = UP;
break;
case .VK_DOWN: case 'S': case 's':
directionY = DOWN;
break;
}
}
@Override
public void ( event ) {
switch( event.() ) {
case .VK_LEFT: case 'A': case 'a':
case .VK_RIGHT: case 'D': case 'd':
directionX = STOP;
break;
case .VK_UP: case 'W': case 'w':
case .VK_DOWN: case 'S': case 's':
directionY = STOP;
break;
}
}
public void move() {
move( directionX, directionY );
}
public void move( int directionX, int directionY ) {
.x += directionX;
.y += directionY;
.x = ( .x <= minX ) ? minX : .x;
.y = ( .y <= minY ) ? minY : .y;
.x = ( .x >= maxX ) ? maxX : .x;
.y = ( .y >= maxY ) ? maxY : .y;
}
public void ( Graphics g ) {
g.( image, x, y, IMGSIZE, IMGSIZE, null );
}
public int directionX() {
return directionX;
}
public int directionY() {
return directionY;
}
public int backgroundWidth(){
return maxX;
}
public int backgroundHeight(){
return maxY;
}
}
package move;
public CollidableObject ObjectByKey
{
public CollidableObject( final String image, int x, int y, int width, int height ) {
( image, x, y, 0, 40, width, height );
}
public CollidableObject( int directionX, int directionY, final String image, int width, int height ) {
( image, 0, 40, width, height );
.directionX = directionX;
.directionY = directionY;
if ( ( directionX == STOP ) && ( directionY == DOWN ) ) {
.x = (int)( Math.random() * .maxX );
}
else if ( ( directionX == RIGHT ) && ( directionY == STOP ) ) {
.y = IMGSIZE + (int)( Math.random() * ( .maxY - IMGSIZE ) );
}
}
@Override
public void move( int directionX, int directionY ) {
final int SPEED = 20;
.move( directionX * SPEED, directionY * SPEED );
}
public boolean collide( ObjectByKey that ) {
return ( Math.abs( .x - that.x ) < IMGSIZE ) && ( Math.abs( .y - that.y ) < IMGSIZE );
}
public boolean collide() {
return ( .x < minX ) || ( maxX < .x ) || ( .y < minY ) || ( maxY < .y );
}
}