wacharoKick.GIF wacharoStandUp.GIF
  tree.GIF mount.GIF
  river.GIF grass.GIF
  town.GIF
  上の画像を右クリックで保存して使ってください

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

public class WacharoQuest2 extends Applet implements Runnable, KeyListener {

    private int width=420,height=600;
    private int wacharoWidth=30,wacharoHeight=40;
    private int walk = 5;
    private int wacharoX=180,wacharoY=240;
    private int walkStatus;
    
    private int screenX=150,screenY=200;
    private int mapX=120,mapY=160;
    
    private  Image img,wacharoImg1,wacharoImg2,treeImg,mountImg,riverImg,grassImg,townImg;
    private  Graphics offG;
    private  Thread th;
    
    private boolean field[][];
    private int[][] map = {
                                {3,3,3,3,3,3,3,3,0,0,0,0,0,0},
                                {3,3,3,3,3,3,3,0,0,0,0,0,0,0},
                                {3,3,3,3,3,3,0,0,0,0,0,0,0,0},
                                {3,3,3,3,3,0,0,0,0,0,0,0,0,0},
                                {3,3,3,3,0,0,0,0,0,0,0,2,2,2},
                                {3,3,3,0,0,0,0,0,0,0,0,2,1,2},
                                {3,3,0,0,0,0,0,0,0,0,0,2,2,2},
                                {0,0,0,0,0,0,0,0,0,0,0,4,4,4},
                                {0,0,0,0,0,0,0,0,0,0,4,4,4,4},
                                {0,0,0,0,0,0,0,0,0,4,4,4,4,4},
                                {0,0,0,0,0,0,0,0,4,4,4,4,4,4},
                                {0,0,0,0,0,0,0,4,4,4,4,4,4,4},
                                {0,0,0,0,0,0,4,4,4,4,4,4,4,4},
                                {0,0,0,0,0,4,4,4,4,4,4,4,4,4},
                                {0,0,0,0,4,4,4,4,4,4,4,4,4,4}
                            };
    
    public void init() {
        setSize(screenX,screenY);
        img = createImage(width,height);
        offG = img.getGraphics();
        field = new boolean[map.length][map[0].length];//[15][14]
        for(int i=0;i<map.length;i++) {
            for(int j=0;j<map[0].length;j++) {
                field[i][j] = true;
            }
        }
        
        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");
        
        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) {
        offG.clearRect(0,0,width,height);
        
        for(int i=0;i<map.length;i++) {
            for(int j=0;j<map[0].length;j++) {
                switch(map[i][j]) {
                    case 0:
                        offG.drawImage(grassImg,j*wacharoWidth,i*wacharoHeight,this);
                        break;
                    case 1:
                        offG.drawImage(houseImg,j*wacharoWidth,i*wacharoHeight,this);
                        break;
                    case 2:
                        offG.drawImage(treeImg,j*wacharoWidth,i*wacharoHeight,this);
                        break;
                    case 3:
                        offG.drawImage(mountImg,j*wacharoWidth,i*wacharoHeight,this);
                        field[i][j]=false;
                        break;
                    case 4:
                        offG.drawImage(riverImg,j*wacharoWidth,i*wacharoHeight,this);
                        field[i][j]=false;
                        break;
                }
            }
        }

        walk(walkStatus);
        
        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,mapX,mapY,mapX+screenX,mapY+screenY,this);
    }
    
    public void walk(int x) {
        switch(x) {
            case 0:
                break;
                
            case 1:
                wacharoX += walk;
                if(mapX<width-screenX && wacharoX-mapX>=wacharoWidth*2) {
                    mapX += walk;
                }
                if(wacharoX%wacharoWidth==0) {
                    walkStatus = 0;
                }
                break;
                
            case 2:
                wacharoX -= walk;
                if(mapX>0 && wacharoX-mapX<=wacharoWidth*2) {
                    mapX -= walk;
                }
                if(wacharoX%wacharoWidth==0) {
                    walkStatus = 0;
                }
                break;
                
            case 3:
                wacharoY -= walk;
                if(mapY>0 && wacharoY-mapY<=wacharoHeight*2) {
                    mapY -= walk;
                }
                if(wacharoY%wacharoHeight==0) {
                    walkStatus = 0;
                }
                break;
                
            case 4:
                wacharoY += walk;
                if(mapY<height-screenY && wacharoY-mapY>=wacharoHeight*2) {
                    mapY += walk;
                }
                if(wacharoY%wacharoHeight==0) {
                    walkStatus = 0;
                }
        }
    }
    
    public void run() {
        while(th == Thread.currentThread()) {
            try {
                Thread.sleep(50);
            } catch(Exception e) {
                e.printStackTrace();
            }
            repaint();
        }
    }
    
    public void keyPressed(KeyEvent e) {
        if(walkStatus==0) {
            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 keyReleased(KeyEvent e) {
    }
    
    public void keyTyped(KeyEvent e) {
    }
}
        

top

2035