QuickHit项目
来源:互联网 发布:java date sethour 编辑:程序博客网 时间:2024/05/01 08:13
首先编写类,游戏类,玩家类,级别类
玩家类的属性:levelNo玩家编号类,currScore玩家当前积分,stratTime当前级别开始时间,elapsedTime
当前级别已用时间
级别类的属性:levelNo各级别编号,strLengh一次输入的字符串长度,strTime各级别输出字符串的次数,timeLimit各级闯关的时间限制
perScore各级别输入一次正确的得分!
游戏类:player玩家属性(玩家来玩游戏)
因为级别类不包括各个级别的具体参数信息,所以增加一个levelParam类,创建一个长度为6的数组,存放各个级别的参数信息
首先game类(先有游戏才能玩):
方法有二:printStr()
printResult()
1:printStr()方法:生成随机的字符串,使用switch选择结构以生成的随机数进行判断生成的字符串。字符串的长度不得大于各级别输出字符串的长度。
int strLength=LevelParam.levels[player.getLevelNo()-1].getStrLength();由于数组下标是从0开始的,获取该级别输入
的字符串的长度定位到数组中的一项要使用(级别号-1 )定位数组下标。创建一个0到5的随机数,创建StringBuffer对象来拼接字符串。该方法返回
一个拼接好了的字符串。
2:long time=System.currentTimeMillis();获取系统当前时间的毫秒数(严谨到毫秒)
(time-player.getStartTime())/1000>LevelParam.levels[player.getLevelNo()-1].getTimeLimit()如果游戏所用时间大于
游戏规定的时间,判断出局!
player.setCurScore(player.getCurScore()+LevelParam.levels[player.getLevelNo()-1].getPerScore());加上当前获得的分数
player.setElapsedTime((int)(time-player.getStartTime())/1000);计算玩游戏所需的时间
输出当前这一关的信息,当进行到第六个级别(最高级别)时,将级别清空,还原为1.
--Player类:创建游戏类(game对象),记录循环的次数,将级别初始为1.
game.getPlayer().setStartTime(System.currentTimeMillis())记录下游戏开始时间!
循环条件,小于输入次数,接收随机生成的字符串,如果用户输入的字符串与该字符串相等,继续游戏,否则,gameOver!
--Text类:直接调用player类的print()方法!
//游戏类
class Game {//玩家对象作为变量
public Player player;
public Game(Player player) {
super();
this.player = player;
}
public Game() {
}
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
Random random = new Random();
public String printstr(){
StringBuffer buffer = new StringBuffer();//创建一个StringBuffer 对象,用来不断的存储和改变字符串内容
int str =Levelparam.leves[player.getLevelNo()-1].getStrLength();
for (int i = 0; i < str; i++) {//采用循环 加上判断的方式, 生成对应长度的字符串
int rand = random.nextInt(str);
switch(rand){
case 0:
buffer.append("Q");
break;
case 1:
buffer.append("W");
break;
case 2:
buffer.append("E");
break;
case 3:
buffer.append("R");
break;
case 4:
buffer.append("D");
break;
case 5:
buffer.append("F");
break;
}
}
System.out.println(buffer.toString());
return buffer.toString(); //返回这个做好的字符串
}
public void printReslut(String out, String jieshou)
{
if(out.equals(jieshou))
{
long time = System.currentTimeMillis();
//如果超时
if((time-player.getStartTime())/1000>Levelparam.leves[player.getLevelNo()-1].getTimeLimit())
{
System.out.println("已超时,您的速度有待提高");
System.exit(1);
}
else
{
//当前说获得分值
player.setCurScore(player.getCurScore()+Levelparam.leves[player.getLevelNo()-1].getPerScore());
//游戏所需时间
player.setElapsedTime((int)(time-player.getStartTime())/1000);
int Level = player.getLevelNo();
int score = player.getCurScore();
int usetime = player.getElapsedTime();
System.out.println("输入正确,您的级别是"+Level+"您的积分是"+score+"已用时间"+usetime);
if(Level==6)
{
System.exit(1);
System.out.println("游戏结束");
}
}
}
else
{
System.out.println("游戏失败");
System.exit(1);
}
}
}//等级类
public class leve {
private int leveNo;private int strLength;
private int strTimes;
private int timeLimit;
private int perScore;
public leve(int leveNo, int strLength, int strTimes, int timeLimit,
int perScore) {
super();
this.leveNo = leveNo;
this.strLength = strLength;
this.strTimes = strTimes;
this.timeLimit = timeLimit;
this.perScore = perScore;
}
public leve() {
super();
}
public int getLeveNo() {
return leveNo;
}
public void setLeveNo(int leveNo) {
this.leveNo = leveNo;
}
public int getStrLength() {
return strLength;
}
public void setStrLength(int strLength) {
this.strLength = strLength;
}
public int getStrTimes() {
return strTimes;
}
public void setStrTimes(int strTimes) {
this.strTimes = strTimes;
}
public int getTimeLimit() {
return timeLimit;
}
public void setTimeLimit(int timeLimit) {
this.timeLimit = timeLimit;
}
public int getPerScore() {
return perScore;
}
public void setPerScore(int perScore) {
this.perScore = perScore;
}
}//Levelparam 类,用来制定游戏等级的规则
public class Levelparam {
public final static leve[] leves = new leve[6];
static{
leves[0]=new leve(1,2,10,30,1);
leves[1]=new leve(2,3,9,26,2);
leves[2]=new leve(3,4,8,22,5);
leves[3]=new leve(4,5,7,18,8);
leves[4]=new leve(5,6,6,15,10);
leves[5]=new leve(6,7,5,12,15);
}
}//玩家类
class Player {
//玩家等级
private int levelNo;
//玩家积分
private int curScore;
//游戏开始时间
private long startTime;
//游戏所用时间
public int getLevelNo() {
return levelNo;
}
public void setLevelNo(int levelNo) {
this.levelNo = levelNo;
}
public int getCurScore() {
return curScore;
}
public void setCurScore(int curScore) {
this.curScore = curScore;
}
public long getStartTime() {
return startTime;
}
public void setStartTime(long startTime) {
this.startTime = startTime;
}
public int getElapsedTime() {
return elapsedTime;
}
public void setElapsedTime(int elapsedTime) {
this.elapsedTime = elapsedTime;
}
public void print(){
Scanner input = new Scanner(System.in);
Game game= new Game(this);//创建游戏实例
game.getPlayer().setLevelNo(1);
for (int i = 0; i < Levelparam.leves.length; i++) {
setLevelNo(getLevelNo()+1);
this.curScore=0;
System.out.println("恭喜你进入下一级别");
game.getPlayer().setStartTime(System.currentTimeMillis());//游戏开始,给Player的开始时间赋值
//内层循环
for(int j=0;j<Levelparam.leves[game.getPlayer().getLevelNo()-1].getStrTimes();j++)
{
String out=game.printstr();//接收游戏输出的字符串
String in=input.next();//接受玩家输入的字符串
game.printReslut(out,in);//判断
}
}
}
}
//测试类
public static void main(String[] args) {
Player py = new Player();
py.print();
}
}
- QuickHit项目
- 项目:QuickHit
- QuickHit项目
- QuickHit 项目
- QuickHit项目
- QuickHit项目
- QuickHit的项目
- QuickHit游戏项目总结
- QuickHit项目总结随记
- 项目实战:QuickHit
- javaOOP项目 QuickHit
- QuickHit游戏开发项目总结
- QuickHit(打字游戏)项目源代码
- Quickhit
- QuickHit
- QuickHit
- QuickHit
- QuickHit项目源代码及小总结
- java SWT:自定义布局(Layout)实现组件自动缩放显示
- PAT B1027
- 使用MyBatis Generator自动创建代码
- cocos2d错误点
- NestedScrollingParent, NestedScrollingChild 详解
- QuickHit项目
- 数据结构顺序查找验证程序
- 学习python的第二十五天-老程序员的忠告
- UVA 1349 Optimal Bus Route Design
- PAT B1028
- Glide加载图片并保存到本地返回file,bitmap
- 谈谈Android 6.0运行时权限理解
- 南京邮电大学离散数学实验三偏序关系中盖住关系的求取及格论中有补格的判定
- 谈谈Android 6.0运行时权限理解