java即时战略代码 即时战略的英文
使用Java面向对象的思想设计一个即时战略游戏的类结构 主要类如下: (1)人口类(Person)
package baidu;
创新互联专业为企业提供兴安盟乌兰浩特网站建设、兴安盟乌兰浩特做网站、兴安盟乌兰浩特网站设计、兴安盟乌兰浩特网站制作等企业网站建设、网页设计与制作、兴安盟乌兰浩特企业网站模板建站服务,十载兴安盟乌兰浩特做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
public class Person {
//生命值
private int lifeValue;
//攻击力
private int attackPower;
//消耗资源数
private int needResource;
public int attack(String select){
return this.lifeValue;
}
public int getLifeValue() {
return lifeValue;
}
public void setLifeValue(int lifeValue) {
this.lifeValue = lifeValue;
}
public int getAttackPower() {
return attackPower;
}
public void setAttackPower(int attackPower) {
this.attackPower = attackPower;
}
public int getNeedResource() {
return needResource;
}
public void setNeedResource(int needResource) {
this.needResource = needResource;
}
}
package baidu;
public class Nurse extends Person {
public void cure(){
System.out.println("begin cure!");
}
}
package baidu;
public class Sapper extends Person {
private int collectRes = 0;
//创造建筑createConstruction(),采集资源collectResource()
public void createConstruction(){
System.out.println("begin to createConstruction...");
}
public int collectResource(){
return collectRes;
}
}
package baidu;
public class Player {
//玩家名称。玩家资源值,玩家所拥有的人口对象,玩家所拥有的建筑对象。
private String player_name;
private int player_res;
private Person player_per;
private Construction player_con;
public String getPlayer_name() {
return player_name;
}
public void setPlayer_name(String player_name) {
this.player_name = player_name;
}
public int getPlayer_res() {
return player_res;
}
public void setPlayer_res(int player_res) {
this.player_res = player_res;
}
public Person getPlayer_per() {
return player_per;
}
public void setPlayer_per(Person player_per) {
this.player_per = player_per;
}
public Construction getPlayer_con() {
return player_con;
}
public void setPlayer_con(Construction player_con) {
this.player_con = player_con;
}
}
package baidu;
public class Person {
//生命值
private int lifeValue;
//攻击力
private int attackPower;
//消耗资源数
private int needResource;
public int attack(String select){
return this.lifeValue;
}
public int getLifeValue() {
return lifeValue;
}
public void setLifeValue(int lifeValue) {
this.lifeValue = lifeValue;
}
public int getAttackPower() {
return attackPower;
}
public void setAttackPower(int attackPower) {
this.attackPower = attackPower;
}
public int getNeedResource() {
return needResource;
}
public void setNeedResource(int needResource) {
this.needResource = needResource;
}
}
package baidu;
import java.util.Scanner;
public class PlayTest {
public static void main(String[] args) {
Player p1 = new Player();
Player p2 = new Player();
//设置第一个玩家的游戏信息
p1.setPlayer_name("player1");
//攻击力
p1.getPlayer_per().attack("a1");
p1.getPlayer_per().setLifeValue(30);
p1.getPlayer_per().setNeedResource(0);
p1.getPlayer_con().setNeedResource(0);
p1.setPlayer_res(0);
//设置第二个玩家的游戏信息
p2.setPlayer_name("player2");
//攻击力
p2.getPlayer_per().attack("a2");
p2.getPlayer_per().setLifeValue(30);
p2.getPlayer_per().setNeedResource(0);
p2.getPlayer_con().setNeedResource(0);
p2.setPlayer_res(0);
//开始准备资源
p1.getPlayer_per();
p1.getPlayer_con();
p2.getPlayer_per();
p2.getPlayer_con();
//游戏规则
String key1 = "a1";
String key2 = "Sc1";
String key3 = "Sr1";
String key4 = "Nc1";
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
if(key1.equals(input)){
p1.getPlayer_per().setLifeValue(p1.getPlayer_per().getLifeValue()+1);
p1.getPlayer_per().setNeedResource(p1.getPlayer_per().getNeedResource()+1);
p2.getPlayer_per().setLifeValue(p2.getPlayer_per().getLifeValue()-1);
}else if(key2.equals(input)){
p1.getPlayer_con().setCurrentSum(+1);
p1.setPlayer_con(p1.getPlayer_con());
}else if(key3.equals(input)){
p1.setPlayer_res(p1.getPlayer_res()+1);
}else if(key4.equals(input)){
p1.getPlayer_per().setLifeValue(+1);
p1.setPlayer_per(p1.getPlayer_per());
}
System.out.println("当前玩家1的进攻以及资源");
System.out.println("当前玩家2的进攻以及资源");
}
}
JAVA可以做出游戏么
可以的,我就做过一个即时战略游戏。Java最大的问题是性能比较差。如果你不做非常复杂的游戏就可以光用Java。
做复杂的2D游戏或者3D游戏可以用JOGL(Java的OpenGL),性能还是比C要差一些,但一般的游戏足够了。
让用户安装JVM并不是大问题。不说现在大部分电脑都已经安装了JRE,你也可以捆绑一个不用安装的JVM和你的游戏一起。比如JRE6一共有86 MB。作为比较Adobe Reader(看pdf文件用的)有41.1 MB,一般不会有人嫌这个东西太大而不下载的。
求java的计时器代码,应该比较简单的,来看看吧。
package test;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Test5 extends Applet {
private final Panel pan = new Panel();
private final Label time = new Label();
private final Button btnGo = new Button("开始");
private final Button btnPouse = new Button("暂停");
private final Button btnReset = new Button("复位");
private final StopwatchThread swThread = new StopwatchThread();
private class btnGoListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
swThread.go();
btnGo.setEnabled(false);
}
}
private class btnPouseListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(btnGo.isEnabled()){
return ;
}
if (btnPouse.getLabel().equals("继续")) {
swThread.go();
btnPouse.setLabel("暂停");
} else if (btnPouse.getLabel().equals("暂停")) {
swThread.noGo();
btnPouse.setLabel("继续");
}
}
}
private class btnResetListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
swThread.reset();
btnGo.setEnabled(true);
btnGo.setLabel("开始");
btnPouse.setLabel("暂停");
}
}
private class StopwatchThread extends Thread {
private boolean going = false;
private long prevElapsed = 0;
private Date startDate = new Date();
private long elapsedTime() {
return prevElapsed +
(going ? new Date().getTime() - startDate.getTime() : 0);
}
private String msToString(long time) {
System.out.println(time+" "+((0*60+2)*1000+999));
if(((99*60+59)*1000+983)=time((99*60+59)*1000+999)=time){//((0*60+2)*1000+983)=time((0*60+2)*1000+999)=time
if (time % 1000 990)
time += 2;
swThread.noGo();
}
String ms, sec, min;
if (time % 10 = 5)
time += 5;
ms = Long.toString(time % 1000);
while (ms.length() 3)
ms = "0" + ms;
ms = ms.substring(0, ms.length() - 1);
time /= 1000;
sec = Long.toString(time % 60);
if (sec.length() == 1) sec = "0" + sec;
time /= 60;
min = Long.toString(time);
return min + ":" + sec + "." + ms;
}
public void go() {
startDate = new Date();
going = true;
}
public void noGo() {
prevElapsed = elapsedTime();
going = false;
}
public void reset() {
going = false;
prevElapsed = 0;
}
public void run() {
while (true) {
time.setText(msToString(elapsedTime()));
yield();
}
}
}
public void init() {
setLayout(new GridLayout(2,2));
setBackground(Color.lightGray);
setForeground(Color.black);
pan.setLayout(new GridLayout(3,2));
pan.add(new Label("计时:"));
time.setForeground(Color.blue);
pan.add(time);
pan.add(btnGo);
pan.add(btnPouse);
pan.add(btnReset);
pan.add(new Label());
add(pan);
btnGo.addActionListener(new btnGoListener());
btnReset.addActionListener(new btnResetListener());
btnPouse.addActionListener(new btnPouseListener());
swThread.setDaemon(true);
swThread.start();
}
public static void main(String[] args) {
Test5 applet = new Test5();
Frame aFrame = new Frame("计时器");
aFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(400, 200);
applet.init();
applet.start();
aFrame.setVisible(true);
}
}
可以改变有注释的那个if语句里面的值来判断什么时候停止
网站栏目:java即时战略代码 即时战略的英文
分享路径:http://azwzsj.com/article/doeghgg.html