《仙剑奇侠传柔情版》Java的简单实现(二)

基于《仙剑奇侠传柔情版》Java的简单实现(二)

2018-12-02 by Kris


 

需要上次的GameFrame.class中窗口框架承载:https://www.cnblogs.com/louis6575/p/10051873.html

package firstDemo;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 * 本类文件表示游戏案例的自定义画板类,也就是游戏的界面
 * @author Louis
 *
 */
    //2 游戏的静态界面
    //2.1 编写自定义画板类,添加绘画方法
    //JAVA中,遵循单继承(类)、多实现(接口)
@SuppressWarnings("serial")
public class GamePanel extends JPanel implements Runnable,KeyListener{
//声明图片路径变量名称
    //2.2.1.定义图片素材的相关内容    
    Image startImage,mengjImage,resumeImage,findxyImage,mengjToljcImage;
    Image[] startButtonImages,resumeButtonImages,lxytImages,lightImages;
    Image[] ToScDoorImages,ToLjcDoorImages,mengjDoorImages,scMapImages;
    Image[] splgifImages,welcomeImages;
        
    Image ljcImage,lxyImage;//用来表示李逍遥当前图片(移动时,需要更新)
    Image dialogImage;
    Image[] lxyUpImages,lxyDownImages,lxyLeftImages,lxyRightImages;
    BufferedImage splImage;
    Image[] awsImages,henImages,ropeImages,azhuImages,chickImages,lchickImages,wcsImages;
    
    private int start_DIST = 120,resume_DIST = 109,limUP = 240,limDOWN = 566,limRIGHT = 250,limLEFT = 10;
    private int LXYX,LXYY,LXYDIR;
    boolean ISLJC,ISSC;
    int ToScDoorX,ToLjcDoorX,mengjDoorX,scMapX,startX,startButtonX,welcomeX,mengjX,lightX,resumeX,resumeButtonX,findxyX;
    int ToScDoorY,ToLjcDoorY,mengjDoorY,scMapY,startY,startButtonY,welcomeY,mengjY,lightY,resumeY,resumeButtonY,findxyY;
    
    int ToScDoorIndex,ToLjcDoorIndex,mengjDoorIndex,scMapIndex,startButtonIndex,resumeButtonIndex,lxytIndex,lightIndex,welcomeIndex;
    
    boolean isStartShow,isstartButtonShow,isResumeShow,isResumeButtonShow,isLxyShow,isLjcShow,isScMapShow;
    boolean isSave,isWelcome,isMengjShow,isLxytShow,isSBNH;
    
    int lxyDir;
    int ljcX,lxyX,lxytX,awsX,henX,ropeX,azhuX,chickX,lchickX,wcsX,dialogX,ChatX,splX;
    int ljcY,lxyY,lxytY,awsY,henY,ropeY,azhuY,chickY,lchickY,wcsY,dialogY,ChatY,splY;
    int awsIndex,henIndex,ropeIndex,azhuIndex,chickIndex,lchickIndex,wcsIndex,lxyIndex,splgifImagesIndex;    //表示阿旺婶当前下标
    String[] ChatMessages1,ChatMessages2,ChatMessages3;
    int ChatIndex1,ChatIndex2,ChatIndex3;
    boolean isChatShow1,isChatShow2,isChatShow3,isSpeedShift;//用来表示聊天内容是否该出现的当前状态值
    BufferedImage ljcRedMap;//李家村红色地图
    BufferedImage scRedMap;//市场红色地图
    private int ClothCode = 0;
    private int UpEdge = 0;    
    private int LeftEdge = 0;
    private int MoveSpeed;    
    

    //构造方法,一般用来做初始化操作    
    
