⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamecanvas.java

📁 用J2ME1.0写的一个比较有意思的游戏.
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Canvas;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.rms.*;
import java.io.*;


public class GameCanvas extends Canvas implements Runnable{

//    定义声音开关
    int sound = 1;

    int onth = 1;//音乐变量,循环播放

    //音乐开关
    int musicx =100;

    //选择菜变量
    int caiselect = 0;

    //监控程序负载
    String game_debug = "";

//    定义全局变量,select菜单的y坐标
    int y = 172;

    //定义开始菜单人物动作
    int ppm = 0;

//    定义menu菜单的hand图标的坐标
    int h_x1 = 55,h_y1 = 85;

//    定义游戏选择原料的界面gameselect
    int gameselect = 0;

    //定义火力交替
    int fireswitch = 1;

//    定义原料选择画面各坐标
    int arrow_x = 32,arrow_y = 164;
    int tb0_x = 20,tb0_y = 170;
    int tb1_x = 20,tb1_y = 170;
    int tb2_x = 20,tb2_y = 170;
    int tb3_x = 20,tb3_y = 170;
    int tb4_x = 20,tb4_y = 170;

//    设置开始炒菜的开关
    int docao = 0;

    //定义各菜得分变量
    int cai1=0,cai2=0,cai3=0,cai4=0,cai5=0,cai6=0;

    //定义所有配料的变量
    int a1=0,a2=0,a3=0,a4=0,a5=0,a6=0,a7=0,a8=0,a9=0,a10=0,a11=0,a12=0,a13=0,a14=0;
    int b1=0,b2=0,b3=0,b4=0,b5=0,b6=0;
    int c1=0,c2=0,c3=0,c4=0;

//    控制火点
    int ff = 1;

    //左右手
    int lefthand = 0,righthand = 0;
    int lhi = 0;//左手用变量

    GameMenu game;

//    线程
    Thread t1;

    //设置步长
    int time_step=50;
    int h_miao=10;

//    定义全局变量,用action来切换场景
    int action = 0;

//    计时器变量,分秒变量
    int timer = 0;
    int fen = 0,miao = 0,hmiao = 0;
    private String m = "";

//    所有图片定义,缺
    Image logo,start,begin,select,menu,hand,gamebg,fire,set,music,top,help;
    Image arrow,tb0,tb1,tb2,tb3,tb4;
    Image firel1,firel2,firem1,firem2,fires1,fires2;
    Image lefthand1,lefthand2,lefthand3,righthand1,righthand2;
    Image finalpic1,finalpic2,cai_hgr,cai_mpdf,cai_fqcd,cai_jjrs,cai_yxqz,cai_tcy,cai_fail;
    Image ing1,ing2,ing3,ppm1,ppm2;
    Image spt01,spt02,spt03,spt00,sptl1,sptl2,sptl3,sptr1,sptr2,sptr3,apple;

