|
|
|
ตอนนี้ เป็น code เกี่ยวกับ knight โจมตี dragon แบบ 1-1 แต่อยากจะเปลื่ยนให้ dragon มี 3 โจมตี knight 1 |
|
|
|
|
|
|
|
ตอนนี้ เป็น code เกี่ยวกับ knight โจมตี dragon แบบ 1-1 แต่อยากจะเปลื่ยนให้ dragon มี 3 โจมตี knight 1
อยากได้แบบ knight ตี dragon 3 ตัว
แล้ว dragon ทั้ง 3 ตัว ตี knight
ผลัดกันตีไปเรื่อยๆ จนเลือด เหลือ 0 แล้วหยุดโจมตี
Code (Java)
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
class Character extends JLabel implements Runnable{
private String name;
private int hp, atk, def;
private Thread t = new Thread(this);
private Character opponent;
Character(String n, int hpMax, int atkMax, int defMax){
name = n;
hp = (int)(Math.random() * (hpMax - 1000)) + 1000;//อยู่ในช่วง 1000 - hpMax
atk = (int)(Math.random() * (atkMax - 500)) + 500;//อยู่ในช่วง 500 - atkMax
def = (int)(Math.random() * (defMax - 100)) + 100;//อยู่ในช่วง 100 - defMax
}
void setOpponent(Character ch){
opponent = ch;
}
public void run(){
long time;
while(hp > 0 && opponent.getHp() > 0){
time = (long)(Math.random() * 1500) + 500;
try {
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(hp > 0 && opponent.getHp() > 0){
opponent.getAttack(atk);
// System.out.println(name + " attack " + opponent.getName());
// System.out.println(opponent.getName() + ": HP = " + opponent.getHp() + ", DEF = " + opponent.getDef() + ", ATK = " + opponent.getAtk());
opponent.setText(opponent.getName() + ": HP = " + opponent.getHp() + ", ATK = " + opponent.getAtk() + ", DEF = " + opponent.getDef());
}
}
}
void start(){
t.start();
}
public String getName(){ return name; }
int getHp(){ return hp; }
int getAtk(){ return atk; }
int getDef(){ return def; }
void getAttack(int attack){
if(attack > def){
hp = hp - attack;
if(hp < 0){
hp = 0;
}
}
}
}
class app extends JFrame{
JLabel lb1 = new JLabel("Dragon");
JLabel lb2 = new JLabel("Knight");
JLabel lb3 = new JLabel("Dragon2");
JLabel lb4 = new JLabel("Dragon3");
JButton b1 = new JButton("Start");
JButton b2 = new JButton("Exit");
JPanel jp = new JPanel(),jp2 = new JPanel();
buttonEvent be = new buttonEvent();
Character c1 = new Character("Knight", 100000, 2000, 1000);
Character c2 = new Character("Dragon", 50000, 3000, 1000);
Character c3 = new Character("Dragon2", 50000, 3000, 1000);
Character c4 = new Character("Dragon3", 50000, 3000, 1000);
app(){
super("Dragon && Knight");
c1.setText(c1.getName() + ": HP = " + c1.getHp() + ", ATK = " + c1.getAtk() + ", DEF = " + c1.getDef());
c2.setText(c2.getName() + ": HP = " + c2.getHp() + ", ATK = " + c2.getAtk() + ", DEF = " + c2.getDef());
c3.setText(c3.getName() + ": HP = " + c3.getHp() + ", ATK = " + c3.getAtk() + ", DEF = " + c3.getDef());
c4.setText(c4.getName() + ": HP = " + c4.getHp() + ", ATK = " + c4.getAtk() + ", DEF = " + c4.getDef());
jp2.setLayout(new GridLayout(4,1));
jp2.add(c1);
jp2.add(c2);
jp2.add(c3);
jp2.add(c4);
add(jp2, BorderLayout.NORTH);
//add(c2, BorderLayout.WEST);
jp.setLayout(new FlowLayout());
b1.addActionListener(be);
b2.addActionListener(be);
jp.add(b1);
jp.add(b2);
add(jp, BorderLayout.SOUTH);
setSize(300, 300);
show();
}
class buttonEvent implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == b1){
JOptionPane.showMessageDialog(null, "You click Start");
c1.setOpponent(c2);
c2.setOpponent(c1);
c1.start();
c2.start();
b1.setEnabled(false);
}
if(e.getSource() == b2){
// JOptionPane.showMessageDialog(null, "You click Exit");
System.exit(0);
}
}
}
}
public class Control_2 {
public static void main(String[] a){
// c1.start();
// c2.start();
app ap = new app();
/*
while(c1.getHp() > 0 && c2.getHp() > 0){
c2.getAttack(c1.getAtk());
System.out.println(c1.getName() + " attack " + c2.getName());
System.out.println(c2.getName() + ": HP = " + c2.getHp() + ", DEF = " + c2.getDef() + ", ATK = " + c2.getAtk());
if(c2.getHp() > 0){
c1.getAttack(c2.getAtk());
System.out.println(c2.getName() + " attack " + c1.getName());
System.out.println(c1.getName() + ": HP = " + c1.getHp() + ", DEF = " + c1.getDef() + ", ATK = " + c1.getAtk());
}
}
*/
}
}
Tag : Java
|
|
|
|
|
|
Date :
2013-10-29 02:29:28 |
By :
nick15 |
View :
1259 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มีภาพประกอบหรือเปล่าครับ
|
|
|
|
|
Date :
2013-10-31 09:28:53 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|