📄 huabu.java
字号:
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class Huabu extends Canvas implements Runnable {
// 游戏状态
final byte LOGO = 0;
final byte MENU = 1;
final byte START = 2;
final byte OPTION = 3;
final byte HELP = 4;
final byte ABOUT = 5;
final byte EXIT = 6;
final short ScreenWidth = 240;
final short ScreenHeight = 320;
byte gamestates = START;
Image logo;
Image logo1;
Image menu0;
Image menu1;
Image menu2;
Image a000;
Image a_0;
Image a_1;
Image a_2;
Thread t;
player p;
Map MM;
NPCGW diren=new NPCGW();
short time = 0;// 计数器
byte buttony = 0;
// //================key===============
final byte UP = -1;
final byte DOWN = -2;
final byte LEFT = -3;
final byte SHEZHI = -4;
final byte FIRE = -5;
final byte L_KEY = -6;
final byte R_KEY = -7;
game spp;
public Huabu(game spp) {
this.spp = spp;
setFullScreenMode(true);// 设置全屏
try {
a000 = Image.createImage("/000.png");
a_0 = Image.createImage("/A_0.png");
a_1 = Image.createImage("/1_1.png");
a_2 = Image.createImage("/1_2.png");
logo = Image.createImage("/logo.png");
logo1 = Image.createImage("/logo1.png");
menu0 = Image.createImage("/Menu0.png");
menu1 = Image.createImage("/Menu1.png");
menu2 = Image.createImage("/Menu2.png");
} catch (IOException e) {
System.out.println("图片出错");
}
p = new player();
MM = new Map();
t = new Thread(this);// 给线程对象分配空间
t.start();// 启动线程
}
protected void paint(Graphics g) {
clearscreen(g);
switch (gamestates) {
case LOGO:
time++;
if (time < 20) {
g.drawImage(logo, ScreenWidth / 2 - logo.getWidth() / 2,
ScreenHeight / 2 - logo.getHeight() / 2, 0);
} else if (time < 40) {
g.drawImage(logo1, ScreenWidth / 2 - logo1.getWidth() / 2,
ScreenHeight / 2 - logo1.getHeight() / 2, 0);
} else if (time < 150) {
// String ss[] = { "1987年香港沦为英国殖民地", ",华人备受欺辱.少年的李小龙",
// "因不甘中国人被称为'东亚病", "夫'而练习中国功夫.没想到李", "小龙竟是一个难得的习武奇",
// "才,可因为得罪了香港的黑社", "会而被迫来到美国西雅图.从", "此,18岁的李小龙开始了独闯",
// "美国的生涯......" };
g.drawImage(a_0, 0, 0, 0);
g.setClip(20, 65, 200, 200);
if (time % 2 == 0) {
g.drawImage(a_1, 20, 135 - time, 0);
} else if (time % 2 == 1) {
g.drawImage(a_2, 20, 135 - time, 0);
}
} else {
gamestates = MENU;
}
break;
case MENU:
// 背景字
time++;
if (time % 2 == 0) {
g.drawImage(menu0, 0, 0, 0);
} else if (time % 2 == 1) {
g.drawImage(menu1, 0, 0, 0);
} else {
gamestates = START;
}
// g.drawImage(menu1, 0, 0, 0);
g.setClip(ScreenWidth / 2 - 1 - (menu2.getWidth() / 2) / 2,
ScreenHeight / 2 - menu2.getHeight() / 2, 60, 18 * 6);
g.drawImage(menu2, ScreenWidth / 2 - (menu2.getWidth() / 2) / 2,
ScreenHeight / 2 - menu2.getHeight() / 2, 0);
// 选项字
g
.setClip(ScreenWidth / 2 - (menu2.getWidth() / 2) / 2,
ScreenHeight / 2 - menu2.getHeight() / 2 + 18
* buttony - 1, 60, 18);
g.drawImage(menu2, ScreenWidth / 2 - (menu2.getWidth() / 2) / 2
- 60, ScreenHeight / 2 - menu2.getHeight() / 2, 0);
break;
case START:
MM.Hua(g);
p.Hua(g);
gunping();
diren.Hua(g);
break;
case OPTION:
g.setClip(0, 0, 240, 320);
g.drawImage(a000, 0, 0, 0);
g.setColor(0xF6B28B);
// 字体
g.setFont(Font.getFont(0, 0, 16));// 0,中,8,小,16 大
// 在屏幕上写字,字,x,y,对齐方式--17居中
g.drawString("游戏设置", ScreenWidth / 2, 40, 17);
break;
case HELP:
g.setClip(0, 0, 240, 320);
g.drawImage(a000, 0, 0, 0);
g.setColor(0);
// 字体
g.setFont(Font.getFont(0, 0, 16));// 0,中,8,小,16 大
// 在屏幕上写字,字,x,y,对齐方式--17居中
g.drawString("游戏帮助", ScreenWidth / 2, 40, 17);
break;
case ABOUT:
g.setClip(0, 0, 240, 320);
g.drawImage(a000, 0, 0, 0);
g.setColor(0x4A0048);
// 字体
g.setFont(Font.getFont(0, 0, 16));// 0,中,8,小,16 大
// 在屏幕上写字,字,x,y,对齐方式--17居中
g.drawString("游戏关于", ScreenWidth / 2, 40, 17);
break;
}
}
// 按键按下
protected void keyPressed(int key) {
switch (key) {
case R_KEY:// 右软件
gamestates = MENU;
break;
}
switch (gamestates) {
case MENU:
switch (key) {
case DOWN:
buttony++;
if (buttony > 5) {
buttony = 0;
}
break;
case UP:
buttony--;
if (buttony < 0) {
buttony = 5;
}
break;
case FIRE:
switch (buttony) {
case 0:
gamestates = START;
break;
case 1:
break;
case 2:
gamestates = OPTION;
break;
case 3:
gamestates = HELP;
break;
case 4:
gamestates = ABOUT;
break;
case 5:
spp.notifyDestroyed();// 退出
break;
}
break;
}
break;
case START:
p.keypressed(key);
break;
}
}
// 按键抬起
protected void keyReleased(int key) {
switch (gamestates) {
case START:
p.keyreleased(key);
break;
}
}
public void run() {
while (true) {
repaint();// 重复调用paint()
try {
Thread.sleep(100);// 100毫秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void clearscreen(Graphics g) {
g.setColor(0xffffff);
g.fillRect(0, 0, 240, 320);
}
public void gunping() {
if (p.RWZT==p.Run )// down
{
if (p.x < 120 - MM.map1.getWidth() / 4 / 2)// 人物没有到屏幕中间,就 人物走
{
p.x += p.speed;
}
// 人物到屏幕中间,地图滚动,不能滚出 最后一屏
else if (MM.x > ScreenWidth - MM.mapchang) {
MM.x -= MM.speed;
}
// 人物到中间,地图到最后,人物走,不 能出屏幕
else if (p.x < ScreenWidth - MM.map1.getWidth() / 6) {
p.x += p.speed;
}
}
if(p.RWZT==p.RunL )// up
{
if (p.x > 120 - MM.map1.getWidth() / 4 / 2)// 人物没有到屏幕中间,就 人物走
{
p.x -= p.speed;
}
// 人物到屏幕中间,地图滚动,不能滚出 最后一屏
else if (MM.x < 0) {
MM.x += MM.speed;
}
// 人物到中间,地图到最后,人物走,不 能出屏幕
else if (p.x > 0) {
p.x -= p.speed;
}
}
}
int playerx0 = 0;
int playery0 = 0;
int playerx1 = 0;
int playerx2 = 0;
public boolean canmove() {
if (p.RWZT==p.Run) {
playerx0 = p.x - MM.x + p.Width / 4;
playery0 = p.y + p.height;
// playerx1 = p.x - MM.x + p.Width / 4 - p.Width / 4 / 3;
// playerx2 = p.x - MM.x + p.Width / 4 - p.Width / 4 / 3 * 2;
}
if (p.RWZT==p.RunL) {
playerx0 = p.x - MM.x;// +pianx;
playery0 = p.y + p.height;
// playerx1 = p.x - MM.x + p.Width / 4 - p.Width / 4 / 3;
// playerx2 = p.x - MM.x + p.Width / 4 - p.Width / 4 / 3 * 2;
}
if (p.RWZT==p.RunX||p.RWZT==p.RunXL) {
playerx0 = p.x - MM.x + p.Width / 4;
playerx0 = p.x - MM.x;// +pianx;//左脚下的点
// playerx1= p.x - ditu.x + p.width-pianx;//右脚下的点
playery0 = p.y + p.height;
playerx1 = p.x - MM.x + p.Width / 4 - p.Width / 4 / 3;
playerx2 = p.x - MM.x + p.Width / 4 - p.Width / 4 / 3 * 2;
}
if (p.RWZT==p.RunS||p.RWZT==p.RunSL) {
playerx0 = p.x - MM.x + p.Width / 4;
playerx0 = p.x - MM.x;// +pianx;//左脚下的点
// playerx1= p.x - ditu.x + p.width-pianx;//右脚下的点
playery0 = p.y + p.height;
playerx1 = p.x - MM.x + p.Width / 4 - p.Width / 4 / 3;
playerx2 = p.x - MM.x + p.Width / 4 - p.Width / 4 / 3 * 2;
}
// 26
int pengzhuang[] = { 5, 6, 12, 22, 23, 24, 92, 93, 94, 95, 96, 110,
113, 114, 128, 129, 130, 131, 132, 210, 228, 248, 266, 281,
284, 410 };
int row0 = playery0 / MM.kulongy;
int list0 = playerx0 / MM.kulongx;
int list1 = playerx1 / MM.kulongx;
int list2 = playerx2 / MM.kulongx;
for (int i = 0; i < 26; i++) {
if (MM.map_2[row0][list0] == pengzhuang[i]
|| MM.map_2[row0][list1] == pengzhuang[i]
|| MM.map_2[row0][list2] == pengzhuang[i]) {
//不能走;
return false;
}
}
return true;
}
// public void qiehuanditu(){
// if(p.x>-(p.Width/4-MM.mapchang)){
// MM.zhuangtai=MM.guanA ;
// }
//
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -