wacharoKick.GIF  wacharoStandUp.GIF  tree.GIF
  mount.GIF  river.GIF  grass.GIF
  town.GIF  castle.GIF  cave.GIF
  street.GIF  weapon.GIF  guard.GIF
  hotel.GIF  house.GIF  black.GIF
  box.GIF
上の画像を右クリックで保存して使ってください

WacharoQuest3.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class WacharoQuest3 extends Applet implements Runnable, KeyListener {

    private final int wacharoWidth=30,wacharoHeight=40;
    private final int screenX=150,screenY=200;
    private final int walk=5;
    private final int time=20;
    
    private int walkStatus;
    private int wacharoX=60,wacharoY=720;
    private int mapX=0,mapY=600,townX=60,townY=80,dungeonX=60,dungeonY=400;
    
    private boolean mapFlag=true,townFlag,dungeonFlag;
    
    private Image imgMap,imgTown,imgDungeon,wacharoImg1,wacharoImg2;
    private Image treeImg,mountImg,riverImg,grassImg,townImg,castleImg,caveImg;
    private Image streetImg,weaponImg,guardImg,hotelImg,houseImg;
    private Image blackImg,boxImg;
    
    private Graphics offGMap,offGTown,offGDungeon;
    private Thread th;
    private Data MAP,TOWN,DUNGEON;
    
    private boolean mapField[][];
    private boolean townField[][];
    private boolean dungeonField[][];
    
    private final int[][] map = {
                                {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
                                {3,3,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,0,3,3,3,3},
                                {3,3,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,3,3,3},
                                {3,6,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0,3,3},
                                {3,0,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0,3,3},
                                {3,0,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0,0,3},
                                {3,0,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0,0,3},
                                {3,0,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,3},
                                {3,0,3,3,0,0,0,0,0,0,0,0,0,0,0,0,4,4,0,0,0,0,3},
                                {3,0,0,0,0,3,3,3,3,0,0,0,0,0,0,4,4,4,4,0,0,0,3},
                                {3,3,3,3,0,3,3,3,3,0,0,2,2,2,0,4,4,4,4,4,0,0,3},
                                {3,3,3,3,3,3,3,3,0,0,0,2,1,2,0,4,4,4,4,4,0,0,3},
                                {3,3,3,3,3,3,3,0,0,0,0,2,2,2,0,4,4,4,4,0,0,0,3},
                                {3,3,3,3,3,0,0,0,0,0,0,4,4,4,4,4,4,4,0,0,0,0,3},
                                {3,3,3,3,0,0,0,0,0,0,4,4,4,4,4,4,4,0,0,0,0,0,3},
                                {3,3,3,0,0,0,0,0,0,4,4,4,4,4,4,4,4,0,0,0,0,0,3},
                                {3,3,0,0,0,0,0,0,4,4,4,4,4,4,4,4,4,4,0,0,0,3,3},
                                {3,0,0,0,0,0,0,4,4,4,4,4,4,4,4,4,4,4,0,0,0,3,3},
                                {3,0,0,0,0,0,4,4,4,4,4,0,0,0,4,4,0,0,0,0,0,3,3},
                                {3,0,0,0,0,4,4,4,4,4,4,0,5,0,4,2,0,0,0,0,3,3,3},
                                {4,4,4,4,4,4,4,4,4,4,4,0,0,0,4,4,0,0,0,0,3,3,3},
                                {4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0,0,3,3,3}
                            };
                            
    private final int[][] town = {
                                    {12,12,12,12,12,12,12,12,12},
                                    {12,10,10,10,11,10,10,10,12},
                                    {12,10,13,10,11,10,14,10,12},
                                    {12,11,11,11,11,11,11,11,12},
                                    {12,10,16,10,11,10,15,10,12},
                                    {12,10,10,10,11,10,10,10,12},
                                    {12,12,12,12,19,12,12,12,12},
                                };
                                
    private final int[][] dungeon = {
                                        {20,20,20,20,20,20,20,20,20},
                                        {20,21,21,21,20,20,20,20,20},
                                        {20,21,22,21,21,21,21,21,20},
                                        {20,21,21,21,20,20,20,21,20},
                                        {20,20,20,20,20,20,20,21,20},
                                        {20,20,20,20,20,20,20,21,20},
                                        {20,21,21,21,21,21,21,21,20},
                                        {20,21,20,20,20,20,20,20,20},
                                        {20,21,20,20,20,20,20,20,20},
                                        {20,21,21,21,21,21,21,21,20},
                                        {20,20,20,20,21,20,20,20,20},
                                        {20,20,20,20,21,20,20,20,20},
                                        {20,21,21,21,21,21,21,21,20},
                                        {20,20,20,20,21,20,20,20,20},
                                        {20,20,20,20,29,20,20,20,20},
                                    };
    
    public void init() {
        setSize(screenX,screenY);
        
        townField = new boolean[town.length][town[0].length];
        dungeonField = new boolean[dungeon.length][dungeon[0].length];
        mapField = new boolean[map.length][map[0].length];
        
        MAP = new Data(map,mapField,mapX,mapY,wacharoWidth,wacharoHeight);
        TOWN = new Data(town,townField,townX,townY,wacharoWidth,wacharoHeight);
        DUNGEON = new Data(dungeon,dungeonField,dungeonX,dungeonY,wacharoWidth,wacharoHeight);
        
        imgMap = createImage(MAP.getWidth(),MAP.getHeight());
        offGMap = imgMap.getGraphics();
        
        imgTown = createImage(TOWN.getWidth(),TOWN.getHeight());
        offGTown = imgTown.getGraphics();
        
        imgDungeon = createImage(DUNGEON.getWidth(),DUNGEON.getHeight());
        offGDungeon = imgDungeon.getGraphics();
        
        wacharoImg1 = getImage(getDocumentBase(),"wacharoKick.GIF");
        wacharoImg2 = getImage(getDocumentBase(),"wacharoStandUp.GIF");
        treeImg = getImage(getDocumentBase(),"tree.GIF");
        mountImg = getImage(getDocumentBase(),"mount.GIF");
        riverImg = getImage(getDocumentBase(),"river.GIF");
        grassImg = getImage(getDocumentBase(),"grass.GIF");
        townImg = getImage(getDocumentBase(),"town.GIF");
        castleImg = getImage(getDocumentBase(),"castle.GIF");
        caveImg = getImage(getDocumentBase(),"cave.GIF");
        streetImg = getImage(getDocumentBase(),"street.GIF");
        weaponImg = getImage(getDocumentBase(),"weapon.GIF");
        guardImg = getImage(getDocumentBase(),"guard.GIF");
        hotelImg = getImage(getDocumentBase(),"hotel.GIF");
        houseImg = getImage(getDocumentBase(),"house.GIF");
        blackImg = getImage(getDocumentBase(),"black.GIF");
        boxImg = getImage(getDocumentBase(),"box.GIF");
        
        addKeyListener(this);
    }
    
    public void start() {
        if(th == null) {
            th = new Thread(this);
            th.start();
        }
    }
    
    public void update(Graphics g) {
        paint(g);
    }
    
    public void paint(Graphics Gr) {
        if(mapFlag) {
            draw(MAP,imgMap,Gr,offGMap,map,mapField);
        }
        if(townFlag) {
            draw(TOWN,imgTown,Gr,offGTown,town,townField);
        }
        if(dungeonFlag) {
            draw(DUNGEON,imgDungeon,Gr,offGDungeon,dungeon,dungeonField);
        }
    }
    
    public void draw(Data data,Image img,Graphics Gr,Graphics offG,int[][] map,boolean[][] field) {
        Image gifImg = null;
        
        offG.clearRect(0,0,data.getWidth(),data.getHeight());
        
        for(int i=0;i<map.length;i++) {
            for(int j=0;j<map[0].length;j++) {
                switch(map[i][j]) {
                    case 0:
                        gifImg = grassImg;
                        break;
                    case 1:
                        gifImg = townImg;
                        break;
                    case 2:
                        gifImg = treeImg;
                        break;
                    case 3:
                        gifImg = mountImg;
                        field[i][j]=false;
                        break;
                    case 4:
                        gifImg = riverImg;
                        field[i][j]=false;
                        break;
                    case 5:
                        gifImg = castleImg;
                        break;
                    case 6:
                        gifImg = caveImg;
                        break;
                        
                    case 10:
                        gifImg = grassImg;
                        break;
                    case 11:
                        gifImg = streetImg;
                        break;
                    case 12:
                        gifImg = treeImg;
                        field[i][j]=false;
                        break;
                    case 13:
                        gifImg = weaponImg;
                        break;
                    case 14:
                        gifImg = guardImg;
                        break;
                    case 15:
                        gifImg = hotelImg;
                        break;
                    case 16:
                        gifImg = houseImg;
                        break;
                    case 19:
                        gifImg = streetImg;
                        break;
                        
                    case 20:
                        gifImg = blackImg;
                        field[i][j]=false;
                        break;
                    case 21:
                        gifImg = streetImg;
                        break;
                    case 22:
                        gifImg = boxImg;
                        break;
                    case 29:
                        gifImg = streetImg;
                        break;
                }
                offG.drawImage(gifImg,j*wacharoWidth,i*wacharoHeight,this);
            }
        }
        
        walk(walkStatus,data);
        
        if(System.currentTimeMillis()%2000<=1000) {
            offG.drawImage(wacharoImg1,wacharoX,wacharoY,this);
        } else {
            offG.drawImage(wacharoImg2,wacharoX,wacharoY,this);
        }
        
        Gr.drawImage(img,0,0,screenX,screenY,data.getX(),data.getY(),data.getX()+screenX,data.getY()+screenY,this);
        
        if(mapFlag==true && walkStatus==0 && map[wacharoY/wacharoHeight][wacharoX/wacharoWidth]==1) {
            townFlag=true;
            mapFlag=false;
            wacharoX=120;
            wacharoY=200;
        }
        
        if(mapFlag==true && walkStatus==0 && map[wacharoY/wacharoHeight][wacharoX/wacharoWidth]==6) {
            dungeonFlag=true;
            mapFlag=false;
            wacharoX=120;
            wacharoY=520;
        }
        
        if(townFlag==true && walkStatus==0 && map[wacharoY/wacharoHeight][wacharoX/wacharoWidth]==19) {
            townFlag=false;
            mapFlag=true;
            wacharoX=360;
            wacharoY=480;
        }
        
        if(dungeonFlag==true && walkStatus==0 && map[wacharoY/wacharoHeight][wacharoX/wacharoWidth]==29) {
            dungeonFlag=false;
            mapFlag=true;
            wacharoX=30;
            wacharoY=160;
        }
    }
    
    public void walk(int x,Data data) {
        int X = data.getX();
        int Y = data.getY();
        int width = data.getWidth();
        int height = data.getHeight();
        
        switch(x) {
            case 0:
                break;
                
            case 1:
                wacharoX += walk;
                if(X<width-screenX && wacharoX-X>=wacharoWidth*2) {
                    X += walk;
                }
                if(wacharoX%wacharoWidth==0) {
                    walkStatus = 0;
                }
                data.setX(X);
                break;
                
            case 2:
                wacharoX -= walk;
                if(X>0 && wacharoX-X<=wacharoWidth*2) {
                    X -= walk;
                }
                if(wacharoX%wacharoWidth==0) {
                    walkStatus = 0;
                }
                data.setX(X);
                break;
                
            case 3:
                wacharoY -= walk;
                if(Y>0 && wacharoY-Y<=wacharoHeight*2) {
                    Y -= walk;
                }
                if(wacharoY%wacharoHeight==0) {
                    walkStatus = 0;
                }
                data.setY(Y);
                break;
                
            case 4:
                wacharoY += walk;
                if(Y<height-screenY && wacharoY-Y>=wacharoHeight*2) {
                    Y += walk;
                }
                if(wacharoY%wacharoHeight==0) {
                    walkStatus = 0;
                }
                data.setY(Y);
        }
    }
    
    public void keyEvent(KeyEvent e,boolean[][] field,int width,int height) {
        if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
            if(wacharoX<width-wacharoWidth && field[wacharoY/wacharoHeight][wacharoX/wacharoWidth+1]==true) {
                walkStatus=1;
            }
        }
        if(e.getKeyCode()==KeyEvent.VK_LEFT) {
            if(wacharoX>0 &&  field[wacharoY/wacharoHeight][wacharoX/wacharoWidth-1]==true) {
                walkStatus=2;
            }
        }
        if(e.getKeyCode()==KeyEvent.VK_UP) {
            if(wacharoY>0 && field[wacharoY/wacharoHeight-1][wacharoX/wacharoWidth]==true) {
                walkStatus=3;
            }
        }
        if(e.getKeyCode()==KeyEvent.VK_DOWN) {
            if(wacharoY<height-wacharoHeight && field[wacharoY/wacharoHeight+1][wacharoX/wacharoWidth]==true) {
                walkStatus=4;
            }
        }
    }
    
    public void run() {
        while(th == Thread.currentThread()) {
            try {
                Thread.sleep(time);
            } catch(Exception e) {
                e.printStackTrace();
            }
            repaint();
        }
    }
    
    public void keyPressed(KeyEvent e) {
        boolean[][] field = null;
        int width=0;
        int height=0;
        
        if(walkStatus==0) {
            if(mapFlag) {
                field = mapField;
                width = MAP.getWidth();
                height = MAP.getHeight();
            }
            if(townFlag) {
                field = townField;
                width = TOWN.getWidth();
                height = TOWN.getHeight();
            }
            if(dungeonFlag) {
                field = dungeonField;
                width = DUNGEON.getWidth();
                height = DUNGEON.getHeight();
            }
            
            keyEvent(e,field,width,height);
        }
    }
    
    public void keyReleased(KeyEvent e) {
    }
    
    public void keyTyped(KeyEvent e) {
    }
}
        

Data.java
public class Data {
    private int X,Y;
    private int width,height;
    
    Data() {
    }
    
    Data(int[][] map,boolean[][] field,int startX,int startY,int wacharoWidth,int wacharoHeight) {
        X = startX;
        Y = startY;
        width = map[0].length*wacharoWidth;
        height = map.length*wacharoHeight;
        
        for(int i=0;i<map.length;i++) {
            for(int j=0;j<map[0].length;j++) {
                field[i][j] = true;
            }
        }
    }
    
    public void setX(int x) {
        X = x;
    }

    public int getX() {
        return X;
    }

    public void setY(int y) {
        Y = y;
    }

    public int getY() {
        return Y;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getWidth() {
        return width;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getHeight() {
        return height;
    }
}
        

top

2449