문제
탑 뷰 맵에서 방향키를 이용하여 캐릭터가 상하좌우로 이동하는 문제입니다 이를 해결하는 다음 프로그램에 대해 빈칸을 채우세요
코드 빈칸 채우기
import javax.swing.*;
import move.TopViewObject;
import move.ViewPanel;
public TopViewGameMain
{
public static void main( String[] args )
{
final String imagePath = "C:\\Users\\user\\Downloads\\JAVA-main\\src\\move\\image\\";
int[][] map = { { 1,1,1,1,1,1,1 },
{ 1,0,0,1,0,0,1 },
{ 1,1,0,1,0,1,1 },
{ 1,0,0,1,0,0,1 },
{ 1,0,1,1,1,0,1 },
{ 1,0,0,0,0,0,1 },
{ 1,1,1,1,1,1,1 }
};
ViewPanel panel =
new ViewPanel(
new TopViewObject( map, 1, 1, imagePath ) );
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 javax.swing.*;
public TopViewObject ObjectByKey
{
protected int[][] map;
protected [] image;
protected final int PATH = 0, WALL = 1, CHARACTER = 2;
public TopViewObject( int[][] map, int x, int y, final String imagePath ) {
( imagePath+"character.png", x, y, 0, 0, map[0].length-1, map.length-1 );
.map = map;
.image = new [3];
.image[PATH ] = new ( imagePath + "path.png" ).();
.image[WALL ] = new ( imagePath + "wall.png" ).();
.image[CHARACTER] = new ( imagePath + "character.png" ).();
}
@Override
public void move( int directionX, int directionY ) {
if( map[y+directionY][x+directionX] != WALL )
.move( directionX, directionY );
}
@Override
public void ( Graphics g ){
for( int y = 0; y <= map.length; y++ ){
for( int x = 0; x <= map[0].length; x++ ){
int index = WALL;
if ( ( .x == x ) && ( .y == y ) )
index = CHARACTER;
else if ( ( minX <= x ) && ( x <= maxX ) && ( minY <= y ) && ( y <= maxY ) )
index = map[y][x];
g.( image[index], x*IMGSIZE, y*IMGSIZE, IMGSIZE, IMGSIZE, null );
}
}
}
@Override
public int backgroundWidth(){
return IMGSIZE * ( map[0].length );
}
@Override
public int backgroundHeight(){
return IMGSIZE * ( map.length );
}
}
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;
}
}