在做一个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 = randomChar(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;
}
}
Game.java
Game类确实太长了,我有罪
在文本文件里
链接:https://pan.baidu.com/s/1KyD04_E_kiQwf5RrpiCiwg?pwd=wqaq
提取码:wqaq
里面顺便还有要用的图片
GameFrame.java
import java.awt.*;
import javax.swing.*;
public class GameFrame extends JFrame{
public GameFrame() throws Exception {
JFrame frame = new JFrame("小刻找蜜饼");
Game game = new Game();
frame.add(game);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
game.start();
}
// public static void main(String[] args) throws Exception {
// GameFrame g = new GameFrame();
// }
}
以上就是游戏的总代码,把GameFrame里main的注释去掉,附上图片运行还是能跑的
图片在这:https://pan.baidu.com/s/1IHhbBxMzkxCeIviv8awuKg?pwd=wqaq
提取码:wqaq
然后长这样
至于开始跳转的界面就随便写一个先吧
import java.awt.event.ActionEvent;
import javax.swing.*;
public class test extends JFrame{
JFrame frame;
JButton b;
test(){
frame = new JFrame("test");
b = new JButton("跳转");
setLayout(null);
b.setSize(60, 60);
b.addActionListener(e -> {
try {
btn_ActionPerformed(e);
} catch (Exception e1) {
e1.printStackTrace();
}
});
frame.getContentPane().add(b);
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void btn_ActionPerformed(ActionEvent event) throws Exception {
frame.setVisible(false);
GameFrame gf = new GameFrame();
}
public static void main(String[] args) {
test t =new test();
}
}
点击跳转按钮就直接白了,点右上角的退出也退出不了,就很懵
不应该只是JFrame之间的跳转吗,JPanel的功能在JFrame中也没变化啊,就很不理解
就想问一下我是哪里错了导致继承于JPanel的Game加载不出来
然后怕有些佬找不到上面的链接再放一次
图片在这:https://pan.baidu.com/s/1IHhbBxMzkxCeIviv8awuKg?pwd=wqaq
提取码:wqaq
感谢佬们