package bbb;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class game1 extends JFrame implements ActionListener{
private static final long serialVersionUID=1L;
cwu A;
JPanel p1;
JPanel p2=new tupanel("p2.jpg");
JPanel p3=new tupanel("p34.jpg");
JPanel p4=new tupanel("p34.jpg");
static Font v=new Font("宋体",1,14);
JButton shop=new JButton("商店");
JButton save=new JButton("保存");
JButton money=new JButton("打工");
JButton exit=new JButton("退出");
JButton train=new JButton("训练");
JButton challeng=new JButton("挑战");
JButton hp1;
JButton hp2;
JButton hp3;
JButton milk;
JButton choco;
JButton beef;
JButton g1;
JButton g2;
JButton g3;
JButton g4;
JButton g5;
JButton g6;
JButton g7;
JButton g8;
public game1(cwu a){
super("宠物养成模块");
A=a;
String path="";
switch(a.ID){
case 0: path="0.jpg";break;
case 1: path="1.jpg";break;
case 2: path="2.jpg";break;
case 3: path="3.jpg";break;
case 4: path="4.jpg";break;
case 5: path="5.jpg";break;
case 6: path="6.jpg";break;
case 7: path="7.jpg";break;
case 8: path="8.jpg";break;
case 9: path="9.jpg";break;
default:break;
}
p1=new tupanel(path);
p2.setLayout(new GridLayout(4,2,0,0));
g1=new JButton("名字:"+a.name);
g2=new JButton("种族:"+a.Kind);
g3=new JButton("等级:"+a.Lv);
g4=new JButton("金币:"+a.Gold);
g5=new JButton("血量:"+a.Hp);
g6=new JButton("体力:"+a.power);
g7=new JButton("攻击:"+a.Atk);
g8=new JButton("防御:"+a.Def);
ZX(g1);
ZX(g2);
ZX(g3);
ZX(g4);
ZX(g5);
ZX(g6);
ZX(g7);
ZX(g8);
p2.add(g1);
p2.add(g2);
p2.add(g3);
p2.add(g4);
p2.add(g5);
p2.add(g6);
p2.add(g7);
p2.add(g8);
bbb.menu.ZT(train);
bbb.menu.ZT(challeng);
bbb.menu.ZT(shop);
bbb.menu.ZT(save);
bbb.menu.ZT(money);
bbb.menu.ZT(exit);
p3.setLayout(new GridLayout(3,2,0,0));
p3.add(train);
p3.add(challeng);
p3.add(shop);
p3.add(money);
p3.add(save);
p3.add(exit);
hp1=new JButton("小型药剂:"+A.h1);
hp2=new JButton("中型药剂:"+A.h2);
hp3=new JButton("大型药剂:"+A.h3);
milk=new JButton("牛奶:"+A.m1);
choco=new JButton("巧克力:"+A.c1);
beef=new JButton("牛肉干:"+A.b1);
p4.setLayout(new GridLayout(3,2,0,0));
ZX(hp1);
ZX(hp2);
ZX(hp3);
ZX(milk);
ZX(choco);
ZX(beef);
p4.add(hp1);
p4.add(milk);
p4.add(hp2);
p4.add(choco);
p4.add(hp3);
p4.add(beef);
shop.addActionListener(this);
save.addActionListener(this);
money.addActionListener(this);
train.addActionListener(this);
challeng.addActionListener(this);
exit.addActionListener(this);
hp1.addActionListener(this);
hp2.addActionListener(this);
hp3.addActionListener(this);
milk.addActionListener(this);
choco.addActionListener(this);
beef.addActionListener(this);
this.setLayout(new FlowLayout());
this.add(p1);
this.add(p2);
this.add(p3);
this.add(p4);
this.setLayout(new GridLayout(2,2,5,5));
this.setSize(700,500);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
Object source=e.getSource();
if(source==hp1){A.h1--;if(JC(A.h1)) {A.Hp+=10;HPc();g5.setText("血量:"+A.Hp);hp1.setText("小型药剂:"+A.h1);}}
else if(source==hp2){A.h2--;if(JC(A.h2)) {A.Hp+=30;HPc();g5.setText("血量:"+A.Hp);hp2.setText("中型药剂:"+A.h2);}}
else if(source==hp3){A.h3--;if(JC(A.h3)) {A.Hp+=100;HPc();g5.setText("血量:"+A.Hp);hp3.setText("大型药剂:"+A.h3);}}
else if(source==milk){A.m1--;if(JC(A.m1)) {A.power+=10;Powerc();g6.setText("体力:"+A.power);milk.setText("牛奶:"+A.m1);}}
else if(source==choco){A.c1--;if(JC(A.c1)) {A.power+=30;Powerc();g6.setText("体力:"+A.power);choco.setText("巧克力:"+A.c1);}}
else if(source==beef){A.b1--;if(JC(A.b1)) {A.power+=100;Powerc();g6.setText("体力:"+A.power);beef.setText("牛肉干:"+A.b1);}}
else if(source==shop){this.dispose();new shop(A);}
else if(source==save){Save(A);}
else if(source==money){this.dispose();new money(A);}
else if(source==train){this.dispose();new train(A);}
else if(source==challeng){this.dispose();new challeng(A);}
else if(source==exit) System.exit(0);
}
private void Powerc() {
if(A.power>100) A.power=100;
}
private void HPc() {
if(A.Hp>A.Hpd) A.Hp=A.Hpd;
}
public static void ZX(JButton x){
x.setFont(v);
x.setFocusPainted(false);
x.setBorderPainted(false);
x.setForeground(Color.white);
x.setContentAreaFilled(false);
x.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
public static boolean JC(int k){
if(k<0) return false;
else return true;
}
public static void Save(cwu obj){
File file =new File("save.dat");
FileOutputStream out;
try {
out = new FileOutputStream(file);
ObjectOutputStream objOut=new ObjectOutputStream(out);
objOut.writeObject(obj);
objOut.flush();
objOut.close();
System.out.println("保存成功。");
} catch (IOException e) {
System.out.println("保存失败!");
e.printStackTrace();
}
}
}
CW.zip_swing_宠物_小游戏
版权申诉
14 浏览量
2022-09-24
09:22:35
上传
评论
收藏 892KB ZIP 举报
APei
- 粉丝: 64
- 资源: 1万+