Month: January 2013

Pixel Detection Code

Cracking game idea this week. A maze.  Draw up a complex maze – then work your way through it.  Winner is best time.

Two ways to implement it.  First is to say create a bunch of objects.  Then use collision detection on those objects. Thats a lot of work.  Second way it to draw the matrix – then use pixel collision detection to determine if the player can move – a bit easier.  Here is an example app with a simple matrix.

 

maze

 

PImage maze;
boolean collision;
PrintWriter output;

int px,py,pw,ph;

void setup()

{

    size(200,200);
    
    output = createWriter("positions.txt"); 
    
    maze = loadImage("maze.png");
    image(maze, 0, 0); // Displays the image from point (0,0) 
    maze.loadPixels();
    
    for (int y = 0; y < maze.height-1; y++) { // Skip top and bottom edges
    for (int x = 0; x < maze.width-1; x++) { // Skip left and right edges
          // Calculate the adjacent pixel for this kernel point
          
          
          int pos =  y *maze.width + x;
          color c = maze.pixels[pos];
          output.println("Pixel: " + x + " y: " +  c);
          int alpha = (c >> 24) & 0xFF;
          int red   = (c >> 16) & 0xFF;
          int green = (c >> 8)  & 0xFF;
          int blue  =  c        & 0xFF;
          output.println("Color Map: " + alpha + " " + red + " " + green + " " + blue);
                   
          
        }
  }
  
  output.flush(); // Writes the remaining data to the file
  output.close(); // Finishes the file
  
  
}



void draw()
{

 background(255);
 fill(255,0,0);
 
 
  image(maze, 0, 0); // Displays the image from point (0,0) 
  maze.loadPixels();
  // Create an opaque image of the same size as the original
 //PImage edgeImg = createImage(maze.width, maze.height, RGB);
  // Loop through every pixel in the image.
  for (int y = 0; y < maze.height-1; y++) { // Skip top and bottom edges
    for (int x = 0; x < maze.width-1; x++) { // Skip left and right edges
          // Calculate the adjacent pixel for this kernel point
          
           int pos =  y *maze.width + x;
          color c = maze.pixels[pos];
          
          int alpha = (c >> 24) & 0xFF;
          int red   = (c >> 16) & 0xFF;
          int green = (c >> 8)  & 0xFF;
          int blue  =  c        & 0xFF;
          
          if ( alpha != 0 ) // is this a non transparent pixel ?
          {
              collision = insideBox(x, y, mouseX-4, mouseY-4,8,8);
              
              if ( collision ) 
              {
                fill(0,255,0);              
                
              }
            
          }
          
          
        }
  }
        
   rect(mouseX-4, mouseY-4, 8,8);
  
}


boolean insideBox(int x, int y, int p1x, int p1y, int w, int h)
// This code checks the cursor is inside the window... 
{
  // This code will return a false if the cursor is not in the button
  if (x > p1x  && x < p1x + w && y > p1y  &&  y  < p1y + h  ) {
       // the cursor is in the box
       return true;
  }
  else {
       // the cursor is not in the box
       return false;
  }
}

Helicopter Game

The helicopter game should be dodging missiles not clouds 🙂

So…

C-802

This is also in DropBox – in sketches\images

This is a PNG image – it has a transparent background.

The best way is to find an image that already is PNG and has a transparent background.

If you find a JPEG – you can down an application call Paint.NET – its free.  http://www.getpaint.net/

Load up your jpeg and then use the magic want to select the background you want to remove.  Then just delete it and save as a PNG – Magic!

 

 

 

Rectangle Collision Detection

This is a simple article on how we can detect if two rectangles are in collision.

You must check this with every frame that is drawn – so it will be part of your main loop.

The based approach is say to have  a rectangular player shape and a rectangular object.

You could take each point of the player rectangle and simply check if it inside the object rectangle.  That will work.

If you remember your coordinate space

These are the rectangles we want to detect.

So you can use a single call to determine if they are in collision:

/**
 * Check if two rectangles collide
 * x_1, y_1, width_1, and height_1 define the boundaries of the first rectangle
 * x_2, y_2, width_2, and height_2 define the boundaries of the second rectangle
 */
boolean rectangle_collision(float x_1, float y_1, float width_1, float height_1, float x_2, float y_2, float width_2, float height_2)
{
  return !(x_1 > x_2+width_2 || x_1+width_1 < x_2 || y_1 > y_2+height_2 || y_1+height_1 < y_2);
}

Assignment 4 – Add an Image

Add an image to the player or doodle jump sketch

The existing draw code is in the player class – in a function called display()

It sets the color and then draws a rectangle.

You need to replace that code with an image of your choice.  Use the processing image here http://processing.org/reference/image_.html

