JavaSE-超级玛丽游戏

/**

 *

 * 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;

      }

   }

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

讲文明的喜羊羊拒绝pua

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值