관리 메뉴

nkdk의 세상

자바 Language 16일째 팩맨과 사운드 본문

My Programing/JAVA

자바 Language 16일째 팩맨과 사운드

nkdk 2008. 3. 8. 01:13
자바 Language 16일째 팩맨과 사운드를 만들었습니다.
툴킷은 가상클래스이다. 이미지는 툴킷으로 가져 왔습니다.

package gui;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.applet.*;
import java.io.File;
import java.net.*;

public class PackMan extends Frame implements KeyListener{
Random aa=new Random();
Image img, img2;
int sese=9;
int sel=1, xx=120,yy=120, d=0, xxx, yyy, scores =0;

public PackMan() {
this.setLayout(null);
this.setResizable(false);
setBounds(100, 100, 300, 300);
setVisible(true);
this.addKeyListener(this);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
xx=e.getX();
yy=e.getY();
repaint();
setTitle("xx="+xx+" yy="+yy+" 좌표 이동");
}
});


}
public void paint(Graphics g) {
if (this.sese== 9) {
super.setTitle(scores+"점");
sese=aa.nextInt(8);
switch(sese) {
case 1:img2=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/pack1.jpg"); break;
case 2:img2=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/pack2.jpg"); break;
case 3:img2=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/pack3.jpg"); break;
case 4:img2=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/pack4.jpg"); break;
case 5:img2=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/pack5.jpg"); break;
case 6:img2=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/pack6.jpg"); break;
case 7:img2=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/pack7.jpg"); break;
case 8:img2=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/pack8.jpg"); break;
// case 1:img2=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/admin.jpg"); break;
// case 2:img2=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/del.gif"); break;
// case 3:img2=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/edit.gif"); break;
// case 4:img2=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/home.gif"); break;
// case 5:img2=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/list.gif"); break;
// case 6:img2=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/next.gif"); break;
// case 7:img2=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/pre.gif"); break;
// case 8:img2=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/write.gif"); break;
}
xxx = aa.nextInt(200)+50;
yyy = aa.nextInt(200)+50;
}
g.drawImage(img2, xxx, yyy, 50,50,this);

switch(sel) {
case 1:img=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/pack1.jpg"); break;
case 2:img=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/pack2.jpg"); break;
case 3:img=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/pack3.jpg"); break;
case 4:img=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/pack4.jpg"); break;
case 5:img=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/pack5.jpg"); break;
case 6:img=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/pack6.jpg"); break;
case 7:img=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/pack7.jpg"); break;
case 8:img=Toolkit.getDefaultToolkit().getImage("D:/javasou/javas/basic/gui/pic/pack8.jpg"); break;
}
g.drawImage(img, xx, yy, this);

}

public static void main(String[] args) {
new PackMan();
}

public void keyPressed(KeyEvent k) {


if((k.getModifiers() & InputEvent.ALT_MASK) !=0)
d=30; else d=10;
int key=k.getKeyCode();
if(key==KeyEvent.VK_RIGHT || key==KeyEvent.VK_KP_RIGHT || key==KeyEvent.VK_NUMPAD6) {
sel =(sel==1)?2:1;
xx+=d; if (xx>=340) xx=-20;
if(xxx <= (xx +40) && xxx >= (xx-40) && yyy <= (yy+40) && yyy >= (yy-40) ) {
try {
File file = new File("D:/javasou/javas/basic/gui/chimes.wav");
AudioClip audioClip = Applet.newAudioClip(file.toURL());
audioClip.play();
}catch(Exception exs) {}
sese = 9; scores +=10;
}
}
if(key==KeyEvent.VK_LEFT || key==KeyEvent.VK_KP_LEFT|| key==KeyEvent.VK_NUMPAD4) {
sel =(sel==3)?4:3;
xx-=d; if (xx<=-40) xx=310;
if(xxx <= (xx +40) && xxx >= (xx-40) && yyy <= (yy+40) && yyy >= (yy-40) ) {
try {
File file = new File("D:/javasou/javas/basic/gui/chimes.wav");
AudioClip audioClip = Applet.newAudioClip(file.toURL());
audioClip.play();
}catch(Exception exs) {}
sese = 9; scores +=10;
}
}
if(key==KeyEvent.VK_UP|| key==KeyEvent.VK_KP_UP || key==KeyEvent.VK_NUMPAD8) {
sel =(sel==7)?8:7;
yy-=d; if (yy<=-40) yy=310;
if(xxx <= (xx +40) && xxx >= (xx-40) && yyy <= (yy+40) && yyy >= (yy-40) ) {
try {
File file = new File("D:/javasou/javas/basic/gui/chimes.wav");
AudioClip audioClip = Applet.newAudioClip(file.toURL());
audioClip.play();
}catch(Exception exs) {}
sese = 9; scores +=10;
}
}
if(key==KeyEvent.VK_DOWN|| key==KeyEvent.VK_KP_DOWN|| key==KeyEvent.VK_NUMPAD2) {
sel =(sel==5)?6:5;
yy+=d; if(yy>=340) yy=-20;
if(xxx <= (xx +40) && xxx >= (xx-40) && yyy <= (yy+40) && yyy >= (yy-40) ) {
try {
File file = new File("D:/javasou/javas/basic/gui/chimes.wav");
AudioClip audioClip = Applet.newAudioClip(file.toURL());
audioClip.play();
}catch(Exception exs) {}
sese = 9; scores +=10;
}
}
repaint();
}

public void keyReleased(KeyEvent k) {
}

public void keyTyped(KeyEvent k) {
}
}


정도가 되겠네요. 주말도 간바뗴~ ^^