I'm new to this but I could use some help with making a simple minigame. Is anybody able to help?

edited November 2016 in Questions about Code

I'm trying to code a little game using a 10x10 grid. That's really all I have so far but what I would like to do is to have a random square in the grid change to a random color and if it's clicked within a second the player scores a point. I also have no idea how to put a little scoreboard above the grid because it will mess it up I think. Could anyone point me in the right direction? Here's what I have so far:

size(500, 500);
background(0);
stroke(255);

for(int i=0; i<width; i+=50)
{
  line(i,0,i,height);
}

for(int w=0; w<height; w+=50)
{
  line(0,w,width,w);
}

Any help is really appreciated. Thanks in advance.

Tagged:

Answers

  • edited November 2016

    so, first of all, you should format properly by highlighting your code in the text box and hitting the "C" at the top of the text editor ;)

    you should create rectangles

    void setup() {
      size(500, 500); 
      background(0); 
      stroke(0);//stroke should be black so you can see outlines of the box
      int x=random(10);
      int y=random(10);
      for (int x=0; x<width/50; x++) {
        for (int y=0; y<height/50; y++) {
          rect(x*50, y*50, 50, 50);
        }
      }
    }
    

    edit: is the processing site down for you? processing.org

  • I just tried that now but I got an error saying "cannot convert from float to int" :( Yeah the sites down for me. Is it always like this?

  • this will change the rectangle you click on to a color, if you still need help just reply

    ArrayList<Rectangle> ra=new ArrayList<Rectangle>();
    class Rectangle{
      private int r,c;
      private color co;
      public Rectangle(int row, int col,color coll){
        r=row;
        c=col;
        co=coll;
      }
      void display(){
        stroke(0);
        fill(co);
        rect(r*50,c*50,50,50);
      }
      int getR(){
        return r;
      }
      int getC(){
        return c;
      }
    }
    
    void setup(){
      size(500,500);
      for(int x=0;x<width/50;x++){
        for(int y=0;y<height/50;y++){
          ra.add(new Rectangle(x,y,255));
        }
      }
    }
    
    void draw(){
      for(int i=0;i<ra.size();i++){
        ra.get(i).display();
      }
    }
    
    void mousePressed(){
      for(int i=0;i<ra.size();i++){
        if(ra.get(i).co==color(#9737ED)){
          ra.get(i).co=255;
        }
        if(ra.get(i).getR()==mouseX/50&&ra.get(i).getC()==mouseY/50){
          ra.get(i).co=color(#9737ED);
        }
      }
    }
    
  • edited November 2016

    edit: spawns random color now

    /**void setup() {
     size(500, 500); 
     background(0); 
     stroke(0);//stroke should be black so you can see outlines of the box
     int xp=(int)(random(10));
     int yp=(int)(random(10));
     for (int x=0; x<width/50; x++) {
     for (int y=0; y<height/50; y++) {
     rect(x*50, y*50, 50, 50);
     }
     }
     }*/
    ArrayList<Rectangle> ra=new ArrayList<Rectangle>();
    class Rectangle {
      private int r, c;
      private color co;
      public Rectangle(int row, int col, color coll) {
        r=row;
        c=col;
        co=coll;
      }
      void display() {
        stroke(0);
        fill(co);
        rect(r*50, c*50, 50, 50);
      }
      int getR() {
        return r;
      }
      int getC() {
        return c;
      }
    }
    
    void setup() {
      size(500, 500);
      for (int x=0; x<width/50; x++) {
        for (int y=0; y<height/50; y++) {
          ra.add(new Rectangle(x, y, 255));
        }
      }
      int i=(int)(random(ra.size()));
      ra.get(i).co=color(#9737ED);
    }
    
    void draw() {
      for (int i=0; i<ra.size(); i++) {
        ra.get(i).display();
      }
    }
    
    void mousePressed() {
      int ij=(int)(random(ra.size()));
      ra.get(ij).co=color(#9737ED);
      /**for (int i=0; i<ra.size(); i++) {
       if (ra.get(i).co==color(#9737ED)) {
       ra.get(i).co=255;
       }*/
      /**if(ra.get(i).getR()==mouseX/50&&ra.get(i).getC()==mouseY/50){
       ra.get(i).co=color(#9737ED);
       }
       }*/
      for (int i=0; i<ra.size(); i++) {
        /**if (ra.get(i).co==color(#9737ED)) {
         ra.get(i).co=255;
         }*/
        if ((ra.get(i).getR()==mouseX/50&&ra.get(i).co==color(#9737ED))&&(ra.get(i).getC()==mouseY/50&&ra.get(i).co==color(#9737ED))) {
          ra.get(i).co=255;
        }
        else{fill(0);text("YOU LOSE",width/4,height/2);}
      }
    }
    

    there is no "losing" but if you click it will disappear otherwise, there is a new color and you can't win

  • I'm really lost now lol Not sure what everything is doing or where it's supposed to be

  • please don't post duplicate questions, it confuses people.

    i have deleted the other.

  • the rectangle code you rejected was a better start imo.

    break the problem into small steps.

    use that code and work out how highlight a single rectangle (pick a random x and y, change the fill colour and redraw just that rectangle)

    then work out how to map the current mouse position to a given rectangle within the grid. it's just a matter of division.

    then check whether the clicked rect is the highlighted rect.

    worry about the timing later.

  • I'm really lost now lol Not sure what everything is doing or where it's supposed to be

    and this is why we (some of us) don't just post full solutions without explanation but rather try and ask questions to get you thinking about things.

    but 'Rejected Answer' 8(

Sign In or Register to comment.