📄 mycanvas.java
字号:
//package src;
import java.util.Timer;
//import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import com.nokia.mid.sound.Sound;
import com.nokia.mid.ui.*;
//import com.nokia.mid.ui.DirectGraphics;
public class MyCanvas extends FullCanvas implements Runnable {
int GameState;//0:logo,1:主菜单,2:开始游戏,3:关于,4:排行榜,:5结束
int width = getWidth(), height = getHeight();//屏幕大小
int Left_x, Left_y, Right_x, Right_y;//左右软键的位置
int key_x, key_y, select_long;//菜单选择项位置及距离
int logo_add, logotime;//logo时间累加及长短
int over_add, overtime;
int select;//开始菜单选择参数;
int Action = 0;// 0:停,1:移动
boolean powerstart;//power开启控制
boolean over;
public int Speed =1;
int add;
int Number;//添加物和怪控制
// int image_i;//循环移动图片
int new_manX, new_manY;
int Score;//层数
// Sound stopdown = new Sound(450,300);
//
// Sound hit = new Sound(600,300);
public boolean running;
Image img_menu = null, img_logo = null, offImg = null, img_wall = null,
img_back = null, img_power = null, img_lift = null, img_energy = null,
img_man = null, img_sharp = null, img_powerC[] = null,img_enemy = null,
img_over;
String string_logo = new String("Press any key to start");
String string_char[] = { "Start", "About", "High Score", "Exit",//0,1,2,3
"ok", "back", "pause", "go", "score: "//4,5,6,7,8
};
String string_about[] = { "5:power","4:Left" ,"6:Right"
};
long ee;
long ss;
public GameRms rms;
public boolean inForm = false;
public int bScore;
Thread t;
Timer time;
ManAction man;
Enemy enemy;
BackCalss back;
LiftThing lift;
EnergyThing energy;
DownMan downman;
Graphics bg = null;//双缓冲画笔
// DirectGraphics dg = null;//旋转画笔
public MyCanvas(DownMan downman) {
this.downman=downman;
running = true;
rms = new GameRms();
rms.readScore();
GameState = 0;
logo_add = 0;
logotime = 10;
SoftKey_position();
Key_Menu_position();
time = new Timer();
back = new BackCalss(width, height);
lift = new LiftThing();
energy = new EnergyThing();
man = new ManAction(this);
enemy = new Enemy(height);
img_powerC = new Image[2];
offImg = Image.createImage(getWidth(), getHeight());
bg = offImg.getGraphics();
// dg = DirectUtils.getDirectGraphics(bg);
Sound stopdown = new Sound(450,300);
Sound hit = new Sound(600,300);
try {
for (int i = 0;i < 2; i++){
img_powerC[i] = Image.createImage("/power" + i + ".png");
}
img_man = Image.createImage("/man.png");
img_enemy = Image.createImage("/enemy.png");
img_sharp = Image.createImage("/sharp.png");
img_logo = Image.createImage("/logo_S40.png");
img_over = Image.createImage("/gameover.png");
img_wall = Image.createImage("/Back.png");
img_back = Image.createImage("/gun.png");
img_power = Image.createImage("/power.png");
img_energy = Image.createImage("/energy.png");
img_lift = Image.createImage("/Lift.png");
} catch (Exception e) {
e.printStackTrace();
}
t = new Thread(this);
t.start();
}
protected void paint(Graphics g) {
ss = System.currentTimeMillis();
bg.setClip(0,0,width,height);
if (GameState == 0) {
gamelogo();
}
if (GameState == 1) {
gamemenu();
}
if (GameState == 2) {
gamestart();
}
if (GameState == 3) {
gameabout();
}
if (GameState == 4) {
gametop();
}
g.drawImage(offImg, 0, 0, Graphics.TOP | Graphics.LEFT);
}
protected void gamelogo() {
bg.drawImage(img_logo, 0, 0, Graphics.TOP | Graphics.LEFT);
bg.setColor(200, 200, 200);
bg.drawString(string_logo, width / 2, height - 15, Graphics.HCENTER
| Graphics.TOP);
}
protected void gamemenu() {
bg.setColor(200, 0, 0);
bg.fillRect(0, 0, width, height);
bg.setColor(0, 0, 0);
bg.drawString(string_char[4], Left_x, Left_y, Graphics.TOP
| Graphics.LEFT);
bg.drawString(string_char[5], Right_x, Right_y, Graphics.TOP
| Graphics.RIGHT);
for (int i = 0; i < 4; i++) {//画主按键
bg.drawString(string_char[i], key_x, key_y + select_long * i,
Graphics.HCENTER | Graphics.TOP);
}
bg.setColor(0, 255, 255);
for (int i = 0; i < 4; i++) {//画被选中按键
if (select == key_y + select_long * i)
bg.drawString(string_char[i], key_x, key_y + select_long * i,
Graphics.HCENTER | Graphics.TOP);
}
System.gc();
}
protected void gamestart() {
bg.setClip(0,0,width,height);
bg.setColor(135, 238, 201);
bg.fillRect(0, 10, width, height - 10);
DrawEnemy();
Drawback();
Drawwall();
DeathCheck();
if(over){
DrawOver();
}else{
DrawMan();
}
DrawPowerStart();
bg.setClip(0,0,width,10);
bg.setColor(250, 250, 250);
bg.fillRect(0, 0, width, 10);
DrawScore();
DrawLift();
DrawPower();
// bg.setColor(0, 0, 0);
// bg.drawString("time时间:"+(ee),40,70,0);
}
protected void gameabout() {
bg.setColor(0, 255, 255);
bg.fillRect(0, 0, width, height);
bg.setColor(0,0,0);
for(int i=0;i<string_about.length;i++){
bg.drawString(string_about[i],40,30+i*15,0);
}
bg.drawString(string_char[5],Right_x,Right_y,Graphics.TOP|Graphics.RIGHT);
}
protected void gametop() {
rms.readScore();
bg.setColor(0, 128, 255);
bg.fillRect(0, 0, width, height);
bg.setColor(0, 0, 0);
bg.drawString(string_char[5],Right_x,Right_y,Graphics.TOP|Graphics.RIGHT);
bg.setColor(0, 0, 0);
bg.drawString("GAME_TOP 5", 30, 10, 0);
bg.setColor(40, 0, 0);
for (int i = 0; i < 5; i++) {
bg.drawString((i + 1) + ". " + rms.name[i], 15, 30 + i * 15, 0);
bg.drawString(" " + rms.score[i], 120, 30 + i * 15, Graphics.TOP
| Graphics.RIGHT);
}
}
protected void DrawOver() {
bg.setClip(width/2-40,40,81,12);
bg.drawImage(img_over,width/2,40,Graphics.TOP|Graphics.HCENTER);
if (rms.inTop(Score)) {
bScore = Score;
inForm = true;
}
}
protected void SoftKey_position() {//左右软键位置
Left_x = 5;
Left_y = height - 13;
Right_x = width - 5;//以右上为锚点
Right_y = height - 13;
}
protected void Key_Menu_position() {//菜单按键位置及距离
key_x = width / 2;
key_y = height / 2 - 10;
select = key_y;
select_long = 15;
}
protected void keyPressed(int keyCode) {//按下按键
if (GameState == 0) {
switch (keyCode) {
case -6:
GameState = 1;
}
}
if (GameState == 1) {
switch (keyCode) {
case -6://左软键
for (int i = 0; i < 4; i++) {
if (select == key_y + select_long * i) {
GameState = 2 + i;
if (i == 0) {
NewState();
}
if (i == 3) {
downman.notifyDestroyed();
}
}
}
break;
case -7://右软键
downman.notifyDestroyed();
break;
case -1://上
select -= select_long;
if (select < key_y)
select = key_y + select_long * 3;
break;
case -2://下
select += select_long;
if (select > key_y + select_long * 3)
select = key_y;
break;
case -5://开火
for (int i = 0; i < 3; i++) {
if (select == key_y + select_long * i) {
GameState = 2 + i;
if (i == 0) {
NewState();
}
}
}
break;
}
}
if (GameState == 2 && !over) {
switch (keyCode) {
case -7:
running = !running;
break;
case 52:
case -3://左
man.MoveAction(-3);
man.imgY = 13;
break;
case 54:
case -4://右
man.MoveAction(-4);
man.imgY = 0;
// man.face = 0;
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -