JAVA 프로그래밍

문제

사이드 뷰 맵에서 방향키를 이용하여 캐릭터가 점프하거나 이동하는 문제입니다 실행순서를 클릭하세요 
 

프로그램 코드

E001	// 이벤트 대기중
E002	// 'e' 키 입력
E003	// 1초마다 타이머 활성화

	import javax.swing.*;
	import move.SideViewObject;
	import move.ViewPanel;

	public class SideViewGameMain
	{
1		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,1,1,1,1,1 },
			                { 1,0,0,0,0,0,0,0,0,0,0,0,1 },
			                { 1,0,0,0,1,1,1,0,0,0,0,0,1 },
			                { 1,0,0,0,0,0,0,0,0,0,0,0,1 },
			                { 1,0,0,0,0,0,1,1,1,1,0,0,1 },
			                { 1,0,0,0,0,0,0,0,0,0,0,0,1 },
			                { 1,0,0,1,1,1,1,0,0,0,1,0,1 },
			                { 1,0,0,0,0,0,0,0,0,0,0,0,1 },
			                { 1,1,1,1,1,1,1,1,1,1,1,1,1 }
			              };
			
2			ViewPanel panel =
3			                  new ViewPanel(
4			                                 new SideViewObject( map, 4, 5, imagePath ) );
			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.awt.event.*;

	public class SideViewObject extends TopViewObject
	{
		private final int SCOPE = 3;
		
S1b		public SideViewObject( int[][] map, int x, int y, String imagePath ) {
S11			super( map, x, y, imagePath );
S1e		}

		@Override
S2b		public void keyPressed( KeyEvent event ) {
S21			super.keyPressed( event );
			switch( event.getKeyCode() ) {
			case 'Q': case 'q':
				jump( LEFT );
				break;
			case 'E': case 'e':
S22				jump( RIGHT );
				break;
			}
S2e		}
		 
S3b		public void jump( int directionX ) {
			final int JUMP_HEIGHT = 2;
			for( int i = 1; i <= JUMP_HEIGHT; i++ ) {
				if( map[y+i*UP][x] == WALL )
					return;
			}
S31			move( directionX, UP*JUMP_HEIGHT );
S3e		}
		
		@Override
S4b		public void move( int directionX, int directionY ) {
			for(; ( map[y+directionY][x+directionX] != WALL ) && ( map[y+directionY+1][x+directionX] != WALL ); directionY++ );
S41			super.move( directionX, directionY );
S4e		}

		@Override
S5b		public void paint( Graphics g ){
			for( int y = this.y - SCOPE, y2 = 0; y <= this.y + SCOPE; y++, y2++ ){
				for( int x = this.x - SCOPE, x2 = 0; x <= this.x + SCOPE; x++, x2++ ){
					int index = WALL;
					if ( ( this.x == x ) && ( this.y == y ) )
						index = CHARACTER;
					else if ( ( minX <= x ) && ( x <= maxX ) && ( minY <= y ) && ( y <= maxY ) )
						index = map[y][x];
					g.drawImage( image[index], x2*IMGSIZE, y2*IMGSIZE, IMGSIZE, IMGSIZE, null );
				}
			}
S5e		}
		
		@Override
		public int backgroundWidth(){
			return IMGSIZE * ( 2*SCOPE + 1 );
		}
		@Override
		public int backgroundHeight(){
			return IMGSIZE * ( 2*SCOPE + 1 );
		}
	}

	package move;

	import java.awt.*;
	import javax.swing.*;

	public class TopViewObject extends ObjectByKey
	{
		protected int[][] map;
		protected Image[] image;
		protected final int PATH = 0, WALL = 1, CHARACTER = 2;
		
T1b		public TopViewObject( int[][] map, int x, int y, final String imagePath ) {
T11			super( imagePath+"character.png", x, y, 0, 0, map[0].length-1, map.length-1 );
			this.map = map;
			this.image = new Image[3];
			this.image[PATH     ] = new ImageIcon( imagePath + "path.png" ).getImage();
			this.image[WALL     ] = new ImageIcon( imagePath + "wall.png" ).getImage();
			this.image[CHARACTER] = new ImageIcon( imagePath + "character.png" ).getImage();
T1e		}
		
		@Override
T2b		public void move( int directionX, int directionY ) {
			if( map[y+directionY][x+directionX] != WALL )
T21				super.move( directionX, directionY );
T2e		}

		@Override
T3b		public void paint( Graphics g ){
			for( int y = 0; y <= map.length; y++ ){
				for( int x = 0; x <= map[0].length; x++ ){
					int index = WALL;
					if ( ( this.x == x ) && ( this.y == y ) )
						index = CHARACTER;
					else if ( ( minX <= x ) && ( x <= maxX ) && ( minY <= y ) && ( y <= maxY ) )
						index = map[y][x];
					g.drawImage( image[index], x*IMGSIZE, y*IMGSIZE, IMGSIZE, IMGSIZE, null );
				}
			}
T3e		}
		
		@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 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;
		}
	}








 
실행 순서