Place the variable just above the setup function:

PImage img;

Then load the image in your setup function

then replace the rectangle call with this

image(img, x,y,100,100);  ( replace 100 with the image size you want ) 

If you can use .png format file then all the better

Code Club – Ideas

We now have some initial designs and ideas for the first apps for the code clubs

1 – Platform game – dodgeblock – a working app that will have blocks moving and a player trying to avoid them.

2 – A game thats like the iPhone app – most addictive app.  It should be easy enough to do

3 – A horizontal platform game – the objective is to move from one size left to right

4 – Snake dodge game – using the snake to avoid bubbles or blocks.

All great ideas – we’ll do the development in January – but we need them done!!!! – Homework

Player Class

This is only the player class. It allows you to define a player then move it around the screen. Note the calls in the main loop are very few

 
class Player
{
  
  static final float gravity = 0.14;
  static final float friction = 0.03;
  static final float bounceVel = 6.1;
  
  PImage pimage=null;  // The player image
  public int pWidth=20;
  public int pHeight=20;
  public float maxYVel = 2;
  public float maxXVel = 2;
  public float acceleration=2;
  public boolean useGravity=false; 
  public boolean deadStop=false;
  public boolean edgeStop=false;
  
  float x, y, xVel, yVel;
  int w, h;
  Player(int x, int y)
  {
    this.x = x;
    this.y = y;
    w = h = 20;
    this.useGravity=false;
  }
  
  void display()
  {
    fill(204,0,0);
    rect(x,y,pWidth,pHeight);
  }
  
  void move()
  {
    x += xVel;
    y += yVel;
  
    
    // horizontal
    if (gameOver)
    {
       println("GAME OVER");
       exit();
    }
   
    if (aPressed) 
    {
      xVel -= acceleration;
    }
    else if (sPressed) 
    {
      xVel += acceleration;
    }
    else if (wPressed) 
    {
      yVel -= acceleration;
    }
    else if (zPressed) 
    {
      yVel += acceleration;
    }
    else if (jPressed) 
    {
      yVel = -5;
      deadStop=false;
      useGravity=true;
    }

    else 
    {
      if (deadStop) {
        xVel=0;
        yVel=0;
      }
      
      
    }
    
    
    // X Friction
    if (xVel > 0)
    {
      xVel -= friction;
    }
    else
    {
      xVel += friction;
    }
    
    // Y Friction
    if (yVel > 0)
    {
      yVel -= friction;
    }
    else
    {
      yVel += friction;
    }
    
    // check wrap around before a move
    if (edgeStop)
    {
      if (x > width-w) x = width-w;
      if (x < 0) x = 0;
    
      if (y> height-h) y=height-h;
      if (y<0) y = 0;
    }
    else
    {
      if (x > width-w) x = 0;
      if (x < 0) x = width-w;
    
      if (y> height-h) y=0;
      if (y<0) y = height-h;
      
    }
  
    
    // Check limits on all movement of the player
    if (abs(xVel) <= friction) 
    {
      xVel = 0;
    }
    if (abs(yVel) <= friction) 
    {
      yVel = 0;
    }
    // vertical - always overide - can stop Gravity
    if (useGravity)
    {
      yVel += gravity;
    }
      
    // Limit max velocity
    xVel = min(maxXVel, xVel);
    xVel = max(-maxXVel, xVel);
    // Limit max velocity
    yVel = min(maxYVel, yVel);
    yVel = max(-maxYVel, yVel);
    
    println(xVel);
    println(yVel);
  
    
  }
}


// This is where the sketch starts 
Player p;
boolean wPressed, aPressed, sPressed, zPressed, jPressed;
int score, fallCount;
boolean gameOver;
  
void setup()
{
  size(320, 480);
  frameRate(60);
  initialize();
}
  
void initialize()
{
  score = 0;
  fallCount = 0;
  gameOver = false;
  
  // Create a new player
  p = new Player(width/2, height/2);
  //p.deadStop = true;
  //p.edgeStop=true;

}
  
void draw()
{
  //println(score);
  background(204);
  p.display();
  p.move();
  
//  if (fallCount > 90 ) initialize();
}
  
  
  
void keyPressed()
{
  if (key == 'w') wPressed = true;
  if (key == 'a') aPressed = true;
  if (key == 's') sPressed = true;
  if (key == 'z') zPressed = true;
  if (key == 'j') jPressed = true;
}
  
void keyReleased()
{
  if (key == 'w') wPressed = false;
  if (key == 'a') aPressed = false;
  if (key == 's') sPressed = false;
  if (key == 'z') zPressed = false;
  if (key == 'j') jPressed = false;
}