需要一点帮助让我的标题屏幕出现
Need a little help getting my title screen to appear
当我 运行 此代码时,我得到一个没有任何操作的空白屏幕。我从我完成的另一项任务中获取了 gameState 和 drawScreen,并且游戏运行良好,标题页出现在游戏之前。出于某种原因,我无法让它与这个游戏一起工作。我专注于我的 drawScreen 函数中的帮助,看看是否有我遗漏的东西,以及是否有我需要删除的东西。我再次尝试让标题屏幕出现,这样我在启动程序时就不会出现空白屏幕。
int numBubbles = 40; // initial bubble count
final int BROKEN = -99; // code for "broken", may have other states later
final int MAXDIAMETER = 120; // maximum size of expanding bubble
PVector[] ellipses = { new PVector(150, 150), new PVector(300, 150), new PVector(450, 150)};
int diam = 100;
int mouseClicks = 15;
int gameState;
public final int INTRO = 1;
public final int PLAY = 2;
public final int PAUSE = 3;
public final int GAMEOVERWIN = 4;
public final int GAMEOVERLOSE = 5;
PFont font;
ArrayList pieces; // all the playing pieces
// CHANGE: variable to keep score
int score = 0;
void setup() {
pieces = new ArrayList(numBubbles);
background(0);
size(640, 640);
font = createFont("Cambria", 32);
frameRate(24);
noStroke();
smooth();
for(int i = 0; i < numBubbles; i++) {
pieces.add(new Bubble(random(width), random(height), 30, i, pieces));
}
}
void mousePressed() {
// on click, create a new burst bubble at the mouse location and add it to the field
Bubble b = new Bubble(mouseX,mouseY,2,numBubbles,pieces);
// CHANGE: decrementing the score once so that score for new bubbles is not counted
--score;
b.burst();
pieces.add(b);
numBubbles++;
if (mouseButton == LEFT) { mouseClicks--; } else { mouseClicks = 15; }
}
void draw() {
switch(gameState)
{
case INTRO:
drawScreen("Welcome!", "Press s to start");
break;
case PAUSE:
drawScreen("PAUSED", "Press p to resume");
break;
case GAMEOVERWIN:
drawScreen("GAME OVER, YOU WON!", "Press s to try again");
break;
case GAMEOVERLOSE:
drawScreen("GAME OVER, YOU LOST", "Press s to try again");
break;
case PLAY:
background(0);
if(mouseClicks >= 0 && score !=40)
gameState = GAMEOVERLOSE;
else
gameState = GAMEOVERWIN;
for(int i = 0; i < numBubbles; i++) {
Bubble b = (Bubble)pieces.get(i); // get the current piece
if(b.diameter < 1) { // if too small, remove
pieces.remove(i);
numBubbles--;
i--;
}
else {
// check collisions, update state, and draw this piece
if(b.broken == BROKEN) { // only bother to check collisions with broken bubbles
b.collide();
}
}
b.update();
b.display();
}
// CHANGE: printing the score to screen
textSize(32);
fill(0, 102, 153);
text(score , 10, 30);
text(mouseClicks+"Clicks remaing", 0,600,width,height);
break;
}
}
void drawScreen(String title, String instructions)
{
background(0);
// draw title
fill(255,100,0);
textSize(60);
textAlign(CENTER, BOTTOM);
text(title, width/2, height/2);
// draw instructions
fill(255,255,255);
textSize(32);
textAlign(CENTER, TOP);
text(instructions, width/2, height/2);
}
您似乎从未设置过 gameState
的初始值。它将默认为 0
,这不是您的 switch 语句中的一种情况。
用 1 (int gameState = 1;
) 初始化变量,或者将 INTRO
大小写更改为零,您应该会看到介绍屏幕弹出。
当我 运行 此代码时,我得到一个没有任何操作的空白屏幕。我从我完成的另一项任务中获取了 gameState 和 drawScreen,并且游戏运行良好,标题页出现在游戏之前。出于某种原因,我无法让它与这个游戏一起工作。我专注于我的 drawScreen 函数中的帮助,看看是否有我遗漏的东西,以及是否有我需要删除的东西。我再次尝试让标题屏幕出现,这样我在启动程序时就不会出现空白屏幕。
int numBubbles = 40; // initial bubble count
final int BROKEN = -99; // code for "broken", may have other states later
final int MAXDIAMETER = 120; // maximum size of expanding bubble
PVector[] ellipses = { new PVector(150, 150), new PVector(300, 150), new PVector(450, 150)};
int diam = 100;
int mouseClicks = 15;
int gameState;
public final int INTRO = 1;
public final int PLAY = 2;
public final int PAUSE = 3;
public final int GAMEOVERWIN = 4;
public final int GAMEOVERLOSE = 5;
PFont font;
ArrayList pieces; // all the playing pieces
// CHANGE: variable to keep score
int score = 0;
void setup() {
pieces = new ArrayList(numBubbles);
background(0);
size(640, 640);
font = createFont("Cambria", 32);
frameRate(24);
noStroke();
smooth();
for(int i = 0; i < numBubbles; i++) {
pieces.add(new Bubble(random(width), random(height), 30, i, pieces));
}
}
void mousePressed() {
// on click, create a new burst bubble at the mouse location and add it to the field
Bubble b = new Bubble(mouseX,mouseY,2,numBubbles,pieces);
// CHANGE: decrementing the score once so that score for new bubbles is not counted
--score;
b.burst();
pieces.add(b);
numBubbles++;
if (mouseButton == LEFT) { mouseClicks--; } else { mouseClicks = 15; }
}
void draw() {
switch(gameState)
{
case INTRO:
drawScreen("Welcome!", "Press s to start");
break;
case PAUSE:
drawScreen("PAUSED", "Press p to resume");
break;
case GAMEOVERWIN:
drawScreen("GAME OVER, YOU WON!", "Press s to try again");
break;
case GAMEOVERLOSE:
drawScreen("GAME OVER, YOU LOST", "Press s to try again");
break;
case PLAY:
background(0);
if(mouseClicks >= 0 && score !=40)
gameState = GAMEOVERLOSE;
else
gameState = GAMEOVERWIN;
for(int i = 0; i < numBubbles; i++) {
Bubble b = (Bubble)pieces.get(i); // get the current piece
if(b.diameter < 1) { // if too small, remove
pieces.remove(i);
numBubbles--;
i--;
}
else {
// check collisions, update state, and draw this piece
if(b.broken == BROKEN) { // only bother to check collisions with broken bubbles
b.collide();
}
}
b.update();
b.display();
}
// CHANGE: printing the score to screen
textSize(32);
fill(0, 102, 153);
text(score , 10, 30);
text(mouseClicks+"Clicks remaing", 0,600,width,height);
break;
}
}
void drawScreen(String title, String instructions)
{
background(0);
// draw title
fill(255,100,0);
textSize(60);
textAlign(CENTER, BOTTOM);
text(title, width/2, height/2);
// draw instructions
fill(255,255,255);
textSize(32);
textAlign(CENTER, TOP);
text(instructions, width/2, height/2);
}
您似乎从未设置过 gameState
的初始值。它将默认为 0
,这不是您的 switch 语句中的一种情况。
用 1 (int gameState = 1;
) 初始化变量,或者将 INTRO
大小写更改为零,您应该会看到介绍屏幕弹出。