홈>
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." );
}
}
}
트렌드
- OpenCv의 폴더에서 여러 이미지 읽기 (python)
- 파이썬 셀레늄 모든 "href"속성 가져 오기
- git commit - 자식 - 로컬 커밋 된 파일에 대한 변경을 취소하는 방법
- html - 자바 스크립트 - 클릭 후 변경 버튼 텍스트 변경
- JSP에 대한 클래스를 컴파일 할 수 없습니다
- javascript - 현재 URL에서 특정 div 만 새로 고침/새로 고침
- jquery - JavaScript로 현재 세션 값을 얻으시겠습니까?
- javascript - swiperjs에서 정지, 재생 버튼 추가
- vue.js - axios를 사용하여 서버에 이미지를 업로드하는 방법
- python - 문자열에서 특정 문자 제거
모든 필드 목록을 작성하십시오.
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 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(); }
에 그린다 개체 :이제 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); }
) ) 보드 상단의 개별 위치에서 플레이어를 끌어옵니다.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(); } }
) :제안을 질문의 코드에 적용한 예를 참조하십시오. 이 예제는 게임 로직을 제공하지 않습니다. 단지 주사위를 플레이어의 위치에 추가하고 플레이어를 보드 주위로 움직입니다.