复制代码 1 package com.tarena.downwell; 2 3 import java.awt.image.BufferedImage; 4 /** 5 * 所有台阶的父类(包括顶部的刺) 6 * @author Misaki 7 * 8 */ 9 public class Board { 10 protected int x;//横坐标 11 protected int y;//纵坐标 12 protected BufferedImage image;//图片 13 protected int width;//图片宽度 14 protected int height;//图片高度 15 /* 16 * 下面都是用elipse生成的get/set方法 17 */ 18 public int getX() { 19 return x; 20 } 21 public void setX(int x) { 22 this.x = x; 23 } 24 public int getY() { 25 return y; 26 } 27 public void setY(int y) { 28 this.y = y; 29 } 30 public BufferedImage getImage() { 31 return image; 32 } 33 public void setImage(BufferedImage image) { 34 this.image = image; 35 } 36 public int getWidth() { 37 return width; 38 } 39 public void setWidth(int width) { 40 this.width = width; 41 } 42 public int getHeight() { 43 return height; 44 } 45 public void setHeight(int height) { 46 this.height = height; 47 } 48 49 } 复制代码 复制代码 package com.tarena.downwell; import java.util.Random; /** * 弹簧台阶 * @author Misaki * */ public class BoardFlip extends Board implements MoveObject,Function{ private int index = 0;//播放动画时用来控制图片数组下标 private boolean flag = true;//true就是没被踩过,踩过就false public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } public BoardFlip(){//构造 Random rnd = new Random(); this.image = Game.board_e[0];//图片设为图片数组中第一个图 this.width = this.image.getWidth();//图片宽度 this.height = this.image.getHeight();//图片高度 this.x = rnd.nextInt(Game.WIDTH-this.width-10);//横坐标在游戏框范围类随机 this.y = Game.HEIGHT;//纵坐标为窗体的高度(正好在窗体之外) } @Override public void move() { this.y -= ySpeed; } /** * 这个方法每个功能性方块都差不多,只解释一次 */ public void flash(){ if(this.index==6){//如果图片播放到最后一个 this.image = Game.board_e[0];//重新把图片设为开始的图片 this.index = 0;//重置index this.flag = true;//台阶改为正常(没被踩过,总不能踩一次鬼畜一天吧) }else{//如果图片还没播放完 this.image = Game.board_e[++this.index];//播放下一个图片 } } /** * 执行功能 */ @Override public void function(Javaman man) { man.flipSpeed = -5;//给个向上的速度 this.flag = false;//说明这个方块被踩了,执行动画,动画完了再改回true } } 复制代码 复制代码 1 package com.tarena.downwell; 2 3 import java.util.Random; 4 /** 5 * 传送带(往左) 6 * @author Misaki 7 * 8 */ 9 public class BoardMoveL extends Board implements MoveObject,Function{ 10 private int index = 0; 11 12 public BoardMoveL(){//见BoradFlip 13 Random rnd = new Random(); 14 this.image = Game.board_b[0]; 15 this.width = this.image.getWidth(); 16 this.height = this.image.getHeight(); 17 this.x = rnd.nextInt(Game.WIDTH-this.width-10); 18 this.y = Game.HEIGHT; 19 } 20 @Override 21 public void move() {//传送带的动画是从头播放到尾的,就让它随着台阶向上移动一起动吧 22 if(this.index==3){//这个跟BoradFlip里flash结构一样 23 this.image = Game.board_b[0]; 24 this.index = 0; 25 }else{ 26 this.image = Game.board_b[++this.index]; 27 } 28 this.y -= ySpeed; 29 } 30 /** 31 * 给个往左的速度,区分与按键盘给的速度,最后叠加 32 */ 33 @Override 34 public void function(Javaman man) { 35 man.moveSpeed = -1; 36 } 37 //下面的都用不到,只是实现接口必须重写。(好吧我设计也有点问题) 38 @Override 39 public void flash() { 40 41 } 42 @Override 43 public boolean isFlag() { 44 return false; 45 } 46 @Override 47 public void setFlag(boolean flag) { 48 49 } 50 51 } 复制代码 复制代码 1 package com.tarena.downwell; 2 3 import java.util.Random; 4 /** 5 * 传送带(往右) 6 * @author Misaki 7 * 8 */ 9 public class BoardMoveR extends Board implements MoveObject,Function{ 10 private int index = 0; 11 12 public BoardMoveR(){//见BoradFlip 13 Random rnd = new Random(); 14 this.image = Game.board_c[0]; 15 this.width = this.image.getWidth(); 16 this.height = this.image.getHeight(); 17 this.x = rnd.nextInt(Game.WIDTH-this.width-10); 18 this.y = Game.HEIGHT; 19 } 20 @Override 21 public void move() {//见BoardMoveL 22 if(this.index==3){ 23 this.image = Game.board_c[0]; 24 this.index = 0; 25 }else{ 26 this.image = Game.board_c[++this.index]; 27 } 28 this.y -= ySpeed; 29 } 30 @Override 31 public void function(Javaman man) {//见BoardMoveL 32 man.moveSpeed = 1; 33 } 34 @Override 35 public void flash() { 36 // TODO Auto-generated method stub 37 38 } 39 @Override 40 public boolean isFlag() { 41 // TODO Auto-generated method stub 42 return false; 43 } 44 @Override 45 public void setFlag(boolean flag) { 46 // TODO Auto-generated method stub 47 48 } 49 } 复制代码 复制代码 1 package com.tarena.downwell; 2 3 import java.util.Random; 4 /** 5 * 钉子台阶 6 * @author Misaki 7 * 8 */ 9 public class BoardNail extends Board implements MoveObject,Function{ 10 private boolean flag = true; 11 public BoardNail(){//见BoradFlip 12 Random rnd = new Random(); 13 this.image = Game.board_f; 14 this.width = this.image.getWidth(); 15 this.height = this.image.getHeight(); 16 this.x = rnd.nextInt(Game.WIDTH-this.width-10); 17 this.y = Game.HEIGHT; 18 } 19 20 @Override 21 public void move() { 22 this.y -= ySpeed; 23 } 24 25 @Override 26 public void function(Javaman man) { 27 if(flag){//如果没被踩过 28 man.subLife();//减命 29 flag = false;//改为踩过 30 } 31 } 32 33 @Override 34 public void flash() { 35 // TODO Auto-generated method stub 36 37 } 38 39 @Override 40 public boolean isFlag() { 41 // TODO Auto-generated method stub 42 return false; 43 } 44 45 @Override 46 public void setFlag(boolean flag) { 47 // TODO Auto-generated method stub 48 49 } 50 } 复制代码 复制代码 package com.tarena.downwell; import java.util.Random; /** * 普通台阶 * @author Misaki * */ public class BoardNormal extends Board implements MoveObject{ public BoardNormal(){//见BoradFlip Random rnd = new Random(); this.image = Game.board_a; this.width = this.image.getWidth(); this.height = this.image.getHeight(); this.x = rnd.nextInt(Game.WIDTH-this.width-10); this.y = Game.HEIGHT; } /** * 随机产生一个普通方块,只是为了init() * @return */ public static BoardNormal randomOne(){ BoardNormal b = new BoardNormal(); Random rnd = new Random(); b.y -= rnd.nextInt(800);//设为800是让它一开始就在游戏框里 return b; } @Override public void move() { this.y -= ySpeed; } } 复制代码 复制代码 1 package com.tarena.downwell; 2 3 import java.util.Random; 4 import java.util.Timer; 5 import java.util.TimerTask; 6 /** 7 * 易碎台阶(踩上去过一会就掉下去) 8 * @author Misaki 9 * @version 1.1 10 */ 11 public class BoardOnce extends Board implements MoveObject,Function{ 12 private int index = 0; 13 private boolean flag = true; 14 15 16 public boolean isFlag() { 17 return flag; 18 } 19 public void setFlag(boolean flag) { 20 this.flag = flag; 21 } 22 public BoardOnce(){//见BoradFlip 23 Random rnd = new Random(); 24 this.image = Game.board_d[0]; 25 this.width = this.image.getWidth(); 26 this.height = this.image.getHeight(); 27 this.x = rnd.nextInt(Game.WIDTH-this.width-10); 28 this.y = Game.HEIGHT; 29 } 30 @Override 31 public void move() { 32 this.y -= ySpeed; 33 } 34 public void flash(){ 35 if(this.index==5){ 36 this.image = Game.board_d[0]; 37 this.index = 0; 38 this.flag = true; 39 }else{ 40 this.image = Game.board_d[++this.index]; 41 } 42 } 43 @Override 44 public void function(Javaman man) { 45 Timer timer = new Timer(); 46 timer.schedule(new TimerTask(){ 47 48 @Override 49 public void run() { 50 man.setY(y+height);//200ms后让小人穿过去 51 flag = false;//台阶改为没被踩过 52 } 53 54 }, 200);//只有两个参数的重载。200毫秒后执行run(),只执行一次哦! 55 } 56 } 复制代码 复制代码 1 package com.tarena.downwell; 2 /** 3 * 功能性台阶接口(弹起来,传送带转起来,刺死你,穿过去) 4 * @author Misaki 5 * 6 */ 7 public interface Function { 8 void function(Javaman man);//执行功能 9 void flash();//播放专属动画 10 /* 11 * 关于Flag的get/set 12 * Flag判断这个方块功能被没被执行,或者能不能执行 13 */ 14 boolean isFlag(); 15 void setFlag(boolean flag); 16 } 复制代码 复制代码 1 package com.tarena.downwell; 2 3 import java.awt.image.BufferedImage; 4 /** 5 * 小人类 6 * @author Misaki 7 * 8 */ 9 public class Javaman { 10 private int x;//横坐标 11 private int y;//纵坐标 12 private BufferedImage image;//图片 13 private int index = 0;//用于切换图片,形成动画 14 private int width;//图片宽 15 private int height;//图片高 16 private int ySpeed;//纵向速度 17 private int xSpeed;//横向速度 18 public int flipSpeed = 0;//弹簧台阶提供的向上速度 19 public int moveSpeed = 0;//传送带提供的横向速度 20 private boolean isManOnFloor;//是否在台阶上 21 private int life = 12;//生命 22 23 public Javaman(int x, int y) { 24 super(); 25 this.x = x; 26 this.y = y; 27 this.image = Game.javaman_s; 28 this.width = this.image.getWidth(); 29 this.height = this.image.getHeight(); 30 this.xSpeed = 0; 31 this.ySpeed = 2; 32 } 33 34 public void subLife(){//生命减一 35 life--; 36 } 37 38 public void die(){//死亡 39 life = 0; 40 } 41 42 public void addLife(){ 43 this.life++; 44 } 45 46 public int getLife(){ 47 return life; 48 } 49 50 public void move(){//横向移动 51 this.image = Game.javaman_s;//默认情况设为没有移动的图片 52 if(this.x>10 && this.x+this.width0){//如果往右移 54 if(isManOnFloor){//如果在台阶上往右移 55 /* 56 * 播放台阶上右移的动画 57 */ 58 this.image = Game.javaman_r[index++]; 59 if(index>2){ 60 index = 0; 61 } 62 }else{//在空中 63 /* 64 * 播放空中右移的动画 65 */ 66 this.image = Game.javaman_rd[index++]; 67 if(index>2){ 68 index = 0; 69 } 70 } 71 }else if(xSpeed<0){//如果往左移 72 if(isManOnFloor){//如果在台阶上往左移 73 /* 74 * 播放台阶上左移的动画 75 */ 76 this.image = Game.javaman_l[index++]; 77 if(index>2){ 78 index = 0; 79 } 80 }else{//在空中 81 /* 82 * 播放空中左移的动画 83 */ 84 this.image = Game.javaman_ld[index++]; 85 if(index>2){ 86 index = 0; 87 } 88 } 89 } 90 this.x += xSpeed+moveSpeed;//键盘监听提供速度+传送带提供速度 91 }else if(this.x<=10){//在画面最左端 92 this.x++;//弹回来! 93 }else {//画面最右端 94 this.x--;//弹回来! 95 } 96 } 97 int flipFlag = 0;//控制被弹起来的情况下,向上速度减小的频率,快了弹得太低,慢了弹到顶了 98 public void drop(){ 99 if(flipSpeed!=0){//如果被弹起来 100 flipFlag++; 101 if(this.y>45){//如果没到顶 102 this.y += flipSpeed;//那就勇敢的上! 103 }else{//到顶了。。 104 flipSpeed = 0;//别上了,乖乖下去 105 // life--;//到顶了难道不减血?(注释原因为后来游戏主类写了判断减血的方法) 106 } 107 if(flipFlag%15==0){//到时间减速了,你想上天啊? 108 flipSpeed++;//负的数加加不就是减速吗? 109 } 110 }else{//没被弹起来 111 this.y += ySpeed;//正常下落吧。 112 } 113 } 114 115 public boolean isManOnFloor() { 116 return isManOnFloor; 117 } 118 119 public void setManOnFloor(boolean isManOnFloor) { 120 this.isManOnFloor = isManOnFloor; 121 } 122 123 public BufferedImage getImage() { 124 return image; 125 } 126 127 public void setImage(BufferedImage image) { 128 this.image = image; 129 } 130 131 public int getX() { 132 return x; 133 } 134 135 public void setX(int x) { 136 this.x = x; 137 } 138 139 public int getY() { 140 return y; 141 } 142 143 public void setY(int y) { 144 this.y = y; 145 } 146 147 public int getWidth() { 148 return width; 149 } 150 151 public void setWidth(int width) { 152 this.width = width; 153 } 154 155 public int getHeight() { 156 return height; 157 } 158 159 public void setHeight(int height) { 160 this.height = height; 161 } 162 163 public int getySpeed() { 164 return ySpeed; 165 } 166 167 public void setySpeed(int ySpeed) { 168 this.ySpeed = ySpeed; 169 } 170 171 public int getxSpeed() { 172 return xSpeed; 173 } 174 175 public void setxSpeed(int xSpeed) { 176 this.xSpeed = xSpeed; 177 } 178 179 180 } 复制代码 复制代码 1 package com.tarena.downwell; 2 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.Graphics; 6 import java.awt.event.KeyAdapter; 7 import java.awt.event.KeyEvent; 8 import java.awt.image.BufferedImage; 9 import java.io.IOException; 10 import java.util.ArrayList; 11 import java.util.Iterator; 12 import java.util.List; 13 import java.util.Random; 14 import java.util.Timer; 15 import java.util.TimerTask; 16 17 import javax.imageio.ImageIO; 18 import javax.swing.JFrame; 19 import javax.swing.JPanel; 20 21 /** 22 * 游戏的主类 23 * @author Misaki 24 * @version 3.2 25 * 1.将MoveObject里的常量改为负数。 26 * 2.火箭速度增大,且现在火箭根据当时小人的横坐标来生成 27 * 3.弹簧台阶现在不会连续弹起小人,即小人只有掉下来踩到台阶才会触发弹簧 28 * 4.加入游戏结束画面,游戏结束时会用图片显示下了多少层。 29 */ 30 public class Game extends JPanel{ 31 public static final int WIDTH = 500;//窗体宽度 32 public static final int HEIGHT = 900;//窗体高度 33 public static final int RUNNING = 0;//游戏运行 34 public static final int PAUSE = 1;//游戏暂停 35 public static final int START = 2;//游戏开始 36 public static final int GAME_OVER = 3;//游戏结束 37 38 private Timer timer;//定时器 39 private int intervel = 10;//定时器间隔 40 private Random rnd = new Random();//产生随机数 41 private int score = 0;//分数,即下了多少层,由于台阶上升速度一定,可以按时间增长 42 private int state = START;//游戏状态,默认为运行 43 private int level = 0; 44 45 private BoardTop boardtop = new BoardTop();//顶部尖刺 46 private Javaman man;//小人 47 private List boards = new ArrayList ();//台阶集合 48 private List apples = new ArrayList ();//苹果集合 49 private List monsters = new ArrayList ();//怪物集合 50 private List rockets = new ArrayList ();//火箭集合 51 /** 静态图片 */ 52 public static BufferedImage background; 53 public static BufferedImage pause; 54 public static BufferedImage start; 55 public static BufferedImage gameover; 56 57 public static BufferedImage[] num = new BufferedImage[10]; 58 59 public static BufferedImage apple; 60 public static BufferedImage monster; 61 public static BufferedImage rocket; 62 63 public static BufferedImage board_top; 64 public static BufferedImage board_a; 65 public static BufferedImage[] board_b = new BufferedImage[4]; 66 public static BufferedImage[] board_c = new BufferedImage[4]; 67 public static BufferedImage[] board_d = new BufferedImage[6]; 68 public static BufferedImage[] board_e = new BufferedImage[7]; 69 public static BufferedImage board_f; 70 71 public static BufferedImage[] javaman_c = new BufferedImage[3]; 72 public static BufferedImage[] javaman_l = new BufferedImage[3]; 73 public static BufferedImage[] javaman_ld = new BufferedImage[3]; 74 public static BufferedImage[] javaman_r = new BufferedImage[3]; 75 public static BufferedImage[] javaman_rd = new BufferedImage[3]; 76 public static BufferedImage javaman_s; 77 /** 静态块,加载所有图片 */ 78 static{ 79 try { 80 background = ImageIO.read(Game.class.getResource("background.gif")); 81 start = ImageIO.read(Game.class.getResource("start.gif")); 82 pause = ImageIO.read(Game.class.getResource("pause.png")); 83 gameover = ImageIO.read(Game.class.getResource("gameover.gif")); 84 85 board_top = ImageIO.read(Game.class.getResource("board/board_top.gif")); 86 board_a = ImageIO.read(Game.class.getResource("board/board_a.gif")); 87 apple = ImageIO.read(Game.class.getResource("apple.png")); 88 monster = ImageIO.read(Game.class.getResource("monster.png")); 89 rocket = ImageIO.read(Game.class.getResource("rocket.png")); 90 91 num[0] = ImageIO.read(Game.class.getResource("num/number_0.gif")); 92 num[1] = ImageIO.read(Game.class.getResource("num/number_1.gif")); 93 num[2] = ImageIO.read(Game.class.getResource("num/number_2.gif")); 94 num[3] = ImageIO.read(Game.class.getResource("num/number_3.gif")); 95 num[4] = ImageIO.read(Game.class.getResource("num/number_4.gif")); 96 num[5] = ImageIO.read(Game.class.getResource("num/number_5.gif")); 97 num[6] = ImageIO.read(Game.class.getResource("num/number_6.gif")); 98 num[7] = ImageIO.read(Game.class.getResource("num/number_7.gif")); 99 num[8] = ImageIO.read(Game.class.getResource("num/number_8.gif")); 100 num[9] = ImageIO.read(Game.class.getResource("num/number_9.gif")); 101 102 103 board_b[0] = ImageIO.read(Game.class.getResource("board/board_b1.gif")); 104 board_b[1] = ImageIO.read(Game.class.getResource("board/board_b2.gif")); 105 board_b[2] = ImageIO.read(Game.class.getResource("board/board_b3.gif")); 106 board_b[3] = ImageIO.read(Game.class.getResource("board/board_b4.gif")); 107 108 board_c[0] = ImageIO.read(Game.class.getResource("board/board_c1.gif")); 109 board_c[1] = ImageIO.read(Game.class.getResource("board/board_c2.gif")); 110 board_c[2] = ImageIO.read(Game.class.getResource("board/board_c3.gif")); 111 board_c[3] = ImageIO.read(Game.class.getResource("board/board_c4.gif")); 112 113 board_d[0] = ImageIO.read(Game.class.getResource("board/board_d1.gif")); 114 board_d[1] = ImageIO.read(Game.class.getResource("board/board_d2.gif")); 115 board_d[2] = ImageIO.read(Game.class.getResource("board/board_d3.gif")); 116 board_d[3] = ImageIO.read(Game.class.getResource("board/board_d4.gif")); 117 board_d[4] = ImageIO.read(Game.class.getResource("board/board_d5.gif")); 118 board_d[5] = ImageIO.read(Game.class.getResource("board/board_d6.gif")); 119 120 board_e[0] = ImageIO.read(Game.class.getResource("board/board_e1.gif")); 121 board_e[1] = ImageIO.read(Game.class.getResource("board/board_e2.gif")); 122 board_e[2] = ImageIO.read(Game.class.getResource("board/board_e3.gif")); 123 board_e[3] = ImageIO.read(Game.class.getResource("board/board_e4.gif")); 124 board_e[4] = ImageIO.read(Game.class.getResource("board/board_e5.gif")); 125 board_e[5] = ImageIO.read(Game.class.getResource("board/board_e6.gif")); 126 board_e[6] = ImageIO.read(Game.class.getResource("board/board_e7.gif")); 127 128 board_f = ImageIO.read(Game.class.getResource("board/board_f.gif")); 129 130 javaman_c[0] = ImageIO.read(Game.class.getResource("man/javaman_c1.gif")); 131 javaman_c[1] = ImageIO.read(Game.class.getResource("man/javaman_c2.gif")); 132 javaman_c[2] = ImageIO.read(Game.class.getResource("man/javaman_c3.gif")); 133 134 javaman_l[0] = ImageIO.read(Game.class.getResource("man/javaman_l1.gif")); 135 javaman_l[1] = ImageIO.read(Game.class.getResource("man/javaman_l2.gif")); 136 javaman_l[2] = ImageIO.read(Game.class.getResource("man/javaman_l3.gif")); 137 138 javaman_ld[0] = ImageIO.read(Game.class.getResource("man/javaman_ld1.gif")); 139 javaman_ld[1] = ImageIO.read(Game.class.getResource("man/javaman_ld2.gif")); 140 javaman_ld[2] = ImageIO.read(Game.class.getResource("man/javaman_ld3.gif")); 141 142 javaman_r[0] = ImageIO.read(Game.class.getResource("man/javaman_r1.gif")); 143 javaman_r[1] = ImageIO.read(Game.class.getResource("man/javaman_r2.gif")); 144 javaman_r[2] = ImageIO.read(Game.class.getResource("man/javaman_r3.gif")); 145 146 javaman_rd[0] = ImageIO.read(Game.class.getResource("man/javaman_rd1.gif")); 147 javaman_rd[1] = ImageIO.read(Game.class.getResource("man/javaman_rd2.gif")); 148 javaman_rd[2] = ImageIO.read(Game.class.getResource("man/javaman_rd3.gif")); 149 150 javaman_s = ImageIO.read(Game.class.getResource("man/javaman_s.gif")); 151 152 } catch (IOException e) { 153 e.printStackTrace(); 154 } 155 } 156 /** 157 * 游戏初始化 158 */ 159 public void init(){ 160 boards.clear();//清空所有台阶 161 apples.clear(); 162 monsters.clear(); 163 rockets.clear(); 164 enterFlag = isDieFlag = scoreFlag =0; 165 man = null;//重置小人 166 score = 0;//重置分数 167 level = 0; 168 state = START; 169 for(int i=0;i<5;i++){//随机产生5个台阶 170 boards.add(BoardNormal.randomOne((i+1)*100)); 171 } 172 Board b = boards.get(rnd.nextInt(5));//随便从这5个台阶中挑出一个 173 /* 174 * 创建一个小人,坐标为上边随机选取台阶的正上方 175 */ 176 man = new Javaman(b.getX()+b.getWidth()/2-javaman_s.getWidth()/2,b.getY()-javaman_s.getHeight()); 177 } 178 179 int enterFlag = 0;//控制台阶入场的频率 180 /** 181 * 台阶入场 182 */ 183 public void enterAction(){ 184 enterFlag++; 185 if(enterFlag%100==0){ 186 int index = rnd.nextInt(6); 187 /* 188 * 随机创建6个台阶中的一个 189 */ 190 switch(index){ 191 case 0 : 192 boards.add(new BoardFlip()); 193 break; 194 case 1: 195 boards.add(new BoardMoveL()); 196 break; 197 case 2: 198 boards.add(new BoardMoveR()); 199 break; 200 case 3: 201 boards.add(new BoardNail()); 202 break; 203 case 4: 204 boards.add(new BoardNormal()); 205 break; 206 case 5: 207 boards.add(new BoardOnce()); 208 break; 209 } 210 } 211 if(enterFlag%(3000+level*500)==0){//每半分钟出一个苹果奖励 212 apples.add(new Apple());//往数组里加一个新的苹果 213 } 214 215 if(enterFlag%(1000-level*100)==0){ 216 Board b = boards.get(rnd.nextInt(boards.size())); 217 int x = b.getX()+b.getWidth()/2-15; 218 int y = b.getY()-30; 219 int ox1 = b.getX(); 220 int ox2 = b.getX()+b.getWidth(); 221 monsters.add(new Monster(x,y,ox1,ox2)); 222 } 223 224 if(enterFlag%(1500-level*100)==0){ 225 rockets.add(new Rocket(man.getX())); 226 } 227 } 228 /** 229 * 判断小人是不是站在台阶上 230 */ 231 public void isManOnFloor(){ 232 man.setManOnFloor(false);//默认为小人不在台阶上 233 man.moveSpeed = 0;//将小人的横向速度重置为0 234 for(Iterator it = boards.iterator();it.hasNext();){//遍历集合 235 Board b = it.next();//取出集合元素 236 int manx = man.getX(); 237 int many = man.getY()+man.getHeight(); 238 int bx = b.getX(); 239 int by = b.getY(); 240 /* 241 * 判断小人坐标和当前取出的台阶坐标是否重合,重合则把小人改成”站在台阶上“ 242 */ 243 if(manx>bx-man.getWidth() && manx by && many it = boards.iterator();it.hasNext();){//遍历!! 290 Board b = it.next();//取! 291 int by = b.getY(); 292 int ty = boardtop.getY(); 293 /* 294 * 如果台阶高度到了顶部刺的高度 295 */ 296 if(by it = monsters.iterator();it.hasNext();){ 301 Monster m = it.next(); 302 int my = m.getY(); 303 int ty = boardtop.getY(); 304 if(my it = rockets.iterator();it.hasNext();){ 309 Rocket r = it.next(); 310 int ry = r.getY(); 311 int ty = boardtop.getY(); 312 if(ry HEIGHT){//如果掉下去了,当然是指掉到头了! 324 man.die();//死吧死吧! 325 }else if(my<=boardtop.getY()+boardtop.getHeight()){//如果碰到刺了 326 if(isDieFlag%50==0){//控制下血量下降频率,50*10ms降低一次 327 man.subLife();//小人生命减一。 328 } 329 isDieFlag++;//这个不懂???好吧直接问我 330 }else{ 331 isDieFlag = 0;//如果小人不在上面的地方,那么重置下,不然下次撞到刺可能就又要等500ms才掉血。 332 } 333 for(Iterator it = monsters.iterator();it.hasNext();){ 334 Monster a = it.next(); 335 int ax1 = a.getX(); 336 int ax2 = a.getX()+a.getWidth(); 337 int ay1 = a.getY(); 338 int ay2 = a.getY()+a.getHeight(); 339 int mx1 = man.getX(); 340 int mx2 = man.getX()+man.getWidth(); 341 int my1 = man.getY(); 342 int my2 = man.getY()+man.getHeight(); 343 if(((mx1>=ax1 && mx1<=ax2)||(mx2>=ax1 && mx2<=ax2)) 344 &&((my1>=ay1 && my1<=ay2)||(my2>=ay1 && my2<=ay2)) 345 ){ 346 man.subLife(); 347 it.remove(); 348 } 349 } 350 for(Iterator it = rockets.iterator();it.hasNext();){ 351 Rocket r = it.next(); 352 int rx1 = r.getX(); 353 int rx2 = r.getX()+r.getWidth(); 354 int ry1 = r.getY(); 355 int ry2 = r.getY()+r.getHeight(); 356 int mx1 = man.getX(); 357 int mx2 = man.getX()+man.getWidth(); 358 int my1 = man.getY(); 359 int my2 = man.getY()+man.getHeight(); 360 if(((mx1>=rx1 && mx1<=rx2)||(mx2>=rx1 && mx2<=rx2)) 361 &&((my1>=ry1 && my1<=ry2)||(my2>=ry1 && my2<=ry2)) 362 ){ 363 man.subLife(); 364 it.remove(); 365 } 366 } 367 } 368 /** 369 * 判断游戏结束了没 370 */ 371 public void gameover(){ 372 if(man.getLife()<=0){//如果生命值为0了 373 state=GAME_OVER; 374 } 375 } 376 int scoreFlag = 0;//用于计时 377 /** 378 * 得分,2秒加一层 379 */ 380 public void getScore(){ 381 scoreFlag++; 382 if(scoreFlag%200==0){//200*10ms加一层 383 score++; 384 if(score%20==0 && level<9){ 385 level++; 386 } 387 } 388 } 389 390 public void getLife(){ 391 boolean getlife = false; 392 for(Iterator it = apples.iterator();it.hasNext();){ 393 Apple a = it.next(); 394 /* 395 * 苹果和小人的四个界点坐标 396 */ 397 int ax1 = a.getX(); 398 int ax2 = a.getX()+a.getWidth(); 399 int ay1 = a.getY(); 400 int ay2 = a.getY()+a.getHeight(); 401 int mx1 = man.getX(); 402 int mx2 = man.getX()+man.getWidth(); 403 int my1 = man.getY(); 404 int my2 = man.getY()+man.getHeight(); 405 if(((mx1>=ax1 && mx1<=ax2)||(mx2>=ax1 && mx2<=ax2)) 406 &&((my1>=ay1 && my1<=ay2)||(my2>=ay1 && my2<=ay2))){ 407 getlife = true; 408 it.remove(); 409 } 410 } 411 412 if(getlife){ 413 man.addLife(); 414 } 415 416 } 417 418 public void action(){ 419 this.addKeyListener(new KeyAdapter(){//键盘监听 420 /** 421 * 键盘被按下时给小人速度 422 */ 423 @Override 424 public void keyPressed(KeyEvent e) { 425 int key = e.getKeyCode();//获取按下的键是个啥 426 switch(key){ 427 case KeyEvent.VK_LEFT://按的是方向左: 428 man.setxSpeed(-2);//给个往左移的速度 429 break; 430 case KeyEvent.VK_RIGHT://按的是方向右: 431 man.setxSpeed(2);//给个往右移的速度啊! 432 break; 433 case KeyEvent.VK_SPACE://按的是空格: 434 if(state==RUNNING){ 435 state=PAUSE; 436 }else if(state==PAUSE){ 437 state=RUNNING; 438 }else if(state==START){ 439 state=RUNNING; 440 }else if(state==GAME_OVER){ 441 init();//游戏初始化,所有台阶和小人重新生成 442 state=START; 443 } 444 break; 445 } 446 } 447 448 /** 449 * 键盘松开时小人你就憋动啦! 450 */ 451 @Override 452 public void keyReleased(KeyEvent e) { 453 man.setxSpeed(0);//小人横向速度重置为0 454 } 455 456 }); 457 /* 458 * 这两句坑了我半天,先开始没加键盘怎么按都没反应 459 * 直到加了这两句,也是从俄罗斯方块代码里看的。意思不是很了解,也懒得了解了 460 */ 461 this.setFocusable(true); 462 this.requestFocus(); 463 464 timer = new Timer();//把上面声明的那个定时器引用指个对象给他,不然会报空指针哦~ 465 timer.schedule(new TimerTask(){ 466 /** 467 * 重写run 468 */ 469 @Override 470 public void run() { 471 /* 472 * 想知道下面是干啥的你鼠标移上去呗 473 */ 474 if(state==RUNNING){ 475 enterAction(); 476 isManOnFloor(); 477 update(); 478 outOfBoundsAction(); 479 getLife(); 480 getScore(); 481 isDie(); 482 gameover(); 483 } 484 repaint();//重画下,不然你玩张静态图片啊? 485 } 486 487 }, 0, intervel);//intervel没忘吧,上面有写,计时器时间间隔,10ms一次哦 488 } 489 /** 490 * 主方法,程序的入口 491 * @param args 492 */ 493 public static void main(String[] args) { 494 JFrame frame = new JFrame("Downwell");//新建一个窗体,标题为Downwell <-啥意思?百度去! 495 Game game = new Game();//新建一个游戏 496 game.init();//第一次运行你不得初始化啊! 497 frame.add(game);//把游戏添加到窗体里去(游戏这个类是继承了JPanel了才可以添加的,别乱加别的对象哦) 498 frame.setSize(WIDTH, HEIGHT);//设置窗体宽高 499 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认关闭操作(关闭窗体则程序结束) 500 frame.setResizable(false);//设置窗体大小不能被改变(不懂就注释了这句然后运行下,用鼠标拖下窗体四角) 501 frame.setLocationRelativeTo(null);//窗体居中显示 502 frame.setVisible(true);//设置窗体可见,并提醒程序快点调用paint()方法 503 game.action();//游戏开始! 504 } 505 /** 506 * 重写paint方法 507 */ 508 public void paint(Graphics g){ 509 paintStatic(g);//画些静止不动的背景 510 paintBoard(g);//画台阶 511 paintMonster(g);//画怪物 512 paintRocket(g);//画火箭 513 paintApple(g);//画苹果 514 paintJavaman(g);//画小人 515 paintLife(g);//画小人的生命 516 paintScore(g);//画分数 517 paintState(g);//画状态 518 } 519 /* 520 * 介绍下drawImage()六个参数的重载,最后一个直接写null,只介绍前五个 521 * 第一个:要画的图片 522 * 第二,三个:画的图片从哪个坐标开始画 523 * 第四,五个:控制图片大小,按给定的宽高缩放后画出来 524 */ 525 526 527 public void paintRocket(Graphics g){ 528 for(Rocket r:rockets){ 529 g.drawImage(r.getImage(),r.getX(),r.getY(),r.getWidth(),r.getHeight(),null); 530 } 531 } 532 533 public void paintMonster(Graphics g){ 534 for(Monster m:monsters){ 535 g.drawImage(m.getImage(),m.getX(),m.getY(),m.getWidth(),m.getHeight(),null); 536 } 537 } 538 539 public void paintState(Graphics g){ 540 if(state==PAUSE){ 541 g.drawImage(pause,0,0,WIDTH,HEIGHT,null); 542 } 543 if(state==START){ 544 g.drawImage(start,0,0,WIDTH,HEIGHT,null); 545 } 546 if(state==GAME_OVER){ 547 int x = 350; 548 int temp = score; 549 g.drawImage(gameover, 0, 0, WIDTH,HEIGHT,null); 550 do{ 551 int n = temp%10; 552 g.drawImage(num[n],x,500,100,100,null); 553 temp /= 10; 554 x -= 120; 555 }while(temp>0); 556 } 557 } 558 559 public void paintApple(Graphics g){ 560 for(Apple a:apples){ 561 g.drawImage(a.getImage(),a.getX(),a.getY(),a.getWidth(),a.getHeight(),null); 562 } 563 } 564 565 public void paintScore(Graphics g){ 566 Font font = new Font(Font.SANS_SERIF,Font.BOLD,25);//设置字体,我不想查字体的参数了,直接照搬飞机大战 567 g.setColor(new Color(0xffffff));//白色的 568 g.setFont(font);//把画笔字体设置为上面创建的字体 569 g.drawString("SCORE:"+score, WIDTH-150, 25);//这个坐标是慢慢试出来的,想改就改。 570 } 571 572 public void paintLife(Graphics g){ 573 Font font = new Font(Font.SANS_SERIF,Font.BOLD,25);//设置字体,我不想查字体的参数了,直接照搬飞机大战 574 g.setColor(new Color(0xffffff));//白色的 575 g.setFont(font);//把画笔字体设置为上面创建的字体 576 g.drawString("LIFE:"+man.getLife(), 10, 25);//这个坐标是慢慢试出来的,想改就改。 577 } 578 /** 579 * 画台阶 580 * @param g 画笔,我只写一次,不需要太纠结画笔是什么 581 */ 582 public void paintBoard(Graphics g){ 583 for(Board b:boards){//增强for遍历集合 584 g.drawImage(b.getImage(), b.getX(), b.getY(), null);//画! 585 } 586 } 587 /** 588 * 画小人 589 * @param g 590 */ 591 public void paintJavaman(Graphics g){ 592 g.drawImage(man.getImage(), man.getX(), man.getY(), null);//画!! 593 } 594 /** 595 * 画背景 596 * @param g 597 */ 598 public void paintStatic(Graphics g){ 599 g.drawImage(background, 0, 0, WIDTH, HEIGHT, null);//画背景!!! 600 g.drawImage(boardtop.image, boardtop.x, boardtop.y, boardtop.width, boardtop.height, null);//画顶部那个刺!!!! 601 } 602 } 复制代码 复制代码 1 package com.tarena.downwell; 2 /** 3 * 可动台阶(除了顶部刺都算) 4 * @author Misaki 5 * 6 */ 7 public interface MoveObject { 8 final int ySpeed = 1;//每次往上移动一个像素 9 void move();//移动! 10 } 复制代码 复制代码 1 package com.tarena.downwell; 2 3 import java.awt.image.BufferedImage; 4 import java.util.Random; 5 6 public class Monster implements MoveObject{ 7 private int x; 8 private int y; 9 private int ox1;//移动时的左界限 10 private int ox2;//移动时的右界限 11 private BufferedImage image; 12 private int width; 13 private int height; 14 private int xSpeed; 15 16 public Monster(int x,int y,int ox1,int ox2){ 17 this.x = x; 18 this.y = y; 19 this.ox1 = ox1; 20 this.ox2 = ox2; 21 this.image = Game.monster; 22 this.width = 30; 23 this.height = 30; 24 this.xSpeed = -1 + new Random().nextInt(2)*2;//横向速度为-1或1 25 } 26 27 @Override 28 public void move() { 29 if(x<=ox1){ 30 xSpeed = 1; 31 } 32 if(x+width>=ox2){ 33 xSpeed = -1; 34 } 35 this.x += xSpeed; 36 this.y += ySpeed; 37 } 38 39 public int getX() { 40 return x; 41 } 42 43 public void setX(int x) { 44 this.x = x; 45 } 46 47 public int getY() { 48 return y; 49 } 50 51 public void setY(int y) { 52 this.y = y; 53 } 54 55 public int getOx1() { 56 return ox1; 57 } 58 59 public void setOx1(int ox1) { 60 this.ox1 = ox1; 61 } 62 63 public int getOx2() { 64 return ox2; 65 } 66 67 public void setOx2(int ox2) { 68 this.ox2 = ox2; 69 } 70 71 public BufferedImage getImage() { 72 return image; 73 } 74 75 public void setImage(BufferedImage image) { 76 this.image = image; 77 } 78 79 public int getxSpeed() { 80 return xSpeed; 81 } 82 83 public void setxSpeed(int xSpeed) { 84 this.xSpeed = xSpeed; 85 } 86 87 public int getWidth() { 88 return width; 89 } 90 91 public int getHeight() { 92 return height; 93 } 94 95 } 复制代码 复制代码 1 package com.tarena.downwell; 2 3 import java.awt.image.BufferedImage; 4 import java.util.Random; 5 6 public class Rocket { 7 private int x; 8 private int y; 9 private BufferedImage image; 10 private int width; 11 private int height; 12 private int ySpeed; 13 14 public Rocket(){ 15 Random rnd = new Random(); 16 this.x = rnd.nextInt(Game.WIDTH-this.width-25)+10; 17 this.y = Game.HEIGHT; 18 this.width = 50; 19 this.height = 90; 20 this.image = Game.rocket; 21 this.ySpeed = -5; 22 } 23 public Rocket(int x){ 24 this(); 25 this.x = x; 26 } 27 28 public void move(){ 29 this.y += ySpeed; 30 } 31 32 public int getX() { 33 return x; 34 } 35 36 public void setX(int x) { 37 this.x = x; 38 } 39 40 public int getY() { 41 return y; 42 } 43 44 public void setY(int y) { 45 this.y = y; 46 } 47 48 public BufferedImage getImage() { 49 return image; 50 } 51 52 public void setImage(BufferedImage image) { 53 this.image = image; 54 } 55 56 public int getWidth() { 57 return width; 58 } 59 60 public void setWidth(int width) { 61 this.width = width; 62 } 63 64 public int getHeight() { 65 return height; 66 } 67 68 public void setHeight(int height) { 69 this.height = height; 70 } 71 72 public int getySpeed() { 73 return ySpeed; 74 } 75 76 public void setySpeed(int ySpeed) { 77 this.ySpeed = ySpeed; 78 } 79 80 } 复制代码 复制代码 1 package com.tarena.downwell; 2 3 import java.awt.image.BufferedImage; 4 import java.util.Random; 5 6 public class Apple { 7 private int x; 8 private int y; 9 private BufferedImage image; 10 private int width; 11 private int height; 12 private int xSpeed; 13 private int ySpeed; 14 15 public Apple(){ 16 Random rnd = new Random(); 17 this.x = rnd.nextInt(Game.WIDTH-this.width-20)+10; 18 this.y = rnd.nextInt(Game.HEIGHT-65)+65; 19 this.width = 30; 20 this.height = 30; 21 this.image = Game.apple; 22 switch(rnd.nextInt(2)){ 23 case 0: 24 xSpeed = -2;break; 25 case 1: 26 xSpeed = 2;break; 27 } 28 switch(rnd.nextInt(2)){ 29 case 0: 30 ySpeed = -2;break; 31 case 1: 32 ySpeed = 2;break; 33 } 34 } 35 36 public void move(){ 37 if(this.x<=10){ 38 this.xSpeed = 2; 39 } 40 if(this.x>=Game.WIDTH-20){ 41 this.xSpeed = -2; 42 } 43 if(this.y<=65){ 44 this.ySpeed = 2; 45 } 46 if(this.y>=Game.HEIGHT-this.height){ 47 this.ySpeed = -2; 48 } 49 this.x += xSpeed; 50 this.y += ySpeed; 51 } 52 53 public int getX() { 54 return x; 55 } 56 public void setX(int x) { 57 this.x = x; 58 } 59 public int getY() { 60 return y; 61 } 62 public void setY(int y) { 63 this.y = y; 64 } 65 public BufferedImage getImage() { 66 return image; 67 } 68 public void setImage(BufferedImage image) { 69 this.image = image; 70 } 71 public int getxSpeed() { 72 return xSpeed; 73 } 74 public void setxSpeed(int xSpeed) { 75 this.xSpeed = xSpeed; 76 } 77 public int getySpeed() { 78 return ySpeed; 79 } 80 public void setySpeed(int ySpeed) { 81 this.ySpeed = ySpeed; 82 } 83 public int getWidth() { 84 return width; 85 } 86 public int getHeight() { 87 return height; 88 } 89 90 } 复制代码