利用JAVA制作一个简单的小
游戏,不要从网上找代码,那些没用,另外最好代码旁边有注释 说明这行代码有什么作用
package FishGame;
import java.io.IOException;
import java.util.*;
import javax.microedition.lcdui.*;
public class Fish_Canvas extends Canvas implements Runnable{
/**屏幕宽*/
int width;
/**屏幕高*/
int height;
/**玩家得分*/
int score;
/**随机数对象*/
Random r;
/**游戏和菜单之间的控制变量,true代表游戏状态顷态,false代表不在游戏状态*/
boolean isGame;
/**true代表暂停,false代表不暂停*/
boolean isPaused;
/**选择状态值 */
int status = 1;
/**菜单画面 1*/
final int MENU = 1;
/**开始画面 2*/
final int START = 2;
/**游戏简介 3*/
final int SYNOPSIS = 3;
/**游戏帮助 4*/
final int HELP = 4;
/**退出游戏 5*/
final int QU
IT = 5;
/**菜单数组*/
String[] menu = {
"\u5F00\u59CB\u6E38\u620F",
"\u6E38\u620F\u7B80\u4ECB",
"\u6E38\u620F\u5E2E\u52A9",
"\u9000\u51FA\u6E38\u620F"
};
String[] menu2= {
"\u7EE7\u7EED\u6E38\u620F",
"\u6E38\u620F\u7B80\u4ECB",
"\u6E38\u620F\u5E2E\u52A9",
"\u9000\u51FA\u6E38\u620F"
};
/**选择框的宽*/
final int CASE_WIDTH = 45;
/**选橡配择框的高*/
final int CASE_HEIGHT = 15;
/**当前选中的菜单项的序号*/
int index;
/**菜单第一个字的x*/梁乎指
final int MENUX = 60;
/**菜单第一个字的y*/
final int MENUY = 60;
/**菜单之间的间隔*/
final int MENU_SPACE = 20;
/**帮助字符串数组*/
String[] help = {
"\u4E0A\u79FB\uFF1A\u63092\u6216\u4E0A\u65B9\u5411\u952E",
"\u4E0B\u79FB\uFF1A\u63098\u6216\u4E0B\u65B9\u5411\u952E",
"\u5DE6\u79FB\uFF1A\u63094\u6216\u5DE6\u65B9\u5411\u952E",
"\u53F3\u79FB\uFF1A\u63096\u6216\u53F3\u65B9\u5411\u952E",
"\u6682\u505C\uFF1A\u63095\u6216OK\u952E",
"\u9000\u51FA\uFF1A\u6309\u5DE6\u8F6F\u952E"
};
/**帮助第一个字的x*/
final int HELPX = 30;
/**帮助第一个字的y*/
final int HELPY = 40;
/**帮助字符串数组之间的间隔*/
final int HELP_SPACE = 20;
/**简介字符串数组*/
String[] synopsis = {
"\u6E38\u620F\u7C7B\u578B\uFF1A\u76CA\u667A\u7C7B",
"\u4F5C\u8005\uFF1A\u738B\u5C11\u6C5F",
"\u7248\u672C\uFF1Av9.0",
"QQ: 349126394",
"Email: shaojiang216@163.com"
};
/**简介第一个字的x*/
final int SYNOPSISX = 20;
/**简介第一个字的y*/
final int SYNOPSISY = 40;
/**简介字符串数组之间的间隔*/
final int SYNOPSIS_SPACE = 20;
/**游戏结束字符串*/
String gameOver = "GAME OVER!";
/**游戏结束字符串的宽*/
int gameOver_width;
/**字符串的高*/
int gameOver_height;
/**游戏结束字符串左上x*/
int gameOver_x;
/**游戏结束字符串左上y*/
int gameOver_y;
/** 按键状态 */
int key;
/**二进制倒数第一位的数字*/
final int UP = 1;
/**二进制倒数第二位的数字*/
final int DOWN = 1 << 1;
/**二进制倒数第三位的数字*/
final int LEFT = 1 << 2;
/**二进制倒数第四位的数字*/
final int RIGHT = 1 << 3;
/**箭头上键*/
final int KEY_UP = -1;
/**箭头下键*/
final int KEY_DOWN = -2;
/**箭头左键*/
final int KEY_LEFT = -3;
/**箭头右键*/
final int KEY_RIGHT = -4;
/**确定键*/
final int KEY_FIRE = -5;
/**右软键(返回键或暂停键)*/
final int KEY_BACK = -7;
/**左软键(退出键)*/
final int KEY_QUIT = -6;
/**玩家鱼左上x*/
int player_fishx;
/**玩家鱼左上y*/
int player_fishy;
/**玩家鱼宽*/
int playerfish_width;
/**玩家鱼高*/
int playerfish_height;
/**玩家鱼的类型0~9*/
int playerfish_size;
/**玩家鱼的方向,0代表向左,1代表向右*/
int playerfish_direct;
/**玩家鱼的速度*/
final int PLAYER_SPEED = 10;
/**敌人鱼左上x*/
int[] enemyFish_fishx;
/**敌人鱼左上y*/
int[] enemyFish_fishy;
/**敌人鱼宽*/
int enemyFish_width;
/**敌人鱼高*/
int enemyFish_height;
/**敌人鱼的类型0~10*/
int[] enemyFish_size;
/**敌人鱼的方向,0代表向左,1代表向右*/
int[] enemyFish_direct;
/**敌人鱼从左端出来*/
final int DIRECT_LEFT = 1;
/**敌人鱼从右端出来*/
final int DIRECT_RIGHT = 0;
/**敌人鱼的最大数量*/
final int MAX_NUM = 6;
/**玩家鱼的速度*/
int enemy_speed = 8;
/**敌人鱼序号*/
int enemyFish_index;
/**敌人鱼是否可见*/
boolean[] isVisible;
/**鱼的尾巴状态,0代表向内,1代表向外*/
int fish_tail;
/**控制尾巴摆动的变量*/
int tmp_tail;
/**游戏是否结束,true代表结束,false代表未结束*/
boolean isGameOver;
/**背景图片对象*/
Image backGround_ima;
/**玩家鱼向左图片对象数组*/
Image[][][] playerFish_ima;
/**敌人鱼图片对象数组*/
Image[][][] enemyFish_ima;
/**左上锚点*/
final int TOP_LEFT = Graphics.TOP|Graphics.LEFT;
/**左下锚点*/
final int BOTTOM_LEFT = Graphics.BOTTOM|Graphics.LEFT;
/**右下锚点*/
final int BOTTOM_RIGHT = Graphics.BOTTOM|Graphics.RIGHT;
/**创建字体对象*/
Font f;
/**线程对象*/
Thread thread;
/**
* 构造方法
*/
public Fish_Canvas(){
//初始字体对象
f = Font.getDefaultFont();
//初始随机数对象
r = new Random();
//设置全屏
setFullScreenMode(true);
//获得宽高
width = getWidth();
height = getHeight();
gameOver_width = f.stringWidth(gameOver);
gameOver_height = f.getHeight();
gameOver_x = width/2 - gameOver_width/2;
gameOver_y = height/2 - gameOver_height/2;
//导入图片
importIma();
//初始游戏界面
gameInit();
thread = new Thread(this);
thread.start();
}
/**
* 线程
*/
public void run() {
try {
while(true){
while(isGame){
Thread.sleep(100);
keyContinue();
if(!isGameOver){
Thread.sleep(50);
tmp_tailIncrease();
}else{
status = MENU;
//显示一下游戏结束的字样
isGameOver = true;
repaint();
Thread.sleep(2000);
//返回菜单页面
isGameOver = false;
//初始数据
score = 0;
isGame = false;
isPaused = false;
gameInit();
break;
}
enemyFishMove();
process();
upgrade();
repaint();
}
repaint();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 导入图片
*/
private void importIma(){
playerFish_ima = new Image[10][2][2];
enemyFish_ima = new Image[11][2][2];
try {
backGround_ima = Image.createImage("/background.png");
for(int i = 0;i< playerFish_ima.length;i ++){
for(int j = 0;j< playerFish_ima[i].length;j ++){
for(int k = 0;k< playerFish_ima[i][j].length;k ++){
playerFish_ima[i][j][k] = Image.createImage("/playerfish/playerfish"+(i+1)+(char)('a'+k)+(char)('l'+j*('r'-'l'))+".png");
}
}
}
for(int i = 0;i< enemyFish_ima.length;i ++){
for(int j = 0;j< enemyFish_ima[i].length;j ++){
for(int k = 0;k< enemyFish_ima[i][j].length;k ++){
enemyFish_ima[i][j][k] = Image.createImage("/enemyfish/enemyfish"+(i+1)+(char)('a'+k)+(char)('l'+j*('r'-'l'))+".png");
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 初始游戏界面
*/
private void gameInit(){
//初始玩家鱼的大小和位置
initEnemyFish();
//初始化敌人鱼位置和方向
enemyFish_fishx = new int[MAX_NUM];
enemyFish_fishy = new int[MAX_NUM];
enemyFish_direct = new int[MAX_NUM];
enemyFish_size = new int[MAX_NUM];
isVisible = new boolean[MAX_NUM];
for(int i = 0;i < MAX_NUM;i++){
initEnemyFish(i);
isVisible[i] = true;
}
}
/**
* 初始化玩家鱼的数据
*/
private void initEnemyFish(){
playerfish_size = 0;
player_fishx = width/2 - playerfish_width/2;
player_fishy = height/2 - playerfish_height/2;
}
/**
* 初始化敌人鱼的数据
* @param index 敌人鱼的序号
*/
private void initEnemyFish(int index){
//随机方向
enemyFish_direct[index] = Math.abs(r.nextInt()%2);
//x坐标
switch(enemyFish_direct[index]){
case DIRECT_LEFT:
enemyFish_fishx[index] = -enemyFish_ima[enemyFish_size[index]][enemyFish_direct[index]][fish_tail].getWidth();
break;
case DIRECT_RIGHT:
enemyFish_fishx[index] = width;
break;
}
//随机y坐标
enemyFish_fishy[index] = Math.abs(r.nextInt() % (height - enemyFish_ima[enemyFish_size[index]][enemyFish_direct[index]][fish_tail].getWidth()));
//随机鱼的大小
switch(playerfish_size){}
if(playerfish_size < 9){//玩家鱼
enemyFish_size[index] = Math.abs(r.nextInt() % (playerfish_size+2));
}else{
enemyFish_size[index] = Math.abs(r.nextInt() % 11);
}
}
/**
* 总绘制
*/
protected void paint(Graphics g) {
switch(status){
case MENU:
if(isGameOver){
gameOver(g);
}else{
menuPaint(g);
}
break;
case START:
GamePaint(g);
break;
case SYNOPSIS:
synopsisPaint(g);
break;
case HELP:
helpPaint(g);
break;
case QUIT:
Fish_MIDlet.quitApp();
break;
}
}
/**
* 绘制游戏菜单选项
* @param g 画笔
*/
private void menuPaint(Graphics g){
//背景
g.setColor(0x0039A5FF);
g.fillRect(0, 0, width, height);
//绘制文字
for(int i = 0;i < menu.length;i++){
if(i == index){
g.setColor(255,0,0);
}else{
g.setColor(0);
}
if(isPaused){
//继续游戏
g.drawString(menu2[i],MENUX, MENUY + i * MENU_SPACE,TOP_LEFT);
}else{
//开始游戏
g.drawString(menu[i],MENUX, MENUY + i * MENU_SPACE,TOP_LEFT);
}
g.setColor(0);
}
// 绘制选择框
g.drawRect(MENUX,MENUY + index * MENU_SPACE,CASE_WIDTH,CASE_HEIGHT);
}
/**
* 绘制帮助
* @param g 画笔
*/
private void helpPaint(Graphics g){
//背景颜色
g.setColor(0x0039A5FF);
g.fillRect(0, 0, width, height);
for(int i = 0;i < help.length;i++){
g.setColor(0);
g.drawString(help[i],HELPX, HELPY + i * HELP_SPACE,TOP_LEFT);
g.setColor(0);
}
back(g);
}
/**
* 绘制简介
* @param g 画笔
*/
private void synopsisPaint(Graphics g){
//背景颜色
g.setColor(0x0039A5FF);
g.fillRect(0, 0, width, height);
for(int i = 0;i < synopsis.length;i++){
g.setColor(0);
g.drawString(synopsis[i],SYNOPSISX, SYNOPSISY + i * SYNOPSIS_SPACE,TOP_LEFT);
}
g.setColor(0);
back(g);
}
/**
* 绘制游戏界面
* @param g
*/
private void GamePaint(Graphics g){
backGroundPaint(g);
playerFishPaint(g);
enemyFishPaint(g);
scorePaint(g);
isPausedPaint(g);
}
/**
* 返回
* @param g 画笔
*/
private void back(Graphics g){
g.setColor(0);
g.drawString("\u8FD4\u56DE",width - 2, height-2, BOTTOM_RIGHT);
}
/**
* 绘制游戏结束
* @param g 画笔
*/
private void gameOver(Graphics g){
g.setColor(0);
g.drawString(gameOver, gameOver_x, gameOver_y,TOP_LEFT);
}
/**
* 绘制游戏背景
* @param g 画笔
*/
private void backGroundPaint(Graphics g){
g.setColor(0x0048A2C8);
g.fillRect(0, 0, width, height);
g.drawImage(backGround_ima, 2, 0, TOP_LEFT);
}
/**
* 绘制得分和暂停
* @param g
*/
private void scorePaint(Graphics g){
g.setColor(0);
g.drawString("\u5F97\u5206\uFF1A"+ score, 0, 0, TOP_LEFT);
}
/**
* 绘制得分和暂停
* @param g
*/
private void isPausedPaint(Graphics g){
g.setColor(0x00ffffff);
f = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_LARGE);
g.fillRect(width - 25, height - gameOver_height, 25, gameOver_height);
g.setColor(0);
g.drawString("\u6682\u505C", width, height, BOTTOM_RIGHT);
}
/**
* 绘制玩家鱼
* @param g 画笔
*/
private void playerFishPaint(Graphics g){
g.drawImage(playerFish_ima[playerfish_size][playerfish_direct][fish_tail], player_fishx, player_fishy, TOP_LEFT);
}
/**
* 绘制敌人鱼
* @param g 画笔
*/
private void enemyFishPaint(Graphics g){
for(int i = 0;i < MAX_NUM;i++){
if(isVisible[i]){
g.drawImage(enemyFish_ima[enemyFish_size[i]][enemyFish_direct[i]][fish_tail],enemyFish_fishx[i],enemyFish_fishy[i], TOP_LEFT);
}
}
}
/**
* 按键处理,将按下的键记为1
*/
protected void keyPressed(int keyCode) {
//System.out.println(keyCode);
if(!isGame){
//返回主菜单(有开始游戏)
if(keyCode == KEY_BACK){
status = MENU;
isGame = false;
}
if(status == MENU){
menuKeyCode(keyCode);
}
repaint();
}else{
//返回主菜单(有继续游戏)
if(keyCode == KEY_BACK){
status = MENU;
isGame = false;
isPaused = true;
}
//记忆按键
switch(keyCode){
case KEY_NUM2:
case KEY_UP:
key |= UP;
break;
case KEY_NUM8:
case KEY_DOWN:
key |= DOWN;
break;
case KEY_NUM4:
case KEY_LEFT:
if(playerfish_direct != 0){//如果鱼头朝右,改为朝左
playerfish_direct = 0;
}
key |= LEFT;
break;
case KEY_NUM6:
case KEY_RIGHT:
if(playerfish_direct != 1){//如果鱼头朝左,改为朝右
playerfish_direct = 1;
}
key |= RIGHT;
break;
}
}
}
/**
* 释放按键处理,将释放的键记为0
*/
protected void keyReleased(int keyCode) {
if(isGame){
switch(keyCode){
case KEY_NUM2:
case KEY_UP:
key &= ~UP;
break;
case KEY_NUM8:
case KEY_DOWN:
key &= ~DOWN;
break;
case KEY_NUM4:
case KEY_LEFT:
key &= ~LEFT;
break;
case KEY_NUM6:
case KEY_RIGHT:
key &= ~RIGHT;
break;
}
}
}
/**
* 菜单按键处理
* @param keyCode 键值
*/
private void menuKeyCode(int keyCode){
switch(keyCode){
case KEY_NUM2:
case KEY_UP:
if(index > 0){
index--;
}else{
index = menu.length - 1;
}
break;
case KEY_NUM8:
case KEY_DOWN:
if(index < menu.length - 1){
index++;
}else{
index = 0;
}
break;
case KEY_NUM5:
case KEY_FIRE:
switch(index){
case 0:
status = START;
isGame = true;
break;
case 1:
status = SYNOPSIS;
break;
case 2:
status = HELP;
break;
case 3:
status = QUIT;
break;
}
break;
}
}
/**
* 按键逻辑处理方法
*/
private void keyContinue() {
switch (key) {
case UP:
upMove();
break;
case DOWN:
downMove();
break;
case LEFT:
leftMove();
break;
case RIGHT:
rightMove();
break;
case -6:
isGame = !isGame;
repaint();
break;
}
}
/**
* 向左移动
*/
private void leftMove(){
if(player_fishx > 0){//如果没有达到左边界
player_fishx -= PLAYER_SPEED;
}
}
/**
* 向右移动
*/
private void rightMove(){
if(player_fishx < width-playerFish_ima[playerfish_size][playerfish_direct][fish_tail].getWidth()){//如果没有达到右边界
player_fishx += PLAYER_SPEED;
}
}
/**
* 向上移动
*/
private void upMove(){
if(player_fishy > 0){//如果没有达到上边界
player_fishy -= PLAYER_SPEED;
}
}
/**
* 向下移动
*/
private void downMove(){
if(player_fishy < height-playerFish_ima[playerfish_size][playerfish_direct][fish_tail].getHeight()){//如果没有达到下边界
player_fishy += PLAYER_SPEED;
}
}
/**
* 敌人鱼的移动
*/
private void enemyFishMove(){
for(int i = 0;i < MAX_NUM;i++){
switch (enemyFish_direct[i]) {
case DIRECT_RIGHT:
//边界检测
if(enemyFish_fishx[i] + enemyFish_ima[enemyFish_size[i]][enemyFish_direct[i]][fish_tail].getWidth() > 0){
enemyFish_fishx[i] -= enemy_speed;
}else{
initEnemyFish(i);
}
break;
case DIRECT_LEFT:
if(enemyFish_fishx[i] < width){
enemyFish_fishx[i] += enemy_speed;
}else{
initEnemyFish(i);
}
break;
}
}
}
/**
* 控制鱼尾巴摆动
*/
private void tmp_tailIncrease(){
tmp_tail ++;
if(tmp_tail % 2 == 1){
fish_tail ++;
}else{
fish_tail --;
}
}
/**
* 矩形检测方法
* @param x1 矩形1左上角x坐标
* @param y1 矩形1左上角y坐标
* @param w1 矩形1宽度
* @param h1 矩形1高度
* @param x2 矩形2左上角x坐标
* @param y2 矩形2左上角y坐标
* @param w2 矩形2宽度
* @param h2 矩形2高度
* @return true代表重叠,false代表不重叠
*/
private boolean rectCollis(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2){
//水平距离
int xDis = Math.abs((x1 + w1/2) - (x2 + w2/2));
//垂直距离
int yDis = Math.abs((y1 + h1/2) - (y2 + h2/2));
//判断是否重叠
if(xDis <= (w1 + w2)/2 && yDis <= (h1 + h2)/2){
return true;
}else{
return false;
}
}
/**
* 判断是否有鱼被吃
* @return 被吃敌人鱼的序号
*/
private int eat(){
boolean b = false;
for(int i = 0;i < MAX_NUM;i ++){
if(enemyFish_fishx[i] + enemyFish_width > 0&& enemyFish_fishx[i] < width){
b = rectCollis(player_fishx,player_fishy,playerFish_ima[playerfish_size][playerfish_direct][fish_tail].getWidth(),playerFish_ima[playerfish_size][playerfish_direct][fish_tail].getHeight(),enemyFish_fishx[i],enemyFish_fishy[i],enemyFish_ima[enemyFish_size[i]][enemyFish_direct[i]][fish_tail].getWidth(),enemyFish_ima[enemyFish_size[i]][enemyFish_direct[i]][fish_tail].getHeight());
}
if(b){
return i;
}
}
return -1;
}
/**
* 碰撞处理
*/
private void process(){
int temp_index = eat();
if(temp_index != -1){
if(isVisible[temp_index]){
if(playerfish_size >= enemyFish_size[temp_index]){
score += 10;
initEnemyFish(temp_index);
}else{
isGameOver = true;
}
}
}
}
/**
* 升级(玩家鱼变大)
*/
private void upgrade(){
switch(score){
case 100:
playerfish_size = 1;
break;
case 200:
playerfish_size = 2;
enemy_speed = 9;
break;
case 300:
playerfish_size = 3;
break;
case 400:
playerfish_size = 4;
enemy_speed = 10;
break;
case 500:
playerfish_size = 5;
break;
case 600:
playerfish_size = 6;
enemy_speed = 11;
break;
case 700:
playerfish_size = 7;
break;
case 800:
playerfish_size = 8;
enemy_speed = 12;
break;
case 900:
playerfish_size = 9;
enemy_speed = 14;
break;
}
}
}
去
www.verycd.com里找找,我在里槐滑孝面看到了一个五子棋的视频教程,一个铅稿贪吃蛇的视频教程,前两天还看到了让闭一个马里奥的视频教程(这个应该没有完呢),都是java的。如果你要是学习或做毕设的话,会很有用。
恐怕你这个要去找威客网,出赏金让人给你办了