《游戏学习》Java实现的开⼼农场简易版源码以及素材【游戏介绍】
该游戏是由java实现的开⼼农场简易版,主要功能包含 播种、⽣长、开花、结果、收获五个功能操作。
项⽬代码有4个java类和图⽚素材实现
BackgroundPanel类 背景⾯板对象
Crop类 处理图⽚
Farm类 农场对象
MainFrame类 游戏主⾯板
【项⽬结构】
【项⽬代码】
农场 Farm 类
import java.io.Serializable;
import java.util.Date;
public class Farm implements Serializable {
private static final long serialVersionUID = 1L;
public int state = 0;
public Date lastTime = null;
public String seed(Crop crop,String picture){
String returnValue = "";
if(state == 0){
crop.setIcon(picture);
state = 1;
lastTime = new Date();
}else{
returnValue = getMessage()+",不能播种";
}
return returnValue;
}
public String grow(Crop crop,String picture){
String returnValue = "";
String returnValue = "";
if(state == 1){
crop.setIcon(picture);
state = 2;
lastTime = new Date();
}else{
returnValue = getMessage()+",不能⽣长";
}
return returnValue;
}
public String bloom(Crop crop,String picture){ String returnValue = "";
if(state == 2){
crop.setIcon(picture);
state = 3;
lastTime = new Date();
}else{
returnValue = getMessage()+",不能开花";
}
return returnValue;
}
public String fruit(Crop crop,String picture){
String returnValue = "";
if(state == 3){
crop.setIcon(picture);
state = 4;
lastTime = new Date();
}else{
returnValue = getMessage()+",不能结果";
}
return returnValue;
}
public String harvest(Crop crop,String picture){ String returnValue="";
if(state==4){
crop.setIcon(picture);
state = 0;
lastTime = null;
}else{
returnValue = getMessage()+",不能收获!";
}
return returnValue;
}
public String getMessage() {
String message = "";
switch(state){
case 0:
message = "作物还没有播种";
break;
case 1:
message = "作物刚刚播种";
break;
case 2:
message = "作物正在⽣长";
break;
break;
case 3:
message = "作物正处于开花期";
break;
case 4:
message = "作物已经结果";
break;
}
return message;
}
}
启动类 MainFrame
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
SimpleDateFormat;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
public class MainFrame extends JFrame implements MouseListener,Runnable{
int fruitNumber = 0;
Farm[] farms = new Farm[9];
Crop[] crops = new Crop[9];
Farm farm = new Farm();
Crop crop = new Crop();
JLabel storage;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
FileInputStream fis = null;
FileOutputStream fos = null;
DataInputStream dis = null;
DataOutputStream dos = null;
//⽣长期1⼩时,开花期2⼩时,结果期3⼩时
public static final long GROWTIME = 1000*60,BLOOMTIME = 1000*60*2,FRUITTIME = 1000*60*3;
public MainFrame(){
super();
setTitle("打造⾃⼰的开⼼农场");
setBounds(500,200,900,600);//设置窗⼝位置和⼤⼩
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(this);
addMouseListener(this);
final BackgroundPanel backgroundPanel = new BackgroundPanel();
Image bk = DefaultToolkit().getImage("img/farmBackground.png");
backgroundPanel.setImage(bk);
backgroundPanel.setBounds(0,0,855,553);//设置游戏⾯板位置和⼤⼩
getContentPane().add(backgroundPanel);
简单的java游戏代码storage = new JLabel();
storage.setHorizontalAlignment(SwingConstants.CENTER);
storage.setText("您的仓库没有任何果实,快快播种吧!");
storage.setBounds(200,70,253,28);//标签位置
backgroundPanel.add(storage);
initlize();
for(Crop temp:crops){
backgroundPanel.add(temp);
}
final JButton button_1 = new JButton();//播种
button_1.setRolloverIcon(new ImageIcon("img/播种1.png"));//移动到图标上显⽰的图⽚ button_1.setBorderPainted(false);
button_1.setContentAreaFilled(false);
button_1.setIcon(new ImageIcon("img/播种.png"));//正常显⽰图⽚
button_1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(farm == null)//如果没有先选中⼟地
return ;
String message = farm.seed(crop, "img/seed.png");
if(!message.equals("")){
JOptionPane.showMessageDialog(null, message);
}
}
});
button_1.setBounds(140,477,56,56);//29, 185, 56, 56
backgroundPanel.add(button_1);
final JButton button_2 = new JButton();//⽣长
button_2.setContentAreaFilled(false);
button_2.setBorderPainted(false);
button_2.setRolloverIcon(new ImageIcon("img/⽣长1.png"));
button_2.setIcon(new ImageIcon("img/⽣长.png"));
button_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(farm == null)//如果没有先选中⼟地
return ;
String message = w(crop, "img/grow.png");
if(!message.equals("")){
JOptionPane.showMessageDialog(null, message);
}
}
});
backgroundPanel.add(button_2);
button_2.setBounds(280,477,56,56);//114,185,56,56
final JButton button_3 = new JButton();
button_3.setContentAreaFilled(false);
button_3.setBorderPainted(false);
button_3.setRolloverIcon(new ImageIcon("img/开花1.png"));
button_3.setIcon(new ImageIcon("img/开花.png"));
button_3.addActionListener(new ActionListener() {
button_3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(farm == null)//如果没有先选中⼟地
return ;
String message = farm.bloom(crop, "img/bloom.png");
if(!message.equals("")){
JOptionPane.showMessageDialog(null, message);
}
}
});
backgroundPanel.add(button_3);
button_3.setBounds(420,477,56,56);//199,185,56,56;
final JButton button_4 = new JButton();
button_4.setContentAreaFilled(false);
button_4.setBorderPainted(false);
button_4.setRolloverIcon(new ImageIcon("img/结果1.png")); button_4.setIcon(new ImageIcon("img/结果.png"));
button_4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(farm == null)//如果没有先选中⼟地
return ;
String message = farm.fruit(crop,"img/fruit.png");
if(!message.equals("")){
JOptionPane.showMessageDialog(null, message);
}
}
});
backgroundPanel.add(button_4);
button_4.setBounds(560,477,56,56);
final JButton button_5 = new JButton();
button_5.setContentAreaFilled(false);
button_5.setBorderPainted(false);
button_5.setRolloverIcon(new ImageIcon("img/收获1.png")); button_5.setIcon(new ImageIcon("img/收获.png"));
button_5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(farm == null)//如果没有先选中⼟地
return ;
String message = farm.harvest(crop, "");
if(!message.equals("")){
JOptionPane.showMessageDialog(null, message);
}else{
fruitNumber++;
storage.setText("您的仓库现在有"+fruitNumber+"个果实."); }
}
});
backgroundPanel.add(button_5);
button_5.setBounds(700,477,56,56);//369,185,56,56
new Thread(this).start();
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论