Package org.filsa.nikujaga.pong

Source Code of org.filsa.nikujaga.pong.PongComponent

package org.filsa.nikujaga.pong;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;

import org.filsa.nikujaga.pong.entity.Ball;
import org.filsa.nikujaga.pong.entity.Field;
import org.filsa.nikujaga.pong.entity.Goalie;
import org.filsa.nikujaga.pong.math.Vec2;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

/**
* Provides the primary graphic interface (extends awt Canvas) for the game
* and also acts as the main game thread (implements Runnable).
*
*
*
* @author phil
*
*/
public class PongComponent extends Canvas implements Runnable {

  private static final long serialVersionUID = 2006465463990623227L;
    public static final int GAME_WIDTH = 600;
    public static final int GAME_HEIGHT = GAME_WIDTH * 3 / 4;
    public static final int FIELD_WIDTH = GAME_WIDTH * 9/ 10;
    public static final int FIELD_HEIGHT = GAME_HEIGHT * 9/ 10;
   
   
    public static final int SCALE = 1;
  private static final long SLEEP_TIME = 4;

  private boolean running = true;
  private boolean isPaused = false;

    private Logger log = LoggerFactory.getLogger(PongComponent.class);

    public PongComponent() {
        this.setPreferredSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE));
        this.setMinimumSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE));
        this.setMaximumSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE));
    }

  private void init() {
   
    // -- GRAPHICS init
    //setFocusTraversalKeysEnabled(false);
    requestFocus();   
    createBufferStrategy(2);
   
    // -- GAME init
    GameManager.INSTANCE.init(GAME_WIDTH, GAME_HEIGHT);
    this.addKeyListener(new InputHandler(GameManager.INSTANCE.keys));

  }

  private void render(Graphics g) {
    g.setColor(Color.black);
    g.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);

    GameManager.INSTANCE.render(g);
  }

  public void run() {
   
    log.info("Started run thread...");
    init();
   
    while (running) {//gameLoop
     
      GameManager.INSTANCE.handleInput(); // update state

      if (GameManager.INSTANCE.isPaused) {
        continue;
      }

      GameManager.INSTANCE.tick(); // update state

      BufferStrategy bs = this.getBufferStrategy();
      Graphics g = bs.getDrawGraphics();
     
      render(g);
     
      g.dispose();
      bs.show();
      Toolkit.getDefaultToolkit().sync();
      try {
        Thread.sleep(SLEEP_TIME);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }

  /**
   * Call to start the game.
   * Sets up the game thread and starts it.
   */
  public void start() {
        running = true;
        Thread thread = new Thread(this);
        thread.setPriority(Thread.MAX_PRIORITY);
        thread.start();
  }
 
  public void stop() {
    running = false;
  }
}
TOP

Related Classes of org.filsa.nikujaga.pong.PongComponent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.