Java大鱼吃小鱼游戏,今天你吃了吗?
Posted 编程界明世隐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java大鱼吃小鱼游戏,今天你吃了吗?相关的知识,希望对你有一定的参考价值。
Java大鱼吃小鱼小游戏
作者简介
作者名:编程界明世隐
简介:CSDN博客专家,从事软件开发多年,精通Java、javascript,博主也是从零开始一步步把学习成长、深知学习和积累的重要性,喜欢跟广大ADC一起打野升级,欢迎您关注,期待与您一起学习、成长、起飞!
系列目录
1. Java俄罗斯方块
2. Java五子棋小游戏
3. 老Java程序员花一天时间写了个飞机大战
4. Java植物大战僵尸
5. 老Java程序员花2天写了个连连看
6. Java消消乐(天天爱消除)
7. Java贪吃蛇小游戏
8. Java扫雷小游戏
9. Java坦克大战
10. Java迷宫小游戏
引言:
前几天回乡下老家,看到小鱼在小河里游来游去,手痒就一度想下河去捞,但我女儿提醒我不要吃野生动物,这一下子让我回到了现实,原来我已经30多岁了,还真是老程序员,那既然老程序员不能去下河捞鱼,我自己做个大鱼吃小鱼的游戏不过分吧!于是它来了,它来了,它扛着电脑和 jdk 就来了。
效果图
实现思路
因为做的是简单版本的,所以思路也很简单。
- 绘制窗口。
- 创建菜单
- 创建我方鱼
- 创建敌鱼
- 键盘事件监听
- 创建主线程,重新绘制界面,并且定时创建敌鱼。
- 处理其他细
代码实现
创建窗口
首先创建一个游戏窗体类GameFrame,继承至JFrame,用来显示在屏幕上(window的对象),每个游戏都有一个窗口,设置好窗口标题、尺寸、布局等就可以。
package com.view;
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class FishFrame extends JFrame {
public int window_Width = 1180, window_Height = 640;
public FishFrame() {
setTitle("大鱼吃小鱼");//窗口标题
setSize(window_Width, window_Height);//窗口分辨率
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击关闭按钮是关闭程序
setLocationRelativeTo(null); //设置居中
setResizable(false); //不允许修改界面大小
setVisible(true);//窗口是否可以显示 true---false
}
}
创建面板容器GamePanel继承至JPanel
package com.view;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GamePanel extends JPanel{
GamePanel gamePanel=this;
private JFrame mainFrame=null;
//构造里面初始化相关参数
public GamePanel(JFrame frame){
this.setLayout(null);
mainFrame = frame;
}
}
再创建一个Main类,来启动这个窗口,用来启动。
package com.main;
import com.view.FishFrame;
import com.view.GamePanel;
public class Main {
public static void main(String[] args) {
FishFrame frame = new FishFrame();
GamePanel panel = new GamePanel(frame);
frame.add(panel);
frame.setVisible(true);//设定显示
}
}
右键执行这个Main类,窗口建出来了
绘制背景图
创建Picture 类,负责加载所有的图片,方便调用
package com.utils;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
public class Picture {
public static BufferedImage bg0,FishL,FishR;
public static List badFishImages =new ArrayList() ;
public static List badFishImages1 =new ArrayList() ;
//静态代码块---程序启动,在加载类的时候,就会执行
static {
try {
bg0 = ImageIO.read(Picture.class.getClassLoader().getResourceAsStream("img/bg0.jpg"));
FishL = ImageIO.read(Picture.class.getClassLoader().getResourceAsStream("img/fishIcon0.png"));
FishR = ImageIO.read(Picture.class.getClassLoader().getResourceAsStream("img/fishIcon0R.png"));
BufferedImage temp;
for (int i = 1; i <= 18; i++) {
temp = ImageIO.read(Picture.class.getClassLoader().getResourceAsStream("img/fishIcon"+i+".png"));
badFishImages.add(temp);
temp = ImageIO.read(Picture.class.getClassLoader().getResourceAsStream("img/fishIconL"+i+".png"));
badFishImages1.add(temp);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
在GamePanel中重写paint方法,并绘制背景图。
@Override
public void paint(Graphics g) {
super.paint(g);
//绘制背景图
g.drawImage(Picture.bg0, 0,0,null);
}
运行如下:
创建菜单
//初始化按钮
private void initMenu(){
// 创建菜单及菜单选项
JMenuBar jmb = new JMenuBar();
JMenu jm1 = new JMenu("游戏");
jm1.setFont(new Font("思源宋体", Font.BOLD, 15));// 设置菜单显示的字体
JMenuItem jmi1 = new JMenuItem("开始新游戏");
JMenuItem jmi2 = new JMenuItem("退出");
jmi1.setFont(new Font("思源宋体", Font.BOLD, 15));
jmi2.setFont(new Font("思源宋体", Font.BOLD, 15));
jm1.add(jmi1);
jm1.add(jmi2);
jmb.add(jm1);
mainFrame.setJMenuBar(jmb);// 菜单Bar放到JFrame上
jmi1.addActionListener(this);
jmi1.setActionCommand("Restart");
jmi2.addActionListener(this);
jmi2.setActionCommand("Exit");
}
此时直接把这个代码加入到GamePanel中,发现是会报错的,需要实现ActionListener,并重写actionPerformed 方法。
此时GamePanel的代码如下:
package com.view;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
import com.utils.Picture;
public class GamePanel extends JPanel implements ActionListener{
GamePanel gamePanel=this;
private JFrame mainFrame=null;
//构造里面初始化相关参数
public GamePanel(JFrame frame){
this.setLayout(null);
mainFrame = frame;
//菜单
initMenu();
}
@Override
public void paint(Graphics g) {
super.paint(g);
//绘制背景图
g.drawImage(Picture.bg0, 0,0,null);
}
//初始化按钮
private void initMenu(){
// 创建菜单及菜单选项
JMenuBar jmb = new JMenuBar();
JMenu jm1 = new JMenu("游戏");
jm1.setFont(new Font("思源宋体", Font.BOLD, 15));// 设置菜单显示的字体
JMenuItem jmi1 = new JMenuItem("开始新游戏");
JMenuItem jmi2 = new JMenuItem("退出");
jmi1.setFont(new Font("思源宋体", Font.BOLD, 15));
jmi2.setFont(new Font("思源宋体", Font.BOLD, 15));
jm1.add(jmi1);
jm1.add(jmi2);
jmb.add(jm1);
mainFrame.setJMenuBar(jmb);// 菜单Bar放到JFrame上
jmi1.addActionListener(this);
jmi1.setActionCommand("Restart");
jmi2.addActionListener(this);
jmi2.setActionCommand("Exit");
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("宋体", Font.ITALIC, 18)));
UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("宋体", Font.ITALIC, 18)));
if ("Exit".equals(command)) {
Object[] options = { "确定", "取消" };
int response = JOptionPane.showOptionDialog(this, "您确认要退出吗", "",
JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null,
options, options[0]);
if (response == 0) {
System.exit(0);
}
}else if("Restart".equals(command)){
restart();
}
}
//重新开始
private void restart() {
}
}
创建我方鱼
package com.model;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import com.utils.Dir;
import com.utils.Picture;
import com.view.GamePanel;
public class Fish extends ArrayList<Fish> {
private int x, y,/*主角鱼出生坐标*/
width = 50, height = 30,
speed = 10;
private Dir dir;
private boolean isMoving = false, isLiving = true;
private GamePanel panel;//相互持有
private String group;
private Graphics g;
private BufferedImage image=Picture.FishL;
private int type = 1;//1 右, -1 左
public Fish(int x, int y, Dir dir, GamePanel panel, String group) {
this.x = x;
this.y = y;
this.dir = dir;
this.panel = panel;
this.group = group;
}
public void paint(Graphics g) {
this.g = g;
switch (dir) {
case UP:
image = type==1?Picture.FishR:Picture.FishL;
break;
case DOWN:
image = type==1?Picture.FishR:Picture.FishL;
break;
case LEFT:
type = -1;
image = type==1?Picture.FishR:Picture.FishL;
break;
case RIGHT:
type = 1;
image = type==1?Picture.FishR:Picture.FishL;
break;
}
g.drawImage(image, x, y,width,height, null);
}
public void setDir(Dir dir) {
this.dir = dir;
}
public void setMoving(boolean moving) {
isMoving = moving;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public boolean isLiving() {
return isLiving;
}
public void setLiving(boolean living) {
isLiving = living;
}
public int size() {
return 0;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
问题里的态度