    public GamePanel(){        //获取路径并初始化绘画位置
        ToScDoorImages = new Image[6];ToLjcDoorImages = new Image[6];mengjDoorImages = new Image[6];welcomeImages = new Image[3];
        scMapImages = new Image[3];lxytImages = new Image[2];lightImages = new Image[2];
        startButtonImages = new Image[3];resumeButtonImages = new Image[4];splgifImages = new Image[13];
        
        splgifImages = new Image[13];
        
        awsImages = new Image[17];henImages = new Image[6];ropeImages = new Image[4];azhuImages = new Image[6];
        chickImages = new Image[2];lchickImages = new Image[2];wcsImages = new Image[14];
        lxyUpImages = new Image[8];lxyDownImages = new Image[8];lxyLeftImages = new Image[8];lxyRightImages = new Image[8];
        //初始化数组内容
        isstartButtonShow = true;
        isResumeButtonShow = true;
        isStartShow = true;
        isSBNH = true;
        isMengjShow = false;
        isLxytShow = true;
        isResumeShow = false;
        isWelcome = true;
        isLxyShow = false;
        isChatShow1 = false;isChatShow2 = false;isChatShow3 = false;
        isSpeedShift = false;        
        isScMapShow = false;
        isLjcShow = false;
        
        //李家村背景音乐
                File file = new File("仙剑奇侠传游戏素材/素材/music/0.mid");
                URL url = null;
                try {
                    URI uri = file.toURI();
                    url = uri.toURL();
                }
                catch (MalformedURLException ex) {
                }
                AudioClip clip = Applet.newAudioClip(url);
                clip.loop();
                
        //也就是将阿旺婶的第i张图片内容读取保存到阿旺婶的数组第i下标位置上        
        try {
            //两种路径:绝对路径(以盘符或根目录(/)开始的路径)、相对路径(在java代码中,)    
            startImage = ImageIO.read(new File("仙剑奇侠传游戏素材/开始游戏/start0.png"));            
            mengjImage = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/梦境/梦境.png"));
            mengjToljcImage = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/梦境过渡李家村/0.png"));
            resumeImage = ImageIO.read(new File("仙剑奇侠传游戏素材/返回游戏/resume.png"));
            ljcImage = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李家村/0.png"));            
            dialogImage = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/对话框/1.png"));
            //splImage = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/杀破狼静/0.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        for(int i = 0;i< welcomeImages.length;i++){
            
            try {
                welcomeImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/欢迎界面/"+i+".png"));
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
        for(int i = 0;i < lxytImages.length;i++){
            try {
                lxytImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李逍遥躺下/"+i+".png"));
                lightImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/梦境/"+i+".png"));
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
        for(int i = 0;i < startButtonImages.length;i++){
            try {
                startButtonImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/开始游戏/"+i+".png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        for(int i = 0;i < resumeButtonImages.length;i++){
            try {
                resumeButtonImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/返回游戏/"+i+".png"));
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
        for(int i = 0;i < scMapImages.length;i++){
            try {
                scMapImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李家村市场/"+i+".png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }        
        for(int i = 0;i < awsImages.length;i++){
            try {
                awsImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/阿旺婶/"+i+".png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        for(int i = 0;i < henImages.length;i++){
            try {
                henImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/母鸡/"+i+".png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        for(int i = 0;i < ropeImages.length;i++){
            try {
                ropeImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/跳绳/"+i+".png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        for(int i = 0;i < azhuImages.length;i++){
            try {
                azhuImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/阿朱喂鸡/"+i+".png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        for(int i = 0;i < chickImages.length;i++){
            try {
                chickImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/小鸡/"+i+".png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        for(int i = 0;i < lchickImages.length;i++){
            try {
                lchickImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/小小鸡/"+i+".png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        for(int i = 0;i < wcsImages.length;i++){
            try {
                wcsImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/旺财嫂/"+i+".png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        for(int i = 0;i < lxyDownImages.length;i++){
            try{    //0:李逍遥初始装扮  1:绿色
                if(ClothCode == 0){
                    lxyUpImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李逍遥初始装扮/李逍遥上/"+i+".png"));
                    lxyDownImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李逍遥初始装扮/李逍遥下/"+i+".png"));
                    lxyLeftImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李逍遥初始装扮/李逍遥左/"+i+".png"));
                    lxyRightImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李逍遥初始装扮/李逍遥右/"+i+".png"));
                }else if(ClothCode == 1){
                    lxyUpImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李逍遥绿色套装/李逍遥上/"+i+".png"));
                    lxyDownImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李逍遥绿色套装/李逍遥下/"+i+".png"));
                    lxyLeftImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李逍遥绿色套装/李逍遥左/"+i+".png"));
                    lxyRightImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李逍遥绿色套装/李逍遥右/"+i+".png"));
                }                                    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        for(int i = 0;i < ToScDoorImages.length;i++){
            try {
                ToScDoorImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/华丽传送阵/"+i+".png"));    
                ToLjcDoorImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/华丽传送阵/"+i+".png"));    
                mengjDoorImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/华丽传送阵/"+i+".png"));    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }        
        
        
        for (int i = 0;i < splgifImages.length;i++){
            try {
                splgifImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/杀破狼/"+i+".png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //初始化坐标    
        startX = 0;startY = 0;
        resumeX = -8;resumeY = -40;
        
        startButtonX = 250;startButtonY = 125;
        resumeButtonX = 348;resumeButtonY = 240;
        welcomeX = 115;welcomeY = 145;
        lxytX = 450;lxytY = 450;
        lightX = 400;lightY = 40;
        
        findxyX = 0;findxyY = 0;
        splX = 40;splY = 400;
        ljcX = -200;ljcY = -200;
        lxyX = 175;lxyY = 720;
        awsX = 612;awsY=520;
        henX = 740;henY = 320;ropeX = 900;ropeY = 680;azhuX = 770;azhuY = 350;
        chickX=820;chickY = 350;lchickX=805;lchickY = 340;wcsX = 800;wcsY = 600;
        
        scMapX = -200 - scMapX;ToScDoorX = 1600;ToLjcDoorX = 15;mengjDoorX = 750;
        scMapY = -200 - scMapY;ToScDoorY = 990;ToLjcDoorY = 620;mengjDoorY = 500;
        
        lxyIndex = 0;
        lxyImage = lxyDownImages[lxyIndex];    

        ChatMessages1 = new String[] { "李逍遥:阿旺婶,最近村子里有发生了什么有趣的事情吗?","阿旺嫂:是逍遥啊,也没什么,就是从别处来了个富贵人家的公子哥",
                "李逍遥:这样的啊,那公子长什么样","阿旺嫂:就是衣服上都好似镶了金,还随身带着精美的武器",
                "李逍遥:武器?  是这样的武器 剑吗?","阿旺嫂:我也不是很清楚啊,你可以去问问旺财嫂!"}; 
        ChatMessages2 = new String[] { "阿朱:逍遥,你来的正好,我正要去找你呢","李逍遥:阿朱姐姐,找我什么事啊","阿朱:村里来了个人,他其实是蜀山的长老",
                "李逍遥:蜀山是什么地方啊,很厉害吗","阿朱:你从小就在村子里,自然不知道,蜀山可是一个很大的门派呢","李逍遥:门派,是不是可以教人功法,然后可以让人行侠仗义啊",
                "阿朱:嗯,他是来收徒的,你可以去码头看看","李逍遥:嗯,我这就去!"};

        ChatMessages3 = new String[] { "李逍遥:旺财嫂子,听说村里来了个外人","旺财嫂:对啊,那人长得真的俊啊!","李逍遥:听说他还带着很精美的武器啊","旺财嫂:对,好像是猎户用的弓,还会放光呢!","李逍遥:那他到底是来干什么的啊","阿旺嫂:这个你到时可以问问阿朱妹子,他们聊了好久","李逍遥:好吧,谢谢嫂子"};

        
        //2.2.2.初始化图片素材的相关内容
        try{            
            ljcRedMap = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李家村/RedMap.png"));            
        }catch (IOException e) {
            e.printStackTrace();
        }
        try {
            scRedMap = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李家村市场/RedMap.png"));
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        
    }
    
    //绘画方法
    public void paint(Graphics g){    //绘画
        super.paint(g);    //清空原有画面,重新画dialogImage    
                    
        for(int i = 0;i < lxyDownImages.length;i++){
            try{    //0:李逍遥初始装扮  1:绿色
                if(ClothCode == 0){
                    lxyUpImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李逍遥初始装扮/李逍遥上/"+i+".png"));
                    lxyDownImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李逍遥初始装扮/李逍遥下/"+i+".png"));
                    lxyLeftImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李逍遥初始装扮/李逍遥左/"+i+".png"));
                    lxyRightImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李逍遥初始装扮/李逍遥右/"+i+".png"));
                }else if(ClothCode == 1){
                    lxyUpImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李逍遥绿色套装/李逍遥上/"+i+".png"));
                    lxyDownImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李逍遥绿色套装/李逍遥下/"+i+".png"));
                    lxyLeftImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李逍遥绿色套装/李逍遥左/"+i+".png"));
                    lxyRightImages[i] = ImageIO.read(new File("仙剑奇侠传游戏素材/素材/李逍遥绿色套装/李逍遥右/"+i+".png"));
                }                                    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            findxyImage = ImageIO.read(new File("仙剑奇侠传游戏素材/定位.png"));
        } catch (IOException e1) {
            // TODO 自动生成的 catch 块
            e1.printStackTrace();
        }
        
        if(isLjcShow){            
            ljcX = (this.getWidth() - lxyImage.getWidth(null))/2-lxyX;
            ljcY = (this.getHeight() - lxyImage.getHeight(null))/2-lxyY;
            
            if(ljcY >= UpEdge){
                ljcY = UpEdge;
            }
            if(ljcY <= this.getHeight()-ljcImage.getHeight(null)){
                ljcY = this.getHeight()-ljcImage.getHeight(null);
            }
            if(ljcX >= LeftEdge){
                ljcX = LeftEdge;
            }if(ljcX <= this.getWidth()-ljcImage.getWidth(null)){
                ljcX = this.getWidth()-ljcImage.getWidth(null);
            }
            
            g.drawImage(ljcImage,ljcX,ljcY,this);//g 可以理解成一只画笔    坐标原点是窗口或画板左上角
            //g.drawImage(lxyRightImage,lxyX,lxyY,this);    //第一次的静态图
            g.drawImage(awsImages[awsIndex],awsX + ljcX,awsY + ljcY,this);
            g.drawImage(henImages[henIndex], henX + ljcX,henY + ljcY,this);
            g.drawImage(ropeImages[ropeIndex],ropeX + ljcX,ropeY + ljcY,this);
            g.drawImage(azhuImages[azhuIndex],azhuX + ljcX,azhuY + ljcY,this);
            g.drawImage(chickImages[chickIndex],chickX + ljcX,chickY + ljcY,this);
            g.drawImage(lchickImages[lchickIndex],lchickX + ljcX,lchickY + ljcY,this);
            g.drawImage(wcsImages[wcsIndex],wcsX + ljcX,wcsY + ljcY,this);
            g.drawImage(ToScDoorImages[ToScDoorIndex],ToScDoorX + ljcX,ToScDoorY + ljcY,this);
            dialogX = (this.getWidth() - dialogImage.getWidth(null))/2;
            dialogY = this.getHeight() - dialogImage.getHeight(null);    
            g.drawImage(lxyImage,lxyX + ljcX,lxyY + ljcY,this);    //主角最后画    
            if(isSBNH){    //十八年后
                g.drawImage(mengjToljcImage,0,0,this);
            }
            if(isChatShow1){            
                g.drawImage(dialogImage,dialogX,dialogY,this);
                g.setColor(Color.WHITE);
                g.setFont(new Font("楷体",Font.BOLD,30));
                g.drawString(ChatMessages1[ChatIndex1], dialogX + 80, dialogY + 200);
                //g.drawImage(lxyUpImage[lxyIndex],lxyX,lxyY,this);
                //2.2.绘画静态界面(1个场景+7个配角+1个主角)
                //2.2.将素材文件夹导入工程
                //2.2.3.绘画图片素材的相关内容    
                
            }
            else if(isChatShow2){            
                g.drawImage(dialogImage,dialogX,dialogY,this);
                g.setColor(Color.WHITE);
                g.setFont(new Font("楷体",Font.BOLD,30));
                g.drawString(ChatMessages2[ChatIndex2], dialogX + 80, dialogY + 200);
            }
            else if(isChatShow3){            
                g.drawImage(dialogImage,dialogX,dialogY,this);
                g.setColor(Color.WHITE);
                g.setFont(new Font("楷体",Font.BOLD,30));
                g.drawString(ChatMessages3[ChatIndex3], dialogX + 80, dialogY + 200);
            }
            
        }
        //System.out.println(LXYDIR+","+LXYX+","+LXYY+isSave);
        
        //传送门
        if(isMengjShow && Math.abs(lxyX + lxyImage.getWidth(null)/2 - (mengjDoorX + mengjDoorImages[0].getWidth(null)/2)) < 70
                && Math.abs(lxyY + lxyImage.getHeight(null) - (mengjDoorY + mengjDoorImages[0].getHeight(null)/2)) < 70){    //梦境传送至李家村
            isMengjShow = !isMengjShow;
            lxyX = 200;lxyY = 690;
            ClothCode = 0;            
            //3.2.3.休息停顿操作
            
            try {
                Thread.sleep(80);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else if(isLjcShow == true && Math.abs(lxyX + lxyImage.getWidth(null)/2 - (ToScDoorX + ToScDoorImages[0].getWidth(null)/2)) < 70
                && Math.abs(lxyY + lxyImage.getHeight(null) - (ToScDoorY + ToScDoorImages[0].getHeight(null)/2)) < 70){  //李家村传送至市场
            isLjcShow = !isLjcShow;
            isScMapShow = !isScMapShow;    
            lxyX = 200;lxyY = 645;    
            //3.2.3.休息停顿操作
            try {
                Thread.sleep(80);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else if(isScMapShow == true && Math.abs(lxyX +lxyImage.getWidth(null)/2 - (ToLjcDoorX + ToLjcDoorImages[0].getWidth(null)/2)) < 70
                && Math.abs(lxyY + lxyImage.getHeight(null) - (ToLjcDoorY + ToLjcDoorImages[0].getHeight(null)/2)) < 70){ //市场传送至李家村
            isLjcShow = !isLjcShow;
            isScMapShow = !isScMapShow;    
            lxyX = 1580;lxyY = 990;    
            //3.2.3.休息停顿操作
            try {
                Thread.sleep(80);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }        
        //System.out.println(lxyX+","+lxyY);         //找坐标
        
        //市场边界限制
        if(isScMapShow){
            scMapX = (this.getWidth() - lxyImage.getWidth(null))/2-lxyX;
            scMapY = (this.getHeight() - lxyImage.getHeight(null))/2-lxyY;
            
            if(scMapY >= UpEdge){
                scMapY = UpEdge;
            }
            if(scMapY <= this.getHeight()-scMapImages[0].getHeight(null)){
                scMapY = this.getHeight()-scMapImages[0].getHeight(null);
            }
            if(scMapX >= LeftEdge){
                scMapX = LeftEdge;
            }if(scMapX <= this.getWidth()-scMapImages[0].getWidth(null)){
                scMapX = this.getWidth()-scMapImages[0].getWidth(null);
            }
            //System.out.println(isResumeShow+","+isLjcShow+","+isScMapShow);
            g.drawImage(scMapImages[scMapIndex],scMapX,scMapY,this);
            g.drawImage(ToLjcDoorImages[ToLjcDoorIndex],ToLjcDoorX + scMapX,ToLjcDoorY + scMapY,this);        
            g.drawImage(splgifImages[splgifImagesIndex],splX +200+ scMapX,splY + 20+scMapY,this);            
            g.drawImage(lxyImage,lxyX + scMapX,lxyY + scMapY,this);    //主角最后画                
        }        
        if(isMengjShow){
            g.drawImage(mengjImage,0,0,this);            
            g.drawImage(lxytImages[lxytIndex],lxytX,lxytY,this);
            g.drawImage(lightImages[lightIndex],lightX,lightY,this);
            g.drawImage(mengjDoorImages[mengjDoorIndex],mengjDoorX,mengjDoorY,this);            
            g.drawImage(lxyImage,lxyX,lxyY,this);                
        }
        if(isWelcome){
            welcomeY = this.getHeight() - welcomeImages[welcomeIndex].getHeight(null);
            welcomeX = (this.getWidth() - welcomeImages[welcomeIndex].getWidth(null))/2;
            
            g.drawImage(welcomeImages[welcomeIndex],welcomeX,welcomeY,this);        
        }        
        if(isStartShow){
            g.drawImage(startImage,startX,startY,this);            
            if(isstartButtonShow){
                g.drawImage(startButtonImages[startButtonIndex],startButtonX,startButtonY,this);
            }
        }
        if(isResumeShow){
            //super.paint(g);
            g.drawImage(resumeImage,resumeX,resumeY,this);
            if(isResumeButtonShow){                
                g.drawImage(resumeButtonImages[resumeButtonIndex],resumeButtonX,resumeButtonY,this);                
            }
        }
        //g.drawImage(splImage,splX + ljcX,splY + ljcY,this);    
        
        //g.drawImage(findxyImage, findxyX, findxyY,this);
        //System.out.println(findxyX+","+findxyY);         //找坐标
    }

    //@Overridee
    public void run() {
        // 3.2.实现配角运动
        while(true){
            //3.2.1.修改图片数组下标
            awsIndex++;henIndex++;ropeIndex++;azhuIndex++;chickIndex++;lchickIndex++;wcsIndex++;
            scMapIndex++;ToScDoorIndex++;ToLjcDoorIndex++;lxytIndex++;lightIndex++;
            splgifImagesIndex++;mengjDoorIndex++;
            
            if(splgifImagesIndex > splgifImages.length - 1){
                splgifImagesIndex = 0;
            }
            if(lxytIndex > lxytImages.length - 1){
                lxytIndex = 0;
            }
            if(lightIndex > lightImages.length - 1){
                lightIndex = 0;
            }
            if(ToLjcDoorIndex > ToLjcDoorImages.length-1){
                ToLjcDoorIndex = 0;
            }
            if(mengjDoorIndex > mengjDoorImages.length - 1){
                mengjDoorIndex = 0;
            }
            if(ToScDoorIndex > ToScDoorImages.length-1){
                ToScDoorIndex = 0;                
            }
            if(scMapIndex > scMapImages.length-1){
                scMapIndex = 0;                
            }
            if(awsIndex > awsImages.length-1){
                awsIndex = 0;//补充:判断处理下标边界问题
            }
            if(henIndex > henImages.length-1){
                henIndex = 0;//补充:判断处理下标边界问题
            }
            if(ropeIndex > ropeImages.length-1){
                ropeIndex = 0;//补充:判断处理下标边界问题
            }
            if(azhuIndex > azhuImages.length-1){
                azhuIndex = 0;//补充:判断处理下标边界问题
            }
            if(chickIndex > chickImages.length-1){
                chickIndex = 0;//补充:判断处理下标边界问题
            }
            if(lchickIndex > lchickImages.length-1){
                lchickIndex = 0;//补充:判断处理下标边界问题
            }
            if(wcsIndex > wcsImages.length-1){
                wcsIndex = 0;//补充:判断处理下标边界问题
            }            
            //3.2.3.休息停顿操作
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //3.2.2.刷新界面操作
            repaint();    //重新调用执行paint();
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
        
        
    }

    @Override
    public void keyPressed(KeyEvent e) {
        
        //System.out.println("您按下了按键");
        
        //对话框切换
        if(isChatShow1){
            //聊天内容切换+聊天内容消失
            int KeyCode = e.getKeyCode();
            if(KeyCode == KeyEvent.VK_SPACE){
                ChatIndex1++;
                if(ChatIndex1 > ChatMessages1.length - 1){
                    ChatIndex1 = 0;
                    isChatShow1 = !isChatShow1;
                }
                repaint();
            }
            
        }
        else if(isChatShow2){
            //聊天内容切换+聊天内容消失
            int KeyCode = e.getKeyCode();
            if(KeyCode == KeyEvent.VK_SPACE){
                ChatIndex2++;
                if(ChatIndex2> ChatMessages2.length - 1){
                    ChatIndex2 = 0;
                    isChatShow2 = !isChatShow2;
                }
                repaint();
            }
        }
        else if(isChatShow3){
            //聊天内容切换+聊天内容消失
            int KeyCode = e.getKeyCode();
            if(KeyCode == KeyEvent.VK_SPACE){
                ChatIndex3++;
                if(ChatIndex3> ChatMessages3.length - 1){
                    ChatIndex3 = 0;
                    isChatShow3 = !isChatShow3;
                }
                repaint();
            }
        }
        else{
            //
            //4.2.实现主角运动  也即是需要根据用户按下按键的情况,进行对应的操作(移动或不动)
            //4.2.1.获得用户按下的按键情况
            int KeyCode = e.getKeyCode();
            
            //加速主角移动速度
            if(KeyCode == KeyEvent.VK_SHIFT){
                isSpeedShift = !isSpeedShift;
                MoveSpeed *= 3;
                if(MoveSpeed >= 15){
                    MoveSpeed = 15;
                }
            }else if(!isSpeedShift){
                MoveSpeed = 5;    
            }repaint();            
            
            //返回游戏界面显示,下面是所有用到F4的功能的实现
            if((isLxyShow && KeyCode == KeyEvent.VK_F4) || (!isStartShow && resumeButtonIndex == 0 && KeyCode == KeyEvent.VK_ENTER)){
                isResumeShow = !isResumeShow;
            }else if(isResumeShow && resumeButtonIndex == 1 && KeyCode == KeyEvent.VK_ENTER){
                LXYX = lxyX;LXYY = lxyY;ISLJC = isLjcShow;ISSC = isScMapShow;LXYDIR = lxyDir;
                isResumeShow = !isResumeShow;
                System.out.println(LXYDIR+","+LXYX+","+LXYY+" "+ISLJC+" "+ISSC);
            }else if(isResumeShow && resumeButtonIndex == 2 && KeyCode == KeyEvent.VK_ENTER){
                lxyX = LXYX;lxyY = LXYY;lxyDir = LXYDIR;isLjcShow = ISLJC;isScMapShow = ISSC;
                isResumeShow = !isResumeShow;isSave = true;
            }else if(isResumeShow && resumeButtonIndex == 3 && KeyCode == KeyEvent.VK_ENTER){
                System.exit(0);
            }repaint();
            
            //开始游戏,按钮切换场景,下面时所有用到ENTER的功能实现
            
            if(!isLxyShow && startButtonIndex == 0 && KeyCode == KeyEvent.VK_ENTER){
                //未进入游戏主角不可移动,//进入新游戏
                    isStartShow = !isStartShow;
                    isLjcShow = !isLjcShow;    //startButtonIndex == 0时,显示李家村地图
                    isLxyShow = !isLxyShow;    
                    isWelcome = !isWelcome;
                    isMengjShow = !isMengjShow;                    
                    lxyX = lxytX ;lxyY = lxytY - 100;
                    ClothCode = 1;
            }else if(isSave && isStartShow && startButtonIndex == 1 && KeyCode == KeyEvent.VK_ENTER){
                lxyX = LXYX;lxyY = LXYY;lxyDir = LXYDIR;isLjcShow = ISLJC;isScMapShow = ISSC;isResumeShow = !isResumeShow;
            }repaint();
            
            //控制方向,下面是所有用到方向键的功能的实现
            if(KeyCode == KeyEvent.VK_UP){    
                isWelcome = false;
                findxyY -= MoveSpeed;
                if(isResumeShow){    //Up按钮切换返回游戏界面选项
                    isResumeButtonShow = true;
                    resumeButtonIndex--;
                    resumeButtonY -= resume_DIST;
                    if(resumeButtonIndex < 0){
                        resumeButtonY = limDOWN;
                        resumeButtonIndex = 3;
                    }
                }repaint();
                //李逍遥需要向上移动一次
                //a.图片的位置发生改变
                        
                //当画出返回游戏窗口时,禁止主角移动
                if(isLxyShow && !isResumeShow){                //返回游戏界面主角不可移动    
                    lxyY -= MoveSpeed;    //位置改变,行走
                    lxyIndex++;
                    System.out.println(isMengjShow);
                    if(lxyIndex >= lxyUpImages.length){
                        lxyIndex = 0;                     
                    }                    
                    lxyImage = lxyUpImages[lxyIndex];
                    lxyDir = KeyEvent.VK_UP;
                }
                //补充 :判断李逍遥的障碍物功能                
                if(!isMengjShow){    //梦境之中没有红色障碍地图
                    int isRedX = lxyX + lxyImage.getWidth(null)/2;
                    int isRedY = lxyY + lxyImage.getHeight(null);            
                    //上边界
                    
                    if(isLjcShow){                    
                        if(lxyY <= 0){
                            lxyY = 0;
                        }
                        
                        if(ljcRedMap.getRGB(isRedX, isRedY) == -521461){
                            lxyY += MoveSpeed;
                        }
                    }else if(isScMapShow){
                        if(lxyY <= 0){
                            lxyY = 0;
                        }
                        if(scRedMap.getRGB(isRedX, isRedY) == -65536){
                            lxyY += MoveSpeed;
                        }
                    }repaint();    //进入绘画方法
                }
            //System.out.println(lxyX+","+lxyY);         //找坐标
            //System.out.println(scRedMap.getRGB(isRedX, isRedY));//找红图值                                    
                
            }else if(KeyCode == KeyEvent.VK_DOWN){    //存档界面,DOWN按钮选择
                isWelcome = false;
                findxyY += MoveSpeed;
                if(isResumeShow){    //DOWN按钮切换返回游戏界面选项
                    resumeButtonIndex++;
                    isResumeButtonShow = true;                    
                    resumeButtonY += resume_DIST;                    
                    if(resumeButtonIndex > resumeButtonImages.length - 1){
                        resumeButtonIndex = 0; 
                        resumeButtonY = limUP;
                    }repaint();
                    
                }
                if(isLxyShow && !isResumeShow){                    
                    lxyY += MoveSpeed;                
                    lxyIndex++;
                    if(lxyIndex >= lxyDownImages.length){
                        lxyIndex = 0;                        
                    }repaint();
                    lxyImage = lxyDownImages[lxyIndex];
                    lxyDir = KeyEvent.VK_DOWN;
                }repaint();
                
                if(!isMengjShow){                    
                    int isRedX = lxyX + lxyImage.getWidth(null)/2;
                    int isRedY = lxyY + lxyImage.getHeight(null);                    
                    
                    if(isLjcShow){    //李家村红色障碍判断
                        if(lxyY >= ljcImage.getHeight(null) - lxyImage.getHeight(null)){
                            lxyY = ljcImage.getHeight(null) - lxyImage.getHeight(null);
                        }
                        
                        if(ljcRedMap.getRGB(isRedX, isRedY) == -521461){
                            lxyY -= MoveSpeed;
                        }
                    }repaint();
                    
                    if(isScMapShow){    //市场红色障碍判断
                        if(lxyY >= scMapImages[0].getHeight(null) - lxyImage.getHeight(null)){
                            lxyY = scMapImages[0].getHeight(null) - lxyImage.getHeight(null);
                        }
                        if(scRedMap.getRGB(isRedX, isRedY) == -65536){
                            lxyY -= MoveSpeed;
                        }
                        
                    }repaint();
                }
                
            }else if(KeyCode == KeyEvent.VK_LEFT){
                isWelcome = false;
                findxyX -= MoveSpeed;
                //LEFT按钮切换开始游戏界面选项
                if(isStartShow){                    
                    startButtonX -= start_DIST;
                    startButtonIndex++;                    
                    if(startButtonIndex > startButtonImages.length-1){
                        startButtonIndex = 0;
                        startButtonX = limRIGHT;
                    }
                }repaint();        
                
                if(isLxyShow && !isResumeShow){                    
                    lxyX -= MoveSpeed;                    
                    lxyIndex++;
                    if(lxyIndex >= lxyLeftImages.length){
                        lxyIndex = 0;                        
                    }
                    lxyImage = lxyLeftImages[lxyIndex];
                    lxyDir = KeyEvent.VK_LEFT;
                }repaint();
                
                if(!isMengjShow){
                    
                    int isRedX = lxyX;
                    int isRedY = lxyY + lxyImage.getHeight(null);
                    
                    if(isLjcShow){
                        if(lxyX <= LeftEdge){
                            lxyX = LeftEdge;
                        }
                        
                        if(ljcRedMap.getRGB(isRedX, isRedY) == -521461){
                            lxyX += MoveSpeed;
                        }
                    }
                    
                    if(isScMapShow){    
                        if(lxyX <= LeftEdge){
                            lxyX = LeftEdge;
                        }
                        
                        if(scRedMap.getRGB(isRedX, isRedY) == -65536){
                            lxyX += MoveSpeed;
                        }
                    }
                    repaint();
                }
            }else if(KeyCode == KeyEvent.VK_RIGHT){
                isWelcome = false;
                findxyX += MoveSpeed;
                if(isStartShow){    //RIGHT按钮切换开始游戏界面选项
                    startButtonX += start_DIST;
                    startButtonIndex--;                    
                    if(startButtonIndex < 0){
                        startButtonIndex = 2;
                        startButtonX = limLEFT;
                    }
                }
                if(isLxyShow && !isResumeShow){                    
                    lxyX += MoveSpeed;                    
                    lxyIndex++;
                    if(lxyIndex >= lxyRightImages.length){
                        lxyIndex = 0;                        
                    }
                    lxyImage = lxyRightImages[lxyIndex];
                    lxyDir = KeyEvent.VK_RIGHT;
                }
                
                if(!isMengjShow){                    
                    int isRedX = lxyX + lxyImage.getWidth(null);
                    int isRedY = lxyY + lxyImage.getHeight(null);
                    
                    if(isLjcShow){
                        if(lxyX >= ljcImage.getWidth(null) - lxyImage.getWidth(null)){
                            lxyX = ljcImage.getWidth(null) - lxyImage.getWidth(null);
                        }                    
                        if(ljcRedMap.getRGB(isRedX, isRedY) == -521461){
                            lxyX -= MoveSpeed;
                        }
                    }
                    if(isScMapShow){                    
                        if(lxyX >= scMapImages[0].getWidth(null) - lxyImage.getWidth(null)){
                            lxyX = scMapImages[0].getWidth(null) - lxyImage.getWidth(null);
                        }                    
                        if(scRedMap.getRGB(isRedX, isRedY) == -65536){
                            lxyX -= MoveSpeed;
                        }
                    }
                    repaint();
                }
            }
            else if(KeyCode == KeyEvent.VK_SPACE){
                isWelcome = false;    
                isSBNH = false;

                if(isLjcShow){
                    int x11 = lxyX + lxyImage.getWidth(null)/2;
                    int y11 = lxyY + lxyImage.getHeight(null)/2;
                    int x12 = awsX + awsImages[awsIndex].getWidth(null)/2;
                    int y12 = awsY + awsImages[awsIndex].getHeight(null)/2;
                    if(lxyDir == KeyEvent.VK_LEFT && x11 > x12-50 && x11 < x12+50 && y11 > y12-50 && y11 < y12+50){
                        //实现空格按键控制聊天背景和内容的出现
                        isChatShow1 = !isChatShow1;
                        repaint();
                    }
                    int x21 = lxyX+lxyImage.getWidth(null)/2;
                    int y21 = lxyY + lxyImage.getHeight(null)/2;
                    int x22 = azhuX + azhuImages[azhuIndex].getWidth(null)/2;
                    int y22 = azhuY + azhuImages[azhuIndex].getHeight(null)/2;
                    if(lxyDir == KeyEvent.VK_LEFT && x21 > x22-50 && x21 < x22+50 && y21 > y22-50 && y21 < y22+50){
                        //实现空格按键控制聊天背景和内容的出现
                        isChatShow2 = !isChatShow2;
                        repaint();
                    }
                    int x31 = lxyX+lxyImage.getWidth(null)/2;
                    int y31 = lxyY + lxyImage.getHeight(null)/2;
                    int x32 = wcsX + wcsImages[wcsIndex].getWidth(null)/2;
                    int y32 = wcsY + wcsImages[wcsIndex].getHeight(null)/2;
                    if(lxyDir == KeyEvent.VK_UP && x31 > x32-50 && x31 < x32+50 && y31 > y32-50 && y31 < y32+50){
                        //实现空格按键控制聊天背景和内容的出现
                        isChatShow3 = !isChatShow3;
                        repaint();
                    }
                }
                
            }                        
            else if(KeyCode == KeyEvent.VK_1){
                ClothCode = 0;
            }else if(KeyCode == KeyEvent.VK_2){
                ClothCode = 1;
            }
        }
                
    }

    @Override
    public void keyReleased(KeyEvent e) {
        
    }
}

 

 原素材:

背景:https://www.cnblogs.com/images/cnblogs_com/louis6575/1354506/t_start0.png

新的开始:https://www.cnblogs.com/images/cnblogs_com/louis6575/1354506/t_0.png

旧的回忆:https://www.cnblogs.com/images/cnblogs_com/louis6575/1354506/t_1.png

开发团队:https://www.cnblogs.com/images/cnblogs_com/louis6575/1354506/t_2.png

    

 

 

 

 

 

 

你可能感兴趣的:(《仙剑奇侠传柔情版》Java的简单实现(二))