package shooting;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;
public class GameBoard extends Canvas implements Runnable{
private UserFighter userfighter;
private ArrayList enemies;
private ArrayList missiles;
private StatusBar status;
private Image bg;
private int key;
private Random rand = new Random();
private int timerToEnemyAppear;
private int timerToFire;
public GameBoard() // 게임보드 이미지와 객체생성
{
Toolkit tk = Toolkit.getDefaultToolkit();
bg = tk.getImage("res/22.jpg");
userfighter = new UserFighter();
status = new StatusBar();
//익명 클래스
addKeyListener(new KeyAdapter(){ //키아답터는 키이벤트 빈함수를 이미 포함하고있음
public void keyPressed(KeyEvent e)
{
int key=e.getKeyCode(); //코드값 정수형으로 반환 저장
switch(key)
{
case KeyEvent.VK_LEFT:
case KeyEvent.VK_RIGHT:
case KeyEvent.VK_UP:
case KeyEvent.VK_DOWN:
case KeyEvent.VK_SPACE:
GameBoard.this.key = key;
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
int key=e.getKeyCode();
switch(key)
{
case KeyEvent.VK_LEFT:
if(GameBoard.this.key== KeyEvent.VK_LEFT)
GameBoard.this.key = 0;
break;
case KeyEvent.VK_RIGHT:
if(GameBoard.this.key== KeyEvent.VK_RIGHT)
GameBoard.this.key = 0;
break;
case KeyEvent.VK_UP:
if(GameBoard.this.key== KeyEvent.VK_UP)
GameBoard.this.key = 0;
break;
case KeyEvent.VK_DOWN:
if(GameBoard.this.key== KeyEvent.VK_DOWN)
GameBoard.this.key = 0;
break;
case KeyEvent.VK_SPACE:
if(GameBoard.this.key== KeyEvent.VK_SPACE)
GameBoard.this.key = 0;
break;
}
}
});
enemies = new ArrayList();
missiles = new ArrayList();
new Thread(this).start(); //별도 변수명 저장안하고 실행
}
public void run()
{
while(true)
{
updateItems();
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void updateItems()
{
if(key != 0 )
userfighter.move(key);
for(int i=0; i<enemies.size(); i++) // 적기 이동
((EnemyFighter)enemies.get(i)).move();
if(timerToEnemyAppear-- == 0) //적기출현 빈도수 설정
{
enemies.add(new EnemyFighter(this));
timerToEnemyAppear = (rand.nextInt(500))+500/17;
}
//파이터 미사일 발싸
UserMissile um = userfighter.fire();
if(timerToFire>0)
timerToFire--;
if(key == KeyEvent.VK_SPACE) // 스페이스키 눌렀을때 미사일을 일정간격으로 발싸@@
{
if(timerToFire == 0)
{
missiles.add(um);
timerToFire =7;
}
}
for(int i=0; i<missiles.size(); i++) // 각 미사일 무브
((UserMissile)missiles.get(i)).move();
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g)
{
Image buf = createImage(this.getWidth(), this.getHeight());
Graphics g1 = buf.getGraphics();
g1.drawImage(bg, 0, 0, this); //배경그리기 , 더블버퍼링
userfighter.paint(g1, this);
for(int i=0; i<enemies.size(); i++) // 적기 그리기
((EnemyFighter)enemies.get(i)).paint(g1);
for(int i=0; i<missiles.size(); i++) //미사일 그리기
((UserMissile)missiles.get(i)).paint(g1);
g.drawImage(buf, 0, 0, this);
}
}
'개발 > java' 카테고리의 다른 글
자바 멀티쓰레드 충돌 이유 (0) | 2013.10.28 |
---|---|
자바 awt 윈도우창에 메뉴바 만들기 연습 (0) | 2013.10.18 |
자바 이미지 부분을 불러와 출력하기 (0) | 2013.10.10 |
자바 윈도우창에 출력해보기 (0) | 2013.10.10 |
자바 오목게임 만들기(랭킹뷰) (0) | 2013.10.08 |