赞
踩
游戏需要的图片在尾部进行获取。
运行结果如下:
游戏测试类,主要负责调用,进入,绘画
- package game;
-
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.util.ArrayList;
- import java.util.List;
-
- public class GameWin extends JFrame {
- //0未开始 1 运行中 2 商店 3 失败 4 胜利
- static int state;
- //存储金块,石块
- List<Object> objectList = new ArrayList<>();
- Bg bg = new Bg();
- //引入线类
- line line = new line(this);
- //引入金块类
- //Gold gold=new Gold();
- {
- //是否可以放置
- boolean isPlace = true;
- for (int i = 0; i < 11; i++) {
- double random = Math.random();
- Gold gold;//存放当前生成的金块
- if (random < 0.3) {
- gold = new GoldMini();
- } else if (random < 0.7) {
- gold = new Gold();
- } else {
- gold = new GoldPlus();
- }
-
- for (Object obj : objectList) {
- if (gold.getRec().intersects(obj.getRec())) {
- //不放置,需要重新生成
- isPlace = false;
- }
- }
- if (isPlace) {
- objectList.add(gold);
- } else {
- isPlace = true;
- i--;
- }
- }
- for (int i = 0; i < 5; i++) {
- Rock rock = new Rock();
- for (Object obj : objectList) {
- if (rock.getRec().intersects(obj.getRec())) {
- isPlace = false;
- }
- }
- if (isPlace) {
- objectList.add(rock);
- } else {
- isPlace = true;
- i--;
- }
- }
- }
- //引入新图片类
- Image offScreenImage;
- void launch() {
- //设置窗口是否可见
- this.setVisible(true);
- //设置窗口大小
- this.setSize(768, 1000);
- //设置窗口位置
- this.setLocationRelativeTo(null);
- //设置窗口标题
- this.setTitle("黄金矿工");
- //设置关闭窗口的方法
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- //创建鼠标点击事件
- addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- super.mouseClicked(e);
- switch (state) {
- case 0:
- if (e.getButton() == 3) {
- state = 1;
- bg.startTime = System.currentTimeMillis();
- }
- break;
- case 1:
- //左键编码为1
- //左右摇摆点击左键
- if (e.getButton() == 1 && line.state == 0) {
- line.state = 1;
- }
- //抓取返回,点击右键
- if (e.getButton() == 3 && line.state == 3 && Bg.waterNum > 0) {
- Bg.waterFlag = true;
- Bg.waterNum--;
- }
- break;
- case 2:
- if (e.getButton() == 1) {
- bg.shop = true;
- }
- if (e.getButton() == 3) {
- state = 1;
- bg.startTime = System.currentTimeMillis();
- }
- break;
- case 3:
- case 4:
- if (e.getButton() == 1) {
- state = 0;
- bg.reGame();
- line.reGame();
- }
- break;
- default:
- break;
- }
- }
- });
- while (true) {
- repaint();
- nextlevel();
- //设置延迟
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- //下一关
- public void nextlevel() {
- if (bg.gameTime() && state == 1) {
- if (Bg.count >= bg.goal) {
- if (Bg.level == 5) {
- state = 4;
- } else {
- state = 2;
- Bg.level++;
- }
- } else {
- state = 3;
- }
- dispose();
- GameWin gameWin = new GameWin();
- gameWin.launch();
- }
- }
- @Override
- public void paint(Graphics g) {
- //解决图片闪动问题
- offScreenImage = this.createImage(768, 1000);
- Graphics gImage = offScreenImage.getGraphics();
- bg.paintSelf(gImage);
- if (state == 1) {
- //绘制物体
- for (Object obj : objectList) {
- obj.paintSelf(gImage);
- }
- line.paintSelf(gImage);
- }
- //gold.paintSelf(gImage);
- g.drawImage(offScreenImage, 0, 0, null);
- }
-
- public static void main(String[] args) {
- //调用上面类
- GameWin gameWin = new GameWin();
- //调用类中方法
- gameWin.launch();
- }
- }
游戏实体类,父类object,该类主用于生成金块于石块的参数,然后以继承的方式去设置对应值
- package game;
-
- import java.awt.*;
-
- public class Object {
- //坐标
- int x;
- int y;
- //宽高
- int width;
- int height;
- //图片
- Image img;
- //标记,是否可移动
- boolean flag;
- //质量
- int m;
- //积分
- int count;
- //类型 1金块 2石块
- int type;
- void paintSelf(Graphics g){
- g.drawImage(img,x,y,null);
- }
-
- public int getWidth() {
- return width;
- }
- //获取矩形
- public Rectangle getRec(){
- return new Rectangle(x,y,width,height);
- }
- }
游戏实体类,金块类,该类继承object类,来设置金块的大小位置等
- package game;
-
- import java.awt.*;
-
- /*金块类*/
- public class Gold extends Object{
- /*普通金块*/
- Gold(){
- this.x=(int)(Math.random()*700);
- this.y=(int)(Math.random()*550+300);
- this.width=52;
- this.height=52;
- this.flag=false;
- this.m=30;
- this.count=4;
- this.type=1;
- this.img= Toolkit.getDefaultToolkit().getImage("imgs/gold1.gif");
- }
- }
- /*迷你金块*/
- class GoldMini extends Gold{
- GoldMini(){
- this.width=36;
- this.height=36;
- this.m=15;
- this.count=2;
- this.img=Toolkit.getDefaultToolkit().getImage("imgs/gold0.gif");
- }
- }
- /*大号金块*/
- class GoldPlus extends Gold{
- GoldPlus(){
- this.x=(int)(Math.random()*650);
- this.width=105;
- this.height=105;
- this.m=60;
- this.count=8;
- this.img=Toolkit.getDefaultToolkit().getImage("imgs/gold2.gif");
- }
- }
游戏实体类,石块类,该类继承object类,来设置金块的大小位置等
- package game;
-
- import java.awt.*;
-
- public class Rock extends Object{
- /*石块类*/
- Rock(){
- this.x=(int)(Math.random()*700);
- this.y=(int)(Math.random()*550+300);
- this.width=71;
- this.height=71;
- this.flag=false;
- this.m=50;
- this.count=1;
- this.type=2;
- this.img= Toolkit.getDefaultToolkit().getImage("imgs/rock1.png");
- }
- }
游戏实体类线类,该类设置游戏的线条长度以及判断是否勾到
- package game;
-
- import java.awt.*;
-
- public class line {
- //起点坐标
- int x=380;
- int y=180;
- //终点坐标
- int endx=500;
- int endy=500;
- //线长
- double length = 100;
- //线长最小值
- double MIN_length = 100;
- //线长最大值
- double MAX_length = 750;
- double n = 0;
- //方向
- int dir = 1;
- //状态 0 摇摆 1 抓取 2 收回 3 抓取返回
- int state;
- //钩爪图片
- Image hook = Toolkit.getDefaultToolkit().getImage("imgs/hook.png");
- GameWin frame;
- line(GameWin frame){this.frame=frame;}
- //碰撞检测,检测物体是否被抓取
- void logic(){
- for(Object obj:this.frame.objectList){
- if(endx>obj.x && endx<obj.x+obj.width
- && endy>obj.y && endy< obj.y+obj.height){
- state=3;
- obj.flag=true;
- }
- }
-
- }
- //绘制方法
- void lines(Graphics g){
- endx = (int) (x +length*Math.cos(n*Math.PI));
- endy = (int) (y +length*Math.sin(n*Math.PI));
- //设置线的颜色
- g.setColor(Color.red);
- //绘制线
- g.drawLine(x-1,y,endx-1,endy);
- g.drawLine(x,y,endx,endy);
- g.drawLine(x+1,y,endx+1,endy);
- //绘制钩爪
- g.drawImage(hook,endx-36,endy-2,null);
- }
- void paintSelf(Graphics g){
- logic();
- switch (state){
- case 0:
- //根据角度设置方向
- if(n<0.1){ dir = 1;}
- else if (n>0.9){dir = -1;}
- //设置线的旋转
- n=n+0.005*dir;
- lines(g);
- break;
- case 1:
- //伸长
- if(length<MAX_length){
- length=length+5;
- lines(g);
- }else {state=2;}
- break;
- case 2:
- //收回
- if(length>MIN_length){
- length=length-5;
- lines(g);
- }else {
- state=0;
- }
- break;
- case 3:
- int m=1;
- if(length>MIN_length){
- length=length-5;
- lines(g);
- for(Object obj:this.frame.objectList){
- if(obj.flag){
- m=obj.m;
- obj.x=endx-obj.getWidth()/2;
- obj.y=endy;
- if(length<=MIN_length){
- obj.x=-150;
- obj.y=-150;
- obj.flag=false;
- Bg.waterFlag=false;
- //加分
- Bg.count+=obj.count;
- state=0;
- }
- if (Bg.waterFlag){
- if (obj.type==1){
- m=1;
- }
- if (obj.type==2){
- obj.x=-150;
- obj.y=-150;
- obj.flag=false;
- Bg.waterFlag=false;
- state=2;
- }
- }
- }
- }
- }
- try {
- Thread.sleep(m);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- break;
-
-
-
- default:
- }
- }
- //重置线
- void reGame(){
- n=0;
- length=100;
- }
- }
游戏实体类背景类,该类用于设置背景,设置关卡数,设置过关得分项,以及药水等道具
- package game;
- import java.awt.*;
- public class Bg {
- //管卡数
- static int level=1;
- //目标得分
- int goal = level*15;
- //总分
- static int count=0;
- //药水数量
- static int waterNum=3;
- //药水状态,默认为F,T标识正在使用
- static boolean waterFlag = false;
- //开始时间
- long startTime;
- //结束时间
- long endTime;
- //药水价格
- int price = (int) (Math.random()*10);
- //是否进入商店 f 不购买
- boolean shop=false;
- //载入药水图片
- Image water = Toolkit.getDefaultToolkit().getImage("imgs/water.png");
- //设置背景图片
- Image bg = Toolkit.getDefaultToolkit().getImage("imgs/bg.jpg");
- Image bg1 = Toolkit.getDefaultToolkit().getImage("imgs/bg1.jpg");
- Image peo = Toolkit.getDefaultToolkit().getImage("imgs/peo.png");
- //传入画笔元素
- void paintSelf(Graphics g){
- g.drawImage(bg1,0,0,null);
- g.drawImage(bg,0,200,null);
- switch (GameWin.state){
- case 0:
- drawWord(g,20,Color.green,"准备开始(右键开始、左键抓取,右键快速收回)",180,400);
- break;
- case 1:
- g.drawImage(peo,310,50,null);
- //调用传参
- drawWord(g,30,Color.black,"积分:"+count,30,150);
- //绘制药水
- g.drawImage(water,450,40,null);
- drawWord(g,30,Color.black,"*"+waterNum,510,70);
- //关卡数
- drawWord(g,20,Color.black,"第"+level+"关",30,60);
- //目标积分
- drawWord(g,30,Color.black,"目标积分"+goal,30,110);
- //时间组件
- endTime = System.currentTimeMillis();
- long tim = 20-(endTime-startTime)/1000;
- drawWord(g,30,Color.black,"时间"+(tim>0?tim:0),520,150);
- break;
- case 2:
- g.drawImage(water,300,400,null);
- drawWord(g,30,Color.black,"价格:"+price,300,500);
- drawWord(g,20,Color.black,"是否购买?(左键购买)",300,550);
- if (shop){
- count=count-price;
- waterNum++;
- shop=false;
- GameWin.state=1;
- startTime=System.currentTimeMillis();
- }
- break;
- case 3:
- drawWord(g,40,Color.cyan,"失败(左键准备)",250,350);
- drawWord(g,80,Color.cyan,"积分:"+count,200,450);
- break;
- case 4:
- drawWord(g,40,Color.RED,"成功(左键准备)",250,350);
- drawWord(g,80,Color.RED,"积分:"+count,200,450);
- break;
- default:
- break;
- }
-
- }
- //t倒计时完成 f正在倒计时
- boolean gameTime(){
- long tim = (endTime-startTime)/1000;
- if (tim>20){
- return true;
- }
- return false;
- }
- //重置元素
- void reGame(){
- //管卡数
- level=1;
- //目标得分
- goal = level*15;
- //总分
- count=0;
- //药水数量
- waterNum=3;
- //药水状态,默认为F,T标识正在使用
- waterFlag = false;
- }
- //绘制字符串
- public static void drawWord(Graphics g,int size,Color color,String str,int x,int y){
- g.setColor(color);
- g.setFont(new Font("仿宋",Font.BOLD,size));
- g.drawString(str,x,y);
-
- }}
游戏需要的场景图片
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。