Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.05 KB | None | 0 0
  1. Game:
  2.  
  3. package net.***.game;
  4. import java.awt.BorderLayout;
  5. import java.awt.Canvas;
  6. import java.awt.Color;
  7. import java.awt.Dimension;
  8. import java.awt.Graphics;
  9. import java.awt.image.BufferStrategy;
  10. import java.awt.image.BufferedImage;
  11. import java.awt.image.DataBufferInt;
  12.  
  13. import javax.swing.JFrame;
  14.  
  15. import net.***.game.gfx.Colours;
  16. import net.***.game.gfx.Screen;
  17. import net.***.game.gfx.SpriteSheet;
  18.  
  19. public class Game extends Canvas implements Runnable{
  20.  
  21. private static final long serialVersionUID = 1L;
  22.  
  23. public static final int WIDTH = 160;
  24. public static final int HEIGHT = WIDTH/12*9;
  25. public static final int SCALE = 3;
  26. public static final String NAME = "Game";
  27.  
  28. private JFrame frame;
  29.  
  30. public boolean running = false;
  31. public int tickCount = 0;
  32.  
  33. private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  34. private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
  35. private int[] colours = new int[36 * 6];
  36.  
  37. private Screen screen;
  38. public InputHandler input;
  39.  
  40. public Game() {
  41. setMinimumSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
  42. setMaximumSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
  43. setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
  44.  
  45. frame = new JFrame(NAME);
  46.  
  47. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  48. frame.setLayout(new BorderLayout());
  49.  
  50. frame.add(this, BorderLayout.CENTER);
  51. frame.pack();
  52.  
  53. frame.setResizable(false);
  54. frame.setLocationRelativeTo(null);
  55. frame.setVisible(true);
  56.  
  57. }
  58.  
  59. public void init(){
  60. int index = 0;
  61. for(int r = 0; r < 6; r++){
  62. for(int g = 0; g < 6; g++){
  63. for(int b = 0; b < 6; b++){
  64. int rr = (r * 255 / 5);
  65. int gg = (g * 255 / 5);
  66. int bb = (b * 255 / 5);
  67.  
  68. colours[index++] = rr << 16 | gg << 8 | bb;
  69. }
  70. }
  71. }
  72.  
  73. screen = new Screen(WIDTH, HEIGHT, new SpriteSheet("/spritesheet.png"));
  74. input = new InputHandler(this);
  75. }
  76.  
  77. private synchronized void start() {
  78. running = true;
  79. new Thread(this).start();
  80. }
  81.  
  82. private synchronized void stop() {
  83. running = false;
  84. }
  85.  
  86. public void run() {
  87. long lastTime = System.nanoTime();
  88. double nsPerTick = 1000000000D/60;
  89.  
  90. int ticks = 0;
  91. int frames = 0;
  92.  
  93. long lastTimer = System.currentTimeMillis();
  94. double delta = 0;
  95.  
  96. init();
  97.  
  98. while(running)
  99. {
  100. long now = System.nanoTime();
  101. delta += (now - lastTime) / nsPerTick;
  102. lastTime = now;
  103. boolean shouldRender = true;
  104.  
  105. while(delta >= 1){
  106. ticks++;
  107. tick();
  108. delta -= 1;
  109. shouldRender = true;
  110. }
  111.  
  112. try {
  113. Thread.sleep(2);
  114. } catch (InterruptedException e) {
  115. e.printStackTrace();
  116. }
  117.  
  118. if(shouldRender){
  119. frames++;
  120. render();
  121. }
  122. if(System.currentTimeMillis() - lastTimer >= 1000) {
  123. lastTimer += 1000;
  124. System.out.println(frames + " frames, " + ticks + " ticks.");
  125. frames = 0;
  126. ticks = 0;
  127. }
  128. }
  129. }
  130.  
  131. public void tick() {
  132. tickCount++;
  133.  
  134. if(input.up.isPressed()){
  135. screen.yOffset--;
  136. }
  137. if(input.down.isPressed()){
  138. screen.yOffset++;
  139. }
  140. if(input.right.isPressed()){
  141. screen.xOffset++;
  142. }
  143. if(input.left.isPressed()){
  144. screen.xOffset--;
  145. }
  146. }
  147.  
  148. public void render() {
  149. BufferStrategy bs = getBufferStrategy();
  150. if(bs == null){
  151. createBufferStrategy(3);
  152. return;
  153. }
  154.  
  155. for(int y = 0; y < 32; y++){
  156. for(int x = 0; x < 32; x++){
  157. boolean flipX = x % 2 == 0;
  158. boolean flipY = y % 2 == 0;
  159. screen.render(x << 3, y << 3, 0, Colours.get(555, 505, 055, 550), flipX, flipY);
  160. }
  161. }
  162.  
  163. for(int y = 0; y < screen.height; y++){
  164. for(int x = 0; x < screen.width; x++){
  165. int colourCode = screen.pixels[x + y * screen.width];
  166. if(colourCode < 255) pixels[x + y * WIDTH] = colours[colourCode];
  167. }
  168. }
  169.  
  170. Graphics g = bs.getDrawGraphics();
  171.  
  172. g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  173. g.drawRect(0, 0, getWidth(), getHeight());
  174. g.dispose();
  175. bs.show();
  176. }
  177.  
  178. public static void main(String[] args)
  179. {
  180. new Game().start();
  181. }
  182. }
  183.  
  184. Colours:
  185.  
  186. package net.***.game.gfx;
  187.  
  188. public class Colours {
  189.  
  190. public static int get(int colour1, int colour2, int colour3, int colour4){
  191. return (get(colour4) << 24) + (get(colour3) << 16) + (get(colour2) << 8) + (get(colour1));
  192. }
  193.  
  194. private static int get(int colour) {
  195. if(colour < 0) return 255;
  196. int r = colour/100%10;
  197. int g = colour/10%10;
  198. int b = colour%10;
  199. return r * 36 + g * 6 + b;
  200. }
  201.  
  202. }
  203.  
  204. Screen:
  205.  
  206. package net.***.game.gfx;
  207.  
  208. public class Screen {
  209.  
  210. public static final int MAP_WIDTH = 64;
  211. public static final int MAP_WIDTH_MASK = MAP_WIDTH - 1;
  212.  
  213. public int[] tiles = new int[MAP_WIDTH*MAP_WIDTH];
  214. public int[] colours = new int[MAP_WIDTH*MAP_WIDTH*4];
  215.  
  216. public int[] pixels;
  217.  
  218. public int xOffset = 0;
  219. public int yOffset = 0;
  220.  
  221. public int width;
  222. public int height;
  223.  
  224. public SpriteSheet sheet;
  225.  
  226. public Screen(int width, int height, SpriteSheet sheet){
  227. this.width = width;
  228. this.height = height;
  229. this.sheet = sheet;
  230.  
  231. pixels = new int[width*height];
  232. }
  233.  
  234. public void render(int xPos, int yPos, int tile, int colour, boolean mirrorX, boolean mirrorY){
  235. xPos -= xOffset;
  236. yPos -= yOffset;
  237.  
  238. int xTile = tile % 32;
  239. int yTile = tile / 32;
  240. int tileOffset = (xTile << 3) + (yTile << 3) * sheet.width;
  241. for(int y = 0; y < 8; y++){
  242. int ySheet = y;
  243. if(mirrorY)ySheet = 7 - y;
  244. if(y + yPos < 0 || y + yPos >= height) continue;
  245. for(int x = 0; x < 8; x++){
  246. int xSheet = x;
  247. if(mirrorX)xSheet = 7 - x;
  248. if(x + xPos < 0 || x + xPos >= width) continue;
  249. int col = (colour >> (sheet.pixels[xSheet + ySheet * sheet.width + tileOffset] * 8)) % 255;
  250. if(col < 255) pixels[(x + xPos) + (y + yPos) * width] = col;
  251. }
  252. }
  253. }
  254. }
  255.  
  256. InputHandler:
  257.  
  258. package net.***.game;
  259.  
  260. import java.awt.event.KeyEvent;
  261. import java.awt.event.KeyListener;
  262. import java.util.ArrayList;
  263. import java.util.List;
  264.  
  265. public class InputHandler implements KeyListener{
  266.  
  267. public InputHandler(Game game){
  268. game.addKeyListener(this);
  269. }
  270.  
  271. public class Key{
  272. private int numTimesPressed = 0;
  273. private boolean pressed = false;
  274.  
  275. public boolean isPressed;
  276.  
  277. public int getNumadadTimesPrsesed(){
  278. return numTimesPressed;
  279. }
  280.  
  281. public boolean isPressed(){
  282. return pressed;
  283. }
  284.  
  285. public void toggle(boolean isPressed) {
  286. pressed = isPressed;
  287. if(isPressed) numTimesPressed++;
  288. }
  289. }
  290.  
  291. public Key up = new Key();
  292. public Key down = new Key();
  293. public Key left = new Key();
  294. public Key right = new Key();
  295.  
  296. public void keyPressed(KeyEvent e) {
  297. toggleKey(e.getKeyCode(), true);
  298. }
  299.  
  300. public void keyReleased(KeyEvent e) {
  301. toggleKey(e.getKeyCode(), false);
  302. }
  303.  
  304. public void keyTyped(KeyEvent e) {
  305.  
  306. }
  307.  
  308. public void toggleKey(int keyCode, boolean isPressed){
  309. if(keyCode == KeyEvent.VK_W || keyCode == KeyEvent.VK_UP)
  310. {up.toggle(isPressed);
  311. }
  312. if(keyCode == KeyEvent.VK_A || keyCode == KeyEvent.VK_LEFT)
  313. {left.toggle(isPressed);
  314. }
  315. if(keyCode == KeyEvent.VK_S || keyCode == KeyEvent.VK_DOWN)
  316. {down.toggle(isPressed);
  317. }
  318. if(keyCode == KeyEvent.VK_D || keyCode == KeyEvent.VK_RIGHT)
  319. {right.toggle(isPressed);
  320. }
  321. }
  322. }
  323.  
  324. SpriteSheet:
  325.  
  326. package net.***.game.gfx;
  327.  
  328. import java.awt.image.BufferedImage;
  329. import java.io.IOException;
  330.  
  331. import javax.imageio.ImageIO;
  332.  
  333. public class SpriteSheet {
  334.  
  335. public String path;
  336. public int width;
  337. public int height;
  338.  
  339. public int[] pixels;
  340.  
  341. public SpriteSheet(String path){
  342. BufferedImage image = null;
  343.  
  344. try {
  345. image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
  346. } catch (IOException e) {
  347. e.printStackTrace();
  348. }
  349.  
  350. if(image == null)
  351. {
  352. return;
  353. }
  354.  
  355. this.path = path;
  356. this.width = image.getWidth();
  357. this.height = image.getHeight();
  358.  
  359.  
  360. pixels = image.getRGB(0, 0, width, height, null, 0, width);
  361.  
  362. for(int i = 0; i < pixels.length; i++)
  363. {
  364. pixels[i] = (pixels[i] & 0xff) / 64;
  365. }
  366.  
  367. for(int i = 0; i < 8; i++){
  368. System.out.println(pixels[i]);
  369. }
  370. }
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement