mojo's Blog

명품 JAVA Programming 제 13장 Open Challenge 본문

Java

명품 JAVA Programming 제 13장 Open Challenge

_mojo_ 2021. 8. 9. 11:52

아바타와 괴물 게임 만들기 code

 

import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;

public class Game extends JFrame{
	
	private Avatar user_La;
	private Monster enemy_La;
	
	class Avatar extends JLabel{
		private int x, y;
		
		Avatar(String avatar, int x, int y){
			super(avatar);
			this.x = x;
			this.y = y;
			setSize(15, 15);
			setLocation(x, y);
			setForeground(Color.RED);
		}
		
		public int getX() { return x; }
		
		public int getY() { return y; }
		
		public void setPosition(int x, int y) {
			this.x = x;
			this.y = y;
			setLocation(x, y);
		}
	}
	
	class Monster extends JLabel implements Runnable{
		private int x, y;
		
		Monster(String monster, int x, int y){
			super(monster);
			this.x=x;
			setSize(15, 15);
			setLocation(x, y);
		}
		
		public void run() {
			while(true) {
				int userX = user_La.getX();
				int userY = user_La.getY();
				if(userX > x) {
					x += 5;
					if(userY > y) y += 5;
					else if(userY < y) y -= 5;
				}
				else if(userX < x) {
					x -= 5;
					if(userY > y) y += 5;
					else if(userY < y) y -= 5;
				}
				else {
					if(userY > y) y += 5;
					else if(userY < y) y -= 5;
				}
				setLocation(x, y);
				try {
					Thread.sleep(200);
				} 
				catch(InterruptedException e) { return; }
			}
		}
	}
	
	Game(){
		setTitle("13장 연습하기");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container c = getContentPane();
		c.setLayout(null);
		
		user_La = new Avatar("@", 100, 100);		
		enemy_La = new Monster("M", 200, 200);
		Thread th = new Thread(enemy_La);
		
		c.add(user_La);
		c.add(enemy_La);
		
		addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				int direction = e.getKeyCode();
				char op = e.getKeyChar();
				if(op == 'q') {
					System.exit(0);
				}
				
				if(direction == KeyEvent.VK_UP) {
					int userX = user_La.getX();
					int userY = user_La.getY();
					
					if(userY > 0) {
						user_La.setPosition(userX, userY - 10);
					}
				}
				else if(direction == KeyEvent.VK_DOWN) {
					int userX = user_La.getX();
					int userY = user_La.getY();
					
					if(userY < c.getHeight()) {
						user_La.setPosition(userX, userY + 10);
					}		
				}
				else if(direction == KeyEvent.VK_LEFT) {
					int userX = user_La.getX();
					int userY = user_La.getY();
					
					if(userX > 0) {
						user_La.setPosition(userX - 10, userY);
					}
				}
				else if(direction == KeyEvent.VK_RIGHT) {
					int userX = user_La.getX();
					int userY = user_La.getY();
					
					if(userX < c.getWidth()) {
						user_La.setPosition(userX + 10, userY);
					}
				}
			}
		});
		
		setSize(300, 300); 
		setVisible(true); 
		
		th.start();
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Game();
	}

}

Comments