2014-01-09 1 views
0

Slick2D를 사용하는 11 학년 컴퓨터 과학 프로젝트에서 Tron과 같은 게임을하고 있습니다. 나는 흔적 만들기에 매달렸다. 트레일을 만들려면 어떻게해야합니까? 사각형을 만들면 어떻게 색을 입니까? 다음은 Game 클래스의 코드입니다.Slick2D에서 Tron과 같은 흔적을 만드는 방법

package bmtron; 

import java.awt.Rectangle; 
import org.newdawn.slick.Color; 
import org.newdawn.slick.GameContainer; 
import org.newdawn.slick.Graphics; 
import org.newdawn.slick.Image; 
import org.newdawn.slick.Input; 
import org.newdawn.slick.SlickException; 
import org.newdawn.slick.state.BasicGameState; 
import org.newdawn.slick.state.StateBasedGame; 
import org.newdawn.slick.state.transition.FadeInTransition; 
import org.newdawn.slick.state.transition.FadeOutTransition; 

class Game extends BasicGameState { 

int stateID = -1; 

// Getting the width, height and the scale from Tron.java 
int w = Tron.WIDTH; 
int h = Tron.HEIGHT; 
float s = Tron.SCALE; 

// Variable declaration and initialization 
float player1x = (480*s), player1y = (540*s); 
float player2x = (1440*s), player2y = (540*s); 
int direction1x = 5, direction1y = 0; 
int direction2x = -5, direction2y = 0; 
boolean isDead1 = false, isDead2 = false; 
boolean right1 = true, left1 = false, up1 = false, down1 = false; 
boolean right2 = false, left2 = true, up2 = false, down2 = false; 

// Images that are going to be used in this state 
static Image gameBackground = null; 
static Image player1 = null; 
static Image player2 = null; 

// Bounding boxes 
Rectangle p1 = new Rectangle((int)(player1x),(int)(player1y),(int)(20*s),(int)(20*s)); 
Rectangle p2 = new Rectangle((int)(player2x),(int)(player2y),(int)(20*s),(int)(20*s)); 

public Game(int stateID) { 
    this.stateID = stateID; 
} 

@Override 
public int getID() { 
    return stateID; 
} 

@Override 
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException { 
    gameBackground = new Image("/resources/background/gamebackground.jpg"); 
    player1 = new Image("/resources/images/player1.png"); 
    player2 = new Image("/resources/images/player2.png"); 
} 

@Override 
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws   SlickException { 
    gameBackground.draw(0, 0, w, h); 

    // Drawing the players to the screen 
    if (isDead1 == false){ 
     player1.draw(player1x,player1y,s); 
    }else{ 
     player1.destroy(); 
    } 

    if (isDead2 == false){ 
     player2.draw(player2x,player2y,s); 
    }else{ 
     player2.destroy(); 
    } 
} 

@Override 
public void update(GameContainer gc, StateBasedGame sbg, int i) throws SlickException { 
    // Updating the location of the bounding boxes 
    p1.setLocation((int)(player1x),(int)player1y); 
    p2.setLocation((int)(player2x),(int)player2y); 

    Input input = gc.getInput(); 

    getMovement(gc); 
    // Checking to see if the players went out of bounds 
    if (player1x>(1920*s)||player1x<1){ 
     isDead1 = true; 
     player1.destroy(); 
    }else{ 
     player1x +=direction1x; 
    } 
    if (player1y>(1080*s)||player1y<1){ 
     isDead1 = true; 
     player1.destroy(); 
    }else{ 
     player1y +=direction1y; 
    } 


    if (player2x>(1920*s)||player2x<1){ 
     isDead2 = true; 
     player2.destroy(); 
    }else{ 
     player2x +=direction2x; 
    } 
    if (player2y>(1080*s)||player2y<1){ 
     isDead2 = true; 
     player2.destroy(); 
    }else{ 
     player2y +=direction2y; 
    } 

    // Exit using escape 
    if (input.isKeyPressed(Input.KEY_ESCAPE)) { 
     System.exit(0); 
    } 

    // Checking if the two players collide with each other 
    if (p1.intersects(p2)) { 
     isDead1 = true; 
     isDead2 = true; 
    } 

    // Checking who has won 
    if (isDead1 == true && isDead2 == true) { 
     sbg.enterState(4, new FadeOutTransition(Color.white, 1000), new FadeInTransition(Color.white, 1000)); 
     //System.exit(0); 
    } 
    if (isDead2 == true) { 
     System.err.println("Player 1 has won!"); 
     sbg.enterState(4, new FadeOutTransition(Color.white, 1000), new FadeInTransition(Color.white, 1000)); 
     //System.exit(0); 
    } 
    if (isDead1 == true) { 
     System.err.println("Player 2 has won!"); 
     sbg.enterState(4, new FadeOutTransition(Color.white, 1000), new FadeInTransition(Color.white, 1000)); 
     //System.exit(0); 
    }  
} 

private void getMovement(GameContainer gc) { 
    Input input = gc.getInput(); 

    // Player 1 controls 
    if (input.isKeyDown(Input.KEY_RIGHT)) { 
     right1 = true; 
     left1 = false; 
     up1 = false; 
     down1 = false; 
    } 
    if (input.isKeyDown(Input.KEY_LEFT)) { 
     right1 = false; 
     left1 = true; 
     up1 = false; 
     down1 = false; 
    } 
    if (input.isKeyDown(Input.KEY_UP)) { 
     right1 = false; 
     left1 = false; 
     up1 = true; 
     down1 = false; 
    } 
    if (input.isKeyDown(Input.KEY_DOWN)) { 
     right1 = false; 
     left1 = false; 
     up1 = false; 
     down1 = true; 
    } 

    // Movement of Player 1 
    if (right1 == true && player1y % (20*s) == 0){ 
     direction1x = 5; 
     direction1y = 0; 
    }else if (left1 == true && player1y % (20*s) == 0){ 
     direction1x = -5; 
     direction1y = 0;   
    }else if (up1 == true && player1x % (20*s) == 0){ 
     direction1x = 0; 
     direction1y = -5;   
    }else if (down1 == true && player1x % (20*s) == 0){ 
     direction1x = 0; 
     direction1y = 5; 
    } 

    // Player 2 controls 
    if (input.isKeyDown(Input.KEY_D)) { 
     right2 = true; 
     left2 = false; 
     up2 = false; 
     down2 = false; 
    } 
    if (input.isKeyDown(Input.KEY_A)) { 
     right2 = false; 
     left2 = true; 
     up2 = false; 
     down2 = false; 
    } 
    if (input.isKeyDown(Input.KEY_W)) { 
     right2 = false; 
     left2 = false; 
     up2 = true; 
     down2 = false; 
    } 
    if (input.isKeyDown(Input.KEY_S)) { 
     right2 = false; 
     left2 = false; 
     up2 = false; 
     down2 = true; 
    } 

    // Movement of Player 2 
    if (right2 == true && player2y % (20*s) == 0){ 
     direction2x = 5; 
     direction2y = 0; 
    }else if (left2 == true && player2y % (20*s) == 0){ 
     direction2x = -5; 
     direction2y = 0;   
    }else if (up2 == true && player2x % (20*s) == 0){ 
     direction2x = 0; 
     direction2y = -5;   
    }else if (down2 == true && player2x % (20*s) == 0){ 
     direction2x = 0; 
     direction2y = 5; 
    } 
} 

} 내가 LWJGL 또는 더 슬릭 2D로 근무 한 적이없는

