processing game
processing吧
全部回复
仅看楼主
level 1
Ball[] balls = new Ball[16];/////
////////////////////////////////
int score;
int n = 8;
int dia = 70;
///////////////////////////////////
color[] colors = {
color(252, 36, 36, 178), //RED
color(248, 114, 23, 178), //ORANGE
color(255, 255, 0, 178), //YELLOW
color(76, 196, 23, 178), //GREEN
color(56, 172, 236, 178), //BLUE
color(127, 56, 235, 178), // PURPLE
color(128, 178), //GREY
color(255, 193, 230, 178), //PINK
color(127, 56, 235, 178), // PURPLE
color(248, 114, 23, 178), //ORANGE
color(255, 193, 230, 178), //PINK
color(255, 255, 0, 178), //YELLOW
color(56, 172, 236, 178), //BLUE
color(76, 196, 23, 178), //GREEN
color(248, 114, 23, 178), //ORANGE
color(252, 36, 36, 178), //RED
color(128, 178), //GREY
};
int index;
/////////////////////////////////////////////////
String[] headlines = {
"ORANGE",
"GREY",
"PINK",
"YELLOW",
"GREEN",
"RED",
"BLUE",
"PURPLE",
};
int index1;
////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
int INTRO = 0;
int RUN_GAME = 1;
int GAME_OVER = 2;
int gameState = 1;
void setup() {
size(1000, 700);
imageMode(CENTER);
smooth();
noStroke();
pick();
//////////////////////////////////
for (int i=0; i<balls.length; i++) {
balls[i] = new Ball(random(20, width-20), random(20, height/2-100), dia, 10.0);//size, speed
}
///////////////////////////////////////////////
}
void draw() {
if (gameState == INTRO) intro();
else if (gameState == RUN_GAME) runGame();
else if (gameState == GAME_OVER) gameOver();
}
void intro() {
background(0);
textSize(60);
fill(255);
textAlign(CENTER, BOTTOM);
text("COLOUR\nCHALLENGE", width/2, height/2);
fill(255);
textSize(20);
text("Click anywhere to start", width/2, height/2+height/4);
}
///////////////////////////////////////////////////////////////////////
///////////////RUN GAME STATE
void runGame() {
background(255);
textSize(60);
fill(colors[index]);
textAlign(CENTER, BOTTOM);
text(headlines[index1], width/2, height/2);
if (mousePressed) {
index1 = (index1 + 1) % headlines.length;
printStringToInts(headlines[index1]);
}
//////////////////////////////////////////////////////////
for (int i=0; i<balls.length; i++) {
fill(colors[i]);
balls[i].constrained();
balls[i].update();
balls[i].display();
}
if (mousePressed) {
for (int i=0; i<balls.length; i++) {
if (pow(mouseX-balls[i].x, 2)+pow(mouseY-balls[i].y, 2) < pow(balls[i].D/2, 2)) {
balls[i].unfreeze();
break;
}
}
}
////////////////////////////////////////////////////////////
score++;
fill(0, 255, 0);
textSize(16);
textAlign(LEFT);
text(score, 10, 20);
}
///////////////////////////////////////////////////////////
void printStringToInts(String s) {
// for the length of the String
for (int i=0; i<s.length(); i++) {
// convert each character in the string to an int
// and print it to the console (with a comma added)
print(int(s.charAt(i)) + ",");
}
/////////////////////////////////////////////////////////
}
/////////////////////////////////////////////////////////////
void pick() {
index = int(random(colors.length));
}
void mousePressed() {
pick();
}
void keyPressed() {
pick();
}
///////////////////////////////////////////////////////////////////////////
////////////////GAME OVER STATE
void gameOver() {
fill(255);
textAlign(CENTER);
textSize(48);
text("GAME OVER", width/2, height/2);
textSize(16);
textAlign(CENTER);
text("Click to try again", width/2, height/2+height/4);
}
class Ball {
float x, y;
float D;
float dy;
boolean frozen;
Ball(float x_, float y_, float D_, float dy_) {
x = x_;
y = y_;
D = D_;
dy = dy_;
frozen=true;
}
void unfreeze() {
frozen = false;
}
void freeze() {
frozen = true;
}
void update() {
if (!frozen) y=y+dy;
}
void constrained() {
if (y+D/2>height) freeze();
}
void display() {
noStroke();
ellipse(x, y, D, D);
}
}
请问有没有大神知道 怎么点错颜色就会GameOver?
2017年04月15日 19点04分 1
level 3
可以加个while循环 条件不成立时退出循环
2017年04月16日 12点04分 2
就是条件那里很难写啊…不然用Boolean也可以吧!
2017年04月16日 18点04分
大致看了以下你的代码,建议把关于球的操作都放到类里面。。在类的构造函数中加一个string变量,在初始化的时候把headline里的字符串传入类,然后类根据传入的字符串填充颜色。比较的条件就是把主程序当前的headline字符串与点击的class ball里面的headline对比,如果二者不相等,gameover
2017年04月18日 02点04分
@冷幕※極端 看你这里面还能点击继续,可以用noLoop()结束 if(class ball里面的headline==主程序当前的headline){ gameover(); noLoop() } if(游戏结束){ loop(); } 这几行代码放在mousePressed里
2017年04月18日 02点04分
@风萧飒兮 有偿求助15951930883
2018年03月21日 02点03分
level 3
闲着没事按照你的那个改写了一下,还有一些功能没实现,比如屏幕上球没有的颜色,在中间的字符串不会再显示;球在初始化的时候让他们有一定的间隔,别挤在一起;还可以在每次选择的时候加上一定的时间限制。。。时间仓促没来得及实现。。注释也没咋加,楼主凑合着看吧,,。。
Ball[] balls = new Ball[16];
Text te;
int score;
int n = 8;
int dia = 70;
color[] colors = {
color(252, 36, 36, 178), //RED
color(248, 114, 23, 178), //ORANGE
color(255, 255, 0, 178), //YELLOW
color(76, 196, 23, 178), //GREEN
color(56, 172, 236, 178), //BLUE
color(255, 193, 230, 178), //PINK
color(127, 56, 235, 178), // PURPLE
color(128, 178), //GREY
};
int index;
String[] headlines = {
"ORANGE",
"GREY",
"PINK",
"YELLOW",
"GREEN",
"RED",
"BLUE",
"PURPLE",
};
int index1=int(random(0,8));
boolean Gameover=false;
void setup() {
size(1000, 700);
smooth();
noStroke();
startGame();
textSize(60);
textAlign(CENTER, BOTTOM);}
void draw() {
background(255);
te.display();
for(int i=0;i<16;i++){
balls[i].display();
}
}
void mousePressed(){
if(Gameover==true){
loop();
startGame();
Gameover=false;
}
for(int i=0; i<balls.length; i++){
if(balls[i].over==true){
if(balls[i].colour!=te.h){
background(200);
gameOver();
Gameover=true;
noLoop();
}
}
}
}
void mouseReleased(){
te=new Text(headlines[int(random(0,8))],colors[int(random(0,8))]);
}
void gameOver() {
fill(255);
textAlign(CENTER);
textSize(48);
text("GAME OVER", width/2, height/2);
textSize(16);
textAlign(CENTER);
text("Click to try again", width/2, height/2+height/4);
}
void startGame(){//初始化两个类
background(255);
for (int i=0; i<balls.length; i++) {
balls[i] = new Ball(random(20, width-20), random(20, height/2-100), dia, 10.0,headlines[int(random(0,8))]);//size, speed
}
te=new Text(headlines[int(random(0,8))],colors[int(random(0,8))]);
}class Ball {
float x, y;
float D;
float dy;
boolean over=false;
boolean press=false;
String colour;
Ball(float x_, float y_, float D_, float dy_,String c) {
x = x_;
y = y_;
D = D_;
dy = dy_;
colour=c;
}
void display() {
if(dist(mouseX,mouseY,x,y)<=D/2){//判断鼠标是否在圆上
over=true;
}else{
over=false;
}
if((over==true)&&mousePressed){
press=true;
}
if((press==true)&&(y<height-D/2)){
y+=dy;
}
switch(colour){
case "ORANGE":fill(248, 114, 23, 178);break;
case "GREY":fill(128, 178);break;
case "PINK":fill(255, 193, 230, 178);break;
case "YELLOW":fill(255, 255, 0, 178);break;
case "GREEN":fill(76, 196, 23, 178);break;
case "RED":fill(252, 36, 36, 178);break;
case "BLUE":fill(56, 172, 236, 178);break;
case "PURPLE":fill(127, 56, 235, 178);break;
default:fill(0);break;
}
noStroke();
ellipse(x, y, D, D);
}
}
class Text{//这个类的功能是在页面中央显示字符串。俩成员变量 ,一个字符串一个名字,初始化见start Game()
String h;
color c;
Text(String th,color tc){
h=th;
c=tc;
textSize(60);
textAlign(CENTER);
}
void display(){
fill(c);
text(h, width/2, height/2);
}
}
2017年04月18日 04点04分 3
跑了一下,说不能在colour之间switch,只能在数字之间switch。。。
2017年04月24日 02点04分
我电脑上跑没问题啊,,case的是字符串。是版本问题吗。。我用最新版本
2017年04月24日 15点04分
@风萧飒兮 请问能帮我改一下吗 我的随机出现不知道怎么弄 也是刚学 QQ982522885
2017年12月17日 13点12分
1