Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Need help with collision with a rectangle and a string  (Read 945 times)
0 Members and 1 Guest are viewing this topic.
Offline mdeig

Innocent Bystander





« Posted 2008-03-17 00:40:06 »

I just need some help trying to figure out how to check for the collision with a string and rectangle. I have got collision with my maze size.

thanks for helping

Here is my code if needed to see it.

class maze_racerconst{
   public static int finishxpos,finishypos,startxpos,startypos,playerx,playery;
   public static boolean plup,pldown,plright,plleft;
   public static final int speedx = 1,speedy = 1;
   public static final Font f = new Font("Arial", Font.BOLD, 10);
}

class level{
   char leveldata[] = new char[1400]; // holds the level data in array
   int width,height; // this is the size of the map and what level the player is on
   int xpos = 0;
   int ypos = 0;
   
   level(int w, int h){
      width = w;
      height = h;
   }

   public void levelon(int wl){
      if(wl == 1){
         for(int x = 0; x< maze_racerconst.level1.length(); x++){
            leveldata
  • = maze_racerconst.level1.charAt(x); //pares the string and stores it in an array
         }
      }
   }

    public void levelmake(Graphics g){
         
      for(int x = 0; x<leveldata.length; x++){
         
         if(leveldata
  • == '0'){
            xpos = xpos + 10; //draws the path
         }
         if(leveldata
  • == '1'){
            g.setColor(Color.black);
            g.fillRect(xpos,ypos,10,10); // draws the walls
            xpos = xpos + 10;
         }
         if(leveldata
  • == '7'){
            maze_racerconst.finishxpos = xpos; //draws the finish
            maze_racerconst.finishypos = ypos;
            
            g.setColor(Color.red);
            g.fillRect(xpos,ypos,10,10);
            
            xpos = xpos + 10;
         }
         if(leveldata
  • == '8'){
            maze_racerconst.startxpos = xpos; //draws the start
            maze_racerconst.startypos = ypos;
            
            g.setColor(Color.red);
            g.fillRect(xpos,ypos,10,10);
            
            xpos = xpos + 10;
         }
         
         if(xpos >= width){
            xpos = 0;        //moves the ypos down 10 pixles
            ypos = ypos + 10;
         }
      }
      xpos = 0; //reset the xpos and ypos
      ypos = 0;
      
   }
   
   
}

class Maze_Racerplayer{
   Maze_Racerplayer(){
   }

   public void setplayer(Graphics g){
      g.setColor(Color.green);      //draws the player
      g.setFont(maze_racerconst.f);
      g.drawString("*",maze_racerconst.playerx,maze_racerconst.playery);
      g.drawString(""+maze_racerconst.startxpos,400,15);
      g.drawString(""+maze_racerconst.playery,430,15);
   }

   public void resetplayerstart(){
      maze_racerconst.playerx = maze_racerconst.startxpos+5; //sets the start pos
      maze_racerconst.playery = maze_racerconst.startypos+5;
   }

   boolean isFinished(){
      if(maze_racerconst.playerx >= maze_racerconst.finishxpos && maze_racerconst.playery >= maze_racerconst.finishypos){
         return true; //checks to see if the player reached the finish
      }
      else return false;
   }
   
   public void moveplayer(){
      if(maze_racerconst.plup == true){
         maze_racerconst.playery = maze_racerconst.playery - maze_racerconst.speedy;
         
      }
      if(maze_racerconst.pldown == true){
         maze_racerconst.playery = maze_racerconst.playery + maze_racerconst.speedy;
         
      }
      if(maze_racerconst.plright == true){
         maze_racerconst.playerx = maze_racerconst.playerx + maze_racerconst.speedx;
      }
      if(maze_racerconst.plleft == true){
         maze_racerconst.playerx = maze_racerconst.playerx - maze_racerconst.speedx;
      }
   }
   
   public void checkplayerhit(){
      if(maze_racerconst.playerx <= 0){
         maze_racerconst.playerx = maze_racerconst.playerx + maze_racerconst.speedx;   
      }
      if(maze_racerconst.playerx+3 >= 350){
         maze_racerconst.playerx = maze_racerconst.playerx - maze_racerconst.speedx;
      }
      if(maze_racerconst.playery-7 <= 0){
         maze_racerconst.playery = maze_racerconst.playery + maze_racerconst.speedy;   
      }
      if(maze_racerconst.playery >= 400){
         maze_racerconst.playery = maze_racerconst.playery - maze_racerconst.speedy;
      }
   }
}

