読者です 読者をやめる 読者になる 読者になる

落書きノート

ふと自分が気になった事を書いてます

Javaアプレット シューティングゲーム作成その2

とりあえずここまで出来ました。細かいところはC言語と同じように作っています。

package shooting_game_applet_ver;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;

public class Shooting extends Applet implements Runnable, KeyListener {
    private final int SPEED = 8;
    private final int ENEMY_SPEED = 8;
    private Player player;
    private Enemy enemy;
    private Thread gameThread;
    private boolean left,right,up,down;
    private LinkedList<Enemy> enemy_task;
    private boolean enemy_wait = false;
    private int enemy_cnt = 0;
    
    public void init(){
        setFocusable(true);
        setBackground(Color.black);
        setForeground(Color.white);
        
        player = new Player();
        
        player.player_x = 100;
        player.player_y = 200;
        player.player_img = getImage(getDocumentBase(), "./shooting_game_applet_ver/player1_jiki.png");

        enemy_task = new LinkedList<Enemy>();
        
        addKeyListener(this);
        requestFocus();
    }
    
    public void start(){
        gameThread = new Thread(this);
        gameThread.start();
    }
    
    public void stop(){
        gameThread = null;
    }
    
    public void run(){
        while(gameThread == Thread.currentThread()){
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            if(left)
                player.player_x -= SPEED;
            else if(right)
                player.player_x += SPEED;
            else if(up)
                player.player_y -= SPEED;
            else if(down)
                player.player_y += SPEED;
            
            if(!enemy_wait){
                create_enemy(650, 300);
                enemy_wait = true;
            }
            
            if(enemy_cnt >= 15){
                enemy_wait = false;
                enemy_cnt = 0;
            }
            
            if(enemy_wait){
                enemy_cnt++;
            }
            
            for(Enemy e: enemy_task)
                e.enemy_x -= ENEMY_SPEED;
            
            repaint();
        }
    }
    
    public void keyPressed(KeyEvent e){
        switch(e.getKeyCode()) {
        case KeyEvent.VK_LEFT:
            left = true;
            break;
        case KeyEvent.VK_RIGHT:
            right = true;
            break;
        case KeyEvent.VK_UP:
            up = true;
            break;
        case KeyEvent.VK_DOWN:
            down = true;
            break;
        }
    }
    
    public void keyReleased(KeyEvent e){
        switch(e.getKeyCode()){
        case KeyEvent.VK_LEFT:
            left = false;
            break;
        case KeyEvent.VK_RIGHT:
            right = false;
            break;
        case KeyEvent.VK_UP:
            up = false;
            break;
        case KeyEvent.VK_DOWN:
            down = false;
            break;
        }
    }
    
    public void keyTyped(KeyEvent e){
        
    }
    
    public void paint(Graphics g){
        g.drawImage(player.player_img, player.player_x, player.player_y, this);
        
        if(!enemy_task.isEmpty()){
                for(Enemy e: enemy_task)
                    g.drawImage(e.enemy_img, e.enemy_x, e.enemy_y, this);
        }
    }
    
    public void create_enemy(int enemy_x, int enemy_y){
        enemy = new Enemy();
        enemy.enemy_x = enemy_x;
        enemy.enemy_y = enemy_y;
        enemy.enemy_img = getImage(getDocumentBase(), "./shooting_game_applet_ver/enemy1_1.png");
        enemy_task.add(enemy);
        repaint();
    }
}

package shooting_game_applet_ver;

import java.applet.Applet;
import java.awt.Image;

public class Player extends Applet {
    public int player_x;
    public int player_y;
    public Image player_img;
}

package shooting_game_applet_ver;

import java.applet.Applet;
import java.awt.Image;

public class Enemy extends Applet {
    public int enemy_x;
    public int enemy_y;
    public Image enemy_img;
}

package shooting_game_applet_ver;

import java.applet.Applet;

public class Hit extends Applet {
    public int left;
    public int right;
    public int top;
    public int bottom;
}

考えていると時間がなくなってきたので、間に合わせでC言語と同じようなプログラムの組み方をしている部分もあります。後でまたすっきりさせるように考えて修正しておきます。まだ肝心の当たり判定が作れていないので、次回以降に実装しておきます。実行結果は以下のようになります。

20160120234623