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;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s