public class Maze_Racer extends Applet implements Runnable, KeyListener{
   int levelon = 1;
   private level x;
   private Maze_Racerplayer p;
   Thread trd;
   private Image dblimg;
   private Graphics dblg;
   
   public void init(){
      setBackground(Color.white);
      x = new level(350,400);
      p = new Maze_Racerplayer();
      x.levelon(levelon);
      addKeyListener(this);
   }
   
   public void start(){
      trd = new Thread(this);
      trd.start();
   }
   
   public void stop(){
      trd.stop();
   }
   
   public void destroy(){
   }
   
   public void run(){
      p.resetplayerstart();
      Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
      while(true){
         p.moveplayer();
         p.checkplayerhit();
         repaint();
            try{
               Thread.sleep(20);
            }catch(InterruptedException exc){}
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);      
      }
   }
   public void paint(Graphics g){
      x.levelmake(g);
      p.setplayer(g);
   }
   
   public void update(Graphics g){
      if(dblimg == null){
         dblimg = createImage(this.getSize().width, this.getSize().height);
         dblg = dblimg.getGraphics();
      }
      //clear screen in background
      dblg.setColor (getBackground ());
      dblg.fillRect (0, 0, this.getSize().width, this.getSize().height);
      
      // draw elements in background
      dblg.setColor(getForeground());
      paint(dblg);
      
      g.drawImage(dblimg,0,0, this);
      
      
      
   }
   
      public void keyTyped(KeyEvent e){
      
   }
   
    public void keyPressed(KeyEvent e){
      
      if(e.getKeyCode() == KeyEvent.VK_UP){
         maze_racerconst.plup = true;
         maze_racerconst.pldown = false;
         maze_racerconst.plright = false;
         maze_racerconst.plleft = false;
         
         
      }
      if(e.getKeyCode() == KeyEvent.VK_DOWN){
         maze_racerconst.pldown = true;
         maze_racerconst.plup = false;
         maze_racerconst.plright = false;
         maze_racerconst.plleft = false;
         
         
      }
      if(e.getKeyCode() == KeyEvent.VK_RIGHT){
         maze_racerconst.plright = true;
         maze_racerconst.plup = false;
         maze_racerconst.pldown = false;
         maze_racerconst.plleft = false;
         
         
      }
      if(e.getKeyCode() == KeyEvent.VK_LEFT){
         maze_racerconst.plleft = true;
         maze_racerconst.plup = false;
         maze_racerconst.pldown = false;
         maze_racerconst.plright = false;
         
         
      }
   }
   
   public void keyReleased(KeyEvent e){
      if(e.getKeyCode() == KeyEvent.VK_UP){
         maze_racerconst.plup = false;
         maze_racerconst.pldown = false;
         maze_racerconst.plright = false;
         maze_racerconst.plleft = false;
         
         
      }
      if(e.getKeyCode() == KeyEvent.VK_DOWN){
         maze_racerconst.pldown = false;
         maze_racerconst.plup = false;
         maze_racerconst.plright = false;
         maze_racerconst.plleft = false;
         
         
      }
      if(e.getKeyCode() == KeyEvent.VK_RIGHT){
         maze_racerconst.plright = false;
         maze_racerconst.plup = false;
         maze_racerconst.pldown = false;
         maze_racerconst.plleft = false;
         
         
      }
      if(e.getKeyCode() == KeyEvent.VK_LEFT){
         maze_racerconst.plleft = false;
         maze_racerconst.plup = false;
         maze_racerconst.pldown = false;
         maze_racerconst.plright = false;
         
         
      }
   }   
   
}
Offline Wildern

Junior Member





« Reply #1 - Posted 2008-03-17 19:35:43 »

You could add FontMetrics (there is probably a better way to get them, but this one works)

class maze_racerconst{
   public static int finishxpos,finishypos,startxpos,startypos,playerx,playery;
   public static boolean plup,pldown,plright,plleft;
   public static final int speedx = 1,speedy = 1;
   public static final Font f = new Font("Arial", Font.BOLD, 10);
   public final static FontMetrics fMetrics = new Button().getFontMetrics(f);
}

Then later, you can get the width and the height of a given string and use that in your collision calculations.
fMetrics.stringWidth(foo);
fMetrics.stringHeight(foo);
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (18 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (70 views)
2014-08-11 02:49:23

BurntPizza (47 views)
2014-08-09 21:09:32

BurntPizza (38 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!