Second lecture 9:30am. 

If I want to play with this game:

http://silo.cs.indiana.edu:8346/c212/spr2014/tetris/tutorial.phps

I want to design my own. 

Here's what we might be able to cover today:

https://www.cs.indiana.edu/classes/c212/spr2014/final/stageOne/

How do we start? 

I have a Game and it has a World inside:

import javax.swing.*; 

class Game extends JFrame {
  Game() {
    World world = new World();
    this.add(world);
    this.addKeyListener( world ); 
    this.setVisible(true); 
    this.setSize(200, 400); 
    this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    world.start(); 
  }
  public static void main(String[] args) {
    Game game = new Game(); 
  }
}

The World reacts to key events that the game sends. 

The World also has a timer and it reacts to the action events it generates. 

import javax.swing.*; 
import java.awt.*;
import java.awt.event.*; 

class World extends JComponent implements KeyListener, ActionListener {
  int count;
  public void actionPerformed(ActionEvent e) {
    this.count += 1; 
    System.out.println("Ouch..." + this.count);  
  }
  // http://silo.cs.indiana.edu:8346/cgi-bin/c212/spr2014/textbook?action=next&pic=seveneleven/image111.jpg
  // Using Timer events for animation chapter 11 page 533
  Timer timer;
  World() {
    this.timer = new Timer(1000, this); 
  }
  void start() {
    this.timer.start();  
  }
  public void keyPressed(KeyEvent e) { 
    int value = e.getKeyCode();
    System.out.println("Key pressed: " + (char) value); 
    switch (value) {
      // http://docs.oracle.com/javase/6/docs/api/java/awt/event/KeyEvent.html#VK_LEFT
      case KeyEvent.VK_LEFT : System.out.println("Left arrow pressed."); break;
      case KeyEvent.VK_RIGHT: System.out.println("Right arrow pressed."); break;
      case KeyEvent.VK_UP   : System.out.println("Up arrow pressed."); break;
      case KeyEvent.VK_DOWN : System.out.println("Down arrow pressed."); break;
      case KeyEvent.VK_SPACE: System.out.println("Space bar pressed."); break;
      default: System.out.println("Unknown key pressed."); break; 
    }
  }
  public void keyReleased(KeyEvent e) { }
  public void keyTyped(KeyEvent e) { }
  public void paintComponent(Graphics g) {
    // this.count += 1;
    // System.out.println("I am being called... " + this.count); 
    // g.drawString("Called " + this.count + " times.", 10, 50); 
    g.drawString("Welcome to Tetris!", 50, 50); 
  }
}

How about the actors in the world?

http://silo.cs.indiana.edu:8346/c212/spr2014/04212014/story_files/image007.jpg

import java.awt.*;

class Shape {
  int row, column; 
  Color color;  
  int direction;
  int[][][] rotations; 
  void rotateRight() {
     this.direction = (this.direction + 1) % 4;
  }
  void rotateLeft() {
      this.rotateRight(); 
      this.rotateRight(); 
      this.rotateRight(); 
  }
  Shape(int row, int column) {
    this.row = row; 
    this.column = column; 
  }
}

So I set up a Shape class and prepare to extend it as necessary: 

import java.awt.*; 

class SShape extends Shape {
  SShape(int row, int column) {
    super(row, column);
    this.color = new Color(255, 255, 0); 
    this.rotations = new int[][][] {
      { {0, 1, 1},
        {1, 1, 0},
        {0, 0, 0} },
      { {0, 1, 0},
        {0, 1, 1},
        {0, 0, 1} },
      { {0, 0, 0},
        {0, 1, 1},
        {1, 1, 0} },
      { {1, 0, 0},
        {1, 1, 0},
        {0, 1, 0} },
      
    };
  }
}

Here's another extension:

import java.awt.*; 

class TShape extends Shape {
  TShape(int row, int column) {
    super(row, column);
    this.color = new Color(255, 255, 255); 
    this.rotations = new int[][][] {
      { {1, 1, 1},
        {0, 1, 0},
        {0, 0, 0} },
          
      { {0, 0, 1},
        {0, 1, 1},
        {0, 0, 1} },
          
      { {0, 0, 0},
        {0, 1, 0},
        {1, 1, 1} },
          
      { {1, 0, 0},
        {1, 1, 0},
        {1, 0, 0} },
      
    };
  }
}

Now let's draw these things.

--