>

CompSci 클래스를위한 미니 독점 보드 게임을 만들고 있는데, 보드 주위로 조각을 옮기는 데 도움이 필요합니다. 각 블록의 너비는 90 픽셀이며 조각의 이동량은 주사위 롤을 기준으로합니다. 나는 조각을 어떻게 움직 일지 모른다. 보드 코드를 아래에 붙여 넣었습니다.

나는 그것을 void 에 넣어야할지 모르겠다  와이즈 비즈  또는 keyPressed()  주사위 롤은 키를 누르기 때문입니다.

voidDraw()

import java.util.Random; void setup() { size (700,700); noLoop(); background(205, 230, 208); // four squares on each corner strokeWeight(4); stroke(0); noFill(); rect(0, 0, 125, 125); rect(575, 0, 125, 125); rect(0, 575, 125, 125); rect(575, 575, 125, 125); // squares top side for (int i = 0; i < 5; i++) { if (i == 2 || i == 3) { i=4; } rect(125 + (i * 90), 0, 90, 90); } // squares bottom side for (int i = 0; i < 5; i++) { if (i == 1 || i == 2) { i = 3; } rect(125 + (i * 90), 610, 90, 90); } // squares left side for (int i = 0; i < 5; i++) { if (i == 1 || i == 2) { i = 3; } rect(0, 125 + (90 * i), 90, 90); } // squares right side for (int i = 0; i < 5; i++) { if (i==2 || i ==3){ i = 4; } rect(610, 125 + (90 * i), 90, 90); } //inside square + lines for chance rect(125, 125, 450, 450); rect(0, 0, 700, 700); line(0, 305, 125, 305); line(575, 395, 700, 395); line(305, 575, 305, 700); line(395, 0, 395, 125); // green fill(30, 178, 90); rect(125, 90, 90, 35); rect(215, 90, 90, 35); rect(485, 90, 90, 35); // yellow fill(253, 241, 1); rect(90, 125, 35, 90); rect(90, 395, 35, 90); rect(90, 485, 35, 90); //red fill(224, 25, 33); rect(575, 125, 35, 90); rect(575, 215, 35, 90); rect(575, 485, 35, 90); // blue fill(172, 223, 252); rect(125, 575, 90, 35); rect(395, 575, 90, 35); rect(485, 575, 90, 35); //logo PFont comic; PFont taxandchance = createFont("ComicSansMS", 75); fill (237, 27, 35); comic = createFont("ComicSansMS", 45); textFont(comic); pushMatrix(); translate(215, 490); rotate(radians(315)); translate(-215, -490); text("Mini Monopoly TM", 215, 490); popMatrix(); // go int x = 615; int y = 670; pushMatrix(); fill(0); translate(x,y); rotate(radians(315)); translate(-x, -y); text("GO", x, y); comic = createFont("ComicSansMS", 18); textFont(comic); text("collect $200", x - 14, y + 25); popMatrix(); PFont corner = createFont("ComicSansMS", 35); // free parking x = 50; y = 100; pushMatrix(); fill(0); translate(x, y); rotate(radians(315)); translate(-x, -y); textFont(corner); text("Free", x, y); popMatrix(); // stillwells x = 600; y = 40; pushMatrix(); PFont stillwells = createFont("ComicSansMS", 25); textFont(stillwells); fill(0); translate(x,y); rotate(radians(45)); translate(-x,-y); text("Stillwells", x, y); popMatrix(); // grill x = 38; y = 620; pushMatrix(); fill(0); translate(x, y); rotate(radians(45)); translate(-x, -y); textFont(corner); text("Grill", x, y); popMatrix(); // location names bottom row fill(0); textFont(comic); text("Library", 500, 650); text("$100", 500, 680); text("Elm", 425, 650); text("$100", 418, 680); text("Weth", 145, 650); text("$120", 145, 680); text("Train", (325), 620); text("station:", (325), 640); text("Andover", (320), 660); textFont(taxandchance); fill(0, 175, 237); text("$", 236, 668); // location names top row x = 198; y = 55; pushMatrix(); fill(0); textFont(comic); translate(x, y); rotate(radians(180)); translate(-x, -y); text("Bowld", x, y); text("PHLH", x - 90, y); text("ACB", x - 350, y); text("$260", x, y + 30); text("$260", x - 90, y + 30); text("$280", x - 350, y + 30); text("Train", x - 182, y - 25); text("station:", x - 189, y - 5); text("Hotchkiss", x - 195, y + 25); textFont(taxandchance); fill(0, 175, 237); text("$", x - 261, y + 20); popMatrix(); // locationnames yellow x = 60; y = 511; pushMatrix(); textFont(comic); fill(0); translate(x, y); rotate(radians(90)); translate(-x, -y); text("HR", x, y); text("OMA", x - 90, y); text("Bissell", x -364, y); text("$180", x, y + 30); text("$180", x - 90, y + 30); text("$200", x - 360, y + 30); text("Train", x - 182, y - 25); text("station:", x - 189, y - 5); text("Choate", x - 186, y + 25); textFont(taxandchance); fill(247, 134, 32); text("?", x - 270, y + 25); popMatrix(); // locationnames red x = 640; y = 190; pushMatrix(); textFont(comic); fill(0); translate(x,y); rotate(radians(270)); translate(-x, -y); text("Phelps", x - 5, y); text("Field H.", x - 100, y); text("Goel", x - 363, y); text("$320", x, y + 30); text("$350", x - 90, y + 30); text("$400", x - 370, y + 30); text("Train", x - 182, y - 25); text("station:", x - 189, y - 5); text("Deerfield:", x - 200, y + 25); textFont(taxandchance); fill(247, 134, 32); text("?", x - 270, y + 25); popMatrix(); // tokens fill(226, 53, 143); ellipse(630, 630, 30, 30); fill(1, 110, 203); ellipse(630, 665, 30, 30); println(name1 + " press 'l' to roll!"); } int p1position = 0; int p2position = 0; int p1diceroll; int p2diceroll; String name1 = "Jasmine"; String name2 = "Iliana"; void keyPressed() { noLoop(); Random rand = new Random(); if (key == 'l') { // Obtain a number between [0 - 5]. p1diceroll = rand.nextInt(6) + 1; println("You rolled a " + p1diceroll + "!"); println(name2 + " press 'a' to roll"); } if (key == 'a') { // Obtain a number between [0 - 5]. p2diceroll = rand.nextInt(6) + 1; println("You rolled a " + p2diceroll + "!"); if (p1diceroll > p2diceroll){ System.out.println(name1 + " rolled higher!. They go first."); } if (p2diceroll > p1diceroll) { System.out.println(name2 + " rolled higher!. They go first. "); } if (p2diceroll == p1diceroll) { System.out.println("It's a tie! " + name1 + " goes first by default." ); } } }

  • 답변 # 1

    모든 필드 목록을 작성하십시오. ArrayList 사용 (예 : ArrayList<int[]> fields = new ArrayList<int[]>(); void setup() { // bottom right fields.add(new int[]{575, 575, 125, 125}); // squares bottom side for (int i=4; i>=0; i--){ fields.add(new int[]{125+(i*90), (i==1 || i==2) ? 575 : 610, 90, (i==1 || i==2) ? 125 : 90}); } // bottom left fields.add(new int[]{0, 575, 125, 125}); // squares left side for (int i=4; i>=0; i--){ fields.add(new int[]{0, 125+(90*i), (i==1 || i==2) ? 125 : 90, 90}); } // top left fields.add(new int[]{0, 0, 125, 125}); // squares top side for (int i=0; i<5; i++){ fields.add(new int[]{125+(i*90), 0, 90, (i==2 || i==3) ? 125 : 90}); } // top right fields.add(new int[]{575, 0, 125, 125}); // squares right side for (int i=0; i<5; i++){ fields.add(new int[]{(i==2 || i ==3) ? 575 : 610, 125+(90*i), (i==2 || i ==3) ? 125 : 90, 90}); } // [...] } )  tor은 필드의 사각형을 동적 배열에 저장합니다 :

    예 :

    PGraphics
    
    

    전체 보드를 PGraphics board; void setup() { // [...] size(700, 700); board = createGraphics(700, 700); board.beginDraw(); board.background(205,230,208); // four squares on each corner board.strokeWeight(4); board.stroke(0); board.noFill(); for(int[] f : fields) { board.rect(f[0], f[1], f[2], f[3]); } // [...] board.endDraw(); } 에 그린다  개체 :

    image
    
    

    이제 Processing 게임 루프를 사용할 수 있습니다. 보드를 창으로 그린다 ( int pos_l = 0; int pos_a = 0; void draw() { image(board, 0, 0); // tokens int xl = fields.get(pos_l)[0] + fields.get(pos_l)[2] / 3; int yl = fields.get(pos_l)[1] + fields.get(pos_l)[3] / 3; fill(226, 53, 143); ellipse(xl, yl, 30, 30); int xa = fields.get(pos_a)[0] + fields.get(pos_a)[2] * 2 / 3; int ya = fields.get(pos_a)[1] + fields.get(pos_a)[3] * 2 / 3; fill(1, 110, 203); ellipse(xa, ya, 30, 30); } ) ) 보드 상단의 개별 위치에서 플레이어를 끌어옵니다.

    keyPressed
    
    
    player_l 에서  게임 로직을 구현하고 플레이어의 위치를 ​​변경하는 이벤트 ( player_a void keyPressed() { Random rand = new Random(); if (key == 'l'){ // Obtain a number between [0 - 5]. p1diceroll = rand.nextInt(6) + 1; // [...] pos_l = (pos_l + p1diceroll) % fields.size(); } if (key == 'a'){ // Obtain a number between [0 - 5]. p2diceroll = rand.nextInt(6) + 1; // [...] pos_a = (pos_a + p2diceroll) % fields.size(); } } ) :

    import java.util.Random;
    ArrayList<int[]> fields = new ArrayList<int[]>();
    PGraphics board;
    void setup()
    {
        // bottom right
        fields.add(new int[]{575, 575, 125, 125});
        // squares bottom side
        for (int i=4; i>=0; i--){
            fields.add(new int[]{125+(i*90), (i==1 || i==2) ? 575 : 610, 90, (i==1 || i==2) ? 125 : 90});
        }
        // bottom left
        fields.add(new int[]{0, 575, 125, 125});
        // squares left side
        for (int i=4; i>=0; i--){
            fields.add(new int[]{0, 125+(90*i), (i==1 || i==2) ? 125 : 90, 90});
        }
        // top left
        fields.add(new int[]{0, 0, 125, 125});
        // squares top side
        for (int i=0; i<5; i++){
           fields.add(new int[]{125+(i*90), 0, 90, (i==2 || i==3) ? 125 : 90});
        }
        // top right
        fields.add(new int[]{575, 0, 125, 125});
        // squares right side
        for (int i=0; i<5; i++){
            fields.add(new int[]{(i==2 || i ==3) ? 575 : 610, 125+(90*i), (i==2 || i ==3) ? 125 : 90, 90});
        }
    
        size(700, 700);  
        board = createGraphics(700, 700);
        board.beginDraw();
        board.background(205,230,208);
        // four squares on each corner
        board.strokeWeight(4);
        board.stroke(0);
        board.noFill();
        for(int[] f : fields) {
            board.rect(f[0], f[1], f[2], f[3]);
        }   
        //inside square + lines for chance
        board.rect(125, 125, 450, 450);
        board.rect(0, 0, 700, 700); 
        board.line(0, 305, 125, 305);
        board.line(575, 395, 700, 395);
        board.line(305, 575, 305, 700);
        board.line(395, 0, 395, 125);
        // green
        board.fill(30, 178, 90);
        board.rect(125, 90, 90, 35); 
        board.rect(215, 90, 90, 35); 
        board.rect(485, 90, 90, 35); 
        // yellow
        board.fill(253, 241, 1);
        board.rect(90, 125, 35, 90); 
        board.rect(90, 395, 35, 90); 
        board.rect(90, 485, 35, 90);
        //red
        board.fill(224, 25, 33);
        board.rect(575, 125, 35, 90); 
        board.rect(575, 215, 35, 90); 
        board.rect(575, 485, 35, 90);
        // blue
        board.fill(172, 223, 252);
        board.rect(125, 575, 90, 35); 
        board.rect(395, 575, 90, 35); 
        board.rect(485, 575, 90, 35); 
        //logo
        PFont comic;
        PFont taxandchance = createFont("ComicSansMS", 75);
        board.fill (237, 27, 35); 
        comic = createFont("ComicSansMS", 45);
        board.textFont(comic);
        board.pushMatrix();
        board.translate(215,490);
        board.rotate(radians(315));
        board.translate(-215,-490);
        board.text("Mini Monopoly TM", 215, 490);
        board.popMatrix();
        // go
        int x = 615;
        int y = 670;
        board.pushMatrix();
        board.fill(0);
        board.translate(x,y);
        board.rotate(radians(315));
        board.translate(-x,-y);
        board.text("GO", x, y);
        comic = createFont("ComicSansMS", 18);
        board.textFont(comic); 
        board.text("collect $200", x-14, y+25); 
        board.popMatrix();
        PFont corner = createFont("ComicSansMS", 35);
        // free parking
        x = 50;
        y = 100;
        board.pushMatrix();
        board.fill(0);
        board.translate(x,y);
        board.rotate(radians(315));
        board.translate(-x,-y);
        board.textFont(corner);
        board.text("Free", x, y);
        board.popMatrix();
        // stillwells
        x = 600;
        y = 40;
        board.pushMatrix();
        PFont stillwells = createFont("ComicSansMS", 25);
        board.textFont(stillwells);
        board.fill(0);
        board.translate(x,y);
        board.rotate(radians(45));
        board.translate(-x,-y);
        board.text("Stillwells", x, y);
        board.popMatrix();
        // grill
        x = 38;
        y = 620;
        board.pushMatrix();
        board.fill(0);
        board.translate(x,y);
        board.rotate(radians(45));
        board.translate(-x,-y);
        board.textFont(corner);
        board.text("Grill", x, y);
        board.popMatrix();
        // location names bottom row
        board.fill (0); 
        board.textFont(comic);
        board.text("Library", 500, 650);
        board.text("$100", 500, 680);
        board.text("Elm", 425, 650);
        board.text("$100", 418, 680);
        board.text("Weth", 145, 650);
        board.text("$120", 145, 680);
        board.text("Train", (325), 620);
        board.text("station:", (325), 640);
        board.text("Andover", (320), 660);
        board.textFont(taxandchance);
        board.fill(0, 175,237);
        board.text("$", 236, 668);
        // location names top row
        x = 198;
        y = 55;
        board.pushMatrix();
        board.fill(0);
        board.textFont(comic);
        board.translate(x,y);
        board.rotate(radians(180));
        board.translate(-x,-y);
        board.text("Bowld", x, y);
        board.text("PHLH", x - 90, y);
        board.text("ACB", x - 350, y);
        board.text("$260", x, y+30);
        board.text("$260", x - 90, y+30);
        board.text("$280", x - 350, y+30);
        board.text("Train", x - 182, y-25);
        board.text("station:", x - 189, y-5);
        board.text("Hotchkiss", x - 195, y+25);
        board.textFont(taxandchance);
        board.fill(0, 175,237);
        board.text("$", x-261, y+20);
        board.popMatrix();
        // locationnames yellow
        x = 60;
        y = 511;
        board.pushMatrix();
        board.textFont(comic);
        board.fill(0);
        board.translate(x,y);
        board.rotate(radians(90));
        board.translate(-x,-y);
        board.text("HR", x, y);
        board.text("OMA", x - 90, y);
        board.text("Bissell", x -364, y);
        board.text("$180", x, y+30);
        board.text("$180", x - 90, y+30);
        board.text("$200", x - 360, y+30);
        board.text("Train", x - 182, y-25);
        board.text("station:", x - 189, y-5);
        board.text("Choate", x - 186, y+25);
        board.textFont(taxandchance);
        board.fill(247,134,32);
        board.text("?", x-270, y+25); 
        board.popMatrix();
        // locationnames red
        x = 640;
        y = 190;
        board.pushMatrix();
        board.textFont(comic);
        board.fill(0);
        board.translate(x,y);
        board.rotate(radians(270));
        board.translate(-x,-y);
        board.text("Phelps", x-5, y);
        board.text("Field H.", x - 100, y);
        board.text("Goel", x -363, y);
        board.text("$320", x, y+30);
        board.text("$350", x - 90, y+30);
        board.text("$400", x - 370, y+30);
        board.text("Train", x - 182, y-25);
        board.text("station:", x - 189, y-5);
        board.text("Deerfield:", x - 200, y+25);
        board.textFont(taxandchance);
        board.fill(247,134,32);
        board.text("?", x-270, y+25);
        board.popMatrix();
        board.endDraw();
    }
    int pos_l = 0;
    int pos_a = 0;
    void draw(){
        image(board, 0, 0); 
        // tokens
        int xl = fields.get(pos_l)[0] + fields.get(pos_l)[2] / 3;
        int yl = fields.get(pos_l)[1] + fields.get(pos_l)[3] / 3;
        fill(226, 53, 143); 
        ellipse(xl, yl, 30, 30);
        int xa = fields.get(pos_a)[0] + fields.get(pos_a)[2] * 2 / 3;
        int ya = fields.get(pos_a)[1] + fields.get(pos_a)[3] * 2 / 3;
        fill(1, 110, 203); 
        ellipse(xa, ya, 30, 30);  
    }
    int p1position = 0; 
    int p2position = 0; 
    int p1diceroll;
    int p2diceroll;
    String name1 = "Jasmine";
    String name2 = "Iliana"; 
    void keyPressed(){
        Random rand = new Random();
        if (key == 'l'){
            // Obtain a number between [0 - 5].
            p1diceroll = rand.nextInt(6) + 1;
            println("You rolled a " + p1diceroll + "!"); 
            println(name2 + " press 'a' to roll");
            pos_l = (pos_l + p1diceroll) % fields.size();
        }
        if (key == 'a'){
            // Obtain a number between [0 - 5].
            p2diceroll = rand.nextInt(6) + 1;
            println("You rolled a " + p2diceroll + "!");  
            if (p1diceroll>p2diceroll){
                System.out.println(name1 + " rolled higher!. They go first.");
            }
            if (p2diceroll>p1diceroll){
                System.out.println(name2 + " rolled higher!. They go first. "); 
            }
            if (p2diceroll==p1diceroll){
                System.out.println("It's a tie! " + name1 + " goes first by default." ); 
            }
            pos_a = (pos_a + p2diceroll) % fields.size();
        }
    }
    
    

    제안을 질문의 코드에 적용한 예를 참조하십시오. 이 예제는 게임 로직을 제공하지 않습니다. 단지 주사위를 플레이어의 위치에 추가하고 플레이어를 보드 주위로 움직입니다.

  • 이전 sql - 'dd/mm'형식의 날짜 별 필터링
  • 다음 java - Cloud Firestore Child의 가치에 액세스 할 수 없음