Java Brickbreaker Block Collision无法正常运行

时间:2017-09-28 14:28:10

标签: java

我目前正在编写一个简单的破砖游戏,但是我遇到了一个巨大的障碍。我宣称的碰撞有时会在错误的砖块上发生,同时还有很多内部弹跳的球。最重要的是,绘制非命中区块的循环超出范围,但无论我多么努力修补它,我都无法解决它。任何帮助将非常感激。提前谢谢。

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class brickBreakerApplet extends Applet implements Runnable, KeyListener, MouseListener, MouseMotionListener
{
 private int speed;
 
 int currentLine;
 int x_pos = 30;   
 int y_pos = 100;  
 int x_speed = 1;
 int y_speed = 1;
 int radius = 5;  
 int appletsize_x = 400; 
 int appletsize_y = 300; 
 int mousex=0;
 int mousey=0;
 int score=0;
 int life=3;
 int counter=0;
 int[] bricks;
 Boolean game=false;
 Image background;
 

 private Image dbImage;
 private Graphics dbg;

 public void init()
 {
  setBackground (Color.blue);
  background = getImage(getCodeBase(), "background.gif");
  
  bricks=new int[50];
  
  for (int x=0; x<50; x++){
    bricks[x]=1;
  }
  
  currentLine = 10;
  
  addKeyListener(this);
  addMouseListener(this);
  addMouseMotionListener(this);
 }

 public void start ()
 {
  Thread th = new Thread (this);
  th.start ();
 }

 public void stop()
 {

 }
 
 public void keyPressed(KeyEvent e)
 {
   if (e.getKeyChar() == 'r'){
     x_pos=0;
     y_pos=0;
     x_speed=1;
     y_speed=1;
   }
  currentLine+=20;
 }

 public void keyReleased(KeyEvent e)
 {
//  System.out.println("User released key " + e.getKeyChar());
//  currentLine+=20;
 }

 public void keyTyped(KeyEvent e)
 {

 }

 public void mouseClicked(MouseEvent e)
 {
//  System.out.println("User clicked mouse " + e.getClickCount() + " times!");
//  currentLine+=20;
 }

 public void mouseEntered(MouseEvent e)
 {
//  System.out.println("Mouse entered applet at " + e.getX() + " " + e.getY());
//  currentLine += 20;
 }

 public void mouseExited(MouseEvent e)
 {

 }

 public void mousePressed(MouseEvent e)
 {
   if (game==false){
     x_speed=1;
     y_speed=-1;
     game=true;
   }
   if (life==0){
     x_pos=0;
     y_pos=0;
     x_speed=1;
     y_speed=1;
     life=3;
     score=0;
     game=false;
   }
 }

 public void mouseReleased(MouseEvent e)
 {

 }

 public void mouseMoved(MouseEvent e)
 {
   mousex=e.getX();
   mousey=e.getY();
 }

 public void mouseDragged(MouseEvent e)
 {
 }


 public void destroy()
 {

 }

 public void run ()
 {
  Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

  while (true)
  {

    
   if (x_pos > appletsize_x - radius)
   {
     if (x_speed==1){
       x_speed = -1;
     }
     if (x_speed==2){
       x_speed = -2;
     }
   }
   else if (x_pos < radius)
   {
     if (x_speed==-1){
       x_speed = +1;
     }
     if (x_speed==-2){
       x_speed= 2;
     }
   }
   else if (y_pos < radius)
   {
     y_speed = +1;
   }
   else if (y_pos > appletsize_y - radius)
   {
     death();
   }
   if (game==true){
     
     
     x_pos += x_speed;
     y_pos += y_speed;
     
    //HITTING TWICE, LOTS OF ERRORS WHILE RUNNING CODE, ASK SIR FOR ASSISTANCE TOMORROW. LOOKS PRETTY GOOD THOUGH.
     
     counter=0;
     for (int y=0; y<5; y++){
       for (int x=0; x<10; x++){
         if (y_pos-(radius)==(40+y*10) &&((x_pos+(radius)>=(x*40) && (x_pos+radius<= 40+(x*40)))) && bricks[counter]==1 && y_speed==-1){
           bricks[counter]=0;
           score++;
           y_speed=1;
         }
         counter++;
       }
     }
     
     counter=0;
     for (int y=0; y<5; y++){
       for (int x=0; x<10; x++){
         if (y_pos+(radius)==(30+y*10) && ((x_pos+(radius)>=(x*40) && (x_pos+radius<= 40+(x*40)))) && bricks[counter]==1 && y_speed==1){
           bricks[counter]=0;
           score++;
           y_speed=-1;
         }
         counter++;
       }
     }
     
     counter=0;
     for (int y=0; y<5; y++){
       for (int x=0; x<10; x++){
         if (x_pos+radius==(x*40) && ((y_pos>=(30+(y*10)) && (y_pos<= 40+(y*10)))) && bricks[counter]==1){
           bricks[counter]=0;
           score++;
           x_speed=-1;
         }
         counter++;
       }
     }
     
     counter=0;
     for (int y=0; y<5; y++){
       for (int x=0; x<10; x++){
         if (x_pos-radius==(40+(x*40)) && ((y_pos>=(30+(y*10)) && (y_pos<= 40+(y*10)))) && bricks[counter]==1){
           bricks[counter]=0;
           score++;
           x_speed=1;
         }
         counter++;
       }
     }     
   
     if (y_pos >= 280-(radius))
     {
       {
         if ((x_pos+(radius) >= mousex-30) && (x_pos+(radius) <= mousex-20)){
           score++;
           difficulty();
           x_speed=-2;
           java.awt.Toolkit.getDefaultToolkit().beep();
           
         }
         if ((x_pos+(radius) >= mousex-20) && (x_pos+(radius) <= mousex)){
           score++;
           difficulty();
           x_speed=-1;
           java.awt.Toolkit.getDefaultToolkit().beep();
           
         }
         if ((x_pos+(radius) >= mousex) && (x_pos+(radius) <= mousex+20)){
           score++;
           difficulty();
           x_speed=1;
           java.awt.Toolkit.getDefaultToolkit().beep();
           
         }
         if ((x_pos+(radius) >= mousex+20) && (x_pos+(radius) <= mousex+30)){
           score++;
           difficulty();
           x_speed=2;
           java.awt.Toolkit.getDefaultToolkit().beep();
           
         }
         
       }
     }
   }
   
   if (game==false){
     x_pos=mousex;
     y_pos= 279-radius;
   }
   

   

   repaint();
  

   try
   {
    Thread.sleep (5);
   }
   catch (InterruptedException ex)
   {
    // do nothing
   }
   Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  }
 }
 
 public void update (Graphics g)
 {
  if (dbImage == null)
  {
   dbImage = createImage (this.getSize().width, this.getSize().height);
   dbg = dbImage.getGraphics ();
  }

  dbg.setColor (getBackground ());
  dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

  dbg.setColor (getForeground());
  paint (dbg);
  
  g.drawImage (dbImage, 0, 0, this);
 }

 public void paint (Graphics g)
 {
   if (life>0){
     
     g.drawImage(background,0,0,this);
     
     g.setColor  (Color.cyan);
     
     g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
     
     g.setColor (Color.blue);
     
     g.fillRect (mousex-30, 280, 60, 5);
     
     g.setFont(new Font("Comic Sans MS", Font.PLAIN, 20));
     
     g.setColor (Color.cyan);
     
     g.drawString ("Score: "+score, 5,20);
     
     g.drawString ("Life: "+life, 330,20);
     
     counter=0;
     for (int y=0; y<5; y++){
       for (int x=0; x<10; x++){
         if (bricks[counter]==1){
           g.setColor(Color.blue);
           g.fillRect(x*40, 30+(y*10),40,10);
           g.setColor(Color.black);
           g.drawRect(x*40, 30+(y*10),40,10);
         }
         counter++;
       }
     }
   }
   
   if (life==0){
     g.setColor (Color.black);
     g.fillRect (0,0,400,400);
     g.setColor (Color.white);
     g.setFont (new Font("Times New Roman", Font.BOLD, 48));
     g.drawString ("GAME OVER", 50, 150);
     g.setFont (new Font("Times New Roman", Font.PLAIN, 12));
     g.drawString ("To Try Again, Press Screen", 130, 160);
     g.drawString ("Your Score Was: "+score, 155, 170);
   }
 }
 
 public void death ()
 {
   game=false;
   y_pos= 279-radius;
   life--;
   
 }
 
 public void difficulty()
 {
   y_speed=-1;
 }
 }

0 个答案:

没有答案