	//片头左右移动开关
	int sptml = 0;
	int sptmr = 0;
	int sptx = 22;
	int sptok = 0;
	//水果掉下开关
	int appledown = 1;
	int appley = -176;
	int appley2 = -352;

//    构造函数
    public GameCanvas(GameMenu gameMenu) {
        game = gameMenu;
        t1 = new Thread(this);
        t1.start();
        try {

            fire = Image.createImage("/fire.png");
            arrow = Image.createImage("/arrow.png");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    画笔
    protected void paint(Graphics g) {

//        背景
        g.setColor(255,255,255);

        g.fillRect(0,0,getWidth(),getHeight());





//        判定场景
        if(action==0) {//工作室画面
            paint_start(g);

        } else if(action==1) {//游戏开始选择画面
            paint_begin(g);

        } else if(action==2) {//继续游戏


        } else if(action==3) {//音乐开关画面
            paint_set(g);

        } else if(action==4) {//排行榜界面
            paint_top(g);

        } else if(action==5) {//帮助界面
            paint_help(g);

        } else if(action==11) {//选择菜谱界面
            paint_menu(g);

        } else if(action==12) {//游戏主画面
            paint_gamebg(g);
            paint_fire(g);
            paint_gameselect(g);


        } else if(action==13) {//得分画面
            paint_endbg(g);

        } else if(action==14) {
            paint_lose(g);
        }


        g.setClip(0,0,getWidth(),getHeight());
        g.setColor(0,0,0);
        g.drawString(game_debug,0,208,20);

        if(fen == 10) {
            action = 14;
            docao = 0;
            timer = 0;
            fen = 0;
            miao = 0;
        }

    }

//    绘制工作室画面
    protected void paint_start(Graphics g) {

    	g.setColor(255,255,255);
        g.fillRect(0,0,176,208);
        g.setClip(0,0,176,208);

if(appledown < 30) {
	appledown++;
		try {
				if(logo == null) {
            logo = Image.createImage("/logo.png");
					}
        } catch (IOException e) {e.printStackTrace();}
    		g.drawImage(logo,0,0,20);
    	} else if(appledown < 60) {
    	appledown++;
    	try {
				if(start == null) {
            start = Image.createImage("/start.png");
					}
        } catch (IOException e) {e.printStackTrace();}
    		g.drawImage(start,0,0,20);
    		} else {
    		action = 1;
            play_sound1(2);
    			}
    }

//    绘制游戏选择画面
    protected void paint_begin(Graphics g) {
        try {
			if(begin == null) {
            begin = Image.createImage("/begin.png");
				}
        } catch (IOException e) {
            e.printStackTrace();
        }
        g.drawImage(begin,0,0,Graphics.TOP|Graphics.LEFT);


        try {
			if(ppm1 == null) {
            ppm1 = Image.createImage("/ppm1.png");
				}
			if(ppm2 == null) {
            ppm2 = Image.createImage("/ppm2.png");
				}
        } catch (IOException e) {
            e.printStackTrace();
        }

        if(ppm < 3) {
        	g.drawImage(ppm1,3,19,20);
        	ppm++;
        }else if(ppm < 6) {
        	g.drawImage(ppm2,3,19,20);
        	ppm++;
        }else if(ppm==6){
        g.drawImage(ppm2,3,19,20);
        ppm=0;
        	}

        try {
			if(select == null) {
            select = Image.createImage("/select.png");
				}
        } catch (IOException e) {
        }
        g.setClip(104,172,60,20);
        g.drawImage(select,94,y,Graphics.TOP|Graphics.LEFT);
    }

//    绘制音乐开关画面
    protected void paint_set(Graphics g) {
        try {
			if(set == null) {
            set = Image.createImage("/set.png");
				}
			if(music == null) {
            music = Image.createImage("/music.png");
				}
        } catch (IOException e) {
            e.printStackTrace();
        }
        g.drawImage(set,0,0,Graphics.TOP|Graphics.LEFT);

        g.drawImage(music,musicx,80,20);

    }

//    绘制排行榜画面
    protected void paint_top(Graphics g) {
        try {
			if(top == null) {
            top = Image.createImage("/top.png");
				}
        } catch (IOException e) {
            e.printStackTrace();
        }
        g.drawImage(top,0,0,Graphics.TOP|Graphics.LEFT);

        //数据库读取操作1
        rs1 = openRSAnyway1(dbname1);
        if(rs1 == null) {
        	System.out.println("rs1 == null");
        } else {
        try {
        if(rs1.getNumRecords() == 0) {

        	add1(0);
        } else {
        if(matches1()) {
        	if(save1(cai1)){}
        }
      }
      rs1.closeRecordStore();
    } catch(Exception e) {}
    }

    //数据库读取操作2
        rs2 = openRSAnyway2(dbname2);
        if(rs2 == null) {
        	System.out.println("rs2 == null");
        } else {
        try {
        if(rs2.getNumRecords() == 0) {

        	add2(0);
        } else {
        if(matches2()) {
        	if(save2(cai2)){}
        }
      }
      rs2.closeRecordStore();
    } catch(Exception e) {}
    }

    //数据库读取操作3
        rs3 = openRSAnyway3(dbname3);
        if(rs3 == null) {
        	System.out.println("rs3 == null");
        } else {
        try {
        if(rs3.getNumRecords() == 0) {

        	add3(0);
        } else {
        if(matches3()) {
        	if(save3(cai3)){}
        }
      }
      rs3.closeRecordStore();
    } catch(Exception e) {}
    }

    //数据库读取操作4
        rs4 = openRSAnyway4(dbname4);
        if(rs4 == null) {
        	System.out.println("rs4 == null");
        } else {
        try {
        if(rs4.getNumRecords() == 0) {

        	add4(0);
        } else {
        if(matches4()) {
        	if(save4(cai4)){}
        }
      }
      rs4.closeRecordStore();
    } catch(Exception e) {}
    }

    //数据库读取操作5
        rs5 = openRSAnyway5(dbname5);
        if(rs5 == null) {
        	System.out.println("rs5 == null");
        } else {
        try {
        if(rs5.getNumRecords() == 0) {

        	add5(0);
        } else {
        if(matches5()) {
        	if(save5(cai5)){}
        }
      }
      rs5.closeRecordStore();
    } catch(Exception e) {}
    }

    //数据库读取操作6
        rs6 = openRSAnyway6(dbname6);
        if(rs6 == null) {
        	System.out.println("rs6 == null");
        } else {
        try {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -