2014-12-28 2 views
0

저는 자바 게임 프로그래밍을 시작한 사람입니다. 게임에 대한 저의 큰 문제점이 있습니다. 적과 블록 사이에 충돌을 일으키려고 노력 중이며 그 이유를 알지 못합니다. 그것은 작동해야하지만 초당 한 프레임 속도로 게임을 느리게하고 아무것도하지 않습니다.적과 블록 사이의 Java 마리오 게임 충돌

내가 픽셀로 level.png 픽셀을 읽고 differents의 색상으로 나는 모든의 위치를 ​​설정하고있어 어디 주요 클래스는이 주 초기화() 함수

public void init(){ 
    WIDTH = getWidth(); 
    HEIGHT = getHeight(); 
    tex = new Texture(); 

    BufferImageLoader loader = new BufferImageLoader(); 
    level = loader.loadImage("/level.png"); // loading level 
    cloud = loader.loadImage("/cloud.png"); // loading clouds 

    handler = new Handler(); 
    cam = new Camera(0,0); 
    LoadImageLevel(level); 

    this.addKeyListener(new KeyInput(handler)); 
} 

와 LoadImageLevel 기능보다 게임 불렀다 사물.

private void LoadImageLevel (BufferedImage image){ 
    int w = image.getWidth(); 
    int h = image.getHeight(); 
    //System.out.println(w + " , " + h); 

    for(int xx = 0; xx < h; xx++){ 
     for(int yy = 0; yy < w ; yy++){ 
      int pixel = image.getRGB(xx, yy); 
      int red = (pixel >> 16) & 0xff; 
      int green = (pixel >> 8) & 0xff; 
      int blue = (pixel) & 0xff; 

      if(red == 255 && green == 255 && blue == 255) 
       handler.addObject(new Block(xx*32,yy*32,1,ObjectId.Block)); 
      if(red == 0 && green == 0 && blue == 255) 
       handler.addObject(new Player(xx*32,yy*32,1,handler,ObjectId.Player)); 
      if(red == 0 && green == 255 && blue == 0) 
       handler.addObject(new Enemy(xx*32,yy*32,handler,ObjectId.Enemy)); 
     } 
    } 

} 

클래스 플레이어는 틱에서 콜업이 호출되는 두 가지 중요한 기능인 틱 및 충돌입니다.

