Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Game.java ---------------------------------------------------------------------------------------------
- package com.gmail.olle.fjallstrom.cries;
- import java.awt.Canvas;
- 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 com.gmail.olle.fjallstrom.cries.Game;
- import com.gmail.olle.fjallstrom.graphics.Screen;
- import com.gmail.olle.fjallstrom.input.Keyboard;
- import com.gmail.olle.fjallstrom.penguins.entity.mob.Player;
- import com.gmail.olle.fjallstrom.penguins.level.Level;
- import com.gmail.olle.fjallstrom.penguins.level.RandomLevel;
- public class Game extends Canvas implements Runnable{
- private static final long serialVersionUID = 1L;
- public static int width = 300;
- public static int height = width / 16 * 10;
- public static int scale = 3;
- public static String title = "Penguins";
- private Thread thread;
- private JFrame frame;
- private boolean running = false;
- private Screen screen;
- private RandomLevel level;
- private Keyboard key;
- private Player player;
- private BufferedImage image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB);
- private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
- public Game() {
- Dimension size = new Dimension(width * scale, height * scale);
- setPreferredSize(size);
- setFocusable(true);
- screen = new Screen(width, height);
- frame = new JFrame();
- key = new Keyboard();
- level = new RandomLevel(64, 64);
- player = new Player(key);
- addKeyListener(key);
- }
- public synchronized void start() {
- running = true;
- thread = new Thread(this, "Display");
- thread.start();
- }
- public synchronized void stop() {
- running = false;
- try {
- thread.join();
- } catch (InterruptedException e) {
- e.printStackTrace ();
- }
- }
- public void run() {
- long lastTime = System.nanoTime();
- long timer = System.currentTimeMillis();
- final double ns = 1000000000.0 / 60;
- double delta = 0;
- int frames = 0;
- int updates = 0;
- while (running) {
- long now = System.nanoTime();
- delta += (now - lastTime) / ns;
- lastTime = now;
- while (delta >= 1) {
- update();
- updates++;
- delta--;
- }
- render();
- frames++;
- if (System.currentTimeMillis() - timer > 1000) {
- timer += 1000;
- frame.setTitle(title + " | " + updates + " ups," + frames + " fps");
- updates = 0;
- frames = 0;
- }
- }
- }
- public void update() {
- key.update();
- player.update();
- }
- public void render() {
- BufferStrategy bs = getBufferStrategy();
- if (bs == null) {
- createBufferStrategy(3);
- return;
- }
- screen.clear();
- int xScroll = player.x - screen.width / 2;
- int yScroll = player.y - screen.height / 2;
- level.render(xScroll, yScroll, screen);
- player.render(screen);
- for (int i = 0; i < pixels.length; i++) {
- pixels[i] = screen.pixels[i];
- }
- Graphics g = bs.getDrawGraphics();
- g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
- g.dispose();
- bs.show();
- }
- public static void main(String[] args) {
- Game game = new Game();
- game.frame.setResizable(false);
- game.frame.setTitle(Game.title);
- game.frame.add(game);
- game.frame.pack();
- game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- game.frame.setLocationRelativeTo(null);
- game.frame.setVisible(true);
- game.start();
- }
- }
- Screen.java -------------------------------------------------------------------------------------------
- package com.gmail.olle.fjallstrom.graphics;
- import java.util.Random;
- import com.gmail.olle.fjallstrom.penguins.level.Tiles;
- public class Screen {
- public int width;
- public int height;
- public int[] pixels;
- public static int MAP_SIZE = 64;
- public static int MAP_SIZE_MASK = MAP_SIZE - 1;
- public int xOffset, yOffset;
- public int[] tile = new int[MAP_SIZE * MAP_SIZE];
- private Random random = new Random();
- int xtime = 0, ytime = 0;
- int counter = 0;
- public Screen (int width, int height) {
- this.width = width;
- this.height = height;
- pixels = new int[width * height];
- for (int i = 0; i < MAP_SIZE * MAP_SIZE; i++) {
- tile[i] = random.nextInt(0xffffff);
- }
- }
- public void clear() {
- for (int i = 0; i < pixels.length; i++) {
- pixels[i] = 0;
- }
- }
- public void renderTile(int xp, int yp, Tiles tile) {
- xp -= xOffset;
- yp -= yOffset;
- for (int y = 0; y < tile.sprite.SIZE; y++) {
- int ya = y + yp;
- for (int x = 0; x < tile.sprite.SIZE; x++) {
- int xa = x + xp;
- if (xa < -tile.sprite.SIZE || xa >= width || ya < 0 || ya >= height) break;
- if (xa < 0) xa = 0;
- pixels[xa + ya * width] = tile.sprite.pixels[x + y * tile.sprite.SIZE];
- }
- }
- }
- public void renderPlayer(int xp, int yp, Sprite sprite) {
- xp -= xOffset;
- yp -= yOffset;
- for (int y = 0; y < 32; y++) {
- int ya = y + yp;
- for (int x = 0; x < 32; x++) {
- int xa = x + xp;
- if (xa < -32 || xa >= width || ya < 0 || ya >= height) break;
- if (xa < 0) xa = 0;
- int col = sprite.pixels[x + y * 32];
- if (col != 0xffb200ff)
- pixels[xa + ya * width] = sprite.pixels[x + y * 32];
- }
- }
- }
- public void setOffset(int xOffset, int yOffset) {
- this.xOffset = xOffset;
- this.yOffset = yOffset;
- }
- }
- Sprite.java -------------------------------------------------------------------------------------------
- package com.gmail.olle.fjallstrom.graphics;
- public class Sprite {
- public final int SIZE;
- private int x, y;
- public int[] pixels;
- private SpriteSheet sheet;
- private int i;
- public static Sprite snow = new Sprite(16, 0, 0, SpriteSheet.tiles);
- public static Sprite voidSprite = new Sprite(16, 0x1B87E0);
- //Size, x, y, SpriteSheet.tiles
- public static Sprite player_back = new Sprite(32, 1, 7, SpriteSheet.tiles);
- public static Sprite player_forward = new Sprite(32, 0, 7, SpriteSheet.tiles);
- public static Sprite player_left = new Sprite(32, 2, 7, SpriteSheet.tiles);
- public static Sprite player_right = new Sprite(32, 3, 7, SpriteSheet.tiles);
- public static Sprite player_forward_1 = new Sprite(32, 0, 6, SpriteSheet.tiles);
- public static Sprite player_forward_2 = new Sprite(32, 0, 5, SpriteSheet.tiles);
- public static Sprite player_left_1 = new Sprite(32, 2, 6, SpriteSheet.tiles);
- public static Sprite player_left_2 = new Sprite(32, 2, 5, SpriteSheet.tiles);
- public static Sprite player_right_1 = new Sprite(32, 3, 6, SpriteSheet.tiles);
- public static Sprite player_right_2 = new Sprite(32, 3, 5, SpriteSheet.tiles);
- public static Sprite player_back_1 = new Sprite(32, 1, 6, SpriteSheet.tiles);
- public static Sprite player_back_2 = new Sprite(32, 1, 5, SpriteSheet.tiles);
- public Sprite(int size, int x, int y, SpriteSheet sheet) {
- SIZE = size;
- pixels = new int[SIZE * SIZE];
- this.x = x * size;
- this.y = y * size;
- this.sheet = sheet;
- load();
- }
- public Sprite(int size, int colour) {
- SIZE = size;
- pixels = new int[SIZE * SIZE];
- setColour(colour);
- }
- private void setColour(int colour) {
- for (int i = 0; i < SIZE * SIZE; i++);
- pixels[i] = colour;
- }
- private void load() {
- for (int y = 0; y < SIZE; y++) {
- for (int x = 0; x < SIZE; x++) {
- pixels[x + y * SIZE] = sheet.pixels[(x + this.x) + (y + this.y) * sheet.SIZE];
- }
- }
- }
- }
- SpriteSheet.java --------------------------------------------------------------------------------------
- package com.gmail.olle.fjallstrom.graphics;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- public class SpriteSheet {
- private String path;
- public final int SIZE;
- public int[] pixels;
- public static SpriteSheet tiles = new SpriteSheet("/textures/Sprites.png", 256);
- public SpriteSheet(String path, int size) {
- this.path = path;
- this.SIZE = size;
- pixels = new int[SIZE * SIZE];
- load();
- }
- private void load() {
- try {
- BufferedImage image = ImageIO.read(SpriteSheet.class.getResource(path));
- int w = image.getWidth();
- int h = image.getHeight();
- image.getRGB(0, 0, w, h, pixels, 0, w);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- Keyboard.java -----------------------------------------------------------------------------------------
- package com.gmail.olle.fjallstrom.input;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- public class Keyboard implements KeyListener {
- private boolean[] keys = new boolean[120];
- public boolean up, down, left, right;
- public void update() {
- up = keys[KeyEvent.VK_UP] || keys[KeyEvent.VK_W];
- down = keys[KeyEvent.VK_DOWN] || keys[KeyEvent.VK_S];
- left = keys[KeyEvent.VK_LEFT] || keys[KeyEvent.VK_A];
- right = keys[KeyEvent.VK_RIGHT] || keys[KeyEvent.VK_D];
- }
- public void keyPressed(KeyEvent e) {
- keys[e.getKeyCode()] = true;
- }
- public void keyReleased(KeyEvent e) {
- keys[e.getKeyCode()] = false;
- }
- public void keyTyped(KeyEvent e) {
- }
- }
- Entity.java -------------------------------------------------------------------------------------------
- package com.gmail.olle.fjallstrom.penguins.entity;
- import java.util.Random;
- import com.gmail.olle.fjallstrom.graphics.Screen;
- import com.gmail.olle.fjallstrom.penguins.level.Level;
- public abstract class Entity {
- public int x, y;
- private boolean removed = false;
- protected Level level;
- protected final Random random = new Random();
- public void update() {
- }
- public void render(Screen screen) {
- }
- public void remove() {
- //Remove from level
- removed = true;
- }
- public boolean isRemoved() {
- return removed;
- }
- }
- Mob.java ----------------------------------------------------------------------------------------------
- package com.gmail.olle.fjallstrom.penguins.entity.mob;
- import com.gmail.olle.fjallstrom.graphics.Sprite;
- import com.gmail.olle.fjallstrom.penguins.entity.Entity;
- public abstract class Mob extends Entity {
- protected Sprite sprite;
- protected int dir = 0;
- protected boolean moving = false;
- public void move(int xa, int ya) {
- if (xa > 0) dir = 1;
- if (xa < 0) dir = 3;
- if (ya > 0) dir = 2;
- if (ya < 0) dir = 0;
- if (!collision()) {
- x += xa;
- y += ya;
- }
- }
- public void update() {
- }
- private boolean collision() {
- return false;
- }
- public void render() {
- }
- }
- Player.java -------------------------------------------------------------------------------------------
- package com.gmail.olle.fjallstrom.penguins.entity.mob;
- import com.gmail.olle.fjallstrom.graphics.Screen;
- import com.gmail.olle.fjallstrom.graphics.Sprite;
- import com.gmail.olle.fjallstrom.graphics.SpriteSheet;
- import com.gmail.olle.fjallstrom.input.Keyboard;
- public class Player extends Mob {
- private Keyboard input;
- private Sprite sprite;
- private int anim;
- private boolean walking = false;
- public Player(Keyboard input) {
- this.input = input;
- sprite = Sprite.player_forward;
- }
- public Player(int x, int y) {
- this.x = x;
- this.y = y;
- }
- public void update() {
- int xa = 0, ya = 0;
- if (anim < 7500) anim++;
- else anim = 0;
- if (input.up) ya--;
- if (input.down) ya++;
- if (input.left) xa--;
- if (input.right) xa++;
- if (xa != 0 || ya != 0) {
- move(xa, ya);
- walking = true;
- } else {
- walking = false;
- }
- }
- public void render(Screen screen) {
- if (dir == 2) {
- sprite = Sprite.player_back;
- if (walking) {
- if (anim % 20 > 10) {
- sprite = Sprite.player_back_1;
- }else {
- sprite = Sprite.player_back_2;
- }
- }
- }
- if (dir == 1) {
- sprite = Sprite.player_left;
- if (walking) {
- if (anim % 20 > 10) {
- sprite = Sprite.player_left_1;
- }else {
- sprite = Sprite.player_left_2;
- }
- }
- }
- if (dir == 0) {
- sprite = Sprite.player_forward;
- if (walking) {
- if (anim % 20 > 10) {
- sprite = Sprite.player_forward_1;
- }else {
- sprite = Sprite.player_forward_2;
- }
- }
- }
- if (dir == 3) {
- sprite = Sprite.player_right;
- if (walking) {
- if (anim % 20 > 10) {
- sprite = Sprite.player_right_1;
- }else {
- sprite = Sprite.player_right_2;
- }
- }
- }
- screen.renderPlayer(x - 16, y - 16, sprite);
- }
- }
- Level.java --------------------------------------------------------------------------------------------
- package com.gmail.olle.fjallstrom.penguins.level;
- import com.gmail.olle.fjallstrom.graphics.Screen;
- public class Level {
- protected int width, height;
- protected int[] tiles;
- public Level(int width, int height) {
- this.width = width;
- this.height = height;
- tiles = new int[width * height];
- generateLevel();
- }
- public Level(String path) {
- loadLevel(path);
- }
- private void generateLevel() {
- }
- private void loadLevel(String path) {
- }
- public void update() {
- }
- private void time() {
- }
- public void render(int xScroll, int yScroll, Screen screen) {
- screen.setOffset(xScroll, yScroll);
- int x0 = xScroll >> 4;
- int x1 = (xScroll + screen.width + 16) >> 4;
- int y0 = yScroll >> 4;
- int y1 = (yScroll + screen.height + 16) >> 4;
- for (int y = y0; y < y1; y++) {
- for (int x = x0; x < x1; x++) {
- getTile(x, y).render(x, y, screen);
- }
- }
- }
- public Tiles getTile(int x, int y) {
- if (x < 0 || y < 0 || x >= width || y >= height) return Tiles.voidTile;
- if (tiles[x + y * width] == 0) return Tiles.snow;
- return Tiles.voidTile;
- }
- }
- RandomLevel.java --------------------------------------------------------------------------------------
- package com.gmail.olle.fjallstrom.penguins.level;
- import java.util.Random;
- import com.gmail.olle.fjallstrom.cries.level.Level;
- public class RandomLevel extends Level {
- private static final Random random = new Random();
- public RandomLevel(int width, int height) {
- super(width, height);
- }
- public void generateLevel() {
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- tiles[x + y * width] = random.nextInt(4);
- }
- }
- }
- }
- SnowTile.java -----------------------------------------------------------------------------------------
- package com.gmail.olle.fjallstrom.penguins.level;
- import com.gmail.olle.fjallstrom.graphics.Screen;
- import com.gmail.olle.fjallstrom.graphics.Sprite;
- import com.gmail.olle.fjallstrom.penguins.level.Tiles;
- public class SnowTile extends Tiles {
- public SnowTile(Sprite sprite) {
- super(sprite);
- }
- public void render(int x, int y, Screen screen) {
- screen.renderTile(x << 4, y << 4, this);
- }
- }
- Tiles.java --------------------------------------------------------------------------------------------
- package com.gmail.olle.fjallstrom.penguins.level;
- import com.gmail.olle.fjallstrom.graphics.Screen;
- import com.gmail.olle.fjallstrom.graphics.Sprite;
- public class Tiles {
- public int x, y;
- public Sprite sprite;
- public static Tiles snow = new SnowTile(Sprite.snow);
- public static Tiles voidTile = new VoidTile(Sprite.voidSprite);
- public Tiles(Sprite sprite) {
- this.sprite = sprite;
- }
- public void render(int x, int y, Screen screen) {
- }
- public boolean solid() {
- return false;
- }
- }
- VoidTile.java -----------------------------------------------------------------------------------------
- package com.gmail.olle.fjallstrom.penguins.level;
- import com.gmail.olle.fjallstrom.graphics.Screen;
- import com.gmail.olle.fjallstrom.graphics.Sprite;
- public class VoidTile extends Tiles {
- public VoidTile(Sprite sprite) {
- super(sprite);
- }
- public void render(int x, int y, Screen screen) {
- screen.renderTile(x << 4, y << 4, this);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement