一个java写的弹球小游戏

import java.awt.*;

import java.applet.*;

import javax.swing.*;

import java.awt.event.*;

import java.util.*;

import java.lang.Integer;

/*障碍1

*子弹2

*小车3

*定义三个向量存储下落的字符

*/

public class tanqiu extends JApplet implements Runnable,KeyListener

{

    int kuai[][]=new int[45][38];

    Thread t=null;

shangzu zu=null;

    zidan dan=null;

board che=null;

    jiangpin jiang=null;

    ctrl con=null;

    boolean begin=false;       //判断开始游戏

    int type=-1;

    int stepmod=-1;

    int p=-1;           //pause or move

    int slee=400;

    boolean stepshow=false;

    boolean pause=false;

    boolean reb=false;

    boolean chushi=false;

    Vector zifu=new Vector();

    Vector zx=new Vector();

    Vector zy=new Vector();

    int modle=-1;

    public void init()

    {

        for(int i=0;i<45;i++)

          for(int j=0;j<38;j++)

            kuai[i][j]=0;

        zu=new shangzu(kuai);

        dan=new zidan(kuai);

        che=new board(kuai);

        jiang=new jiangpin(zifu);

        con=new ctrl();

        che.shengcheng();

        addKeyListener(this);

        setFocusable(true);

    }

    public void paint(Graphics g)

    {

        g.drawLine(19,19,470,19);

        g.drawLine(19,19,19,400);

        g.drawLine(470,19,470,400);

        g.drawLine(19,400,470,400);

        g.setColor(Color.green);

        g.setFont(new Font("",2,20));

        g.drawString("重新开始",600,32);

        g.setColor(Color.blue);

        g.drawString("选择级别",600,65);

        if(modle%2==0)

        {

          g.clearRect(489,49,82,22);

          g.setColor(Color.gray);

          g.drawRect(490,23,60,14);

          g.drawLine(550,23,560,30);

          g.drawLine(550,37,560,30);   

        }

        if(modle%2==1)

        {

          g.clearRect(489,19,82,22);   

          g.setColor(Color.gray);

          g.drawRect(490,53,60,14);

          g.drawLine(550,53,560,60);

          g.drawLine(550,67,560,60);

        }

        if(stepshow==true)

        {

            g.setColor(Color.blue);

            g.drawString("一级",650,99);

            g.setColor(Color.blue);

            g.drawString("二级",650,134);

            g.setColor(Color.blue);

            g.drawString("三级",650,164);

            if(stepmod%3==0)

             {

               g.clearRect(530,120,80,20);

               g.clearRect(530,150,80,20);

               g.setColor(Color.gray);

               g.drawRect(530,90,50,19);   

             }

            if(stepmod%3==1)

             {

               g.clearRect(530,90,80,20);

               g.clearRect(530,150,80,20); 

               g.setColor(Color.gray);

               g.drawRect(530,120,50,19);

             }

             if(stepmod%3==2)

             {

               g.clearRect(530,90,80,20);

               g.clearRect(530,120,80,20); 

               g.setColor(Color.gray);

               g.drawRect(530,150,50,19);

             }

         }

        else if(stepshow==false)

          g.clearRect(500,88,180,250);

        if(reb==false)

        {

        for(int i=20;i<470;i+=10)

          for(int j=20;j<400;j+=10)

          {

            if(kuai[i/10-2][j/10-2]==0)

            {

                g.setColor(Color.white);

                g.fill3DRect(i,j,10,10,true);

            }

            else if(kuai[i/10-2][j/10-2]==1)

            {

                g.setColor(Color.blue);

                g.fill3DRect(i,j,10,10,true);

            }

           else if(kuai[i/10-2][j/10-2]==2)

            {

                g.setColor(Color.black);

                g.fillOval(i,j,10,10);

            }

          else if(kuai[i/10-2][j/10-2]==3)

            {

                g.setColor(Color.green);

                g.fill3DRect(i,j,10,10,true);

            }

          }

          if(!(zifu.isEmpty()))

          {

            g.setColor(Color.black);

            g.setFont(new Font("",2,15));

            for(int i=0;i

            {

             g.drawString((String)zifu.elementAt(i),((Integer)zx.elementAt(i)).intValue(),((Integer)zy.elementAt(i)).intValue());

            }

           }

          }

          if(reb==true)

          {

            g.setColor(Color.red);

            g.setFont(new Font("",3,40));

            g.drawString("GAME OVER",88,210);

          }

    }

    public void start()

    {

        t=new Thread(this);

        t.start();

    }

    public void run()

    {

        while(true)

        {

          if(pause==false)

          {

            if(zu.yes==true)

            {

                zu.shengcheng();

                zu.jishu();

                //System.out.println(zu.count);

                zu.yes=false;

            }

            if(begin==true)

            {

                dan.control(1);

                type=1;

                begin=false;

                chushi=true;

            }

            if((begin==false&&chushi==false)||chushi==false)

            {

                dan.control(che.ty);

            }

            if(chushi==false)

            {

                if(kuai[dan.x][dan.y]==1)

                 zu.jianshu();

            }

            /*控制子弹的不撞击情况下的方向

             */

            if(dan.x==44)

            {

                if(type==1)

                  type=2;

                if(type==4)

                  type=3;

            }

            else if(dan.y==0)

            {

                if(type==2)

                  type=3;

                if(type==1)

                  type=4;

            }

            else if(dan.x==0)

            {

                if(type==3)

                  type=4;

                if(type==2)

                  type=1;

            }

            else if(dan.y==36)

            {

                type=0;

                dan.life=false;

                reb=true;

            }

            /*子弹的撞击

             */

            if(dan.life==true)

            {

                if(type==1)

                 {

                   if(kuai[dan.x+1][dan.y-1]==1)

                    {

                     try

                     {

                      kuai[dan.x+1][dan.y-1]=0;

                      type=4;

                      jiang.mode();

                      Integer jx=new Integer((dan.x+1)*10+20);///

                      zx.addElement(jx);

                      Integer jy=new Integer((dan.y-1)*10+20);//

                      zy.addElement(jy);

                     }catch(Exception ee){

                     }

                     zu.jianshu();

                    }

                 }

                else if(type==2)

                 {

                   if(kuai[dan.x-1][dan.y-1]==1)

                    {

                     try

                     {

                      kuai[dan.x-1][dan.y-1]=0;

                      type=3;

                      jiang.mode();

                      Integer jx=new Integer((dan.x-1)*10+20);///

                      zx.addElement(jx);

                      Integer jy=new Integer((dan.y-1)*10+20);/

                      zy.addElement(jy);

                     }catch(Exception ee){

                     }

                     zu.jianshu();

                    }

                 }

               else if(type==3)

                {

                  if(kuai[dan.x-1][dan.y+1]==1)

                  {

                    try

                    {

                      kuai[dan.x-1][dan.y+1]=0;

                      type=2;

                      jiang.mode();

                      Integer jx=new Integer((dan.x-1)*10+20);

                      zx.addElement(jx);

                      Integer jy=new Integer((dan.y+1)*10+20);

                      zy.addElement(jy);

                    }catch(Exception ee){

                   }

                   zu.jianshu();

                }

            }

            else if(type==4)

            {

                if(kuai[dan.x+1][dan.y+1]==1)

                {

                    try

                    {

                      kuai[dan.x+1][dan.y+1]=0;

                      type=1;

                      jiang.mode();

                      Integer jx=new Integer((dan.x+1)*10+20);///

                      zx.addElement(jx);

                      Integer jy=new Integer((dan.y+1)*10+20);/

                      zy.addElement(jy);

                    }catch(Exception ee){

                    }

                    zu.jianshu();

                }

            }

           }

            /*以下处理与小车的碰撞  */

            if(type==3)

            {

                if(kuai[dan.x-1][dan.y+1]==3)

                  type=2;

            }

            if(type==4)

            {

                if(kuai[dan.x+1][dan.y+1]==3)

                  type=1;

            }

            dan.control(type);

            if(!(zy.isEmpty()))

            {

                for(int i=0;i

                {

                    Integer l=new Integer(((Integer)zy.elementAt(i)).intValue()+10);

                    zy.insertElementAt(l,i);

                    zy.remove(i+1);

                }

            }

            /*以下处理字符与小车的撞击

             *字符落到显示区域以外则不见

             */

            if(!(zy.isEmpty()))

            {

                for(int i=0;i

                {

                    if(kuai[((Integer)zx.elementAt(i)).intValue()/10-2][((Integer)zy.elementAt(i)).intValue()/10-2]==3||((Integer)zy.elementAt(i)).intValue()>385)

                     {

                        if(kuai[((Integer)zx.elementAt(i)).intValue()/10-2][((Integer)zy.elementAt(i)).intValue()/10-2]==3)

                        {

                          if(((String)zifu.elementAt(i)).equals("L"))

                          {

                            if(che.width<15&&che.start<44-che.width)

                              che.wider();

                          }

                          if(((String)zifu.elementAt(i)).equals("G"))

                             con.state=false;

                          if(((String)zifu.elementAt(i)).equals("F"))

                          {

                            if(chushi==true)

                              chushi=false;

                            else if(chushi==false)

                              chushi=true;

                          }

                           /*

                            *

                            */

                         }

                       //计算出三个向量的索引

                       zifu.remove(i);

                       zx.remove(i);

                       zy.remove(i);

                     }

                }

            }

            if(con.state==false)

             {

                con.trick++;

                if(con.trick==20)

                 {

                    con.state=true;

                    con.trick=0;

                 }

             }

           if(zu.count==0)

           {

             zu.yes=true;

           }

           }

           try

            {

                Thread.sleep(slee);

            }

            catch(Exception exe)

            {

            }

           //System.out.println(zu.count);

           int cc=0;

           for(int i=0;i<45;i++)

             for(int j=0;j<38;j++)

              {

                if(kuai[i][j]==1)

                  cc++;

              }

           System.out.println(cc);

           if(cc==40)

           {

             zu.yes=true;

           }

           repaint();

         }

    }

  public void keyPressed(KeyEvent ke)

  {

  }

  public void keyReleased(KeyEvent ke)

  {

  }

  public void keyTyped(KeyEvent ke)

  {

    if((""+ke.getKeyChar()).equals("b"))

    {

        begin=true;

        //chushi=true;

        if(reb==true)

          rebegin();

    }

    else if((""+ke.getKeyChar()).equals("d"))

    {

        if(con.state==true)

          che.move(5);

    }

    else if((""+ke.getKeyChar()).equals("a"))

    {

        if(con.state==true)

           che.move(6);

    }

    else if((""+ke.getKeyChar()).equals("w"))

    {

        che.move(7);

    }

    else if((""+ke.getKeyChar()).equals("u"))

    {

        p++;

        if(p%2==0)

          pause=true;

        else if(p%2==1)

          pause=false;

    }

    else if((""+ke.getKeyChar()).equals("i"))

    {

        pause=false;

    }

    if((""+ke.getKeyChar()).equals("k"))//down

     {

        if(stepshow==false)

          modle++;

        if(stepshow==true)

          stepmod++;

     }

    if((""+ke.getKeyChar()).equals("o"))  //VK_ENTER

    {

        if(stepshow==false)

        {

         if(modle%2==0)

          {

            rebegin();

         }

         else if(modle%2==1)

          {

            stepshow=true;

          }

        }

        if(stepshow==true)

        {

            if(stepmod%3==0)

            {

              slee=400;

              stepshow=false;

            }

            else if(stepmod%3==1)

            {

              slee=200;

              stepshow=false;

            }

            else if(stepmod%3==2)

            {

              slee=100;

              stepshow=false;  

            }

        }

    }

  }

public void rebegin()

{

    begin=false;

    pause=false;

    chushi=false;

    p=-1;

    reb=false;

    type=-1;

    stepshow=false;

    zifu=new Vector();

    zx=new Vector();

    zy=new Vector();

    for(int i=0;i<45;i++)

      for(int j=0;j<38;j++)

        kuai[i][j]=0;

    zu=new shangzu(kuai);

    modle=-1;

    stepmod=-1;

    slee=400;

    dan=new zidan(kuai);

    che=new board(kuai);

    jiang=new jiangpin(zifu);

    con=new ctrl();

    che.shengcheng();

}

public void destroy()

{

    //System.exit(0);

}

class shangzu

{

    boolean yes=true;

    int kuai[][];

    int count=0;

    public shangzu(int[][] kuai)

    {

        this.kuai=kuai;

    }

    public void shengcheng()

    {

        for(int i=0;i<150;i++)

        {

            int zux=(int)(Math.random()*41+2);

            int zuy=(int)(Math.random()*14+2);

            kuai[zux][zuy]=1;

        }

    }

    public void jishu()

    {

        for(int i=0;i<45;i++)

          for(int j=0;j<38;j++)

          {

            if(kuai[i][j]==1)

              count++;

          }

    }

    public void jianshu()

    {

        count--;

    }

}

class zidan

{

    int x=4;

    int y=33;

    int kuai[][];

    boolean life=true;

    public zidan(int[][] kuai)

    {

        this.kuai=kuai;

    }

    public void control(int type)

    {

        /*两个临时变量的定义,发生了一件令人快乐的事,

         *使我不必再去定义新的方法来消去撞击的障碍物;

         */

        int linx;

        int liny;

        linx=x;

        liny=y;

        switch(type)

        {

            case 1:

            {

                x++;

                y--;

                break;

            }

            case 2:

            {

                x--;

                y--;

                break;

            }

            case 3:

            {

                x--;

                y++;

                break;

            }

            case 4:

            {

                x++;

                y++;

                break;

            }

            case 0:

            {

                break;

            }

            case 5:

            {

                if(x<40)

                  x++;

                break;

            }

            case 6:

            {

                if(x>0)

                  x--;

                break;

            }

            case 7:

            {

                if(y>18)

                  y--;

                break;

            }

        }

        kuai[linx][liny]=0;

        kuai[x][y]=2;

    }

}

class board

{

    int kuai[][];

    int start=0;

    int lin;

    int cha=0;

    int liny;

    int width=8;

    int ty=0;

    zidan zd=null;

    public board(int[][] kuai)

    {

        this.kuai=kuai;

        zd=new zidan(kuai);

    }

    public void shengcheng()

    {

        for(int i=0;i

          kuai[i][34]=3;

    }

    public void move(int lei)

    {

        ty=lei;

        switch(lei)

        {

            case 5:

            {

                lin=start;

                if(start<45-width)

                {

                    start++;

                    for(int i=start;i

                      if(i<45)

                        kuai[i][34+cha]=3;

                     kuai[lin][34+cha]=0;

                }

                break; 

            }

            case 6:

            {

                lin=start+width-1;

                if(start>0)

                {

                    start--;

                    for(int i=start;i

                     //if(i>45)

                      kuai[i][34+cha]=3;

                    //if(lin<45)

                    kuai[lin][34+cha]=0;

                }

               break;  

            }

            case 7:

            {

                liny=cha;

                if(cha>-15)

                  cha--;

                for(int i=start;i

                   kuai[i][34+cha]=3;

                if(liny>-15)

                 for(int i=start;i

                   kuai[i][34+liny]=0;

                break;

            }

        }

    }

    public void wider()

    {

        width++;

        for(int i=start;i

          if(i<45)

            kuai[i][34+cha]=3;

    }

}

class jiangpin

{

    String letter=null;

    Vector zifu;

    public jiangpin(Vector zifu)

    {

        this.zifu=zifu;

    }

    public void mode()

    {

        int mod=(int)(Math.random()*3);

        switch(mod)

        {

            case 0:

            {

                letter="L";//big

                break;

            }

            case 1:

            {

                letter="G";//nianzhu

                break;

            }

            case 2:

            {

                letter="F";//life

                break;

            }

        }

        zifu.addElement(letter);

    }

}

class ctrl

{

    boolean state=true;

    int trick=0;

}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
package org.crazyit.ball; import java.awt.Image; import java.io.File; import javax.imageio.ImageIO; import java.io.IOException; /** * 小球对象 * * @author yangenxiong yangenxiong2009@gmail.com * @author Kelvin Mak kelvin.mak125@gmail.com * @version 1.0 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br>Copyright (C), 2009-2010, yangenxiong * <br>This program is protected by copyright laws. */ public class Ball extends BallComponent { // 定义球的竖向速度 private int speedY = 10; // 定义弹球的横向速度 private int speedX = 8; // 定义是否在运动 private boolean started = false; // 定义是否结束运动 private boolean stop = false; /** * m 有参数构造器 * * @param panelWidth * int 画板宽度 * @param panelHeight * int 画板高度 * @param offset * int 位移 * @param path * String 图片路径 */ public Ball(int panelWidth, int panelHeight, int offset, String path) throws IOException { // 调用父构造器 super(panelWidth, panelHeight, path); // 设置y坐标 this.setY(panelHeight - super.getImage().getHeight(null) - offset); } /** * 设置横向速度 * * @param speed * int 速度 * @return void */ public void setSpeedX(int speed) { this.speedX = speed; } /** * 设置竖向速度 * * @param speed * int 速度 * @return void */ public void setSpeedY(int speed) { this.speedY = speed; } /** * 设置是否在运动 * * @param b * boolean * @return void */ public void setStarted(boolean b) { this.started = b; } /** * 设置是否结束运动 * * @param b * boolean * @return void */ public void setStop(boolean b) { this.stop = b; } /** * 返回横向速度 * * @return int 速度 */ public int getSpeedX() { return this.speedX; } /** * 返回竖向速度 * * @return int 速度 */ public int getSpeedY() { return this.speedY; } /** * 是否在运动 * * @return boolean 是否在运动 */ public boolean isStarted() { return this.started; } /** * 是否已经结束运动 * * @return boolean 是否已经结束运动 */ public boolean isStop() { return this.stop; } }
好的,以下是一个简单的弹球小游戏Java代码实现: ``` import javax.swing.*; import java.awt.*; import java.awt.event.*; public class BallGame extends JFrame implements ActionListener { Timer timer; Ball ball; Paddle paddle; Brick[] bricks; public BallGame() { setTitle("Ball Game"); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); setResizable(false); ball = new Ball(); paddle = new Paddle(); bricks = new Brick[30]; for (int i = 0; i < bricks.length; i++) { bricks[i] = new Brick(40 * i + 30, 50); } timer = new Timer(10, this); timer.start(); setSize(400, 600); setLocationRelativeTo(null); } public void actionPerformed(ActionEvent e) { ball.move(); paddle.move(); for (int i = 0; i < bricks.length; i++) { if (ball.getBounds().intersects(bricks[i].getBounds())) { ball.reverseY(); bricks[i].setDestroyed(); } } if (ball.getBounds().intersects(paddle.getBounds())) { ball.reverseY(); } if (ball.getY() > getHeight()) { JOptionPane.showMessageDialog(this, "Game Over"); timer.stop(); dispose(); } repaint(); } public void paint(Graphics g) { super.paint(g); ball.draw(g); paddle.draw(g); for (int i = 0; i < bricks.length; i++) { bricks[i].draw(g); } } public static void main(String[] args) { new BallGame(); } class Ball { int x, y, dx, dy, size; public Ball() { x = 200; y = 500; dx = 2; dy = -2; size = 20; } public void move() { x += dx; y += dy; if (x < 0 || x > getWidth() - size) { dx = -dx; } if (y < 0) { dy = -dy; } } public void reverseY() { dy = -dy; } public int getX() { return x; } public int getY() { return y; } public Rectangle getBounds() { return new Rectangle(x, y, size, size); } public void draw(Graphics g) { g.setColor(Color.RED); g.fillOval(x, y, size, size); } } class Paddle { int x, y, dx, sizeX, sizeY; public Paddle() { x = 150; y = 550; dx = 0; sizeX = 100; sizeY = 10; } public void move() { x += dx; if (x < 0) { x = 0; } if (x > getWidth() - sizeX) { x = getWidth() - sizeX; } } public int getX() { return x; } public int getY() { return y; } public Rectangle getBounds() { return new Rectangle(x, y, sizeX, sizeY); } public void draw(Graphics g) { g.setColor(Color.BLUE); g.fillRect(x, y, sizeX, sizeY); } } class Brick { int x, y, sizeX, sizeY; boolean destroyed; public Brick(int x, int y) { this.x = x; this.y = y; sizeX = 40; sizeY = 20; destroyed = false; } public int getX() { return x; } public int getY() { return y; } public Rectangle getBounds() { return new Rectangle(x, y, sizeX, sizeY); } public boolean isDestroyed() { return destroyed; } public void setDestroyed() { destroyed = true; } public void draw(Graphics g) { if (!destroyed) { g.setColor(Color.GREEN); g.fillRect(x, y, sizeX, sizeY); } } } } ``` 这个游戏包含了一个小球,一个挡板和一些砖块,玩家可以通过控制挡板来让小球弹起并击碎砖块。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值