public class Player extends GameObject{ 

private float width = 32, // 48 
     height = 64; // 96 

private float gravity = 0.5f; 
private final float MAX_SPEED = 10; 
private int facing = 1; 
private int last = 0; // last position left or right 
private Handler handler; 

Texture tex = Game.getInstance(); 
private int type; 

private Animation playerWalk, playerWalkLeft,jump; 

public Player(float x, float y,int type , Handler handler ,ObjectId id) { 
    super(x, y, id); 
    this.handler = handler; 
    this.type = type; 

    playerWalk = new Animation(2,tex.player[2],tex.player[3], 
      tex.player[4],tex.player[5]); 
    playerWalkLeft = new Animation(2,tex.player[7],tex.player[8], 
      tex.player[9],tex.player[10]); 
    jump = new Animation(2,tex.player[11],tex.player[12]); 
} 

public void tick(LinkedList<GameObject> object) { 
    x += velX; 
    y += velY; 

    if(velX < 0) facing = -1; 
    else if(velX > 0) facing = 1; 

    if(falling || jumping){ 
     velY += gravity; 

     if(velY > MAX_SPEED){ 
      velY = MAX_SPEED; 
     } 
    } 
    Collision(object); 
    //System.out.println(velX + " " + velY); 

    playerWalk.runAnimation(); 
    playerWalkLeft.runAnimation(); 
    jump.runAnimation(); 
} 

private void Collision(LinkedList<GameObject> object){ 
    for(int i = 0; i < handler.object.size();i++){ 
     GameObject tempObject = handler.object.get(i); 
     if(tempObject.getId() == ObjectId.Block){ 

      if(getBoundsTop().intersects(tempObject.getBounds())){ 
       y = tempObject.getY() + 32; 
       velY = 0; 
      } 


      if(getBounds().intersects(tempObject.getBounds())){ 
       y = tempObject.getY() - height; 
       velY = 0; 
       falling = false; 
       jumping = false; 
      }else 
       falling = true; 

      if(getBoundsRight().intersects(tempObject.getBounds())){ 
       x = tempObject.getX() - width; 
      } 


      if(getBoundsLeft().intersects(tempObject.getBounds())){ 
       x = tempObject.getX() + 35; 
      } 
     } 

     /* new */ 

    } 

} 


public void render(Graphics g) { 

    /* 
    g.setColor(Color.blue); 
    g.fillRect((int)x,(int)y,(int)width,(int)height); 

    Graphics2D g2d = (Graphics2D) g; 
    g.setColor(Color.red); 
    g2d.draw(getBounds()); 
    g2d.draw(getBoundsRight()); 
    g2d.draw(getBoundsLeft()); 
    g2d.draw(getBoundsTop()); 
    */ 

    if(velX != 0){ 
     if (facing == 1){ 
     playerWalk.drawAnimation(g,(int) x, (int)y,32,64); 
     last = 1; 
     } 
     else{ 
      playerWalkLeft.drawAnimation(g,(int) x, (int)y,32,64); 
      last = 0; 
     } 
    } 


    else 
     if (last == 1) 
     g.drawImage(tex.player[1], (int)x,(int) y,32,64,null); 
     else 
     g.drawImage(tex.player[6], (int)x,(int) y,32,64,null); // 6 ,32,64 

    //System.out.println("Y: " + y); // 513 je max 
    if (y >= 513){ 
     g.setColor(Color.red); 
     g.drawString("Game Over", (int) x, 200); 
    } 
} 

public Rectangle getBounds() { 
    return new Rectangle((int) ((int)x+(width/2)-((width/2)/2)),(int) ((int)y+(height/2)),(int)width/2,(int)height/2); 
} 

public Rectangle getBoundsTop() { 
    return new Rectangle((int) ((int)x+(width/2)-((width/2)/2)),(int)y,(int)width/2,(int)height/2); 
} 

public Rectangle getBoundsRight() { 
    return new Rectangle((int) ((int)x+width-5),(int)y+5,(int)5,(int)height-10); 
} 
public Rectangle getBoundsLeft() { 
    return new Rectangle((int)x,(int)y+5,(int)5,(int)height-10); 
} 

플레이어는 블록 충돌과 관련하여 문제가 없습니다. 내가 같은 플레이어의 클래스처럼 적 클래스를 생성하고 그것을 만드는 시도 할 때

public class Block extends GameObject { 

Texture tex = Game.getInstance(); 
private int type; 


public Block(float x, float y,int type,ObjectId id) { 
    super(x, y, id); 
    this.type = type; 
} 

public void tick(LinkedList<GameObject> object) { 

} 

public void render(Graphics g) { 
    if(type == 0) 
     g.drawImage(tex.block[0], (int) x, (int) y ,null); 
    if(type == 1) 
     g.drawImage(tex.block[1], (int) x, (int) y ,null); 
} 


public Rectangle getBounds() { 
    return new Rectangle((int)x,(int)y,32,32); 
} 
} 

는하지만 그냥 게임이 느린 아무것도 할 충돌을 의미한다.

public class Enemy extends GameObject{ 

private Handler handler; 
public Enemy(float x, float y,Handler handler, ObjectId id) { 
    super(x, y, id); 
    this.handler = handler; 
} 

public void tick(LinkedList<GameObject> object) { 
    for(int i = 0; i < handler.object.size();i++){ 
     GameObject tempObject = handler.object.get(i); 
     if(tempObject.getId() == ObjectId.Block){ 

      if(getBoundsTop().intersects(tempObject.getBounds())){ 

      } 


      if(getBounds().intersects(tempObject.getBounds())){ 

      } 

      if(getBoundsRight().intersects(tempObject.getBounds())){ 

      } 


      if(getBoundsLeft().intersects(tempObject.getBounds())){ 

      } 
     } 
    } 
} 

public void render(Graphics g) { 
    g.setColor(Color.red); 
    g.fillRect((int)x,(int) y, 32, 32); 
} 

public Rectangle getBoundsTop() { 
    return new Rectangle((int)x,(int)y,32,32); 
} 

public Rectangle getBoundsLeft() { 
    return new Rectangle((int)x,(int)y,32,32); 
} 

public Rectangle getBoundsRight() { 
    return new Rectangle((int)x,(int)y,32,32); 
} 

public Rectangle getBounds() { 
    return new Rectangle((int)x,(int)y,32,32); 
}} 

나는 경계를 반환 같은 "새로운 Rectangle"가 안 나는 틱 방법 x-- 설정 때 어쨌든 적의 어떤 어떤 움직임을 프로그래머 없다는 것을 알고있다; 적의 막을 막을 때 멈추지 만 노력하지 않는다면 그걸로 뭐가 잘못 됐는지 몰라. 고칠 때 이틀 이상을 보냈다. 도움이된다면 전체 프로젝트 (이클립스)에 대한 링크가 있습니다 You can download it from dropbox

나는 왼쪽과 오른쪽으로 이동하고 블록과 콜리 손을 가지기를 원했을뿐입니다. 그가 "오른쪽으로 등"할 때까지 "돌아서"오른쪽으로 이동을 차단하십시오. 플레이어와 적 사이의 다른 충돌은 저에게 문제가되지 않습니다. 하지만 그냥. 모든 조언에 대해 매우 감사드립니다.

+0

게시 한 코드의 문제점을 파악하기 어렵습니다. 몇 가지 정리를하고 다시 게시하도록 제안 할 수 있습니까? 첫째, 경계를 제거하여 시작하는 것이 좋습니다. 제한 범위 오른쪽 솔루션. 더 나은 방법은 교차로를 테스트 한 다음 물체의 이동 방향을 사용하여 충돌을 피하기 위해 물체를 이동하는 방향을 결정하는 것입니다. – sprinter

답변

0

문제는 getBounds() 방법에 있습니다.

height=32의 사각형을 반환하는 방법은 getBounds()입니다. 그리고 당신의 직사각형이 32 by 32 (fillrect(x,y,32,32)에서 언급했듯이)이기 때문에 getBounds()은 높이와 너비가 교차하여 반환됩니다.

즉, 반환 된 왼쪽 위 하단 또는 오른쪽 경계와 충돌하지 않도록하십시오.

enemy()에서 을로드하는 동안 set.color = red을 선언하고 있습니다. if(red == 0 && green == 255 && blue == 0) 대신 red==255, green==0, blue==0을 시도하십시오.