(java小游戏)打字游戏v1.0

规则
控制台随机出现字符 根据关卡,随机2-5位的随机数
玩家来闯关 输入字符
拿玩家输入的字符 和随机的字符进行对比,如果相同,则游戏继续,如果不同,则游戏结束。

闯关游戏:
第一关,随机字符长度 2位 需要输入八次才能进入下一关,并且,每次输入正确+10,第一关时间限制20秒
第一关,随机字符长度 2位 需要输入八次才能进入下一关,并且,每次输入正确+20,第一关时间限制18秒.。。。
每一关若还有剩余时间 剩余时间*输入加分/5

主要练习oop的相关内容
关卡类 Level.java

package com.nll.oop4;
public class Level {
private int levelNo;
private int strLength;
private int strCount;
private int score;
private int times;
public int getLevelNo() {
 return levelNo;
}
public int getStrLength() {
 return strLength;
}
public int getStrCount() {
 return strCount;
}
public int getScore() {
 return score;
}
public int getTimes() {
 return times;
}
public Level(int levelNo, int strLength, int strCount, int score, int times) {
 super();
 this.levelNo = levelNo;
 this.strLength = strLength;
 this.strCount = strCount;
 this.score = score;
 this.times = times;
}
}

关卡数组LevelParams.java

package com.nll.oop4;
public class LevelParams {
public final static Level []levels=new Level[5];
static {
 levels[0]=new Level(1,2,8,10,10);
 levels[1]=new Level(2,3,7,30,12);
 levels[2]=new Level(3,4,6,40,14);
 levels[3]=new Level(4,5,5,80,16);
 levels[4]=new Level(5,6,4,200,18);
}
}

玩家Player.java

package com.nll.oop4;
import java.util.Scanner;
public class Player {
private int score;
private int levelNo;
private long startTime;
//玩家只负责玩
public void play() {
 Game g=new Game(this);
 Scanner sc=new Scanner(System.in);
  //开始循环,循环我们有多少关
 for (int i = 0; i < LevelParams.levels.length; i++) {
  //每一关开始我们要来设一些值
  this.levelNo++;
  this.startTime=System.currentTimeMillis();
  //获取一个当前的系统时间
  System.out.println("第"+this.levelNo+"关,开始");
    for (int j = 0; j < LevelParams.levels[this.levelNo-1].getStrCount(); j++) {
   //真正的游戏开始
  String out=g.printStr();
  System.out.println(out);
  //用户输入
  String in=sc.nextLine();
  g.result(out, in);
  }
  //每一关完成后,剩余时间,都可以来转化积分
  long end=System.currentTimeMillis();
  int useTime=(int)((end-this.startTime)/1000);
  int freeTime=LevelParams.levels[this.levelNo-1].getTimes()-useTime;
  System.out.println("您这一关剩余时间为:"+freeTime+"秒,已经转化为分数");
 this.setScore(this.score+freeTime*LevelParams.levels[this.levelNo-1].getScore()/5);
 System.out.println("您的积分为:"+(this.score+freeTime*LevelParams.levels[this.levelNo-1].getScore()/5));
 }
  }
public int getScore() {
 return score;
}
public void setScore(int score) {
 this.score = score;
}
public int getLevelNo() {
 return levelNo;
}
public void setLevelNo(int levelNo) {
 this.levelNo = levelNo;
}
public long getStartTime() {
 return startTime;
}
public void setStartTime(long startTime) {
 this.startTime = startTime;
}
public Player(int score, int levelNo, long startTime) {
 super();
 this.score = score;
 this.levelNo = levelNo;
 this.startTime = startTime;
}
public Player() {
 super();
}
}

游戏类Game.java

package com.nll.oop4;
import java.util.Random;
public class Game {
private Player player;public Game(Player player) {
 super();
 this.player = player;
};
//输出字符串
public String printStr() {
 Random r=new Random();
 String out="";
 //随机的值,应该由玩家的关卡数来决定,玩家玩到第几关,就有对应的长度
 int levelNo=player.getLevelNo();//得到第几关
 Level level=LevelParams.levels[levelNo-1];
 int length=level.getStrLength();
  for(int i=0;i<length;i++) {
  double x=r.nextDouble();
  int num=0;
  if(x<0.33) {
     num=r.nextInt(57-48+1)+48;//0-9
  }else if(x<0.66) {
   num=r.nextInt(90-65+1)+65;
   
  }else {
   num=r.nextInt(122-97+1)+97;
   
  }
   out+=(char)num;
  
 }
 return out;
 }
//输入判断
public void result(String out,String in) {
 if(out.equals(in)) {
 //输入正确
  //还没完,因为可能超时
  long end=System.currentTimeMillis();
  int useTime=(int)((end-player.getStartTime())/1000);
  if(useTime>LevelParams.levels[player.getLevelNo()-1].getTimes()) {
   System.out.println("输入超时,您的最终得分是:"+player.getScore());
      System.exit(0);
  }//先来计算这一关输入正确一次的分数
  int score=LevelParams.levels[player.getLevelNo()-1].getScore();
  player.setScore(player.getScore()+score);
  System.out.println("您输入正确,您的积分为"+player.getScore());
  
  
 }else {
   System.out.println("输入错误,您的最终得分为"+player.getScore());
  System.exit(0);
 }
}
}

主函数Test.java

package com.nll.oop4;
import java.util.Random;
public class Test {
public static void main(String[] args) {
Player p=new Player();
p.play();
}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值