Advertisement
PolarPenguin

Episode 30 - 50

Aug 7th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.92 KB | None | 0 0
  1. Game.java ---------------------------------------------------------------------------------------------
  2.  
  3. package com.gmail.olle.fjallstrom.cries;
  4.  
  5. import java.awt.Canvas;
  6. import java.awt.Dimension;
  7. import java.awt.Graphics;
  8. import java.awt.image.BufferStrategy;
  9. import java.awt.image.BufferedImage;
  10. import java.awt.image.DataBufferInt;
  11.  
  12. import javax.swing.JFrame;
  13.  
  14. import com.gmail.olle.fjallstrom.cries.Game;
  15. import com.gmail.olle.fjallstrom.graphics.Screen;
  16. import com.gmail.olle.fjallstrom.input.Keyboard;
  17. import com.gmail.olle.fjallstrom.penguins.entity.mob.Player;
  18. import com.gmail.olle.fjallstrom.penguins.level.Level;
  19. import com.gmail.olle.fjallstrom.penguins.level.RandomLevel;
  20.  
  21. public class Game extends Canvas implements Runnable{
  22. private static final long serialVersionUID = 1L;
  23.  
  24. public static int width = 300;
  25. public static int height = width / 16 * 10;
  26. public static int scale = 3;
  27. public static String title = "Penguins";
  28.  
  29. private Thread thread;
  30. private JFrame frame;
  31. private boolean running = false;
  32.  
  33. private Screen screen;
  34.  
  35. private RandomLevel level;
  36.  
  37. private Keyboard key;
  38.  
  39. private Player player;
  40.  
  41. private BufferedImage image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB);
  42. private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
  43.  
  44. public Game() {
  45. Dimension size = new Dimension(width * scale, height * scale);
  46. setPreferredSize(size);
  47. setFocusable(true);
  48.  
  49. screen = new Screen(width, height);
  50. frame = new JFrame();
  51. key = new Keyboard();
  52. level = new RandomLevel(64, 64);
  53. player = new Player(key);
  54.  
  55. addKeyListener(key);
  56. }
  57.  
  58. public synchronized void start() {
  59. running = true;
  60. thread = new Thread(this, "Display");
  61. thread.start();
  62. }
  63.  
  64. public synchronized void stop() {
  65. running = false;
  66. try {
  67. thread.join();
  68. } catch (InterruptedException e) {
  69. e.printStackTrace ();
  70. }
  71. }
  72.  
  73. public void run() {
  74. long lastTime = System.nanoTime();
  75. long timer = System.currentTimeMillis();
  76. final double ns = 1000000000.0 / 60;
  77. double delta = 0;
  78. int frames = 0;
  79. int updates = 0;
  80. while (running) {
  81. long now = System.nanoTime();
  82. delta += (now - lastTime) / ns;
  83. lastTime = now;
  84. while (delta >= 1) {
  85. update();
  86. updates++;
  87. delta--;
  88. }
  89. render();
  90. frames++;
  91.  
  92. if (System.currentTimeMillis() - timer > 1000) {
  93. timer += 1000;
  94. frame.setTitle(title + " | " + updates + " ups," + frames + " fps");
  95. updates = 0;
  96. frames = 0;
  97. }
  98. }
  99. }
  100. public void update() {
  101. key.update();
  102. player.update();
  103. }
  104.  
  105. public void render() {
  106. BufferStrategy bs = getBufferStrategy();
  107. if (bs == null) {
  108. createBufferStrategy(3);
  109. return;
  110. }
  111. screen.clear();
  112. int xScroll = player.x - screen.width / 2;
  113. int yScroll = player.y - screen.height / 2;
  114.  
  115. level.render(xScroll, yScroll, screen);
  116. player.render(screen);
  117.  
  118. for (int i = 0; i < pixels.length; i++) {
  119. pixels[i] = screen.pixels[i];
  120. }
  121.  
  122. Graphics g = bs.getDrawGraphics();
  123. g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  124. g.dispose();
  125. bs.show();
  126. }
  127.  
  128. public static void main(String[] args) {
  129. Game game = new Game();
  130. game.frame.setResizable(false);
  131. game.frame.setTitle(Game.title);
  132. game.frame.add(game);
  133. game.frame.pack();
  134. game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  135. game.frame.setLocationRelativeTo(null);
  136. game.frame.setVisible(true);
  137.  
  138. game.start();
  139. }
  140.  
  141. }
  142.  
  143. Screen.java -------------------------------------------------------------------------------------------
  144.  
  145. package com.gmail.olle.fjallstrom.graphics;
  146.  
  147. import java.util.Random;
  148.  
  149. import com.gmail.olle.fjallstrom.penguins.level.Tiles;
  150.  
  151. public class Screen {
  152.  
  153. public int width;
  154. public int height;
  155. public int[] pixels;
  156. public static int MAP_SIZE = 64;
  157. public static int MAP_SIZE_MASK = MAP_SIZE - 1;
  158.  
  159. public int xOffset, yOffset;
  160.  
  161. public int[] tile = new int[MAP_SIZE * MAP_SIZE];
  162.  
  163. private Random random = new Random();
  164.  
  165. int xtime = 0, ytime = 0;
  166. int counter = 0;
  167.  
  168. public Screen (int width, int height) {
  169. this.width = width;
  170. this.height = height;
  171. pixels = new int[width * height];
  172.  
  173. for (int i = 0; i < MAP_SIZE * MAP_SIZE; i++) {
  174. tile[i] = random.nextInt(0xffffff);
  175. }
  176. }
  177.  
  178. public void clear() {
  179. for (int i = 0; i < pixels.length; i++) {
  180. pixels[i] = 0;
  181. }
  182.  
  183. }
  184.  
  185. public void renderTile(int xp, int yp, Tiles tile) {
  186. xp -= xOffset;
  187. yp -= yOffset;
  188. for (int y = 0; y < tile.sprite.SIZE; y++) {
  189. int ya = y + yp;
  190. for (int x = 0; x < tile.sprite.SIZE; x++) {
  191. int xa = x + xp;
  192. if (xa < -tile.sprite.SIZE || xa >= width || ya < 0 || ya >= height) break;
  193. if (xa < 0) xa = 0;
  194. pixels[xa + ya * width] = tile.sprite.pixels[x + y * tile.sprite.SIZE];
  195. }
  196.  
  197. }
  198. }
  199.  
  200. public void renderPlayer(int xp, int yp, Sprite sprite) {
  201. xp -= xOffset;
  202. yp -= yOffset;
  203. for (int y = 0; y < 32; y++) {
  204. int ya = y + yp;
  205. for (int x = 0; x < 32; x++) {
  206. int xa = x + xp;
  207. if (xa < -32 || xa >= width || ya < 0 || ya >= height) break;
  208. if (xa < 0) xa = 0;
  209. int col = sprite.pixels[x + y * 32];
  210. if (col != 0xffb200ff)
  211. pixels[xa + ya * width] = sprite.pixels[x + y * 32];
  212.  
  213. }
  214.  
  215. }
  216. }
  217.  
  218. public void setOffset(int xOffset, int yOffset) {
  219. this.xOffset = xOffset;
  220. this.yOffset = yOffset;
  221. }
  222.  
  223. }
  224.  
  225. Sprite.java -------------------------------------------------------------------------------------------
  226.  
  227. package com.gmail.olle.fjallstrom.graphics;
  228.  
  229. public class Sprite {
  230.  
  231. public final int SIZE;
  232. private int x, y;
  233. public int[] pixels;
  234. private SpriteSheet sheet;
  235. private int i;
  236.  
  237. public static Sprite snow = new Sprite(16, 0, 0, SpriteSheet.tiles);
  238. public static Sprite voidSprite = new Sprite(16, 0x1B87E0);
  239.  
  240. //Size, x, y, SpriteSheet.tiles
  241.  
  242. public static Sprite player_back = new Sprite(32, 1, 7, SpriteSheet.tiles);
  243. public static Sprite player_forward = new Sprite(32, 0, 7, SpriteSheet.tiles);
  244. public static Sprite player_left = new Sprite(32, 2, 7, SpriteSheet.tiles);
  245. public static Sprite player_right = new Sprite(32, 3, 7, SpriteSheet.tiles);
  246.  
  247. public static Sprite player_forward_1 = new Sprite(32, 0, 6, SpriteSheet.tiles);
  248. public static Sprite player_forward_2 = new Sprite(32, 0, 5, SpriteSheet.tiles);
  249.  
  250. public static Sprite player_left_1 = new Sprite(32, 2, 6, SpriteSheet.tiles);
  251. public static Sprite player_left_2 = new Sprite(32, 2, 5, SpriteSheet.tiles);
  252.  
  253. public static Sprite player_right_1 = new Sprite(32, 3, 6, SpriteSheet.tiles);
  254. public static Sprite player_right_2 = new Sprite(32, 3, 5, SpriteSheet.tiles);
  255.  
  256. public static Sprite player_back_1 = new Sprite(32, 1, 6, SpriteSheet.tiles);
  257. public static Sprite player_back_2 = new Sprite(32, 1, 5, SpriteSheet.tiles);
  258.  
  259. public Sprite(int size, int x, int y, SpriteSheet sheet) {
  260. SIZE = size;
  261. pixels = new int[SIZE * SIZE];
  262. this.x = x * size;
  263. this.y = y * size;
  264. this.sheet = sheet;
  265. load();
  266. }
  267.  
  268. public Sprite(int size, int colour) {
  269. SIZE = size;
  270. pixels = new int[SIZE * SIZE];
  271. setColour(colour);
  272.  
  273. }
  274.  
  275. private void setColour(int colour) {
  276. for (int i = 0; i < SIZE * SIZE; i++);
  277. pixels[i] = colour;
  278. }
  279.  
  280. private void load() {
  281. for (int y = 0; y < SIZE; y++) {
  282. for (int x = 0; x < SIZE; x++) {
  283. pixels[x + y * SIZE] = sheet.pixels[(x + this.x) + (y + this.y) * sheet.SIZE];
  284. }
  285. }
  286. }
  287.  
  288. }
  289.  
  290. SpriteSheet.java --------------------------------------------------------------------------------------
  291.  
  292. package com.gmail.olle.fjallstrom.graphics;
  293.  
  294. import java.awt.image.BufferedImage;
  295. import java.io.IOException;
  296.  
  297. import javax.imageio.ImageIO;
  298.  
  299. public class SpriteSheet {
  300.  
  301. private String path;
  302. public final int SIZE;
  303. public int[] pixels;
  304.  
  305. public static SpriteSheet tiles = new SpriteSheet("/textures/Sprites.png", 256);
  306.  
  307. public SpriteSheet(String path, int size) {
  308. this.path = path;
  309. this.SIZE = size;
  310. pixels = new int[SIZE * SIZE];
  311. load();
  312. }
  313.  
  314. private void load() {
  315. try {
  316. BufferedImage image = ImageIO.read(SpriteSheet.class.getResource(path));
  317. int w = image.getWidth();
  318. int h = image.getHeight();
  319. image.getRGB(0, 0, w, h, pixels, 0, w);
  320. } catch (IOException e) {
  321. e.printStackTrace();
  322. }
  323. }
  324.  
  325. }
  326.  
  327. Keyboard.java -----------------------------------------------------------------------------------------
  328.  
  329. package com.gmail.olle.fjallstrom.input;
  330.  
  331. import java.awt.event.KeyEvent;
  332. import java.awt.event.KeyListener;
  333.  
  334. public class Keyboard implements KeyListener {
  335.  
  336. private boolean[] keys = new boolean[120];
  337. public boolean up, down, left, right;
  338.  
  339. public void update() {
  340. up = keys[KeyEvent.VK_UP] || keys[KeyEvent.VK_W];
  341. down = keys[KeyEvent.VK_DOWN] || keys[KeyEvent.VK_S];
  342. left = keys[KeyEvent.VK_LEFT] || keys[KeyEvent.VK_A];
  343. right = keys[KeyEvent.VK_RIGHT] || keys[KeyEvent.VK_D];
  344. }
  345.  
  346. public void keyPressed(KeyEvent e) {
  347. keys[e.getKeyCode()] = true;
  348. }
  349.  
  350. public void keyReleased(KeyEvent e) {
  351. keys[e.getKeyCode()] = false;
  352. }
  353.  
  354. public void keyTyped(KeyEvent e) {
  355.  
  356. }
  357.  
  358. }
  359.  
  360. Entity.java -------------------------------------------------------------------------------------------
  361.  
  362. package com.gmail.olle.fjallstrom.penguins.entity;
  363.  
  364. import java.util.Random;
  365.  
  366. import com.gmail.olle.fjallstrom.graphics.Screen;
  367. import com.gmail.olle.fjallstrom.penguins.level.Level;
  368.  
  369. public abstract class Entity {
  370.  
  371. public int x, y;
  372. private boolean removed = false;
  373. protected Level level;
  374. protected final Random random = new Random();
  375.  
  376. public void update() {
  377.  
  378. }
  379.  
  380. public void render(Screen screen) {
  381.  
  382. }
  383.  
  384. public void remove() {
  385. //Remove from level
  386. removed = true;
  387. }
  388.  
  389. public boolean isRemoved() {
  390. return removed;
  391. }
  392.  
  393.  
  394.  
  395. }
  396.  
  397. Mob.java ----------------------------------------------------------------------------------------------
  398.  
  399. package com.gmail.olle.fjallstrom.penguins.entity.mob;
  400.  
  401. import com.gmail.olle.fjallstrom.graphics.Sprite;
  402. import com.gmail.olle.fjallstrom.penguins.entity.Entity;
  403.  
  404. public abstract class Mob extends Entity {
  405.  
  406. protected Sprite sprite;
  407. protected int dir = 0;
  408. protected boolean moving = false;
  409.  
  410. public void move(int xa, int ya) {
  411. if (xa > 0) dir = 1;
  412. if (xa < 0) dir = 3;
  413. if (ya > 0) dir = 2;
  414. if (ya < 0) dir = 0;
  415.  
  416. if (!collision()) {
  417. x += xa;
  418. y += ya;
  419. }
  420. }
  421.  
  422. public void update() {
  423.  
  424. }
  425.  
  426. private boolean collision() {
  427. return false;
  428. }
  429.  
  430. public void render() {
  431.  
  432. }
  433.  
  434.  
  435. }
  436.  
  437. Player.java -------------------------------------------------------------------------------------------
  438.  
  439. package com.gmail.olle.fjallstrom.penguins.entity.mob;
  440.  
  441. import com.gmail.olle.fjallstrom.graphics.Screen;
  442. import com.gmail.olle.fjallstrom.graphics.Sprite;
  443. import com.gmail.olle.fjallstrom.graphics.SpriteSheet;
  444. import com.gmail.olle.fjallstrom.input.Keyboard;
  445.  
  446. public class Player extends Mob {
  447.  
  448. private Keyboard input;
  449. private Sprite sprite;
  450. private int anim;
  451. private boolean walking = false;
  452.  
  453. public Player(Keyboard input) {
  454. this.input = input;
  455. sprite = Sprite.player_forward;
  456. }
  457.  
  458. public Player(int x, int y) {
  459. this.x = x;
  460. this.y = y;
  461. }
  462.  
  463. public void update() {
  464. int xa = 0, ya = 0;
  465. if (anim < 7500) anim++;
  466. else anim = 0;
  467. if (input.up) ya--;
  468. if (input.down) ya++;
  469. if (input.left) xa--;
  470. if (input.right) xa++;
  471.  
  472. if (xa != 0 || ya != 0) {
  473. move(xa, ya);
  474. walking = true;
  475. } else {
  476. walking = false;
  477. }
  478. }
  479.  
  480. public void render(Screen screen) {
  481. if (dir == 2) {
  482. sprite = Sprite.player_back;
  483. if (walking) {
  484. if (anim % 20 > 10) {
  485. sprite = Sprite.player_back_1;
  486. }else {
  487. sprite = Sprite.player_back_2;
  488. }
  489. }
  490. }
  491. if (dir == 1) {
  492. sprite = Sprite.player_left;
  493. if (walking) {
  494. if (anim % 20 > 10) {
  495. sprite = Sprite.player_left_1;
  496. }else {
  497. sprite = Sprite.player_left_2;
  498. }
  499. }
  500. }
  501. if (dir == 0) {
  502. sprite = Sprite.player_forward;
  503. if (walking) {
  504. if (anim % 20 > 10) {
  505. sprite = Sprite.player_forward_1;
  506. }else {
  507. sprite = Sprite.player_forward_2;
  508. }
  509. }
  510. }
  511. if (dir == 3) {
  512. sprite = Sprite.player_right;
  513. if (walking) {
  514. if (anim % 20 > 10) {
  515. sprite = Sprite.player_right_1;
  516. }else {
  517. sprite = Sprite.player_right_2;
  518. }
  519. }
  520. }
  521. screen.renderPlayer(x - 16, y - 16, sprite);
  522.  
  523.  
  524. }
  525.  
  526. }
  527.  
  528. Level.java --------------------------------------------------------------------------------------------
  529.  
  530. package com.gmail.olle.fjallstrom.penguins.level;
  531.  
  532. import com.gmail.olle.fjallstrom.graphics.Screen;
  533.  
  534. public class Level {
  535.  
  536. protected int width, height;
  537. protected int[] tiles;
  538.  
  539. public Level(int width, int height) {
  540. this.width = width;
  541. this.height = height;
  542. tiles = new int[width * height];
  543. generateLevel();
  544. }
  545.  
  546.  
  547.  
  548. public Level(String path) {
  549. loadLevel(path);
  550. }
  551.  
  552. private void generateLevel() {
  553. }
  554.  
  555. private void loadLevel(String path) {
  556. }
  557.  
  558. public void update() {
  559.  
  560. }
  561.  
  562. private void time() {
  563.  
  564. }
  565.  
  566. public void render(int xScroll, int yScroll, Screen screen) {
  567. screen.setOffset(xScroll, yScroll);
  568. int x0 = xScroll >> 4;
  569. int x1 = (xScroll + screen.width + 16) >> 4;
  570. int y0 = yScroll >> 4;
  571. int y1 = (yScroll + screen.height + 16) >> 4;
  572. for (int y = y0; y < y1; y++) {
  573. for (int x = x0; x < x1; x++) {
  574. getTile(x, y).render(x, y, screen);
  575. }
  576. }
  577. }
  578.  
  579. public Tiles getTile(int x, int y) {
  580. if (x < 0 || y < 0 || x >= width || y >= height) return Tiles.voidTile;
  581. if (tiles[x + y * width] == 0) return Tiles.snow;
  582. return Tiles.voidTile;
  583. }
  584.  
  585. }
  586.  
  587. RandomLevel.java --------------------------------------------------------------------------------------
  588.  
  589. package com.gmail.olle.fjallstrom.penguins.level;
  590.  
  591. import java.util.Random;
  592.  
  593. import com.gmail.olle.fjallstrom.cries.level.Level;
  594.  
  595. public class RandomLevel extends Level {
  596.  
  597. private static final Random random = new Random();
  598.  
  599. public RandomLevel(int width, int height) {
  600. super(width, height);
  601. }
  602.  
  603. public void generateLevel() {
  604. for (int y = 0; y < height; y++) {
  605. for (int x = 0; x < width; x++) {
  606. tiles[x + y * width] = random.nextInt(4);
  607. }
  608. }
  609. }
  610.  
  611. }
  612.  
  613. SnowTile.java -----------------------------------------------------------------------------------------
  614.  
  615. package com.gmail.olle.fjallstrom.penguins.level;
  616.  
  617. import com.gmail.olle.fjallstrom.graphics.Screen;
  618. import com.gmail.olle.fjallstrom.graphics.Sprite;
  619. import com.gmail.olle.fjallstrom.penguins.level.Tiles;
  620.  
  621.  
  622. public class SnowTile extends Tiles {
  623.  
  624. public SnowTile(Sprite sprite) {
  625. super(sprite);
  626.  
  627. }
  628.  
  629. public void render(int x, int y, Screen screen) {
  630. screen.renderTile(x << 4, y << 4, this);
  631. }
  632.  
  633.  
  634. }
  635.  
  636. Tiles.java --------------------------------------------------------------------------------------------
  637.  
  638. package com.gmail.olle.fjallstrom.penguins.level;
  639.  
  640. import com.gmail.olle.fjallstrom.graphics.Screen;
  641. import com.gmail.olle.fjallstrom.graphics.Sprite;
  642.  
  643. public class Tiles {
  644.  
  645. public int x, y;
  646. public Sprite sprite;
  647.  
  648. public static Tiles snow = new SnowTile(Sprite.snow);
  649. public static Tiles voidTile = new VoidTile(Sprite.voidSprite);
  650.  
  651. public Tiles(Sprite sprite) {
  652. this.sprite = sprite;
  653. }
  654.  
  655. public void render(int x, int y, Screen screen) {
  656.  
  657. }
  658.  
  659. public boolean solid() {
  660. return false;
  661. }
  662.  
  663. }
  664.  
  665. VoidTile.java -----------------------------------------------------------------------------------------
  666.  
  667. package com.gmail.olle.fjallstrom.penguins.level;
  668.  
  669. import com.gmail.olle.fjallstrom.graphics.Screen;
  670. import com.gmail.olle.fjallstrom.graphics.Sprite;
  671.  
  672. public class VoidTile extends Tiles {
  673.  
  674. public VoidTile(Sprite sprite) {
  675. super(sprite);
  676.  
  677. }
  678.  
  679. public void render(int x, int y, Screen screen) {
  680. screen.renderTile(x << 4, y << 4, this);
  681. }
  682.  
  683.  
  684. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement