Almost a game

Add sounds, lives, graphics, timers, bosses, etc...

var player;
var bullets = [];
var enemies = [];

function setup() {
  createCanvas(600, 450);
    
  // our player is born
  player = new Player();    
}

function draw() {
  background(0);
    
  // display the player
  player.display();   
  
  // how many bullets?    
  fill(255);
  text(bullets.length, 25, 25);
  
  // display the bullets
  for(var i = 0; i < bullets.length; i++)
  {
    bullets[i].display();
      
    // check to see if the bullet is hitting the enemy
    for(var k = 0; k < enemies.length; k++)
    {
        if(dist(bullets[i].x, bullets[i].y, enemies[k].x, enemies[k].y) < 20)
        {
            enemies.splice(k, 1);
        }
    }
      
    // get rid of stray bullets
    if(bullets[i].y < 0)
    {
      bullets.splice(i, 1);
    }
  }
    
  // spawn random enemies
  var hatch = random(0, 100);
  if(hatch < 1)
  {
    enemies.push(new Enemy());  
  }
    
  for(var i = 0; i < enemies.length; i++)
  {
    enemies[i].display();
      
    // get rid of extra enemies
    if(enemies[i].x > width)
    {
        enemies.splice(i, 1);
    }
  }
  
}

function keyPressed()
{
    if(keyCode === RIGHT_ARROW)
    {
        player.direction = "right";
    }
    if(keyCode === LEFT_ARROW)
    {
        player.direction = "left";
    }
    if(key === " ")
    {
        bullets.push(new Bullet(player.x + 10, player.y));
    }
}

function Player()
{
    this.x = width / 2;
    this.y = height - 100;
    this.direction = "stopped";
    
    this.display = function()
    {
        // make it wrap
        if(this.x > width)
        {
            this.x = 0;
        }
        if(this.x < 0)
        {
            this.x = width;
        }
        
        // which way are we going
        if(this.direction === "right")
        {
            this.x = this.x + 3;
        }
        if(this.direction === "left")
        {
            this.x = this.x - 3;
        }
        
        
        fill(0, 255, 0);
        rect(this.x, this.y, 20, 20);
    }
}

function Bullet(startingX, startingY)
{
    this.x = startingX;
    this.y = startingY;
    
    this.display = function()
    {
        this.y = this.y - 5;
        fill(255);
        ellipse(this.x, this.y, 5, 5);
    }
}

function Enemy()
{
    this.x = 0;
    this.y = random(0, 300);
    this.speed = random(1, 5);
    
    this.display = function()
    {
        this.x = this.x + this.speed;
        
        fill(255, 0, 0);
        ellipse(this.x, this.y, 20, 20);
    }
}

Here is the code for adding a "gamestate" to your game. This is the same code that we wrote in class