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[x] = 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[x] == '0'){
xpos = xpos + 10; //draws the path
}
if(leveldata[x] == '1'){
g.setColor(Color.black);
g.fillRect(xpos,ypos,10,10); // draws the walls
xpos = xpos + 10;
}
if(leveldata[x] == '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[x] == '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;
}
}
}