/**
*
* Author:杨伟
*
* Date:2016.5.27
*
* Function:超级玛丽游戏
*
*/
package com.practices;
import java.util.Scanner;
public class SuperMaria {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
char ch;
int width,height;
System.out.println("请设置游戏界面宽和高:");
Scanner in = new Scanner(System.in);
width = in.nextInt();
height = in.nextInt();
Maria maria = new Maria();
maria.setXY(width, height);//设置马里奥的出场位置
Window win = new Window();
win.setWH(width,height);
win.show(maria);//显示初始游戏界面
System.out.println("**********开始游戏**********");
System.out.println("[w]--上\t[s]--下\t[a]--左\t[d]--右");
do{
String str = in.next();
ch = str.charAt(0);
switch(ch){
case 'w': maria.move(0); //上
win.show(maria);
break;
case 's': maria.move(1); //下
win.show(maria);
break;
case 'a': maria.move(2); //左
win.show(maria);
break;
case 'd': maria.move(3); //右
win.show(maria);
break;
}
System.out.println("[w]--上\t[s]--下\t[a]--左\t[d]--右");
}while(ch!='#');
System.out.println("***************游戏结束***************");
}
}
class Window{
private int width;//游戏窗口宽度和高度
private int height;
public void setWH(int width,int height) {
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void show(Maria m){//显示游戏界面
// System.out.println(m.getX());
// System.out.println(m.getY());
for(int i=1;i<=width;i++){
for(int j=1;j<=height;j++){
if(j==m.getX()&&i==m.getY()){
System.out.print("@ ");
}else{
System.out.print("- ");
}
}
System.out.println();
}
}
}
class Maria{
private int x;//Maria的位置
private int y;
private int wighttmp;
private int heighttmp;
public void setXY(int x,int y){
this.x = x/4;
this.y = y;
this.wighttmp = x;
this.heighttmp = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void move(int direction){
switch(direction){
case 0: this.y -= 1;//上
//限制出上边界
if(this.y<=1) this.y = 1;
break;
case 1: //限制出下边界
if(this.y<heighttmp)
this.y += 1;//下
break;
case 2: //限制出左边界
if(this.x>1) this.x -= 1;//左
break;
case 3: //到达右边界后,再回到左边界
this.x = (this.x)%wighttmp+1;//右
break;
}
}
}