Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Game:
- package net.***.game;
- import java.awt.BorderLayout;
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.image.BufferStrategy;
- import java.awt.image.BufferedImage;
- import java.awt.image.DataBufferInt;
- import javax.swing.JFrame;
- import net.***.game.gfx.Colours;
- import net.***.game.gfx.Screen;
- import net.***.game.gfx.SpriteSheet;
- public class Game extends Canvas implements Runnable{
- private static final long serialVersionUID = 1L;
- public static final int WIDTH = 160;
- public static final int HEIGHT = WIDTH/12*9;
- public static final int SCALE = 3;
- public static final String NAME = "Game";
- private JFrame frame;
- public boolean running = false;
- public int tickCount = 0;
- private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
- private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
- private int[] colours = new int[36 * 6];
- private Screen screen;
- public InputHandler input;
- public Game() {
- setMinimumSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
- setMaximumSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
- setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
- frame = new JFrame(NAME);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLayout(new BorderLayout());
- frame.add(this, BorderLayout.CENTER);
- frame.pack();
- frame.setResizable(false);
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- public void init(){
- int index = 0;
- for(int r = 0; r < 6; r++){
- for(int g = 0; g < 6; g++){
- for(int b = 0; b < 6; b++){
- int rr = (r * 255 / 5);
- int gg = (g * 255 / 5);
- int bb = (b * 255 / 5);
- colours[index++] = rr << 16 | gg << 8 | bb;
- }
- }
- }
- screen = new Screen(WIDTH, HEIGHT, new SpriteSheet("/spritesheet.png"));
- input = new InputHandler(this);
- }
- private synchronized void start() {
- running = true;
- new Thread(this).start();
- }
- private synchronized void stop() {
- running = false;
- }
- public void run() {
- long lastTime = System.nanoTime();
- double nsPerTick = 1000000000D/60;
- int ticks = 0;
- int frames = 0;
- long lastTimer = System.currentTimeMillis();
- double delta = 0;
- init();
- while(running)
- {
- long now = System.nanoTime();
- delta += (now - lastTime) / nsPerTick;
- lastTime = now;
- boolean shouldRender = true;
- while(delta >= 1){
- ticks++;
- tick();
- delta -= 1;
- shouldRender = true;
- }
- try {
- Thread.sleep(2);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- if(shouldRender){
- frames++;
- render();
- }
- if(System.currentTimeMillis() - lastTimer >= 1000) {
- lastTimer += 1000;
- System.out.println(frames + " frames, " + ticks + " ticks.");
- frames = 0;
- ticks = 0;
- }
- }
- }
- public void tick() {
- tickCount++;
- if(input.up.isPressed()){
- screen.yOffset--;
- }
- if(input.down.isPressed()){
- screen.yOffset++;
- }
- if(input.right.isPressed()){
- screen.xOffset++;
- }
- if(input.left.isPressed()){
- screen.xOffset--;
- }
- }
- public void render() {
- BufferStrategy bs = getBufferStrategy();
- if(bs == null){
- createBufferStrategy(3);
- return;
- }
- for(int y = 0; y < 32; y++){
- for(int x = 0; x < 32; x++){
- boolean flipX = x % 2 == 0;
- boolean flipY = y % 2 == 0;
- screen.render(x << 3, y << 3, 0, Colours.get(555, 505, 055, 550), flipX, flipY);
- }
- }
- for(int y = 0; y < screen.height; y++){
- for(int x = 0; x < screen.width; x++){
- int colourCode = screen.pixels[x + y * screen.width];
- if(colourCode < 255) pixels[x + y * WIDTH] = colours[colourCode];
- }
- }
- Graphics g = bs.getDrawGraphics();
- g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
- g.drawRect(0, 0, getWidth(), getHeight());
- g.dispose();
- bs.show();
- }
- public static void main(String[] args)
- {
- new Game().start();
- }
- }
- Colours:
- package net.***.game.gfx;
- public class Colours {
- public static int get(int colour1, int colour2, int colour3, int colour4){
- return (get(colour4) << 24) + (get(colour3) << 16) + (get(colour2) << 8) + (get(colour1));
- }
- private static int get(int colour) {
- if(colour < 0) return 255;
- int r = colour/100%10;
- int g = colour/10%10;
- int b = colour%10;
- return r * 36 + g * 6 + b;
- }
- }
- Screen:
- package net.***.game.gfx;
- public class Screen {
- public static final int MAP_WIDTH = 64;
- public static final int MAP_WIDTH_MASK = MAP_WIDTH - 1;
- public int[] tiles = new int[MAP_WIDTH*MAP_WIDTH];
- public int[] colours = new int[MAP_WIDTH*MAP_WIDTH*4];
- public int[] pixels;
- public int xOffset = 0;
- public int yOffset = 0;
- public int width;
- public int height;
- public SpriteSheet sheet;
- public Screen(int width, int height, SpriteSheet sheet){
- this.width = width;
- this.height = height;
- this.sheet = sheet;
- pixels = new int[width*height];
- }
- public void render(int xPos, int yPos, int tile, int colour, boolean mirrorX, boolean mirrorY){
- xPos -= xOffset;
- yPos -= yOffset;
- int xTile = tile % 32;
- int yTile = tile / 32;
- int tileOffset = (xTile << 3) + (yTile << 3) * sheet.width;
- for(int y = 0; y < 8; y++){
- int ySheet = y;
- if(mirrorY)ySheet = 7 - y;
- if(y + yPos < 0 || y + yPos >= height) continue;
- for(int x = 0; x < 8; x++){
- int xSheet = x;
- if(mirrorX)xSheet = 7 - x;
- if(x + xPos < 0 || x + xPos >= width) continue;
- int col = (colour >> (sheet.pixels[xSheet + ySheet * sheet.width + tileOffset] * 8)) % 255;
- if(col < 255) pixels[(x + xPos) + (y + yPos) * width] = col;
- }
- }
- }
- }
- InputHandler:
- package net.***.game;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.util.ArrayList;
- import java.util.List;
- public class InputHandler implements KeyListener{
- public InputHandler(Game game){
- game.addKeyListener(this);
- }
- public class Key{
- private int numTimesPressed = 0;
- private boolean pressed = false;
- public boolean isPressed;
- public int getNumadadTimesPrsesed(){
- return numTimesPressed;
- }
- public boolean isPressed(){
- return pressed;
- }
- public void toggle(boolean isPressed) {
- pressed = isPressed;
- if(isPressed) numTimesPressed++;
- }
- }
- public Key up = new Key();
- public Key down = new Key();
- public Key left = new Key();
- public Key right = new Key();
- public void keyPressed(KeyEvent e) {
- toggleKey(e.getKeyCode(), true);
- }
- public void keyReleased(KeyEvent e) {
- toggleKey(e.getKeyCode(), false);
- }
- public void keyTyped(KeyEvent e) {
- }
- public void toggleKey(int keyCode, boolean isPressed){
- if(keyCode == KeyEvent.VK_W || keyCode == KeyEvent.VK_UP)
- {up.toggle(isPressed);
- }
- if(keyCode == KeyEvent.VK_A || keyCode == KeyEvent.VK_LEFT)
- {left.toggle(isPressed);
- }
- if(keyCode == KeyEvent.VK_S || keyCode == KeyEvent.VK_DOWN)
- {down.toggle(isPressed);
- }
- if(keyCode == KeyEvent.VK_D || keyCode == KeyEvent.VK_RIGHT)
- {right.toggle(isPressed);
- }
- }
- }
- SpriteSheet:
- package net.***.game.gfx;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- public class SpriteSheet {
- public String path;
- public int width;
- public int height;
- public int[] pixels;
- public SpriteSheet(String path){
- BufferedImage image = null;
- try {
- image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
- } catch (IOException e) {
- e.printStackTrace();
- }
- if(image == null)
- {
- return;
- }
- this.path = path;
- this.width = image.getWidth();
- this.height = image.getHeight();
- pixels = image.getRGB(0, 0, width, height, null, 0, width);
- for(int i = 0; i < pixels.length; i++)
- {
- pixels[i] = (pixels[i] & 0xff) / 64;
- }
- for(int i = 0; i < 8; i++){
- System.out.println(pixels[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement