📄 game.java
字号:
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.VideoControl;
public class Game extends Canvas {
Player pl;
VideoControl vc;
Canvas canvas;
private String[] vnames = { "工具1", "工具2", "工具3", "工具4" };
private String[] vname = {"/va/c.mpg","/va/c.gif","/va/c.mpg","/va/c.gif"};
private String[] vtape = {"video/mpeg","image/gif","video/mpeg","image/gif"};
private Image icons;
private Image about;
private Image help;
private int index = 0;
private Image imglogo;
private Image Background;
private Image selectboy;
private String text, text1, text2, text3, text4, text5, text6;
private String[] menulist;
int gametape = 1;
int chouse = 6;
int choused = 0;
int WIDTH = getWidth();
int HEIGHT = getHeight();
int iconx1 = WIDTH - 34, iconx2 = WIDTH - 14, icony1 = 17, icony2 = 34,
iconsy = 17;
static final Font FONT = Font.getFont(64, 4, 8);
static final int TEXTHEIGHT = FONT.getHeight() + 3;
private int movey = 66;
protected void paint(Graphics g) {
try {
imglogo = Image.createImage("/va/index.png");
Background = Image.createImage("/va/Background.png");
selectboy = Image.createImage("/va/selectboy.png");
about = Image.createImage("/va/about.png");
help = Image.createImage("/va/help.png");
} catch (IOException e) {
e.printStackTrace();
}
switch (gametape) {//区分所处的层
case 1://游戏开始的画面
text = "~~按任意键继续~~";
drawFrame(g, 10, 10, WIDTH - 20, HEIGHT - 30, true, 0x0000FF);
drawBackground(g, imglogo);
drawString(g, text, WIDTH / 2, 230,
Graphics.HCENTER | Graphics.TOP, 0xFF0000);
break;
case 2://游戏选单画面
String[] menulist = { "开始游戏", "名言警句", "重新开始", "帮助", "关于", "退出" };
drawFrame(g, 10, 10, WIDTH - 20, HEIGHT - 30, true, 0x0000FF);
drawBackground(g, Background);
g.setColor(0x770077);
g.fillRoundRect(WIDTH / 2 - 42, HEIGHT / 2 - 2, 84, 104, 20, 20);
g.setColor(0x440044);
g.fillRoundRect(WIDTH / 2 - 41, HEIGHT / 2 - 1, 82, 102, 20, 20);
g.setColor(0xFF00FF);
g.fillRoundRect(WIDTH / 2 - 40, HEIGHT / 2, 80, 100, 20, 20);
switch (chouse) {//选单的滚动
case 1:
text1 = menulist[0];
text2 = menulist[1];
text3 = menulist[2];
text4 = menulist[3];
text5 = menulist[4];
text6 = menulist[5];
break;
case 2:
text1 = menulist[1];
text2 = menulist[2];
text3 = menulist[3];
text4 = menulist[4];
text5 = menulist[5];
text6 = menulist[0];
break;
case 3:
text1 = menulist[2];
text2 = menulist[3];
text3 = menulist[4];
text4 = menulist[5];
text5 = menulist[0];
text6 = menulist[1];
break;
case 4:
text1 = menulist[3];
text2 = menulist[4];
text3 = menulist[5];
text4 = menulist[0];
text5 = menulist[1];
text6 = menulist[2];
break;
case 5:
text1 = menulist[4];
text2 = menulist[5];
text3 = menulist[0];
text4 = menulist[1];
text5 = menulist[2];
text6 = menulist[3];
break;
case 6:
text1 = menulist[5];
text2 = menulist[0];
text3 = menulist[1];
text4 = menulist[2];
text5 = menulist[3];
text6 = menulist[4];
break;
}
int y = HEIGHT / 2 + 2;
drawString(g, text1, WIDTH / 2, y, Graphics.HCENTER | Graphics.TOP,
0x0000FF);
drawString(g, text2, WIDTH / 2, y + 15, Graphics.HCENTER
| Graphics.TOP, 0x0000FF);
drawString(g, text3, WIDTH / 2, y + 30, Graphics.HCENTER
| Graphics.TOP, 0x0000FF);
drawString(g, text4, WIDTH / 2, y + 45, Graphics.HCENTER
| Graphics.TOP, 0x0000FF);
drawString(g, text5, WIDTH / 2, y + 60, Graphics.HCENTER
| Graphics.TOP, 0x0000FF);
drawString(g, text6, WIDTH / 2, y + 75, Graphics.HCENTER
| Graphics.TOP, 0x0000FF);
drawString(g, text2, WIDTH / 2, HEIGHT / 2 + 18, Graphics.HCENTER
| Graphics.TOP, 0x00FFFF);
break;
case 3://在选单中选择后进入的不同画面
switch (choused) {
case 1:
text = "这里随机显示不同的-名言警句";
drawFrame(g, 10, 10, WIDTH - 20, HEIGHT - 30, true, 0x0000FF);
drawBackground(g, Background);
g.setColor(0xF00F00);
g.fillRoundRect(40, 40, WIDTH / 2, HEIGHT / 2, 50, 50);
g.setColor(0xF00FF0);
g.fillRoundRect(41, 41, WIDTH / 2 - 2, HEIGHT / 2 - 2, 50, 50);
g.setColor(0xF0F0F0);
drawString(g, text, WIDTH / 2, 20, Graphics.HCENTER
| Graphics.TOP, 0xFF0000);
break;
case 6://开始游戏
/*
* 进入gametape=4,选择恶作剧对象。 进入gametape = 5,
* 绘制背景,游戏角色,调用小图片,判定按键,在小图片间的选择,刷新显示的文字
*/
text = "请选择恶作剧对象";
drawFrame(g, 10, 10, WIDTH - 20, HEIGHT - 30, true, 0x0000FF);
drawString(g, text, WIDTH / 2, 20, Graphics.HCENTER
| Graphics.TOP, 0xFF0000);
drawPicture(g, selectboy, WIDTH / 2, HEIGHT / 2);
int number;
for (number = 1; number < 5; number++) {
text = Integer.toString(number) + "键";
drawString(g, text, 44 * number, 110, Graphics.HCENTER
| Graphics.TOP, 0xFF0000);
}
break;
case 2://重新开始
text = "您还没有记录!!!";
drawFrame(g, 10, 10, WIDTH - 20, HEIGHT - 30, true, 0x0000FF);
drawBackground(g, Background);
g.setColor(0xF00F00);
g.fillRoundRect(40, 40, WIDTH / 2, HEIGHT / 2, 50, 50);
g.setColor(0xF00FF0);
g.fillRoundRect(41, 41, WIDTH / 2 - 2, HEIGHT / 2 - 2, 50, 50);
g.setColor(0xF0F0F0);
drawString(g, text, WIDTH / 2, 20, Graphics.HCENTER
| Graphics.TOP, 0xFF0000);
break;
case 3:
drawFrame(g, 10, 10, WIDTH - 20, HEIGHT - 30, true, 0x0000FF);
drawBackground(g, Background);
drawPicture(g, help, WIDTH / 2, HEIGHT / 2);
text = "帮助信息";
drawString(g, text, WIDTH / 2, 20, Graphics.HCENTER
| Graphics.TOP, 0xFF0000);
g.setClip(54, 64, 132, 166);//设定字体的滚动区域
text = "fffffffffffffffffffffffffffffffff";
drawString(g, text, WIDTH / 2, movey, Graphics.HCENTER
| Graphics.TOP, 0xFF0000);
for (int n = 1; n < 16; n++) {
text = "dddddddddddddddddddddddddddddddddddddddd";
drawString(g, text, WIDTH / 2, movey + n * TEXTHEIGHT,
Graphics.HCENTER | Graphics.TOP, 0xFF0000);
}
break;
case 4:
drawFrame(g, 10, 10, WIDTH - 20, HEIGHT - 30, true, 0x0000FF);
drawBackground(g, Background);
drawPicture(g, help, WIDTH / 2, HEIGHT / 2);
text = "-版权信息-";
drawString(g, text, WIDTH / 2, 20, Graphics.HCENTER
| Graphics.TOP, 0xFF0000);
g.setClip(54, 64, 132, 166);//设定字体的滚动区域
text = "aasasasgagagag";
drawString(g, text, WIDTH / 2, movey, Graphics.HCENTER
| Graphics.TOP, 0xFF0000);
for (int n = 1; n < 16; n++) {
text = "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh";
drawString(g, text, WIDTH / 2, movey + n * TEXTHEIGHT,
Graphics.HCENTER | Graphics.TOP, 0xFF0000);
}
break;
case 5://退出游戏
chuqiMIDlet.quitApp();
break;
}
break;
case 4://游戏进行画面层
drawFrame(g, 10, 10, WIDTH - 20, HEIGHT - 30, true, 0x0000FF);
drawFrame(g, 14, 14, WIDTH - 50, HEIGHT - 60, true, 0xFFFFFF);
drawFrame(g, WIDTH - 36, 14, WIDTH - 216, HEIGHT - 50, true,
0x0000FF);
drawFrame(g, 14, HEIGHT - 46, WIDTH - 50, HEIGHT - 267, true,
0x0000FF);
drawString(g, vnames[index], 34, HEIGHT - 42, Graphics.HCENTER
| Graphics.TOP, 0xFF0000);
drawIcon(g, icons, WIDTH - 33, iconsy);
drawIcon(g, icons, WIDTH - 33, iconsy + 176);
g.setColor(0xFFFF00);
g.drawLine(iconx1, icony1, iconx2, icony1);
g.drawLine(iconx1, icony2, iconx2, icony2);
g.drawLine(iconx1, icony1, iconx1, icony2);
g.drawLine(iconx2, icony1, iconx2, icony2);
break;
case 5:
g.setColor(0x000000);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(0xFFFFFF);
g.drawRect(10,10,WIDTH-20,HEIGHT-30);
g.setColor(0xFFFF66);
g.drawRect(11,11,WIDTH-22,HEIGHT-32);
break;
}
}
private void drawBackground(Graphics g, Image image) {
//话背景类
if (image != null) {
g.drawImage(image, WIDTH / 2, HEIGHT / 2, Graphics.HCENTER
| Graphics.VCENTER);
} else {
drawFrame(g, 0, 0, WIDTH - 1, HEIGHT - 1, false, 0xFFDFBF);
}
return;
}
private void drawPicture(Graphics g, Image image, int px, int py) {
//话小图片
if (image != null) {
//drawFrame(g, px - 3, py - 3, 176 + 6, 208 + 6, true, 0x0000FF);
g.drawImage(image, px, py, Graphics.VCENTER | Graphics.HCENTER);
} else {
drawFrame(g, 0, 0, WIDTH - 1, HEIGHT - 1, false, 0xFFDFBF);
}
return;
}
private void drawIcon(Graphics g, Image icons, int px, int py) {
try {
icons = Image.createImage("/va/128.png");
} catch (Exception e) {
System.out.println("没有得到图片");
}
g.setClip(WIDTH - 34, 17, WIDTH - 219, HEIGHT - 55);
g.drawImage(icons, px, py, Graphics.TOP | Graphics.LEFT);
}
private void drawFrame(Graphics g, int x, int y, int width, int height,
boolean isAlpha, int color) {
////////////////////////////
//开始菜单的字体和地面颜色//
//////////////////////////////
if (isAlpha) {
//DirectGraphics dg = DirectUtils.getDirectGraphics(g);
//dg.setARGBColor(color);
g.setColor(color);
} else {
g.setColor(color);
}
g.fillRect(x, y, width, height);
g.setColor(0x000000);
g.drawRect(x, y, width, height);
g.setColor(0xFFFF00);
g.drawRect(x + 1, y + 1, width - 2, height - 2);
g.setColor(0x800000);
g.drawRect(x + 2, y + 2, width - 4, height - 4);
return;
}
private void drawString(Graphics g, String text, int x, int y, int archor,
int color) {
//书写文字
g.setColor(0x000000);
g.drawString(text, x, y, archor);
g.drawString(text, x + 2, y, archor);
g.drawString(text, x + 1, y - 1, archor);
g.drawString(text, x + 1, y + 1, archor);
g.setColor(color);
g.drawString(text, x + 1, y, archor);
return;
}
private void PlayVideo (int number){
try {
InputStream is = getClass()
.getResourceAsStream(vname[number]);
pl = Manager.createPlayer(is, vtape[number]);
pl.realize();
vc = (VideoControl) pl.getControl("VideoControl");
if (vc != null) {
vc.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);
vc.setDisplayLocation(55, 40);//播放位置
vc.setVisible(true);
pl.setLoopCount(99);
pl.start();
}
}
catch (IOException io) {
System.out.println("输出错误!");
} catch (MediaException me) {
System.out.println("读取音乐错误!");
}
}
protected void keyPressed(int code) {
//按键响应
switch (gametape) {
case 1:
gametape = 2;
repaint();
break;
case 2://选单的滚动
switch (code) {
case -1:
case -3:
if (chouse <= 1) {
chouse = 6;
} else if (chouse > 1) {
chouse--;
}
repaint();
break;
case -2:
case -4:
if (chouse < 6) {
chouse++;
} else if (chouse >= 6) {
chouse = 1;
}
repaint();
break;
case -5://确定,选择
choused = chouse;
gametape = 3;
repaint();
break;
}
break;
case 3://开始游戏
switch (choused) {
case 6://选择恶作剧画面
switch (code) {
case -6:
gametape = 2;
chouse = 6;
choused = 0;
repaint();
break;
case 49:
case 50:
case 51:
case 52://选择进入
gametape = 4;
repaint();
break;
}
case 3://帮助字体滚动显示
case 4:
switch (code) {
case -2:
System.out.println(movey);
if (movey != 66 - 10 * TEXTHEIGHT)//在这里控制,,当N等于一定值是不在变化
movey -= TEXTHEIGHT;
repaint();
break;
case -1:
if (movey != 66)//在这里控制,,当N等于一定值是不在变化
movey += TEXTHEIGHT;
repaint();
break;
case -6:
gametape = 2;
chouse = 6;
choused = 0;
movey = 66;
repaint();
break;
}
break;
case 1:
switch (code) {
case -6:
gametape = 2;
repaint();
break;
}
}
break;
case 4://游戏进行画面
switch (code) {
case -6:
gametape = 2;
index = 0;
iconx1 = WIDTH - 34;
iconx2 = WIDTH - 14;
icony1 = 17;
icony2 = 34;
iconsy = 17;
chouse = 6;
choused = 0;
repaint();
break;
case -5:
gametape = 5;
repaint();
PlayVideo(index);
break;
case -2:
if (icony2 < 240) {
icony1 += 18;
icony2 += 18;
} else if (iconsy > -120) {
iconsy -= 18;
}
if (index < 3) {
index++;
}
System.out.println(index);
System.out.println(vname[index]);
System.out.println(vtape[index]);
repaint();
break;
case -1:
if (icony2 > 34) {
icony1 -= 18;
icony2 -= 18;
} else if (iconsy < 17) {
iconsy += 18;
}
if (index > 0) {
index--;
}
System.out.println(index);
System.out.println(vname[index]);
System.out.println(vtape[index]);
repaint();
break;
}
break;
case 5:
switch (code) {
case -6:
pl.close();
pl = null;
gametape = 4;
index = 0;
iconx1 = WIDTH - 34;
iconx2 = WIDTH - 14;
icony1 = 17;
icony2 = 34;
iconsy = 17;
chouse = 6;
choused = 0;
repaint();
break;
}
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -