本文主要是根据我小时候玩的一款游戏——黄金矿工,来进行创新的游戏项目编写过程,内容较多,所以分为多篇。
package com.sxt;
import javax.swing.*;
public class GameWin extends JFrame {
void launch(){
this.setVisible(true);
this.setSize(500,500);
this.setLocationRelativeTo(null);
this.setTitle("AileenGoldMiner");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
GameWin gameWin = new GameWin();
gameWin.launch();
}
}
package com.sxt;
import javax.swing.*;
import java.awt.*;
public class GameWin extends JFrame {
Bg bg = new Bg();
void launch(){
this.setVisible(true);
this.setSize(768,1000);
this.setLocationRelativeTo(null);
this.setTitle("AileenGoldMiner");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
@Override
public void paint(Graphics g){
bg.paintSelf(g);
}
public static void main(String[] args) {
GameWin gameWin = new GameWin();
gameWin.launch();
}
}
package com.sxt;
import java.awt.*;
public class Bg {
Image bg = Toolkit.getDefaultToolkit().getImage("C://Users//admin//IdeaProjects//The Gold Miner//imgs//bg.jpg");
Image bg1 = Toolkit.getDefaultToolkit().getImage("C://Users//admin//IdeaProjects//The Gold Miner//imgs//bg1.jpg");
Image usaqi = Toolkit.getDefaultToolkit().getImage("C://Users//admin//IdeaProjects//The Gold Miner//imgs//usaqi.gif");
void paintSelf(Graphics g){
g.drawImage(bg1,0,0,null);
g.drawImage(bg,0,200,null);
g.drawImage(usaqi,310,50,null);
}
}
package com.sxt;
import java.awt.*;
public class Line {
//起点坐标
int x = 380;
int y = 180;
//终点坐标
int endx = 500;
int endy = 500;
void paintSelf(Graphics g){
g.setColor(Color.red);
g.drawLine(x,y,endx,endy);
}
}
package com.sxt;
import javax.swing.*;
import java.awt.*;
public class GameWin extends JFrame {
Bg bg = new Bg();
Line line = new Line();
void launch(){
this.setVisible(true);
this.setSize(768,1000);
this.setLocationRelativeTo(null);
this.setTitle("AileenGoldMiner");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
@Override
public void paint(Graphics g){
bg.paintSelf(g);
line.paintSelf(g);
}
public static void main(String[] args) {
GameWin gameWin = new GameWin();
gameWin.launch();
}
}
package com.sxt;
import java.awt.*;
public class Line {
//起点坐标
int x = 380;
int y = 180;
//终点坐标
int endx = 500;
int endy = 500;
//线长
double length = 100;
//为了使其从x轴右边开始转动,所以n设为0
double n = 0;
//红线摆动方向限定 -> 1表示角度增加往左边移动,-1表示角度减小往右边移动
int dir = 1;
void paintSelf(Graphics g){
//红线摆动范围限定 ->根据之前限定可知n的范围是(0-1),
// 所以n最小我们可以设为0.1,最大可设为0.9.
if(n<0.01){
dir = 1;
} else if (n > 0.9) {
dir = -1;
}
n = n + 0.005*dir;
endx = (int) (x + length*Math.cos(n*Math.PI)); //因为角度是double类型所以要强制转换成int类型
endy = (int) (y + length*Math.sin(n*Math.PI));
g.setColor(Color.red);
g.drawLine(x,y,endx,endy);
}
}
package com.sxt;
import javax.swing.*;
import java.awt.*;
public class GameWin extends JFrame {
Bg bg = new Bg();
Line line = new Line();
void launch(){
this.setVisible(true);
this.setSize(768,1000);
this.setLocationRelativeTo(null);
this.setTitle("AileenGoldMiner");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
while(true){
repaint();
//限制摆动速度
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
@Override
public void paint(Graphics g){
bg.paintSelf(g);
line.paintSelf(g);
}
public static void main(String[] args) {
GameWin gameWin = new GameWin();
gameWin.launch();
}
}
package com.sxt;
import java.awt.*;
public class Line {
//起点坐标
int x = 380;
int y = 180;
//终点坐标
int endx = 500;
int endy = 500;
//线长
double length = 100;
//为了使其从x轴右边开始转动,所以n设为0
double n = 0;
//红线摆动方向限定 -> 1表示角度增加往左边移动,-1表示角度减小往右边移动
int dir = 1;
//状态: 0->摇摆 1->抓取 2->收回
int state;
void paintSelf(Graphics g){
//线的状态设置
switch(state){
case 0://左右摇摆
if (n < 0.1) {dir = 1;}
else if (n > 0.9){dir = -1;}
n = n + 0.005*dir;
endx = (int) (x + length*Math.cos(n*Math.PI)); //因为角度是double类型所以要强制转换成int类型
endy = (int) (y + length*Math.sin(n*Math.PI));
g.setColor(Color.red);
g.drawLine(x,y,endx,endy);
break;
case 1://延长线长度
if(length < 500){//如果线长小于500就可以延长
length = length + 10;
endx = (int) (x + length*Math.cos(n*Math.PI)); //因为角度是double类型所以要强制转换成int类型
endy = (int) (y + length*Math.sin(n*Math.PI));
g.setColor(Color.red);
g.drawLine(x,y,endx,endy);
} else {state = 2 ;}//延长后无其他操作,可以将其状态置为0,让它继续执行左右摇摆以及红线的绘制
break;
case 2://红线收回
if(length > 100){
length = length - 10;
endx = (int) (x + length*Math.cos(n*Math.PI)); //因为角度是double类型所以要强制转换成int类型
endy = (int) (y + length*Math.sin(n*Math.PI));
g.setColor(Color.red);
g.drawLine(x,y,endx,endy);
}else{
state = 0;
}
}
}
}
void lines(Graphics g){
endx = (int) (x + length*Math.cos(n*Math.PI)); //因为角度是double类型所以要强制转换成int类型
endy = (int) (y + length*Math.sin(n*Math.PI));
g.setColor(Color.red);
g.drawLine(x,y,endx,endy);
}
void lines(Graphics g){
endx = (int) (x + length*Math.cos(n*Math.PI)); //因为角度是double类型所以要强制转换成int类型
endy = (int) (y + length*Math.sin(n*Math.PI));
g.setColor(Color.red);
g.drawLine(x,y,endx,endy);
}
void paintSelf(Graphics g){
//线的状态设置
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 < 500){//如果线长小于500就可以延长
length = length + 10;
lines(g);
} else {state = 2 ;}//延长后无其他操作,可以将其状态置为0,让它继续执行左右摇摆以及红线的绘制
break;
case 2://红线收回
if(length > 100){
length = length - 10;
lines(g);
}else{
state = 0;
}
}
Object
类作为他们的父类实现代码
package com.sxt;
import java.awt.*;
/**
* 石块和金块的父类,用于抽取共性
*/
public class Object {
//坐标
int x;
int y;
//宽高
int width;
int height;
//图片
Image img;
void paintSelf(Graphics g){
g.drawImage(img,x,y,null);
}
}
package com.sxt;
import java.awt.*;
public class Gold extends Object{
Gold(){
this.x = 300;
this.y = 500;
this.width = 32;
this.height = 52;
this.img = Toolkit.getDefaultToolkit().getImage("C://Users//admin//IdeaProjects//The Gold Miner//imgs//gold1.gif");
}
}
package com.sxt;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class GameWin extends JFrame {
Bg bg = new Bg();
Line line = new Line();
Gold gold = new Gold();
void launch(){
this.setVisible(true);
this.setSize(768,1000);
this.setLocationRelativeTo(null);
this.setTitle("AileenGoldMiner");
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
if(e.getButton() == 1){
line.state=1;
}
}
});
while(true){
repaint();
//限制摆动速度
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
@Override
public void paint(Graphics g){
bg.paintSelf(g);
line.paintSelf(g);
gold.paintSelf(g);
}
public static void main(String[] args) {
GameWin gameWin = new GameWin();
gameWin.launch();
}
}
package com.sxt;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class GameWin extends JFrame {
Bg bg = new Bg();
Line line = new Line();
Gold gold = new Gold();
//定义一个画布
Image offScreenImage;
void launch(){
this.setVisible(true);
this.setSize(768,1000);
this.setLocationRelativeTo(null);
this.setTitle("AileenGoldMiner");
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
if(e.getButton() == 1){
line.state=1;
}
}
});
while(true){
repaint();
//限制摆动速度
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
@Override
public void paint(Graphics g){
//定义画布大小,使其与窗口大小一致
offScreenImage = this.createImage(768,1000);
Graphics gImage = offScreenImage.getGraphics();
//将背景,人物,金块依次画入到画布中
bg.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();
}
}
现在我们的537,就不会闪动啦~
x加上宽度
y加上高度
endx
和endy
是否在这个区域当中。 //判断红线的终点是否在金块矩形的范围内
void logic(){
if (endx > this.frame.gold.x && endx < this.frame.gold.x+this.frame.gold.width
&& endy > this.frame.gold.y && endy < this.frame.gold.y+this.frame.gold.height){
System.out.println(1);
}
}
//线的状态设置
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 < 500){//如果线长小于500就可以延长
length = length + 10;
lines(g);
} else {state = 2 ;}//延长后无其他操作,可以将其状态置为0,让它继续执行左右摇摆以及红线的绘制
break;
case 2://红线收回
if(length > 100){
length = length - 10;
lines(g);
}else{
state = 0;
}
break;
case 3://红线碰到晶块,红线返回的情况
if(length > 100){
length = length - 10;
lines(g);
//金块偏移
this.frame.gold.x = endx - 26;
this.frame.gold.y = endy;
}else {
//金块移除 - >将金块移动到屏幕外
this.frame.gold.x = -150;
this.frame.gold.y = -150;
state = 0;
}
break;
}