在做一个Java小游戏的时候遇到的问题,困扰了几天
知道自己的代码存在很严重的代码复制现象,但是现在还是先解决目前的问题吧
做的是一个叫“小刻找蜜饼”的小游戏
Ceobe.java
-
import java.awt.*;
-
import java.awt.geom.Ellipse2D;
-
import java.awt.image.*;
-
import java.io.*;
-
import javax.imageio.*;
-
public class Ceobe {
-
BufferedImage ceobeImg;
-
int x;
-
int y;
-
int width;
-
int height;
-
int size;
-
double g;
-
double t;
-
double v0;
-
double v;
-
double s;
-
double angle;
-
public Ceobe() throws Exception{
-
cutCircleImage();
-
ceobeImg = ImageIO.read( new File("res/ceobe1.png"));
-
x = 180;
-
y = 250;
-
width = ceobeImg.getWidth();
-
height = ceobeImg.getHeight();
-
size = 40;
-
g = 4;
-
t = 0.25;
-
v0 = 20;
-
v = v0;
-
s = 0;
-
angle = 0;
-
}
-
public void moivng() {
-
double v0 = v;
-
s = v0 * t + g * t * t / 2;
-
y = y - ( int) s;
-
double next_v = v0 - g * t;
-
v = next_v;
-
angle = Math.atan(s / 8);
-
}
-
public void recovering_v0() {
-
v = v0;
-
}
-
public boolean pick_cake(Cake c, char ch) {
-
if((y > c.y - c.size / 2 - size / 2)
-
&& (y < c.y)) {
-
if((x > c.x - size / 2 - c.size / 2)
-
&& (x < c.x + size / 2 + c.size / 2)) {
-
if(ch == c.character1) {
-
c.img = c.deleteImg;
-
c.character1 = c.deletecharacter;
-
c.x = c.deletex;
-
c.y = c.deletey;
-
Game.score += 100 / Char.poetry.length();
-
return true;
-
} else {
-
Game.state = Game.GAME_OVER;
-
}
-
}
-
return false;
-
}
-
return false;
-
}
-
public boolean exsitFrame() {
-
if(y > 600 - height / 2 + 10) {
-
return true;
-
}
-
if(y < -height / 2 - 10) {
-
return true;
-
}
-
return false;
-
}
-
public void cutCircleImage() {
-
try {
-
BufferedImage b_img1 = ImageIO.read(new File("res/ceobe.png"));
-
BufferedImage b_img2 = new BufferedImage(b_img1.getWidth(),
-
b_img1.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
-
Ellipse2D. Double shape = new Ellipse2D.Double(0, 0,
-
b_img1.getWidth(), b_img1.getHeight());
-
Graphics2D g2 = b_img2.createGraphics();
-
g2.setClip(shape);
-
g2.setBackground( null);
-
g2.draw( new Rectangle(b_img2.getWidth(), b_img2.getHeight()));
-
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
-
(RenderingHints.VALUE_TEXT_ANTIALIAS_GASP));
-
g2.drawImage(b_img1, 0, 0, null);
-
g2.dispose();
-
ImageIO.write(b_img2, "png", new File("res/ceobe1.png"));
-
} catch(IOException e) {
-
e.printStackTrace();
-
}
-
}
-
}
Cale.java
-
import java.awt.image.*;
-
import java.io.*;
-
import javax.imageio.*;
-
public class Cake {
-
BufferedImage cakeImg;
-
BufferedImage deleteImg;
-
BufferedImage img;
-
int x;
-
int deletex;
-
int y;
-
int deletey;
-
int width;
-
int height;
-
int size;
-
int distance;
-
int gap;
-
char character;
-
char deletecharacter;
-
char character1;
-
public Cake(int i, int j, char c) throws Exception{
-
character = c;
-
deletecharacter = ' ';
-
character1 = character;
-
cakeImg = ImageIO. read(new File("res/cake.png"));
-
deleteImg = ImageIO. read(new File("res/delete.png"));
-
img = cakeImg;
-
size = 40;
-
distance = 280;
-
gap = 40;
-
width = cakeImg.getWidth();
-
height = cakeImg.getHeight();
-
x = 680 + (i - 1) * distance;
-
deletex = 0;
-
y = 25 + 33 + cakeImg.getHeight() / 2 + j * (gap + height);
-
deletey = 0;
-
}
-
public void moving() {
-
x --;
-
}
-
}
Char.java
-
import java.util.ArrayList;
-
import java.util.Random;
-
public class Char {
-
int x;
-
int y;
-
int distance;
-
int gap;
-
static String poetry;
-
char [] character;
-
char [] newcharacter;
-
Random r = new Random();
-
int n;
-
public Char(int num) {
-
//初始化信息
-
poetry = "春眠不觉晓处处闻啼鸟夜来风雨声花落知多少";
-
character = new char[5];
-
character [4] = poetry.charAt(num);
-
n = 0;
-
ArrayList<Character> ch = new ArrayList<>();
-
while(ch.size() < 4) {
-
int xx = r.nextInt(poetry.length());
-
if(poetry.charAt(xx) != character[4]) {
-
if (!ch.contains(poetry.charAt(xx))) {
-
ch.add(poetry. charAt(xx));
-
character [n] = poetry.charAt(xx);
-
n++;
-
}
-
}
-
}
-
newcharacter = random Char(character);
-
}
-
public static char[] randomChar(char [] c) {
-
int length = c.length;
-
ArrayList<Integer> randomList = new ArrayList<>();
-
while (randomList.size() < length) {
-
int x = new Random().nextInt(length);
-
if (!randomList.contains(x)) {
-
randomList.add(x);
-
}
-
}
-
char[] randomArray = new char[length];
-
for (int i = 0; i < randomArray.length; i++) {
-
randomArray [i] = c[randomList.get(i)];
-
}
-
return randomArray;
-
}
-
}