+0

[java.awt.Graphics] (http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html) 클래스를보십시오. – kukido

답변

-1

그러나이 방법 색상 :

glBegin(GL_QUADS); 
    Color.white(or the color of choice).bind(); 
    glVertex2f(x, y); 
    glVertex2f(x2, y2); 
    glVertex2f(x3, y3); 
    glVertex2f(x4, y4); 
    glEnd(); 

그리고 내가 테스트하는 순간에 주위에 컴퓨터를 해달라고하지만, 페이드 효과를 시도해보십시오.

//call when the effect is to begin 
    //If you don't have a set update rate 
    counter++ 
    //If you lock logic update rate 
    counter += 1.0f * delta 

    private float r, b, g //I think opengl uses RBG, but the name doesn't matter 

    public setFade(){ 
      //the 5 can be changed to however long you want the ribbon to stay visible 
     if(counter > 5); r-- (or r -= 1.0f * delta); 
     if(counter > 5); b-- (or b -= 1.0f * delta); 
     if(counter > 5); g-- (or g -= 1.0f * delta); 
    } 

다음과 같이 사각형을 그립니다.

glClearColor(r, b, g, 0.0f); 
    glBegin(GL_QUADS); 
    Color.white(or the color of choice).bind(); 
    glVertex2f(x, y); 
    glVertex2f(x2, y2); 
    glVertex2f(x3, y3); 
    glVertex2f(x4, y4); 
    glEnd(); 

그리고 이것을 리본의 덩어리로 그립니다.

glClearColor를 전혀 호출하지 않아도 기술적으로 그 효과를 얻을 수 있지만 그 문제는 색상이 삭제되지 않고 잠시 후 성능 문제가 발생하여 페이드 효과가 발생하지 않고 단지

다시 한번 나는 이것을 테스트하기 위해 주변에 컴퓨터를 가지고 있지 않지만 논리가 옳은 것처럼 보이고 작동하지 않는다면 찾고있는 논리 유형에 대한 기본을 설정해야합니다.

+0

제안 해 주셔서 감사합니다. 그러나 화면에 나타나는 흔적을 찾고 있습니다. 여기서 내가 말하는 게임이 어떤 것인지 알지 못한다면 : [link] (http://kryshen.net/games/bmtron.html) – user3179753

+0

좋아, 그러면 좀 더 복잡한 OpenGL 방법을 배우고 싶을지도 모른다. OpenGL은 커스텀 쉐이더를 지원한다는 것을 알고 있습니다. heres [link] (http://www.opengl.org/sdk/docs/tutorials/TyphoonLabs/Chapter_1.pdf). 나는이 튜토리얼이 Java 용으로 만들어 졌다고 생각하지 않지만, LWJGL은 차이점을 최소화하는 훌륭한 일을한다. – mcmonkeyninja

관련 문제