random code


SUBMITTED BY: Guest

DATE: May 3, 2014, 9:48 a.m.

FORMAT: Text only

SIZE: 4.5 kB

HITS: 630

  1. package net.josh.minecraftplatformer;
  2. import java.applet.*;
  3. import java.awt.*;
  4. import javax.swing.*;
  5. public class Component extends Applet implements Runnable { //calls the class
  6. private static final long serialVersionUID = 1L;
  7. private static int pixelSize = 2; //declares the pixel size.
  8. public static int moveFromBorder = 0;
  9. public static double sX = moveFromBorder, sY = moveFromBorder; //declares the variables for side scrolling.
  10. public static double dir = 0;
  11. public static Dimension size = new Dimension(700, 560); //declares screen size.
  12. public static Dimension pixel = new Dimension(size.width / pixelSize, size.height / pixelSize); //sets the pixel size.
  13. public static String name = "Minecraft 2D Platformer."; //sets the variable for the name
  14. public static boolean isRunning = false; //boolean for game running.
  15. public static boolean isMoving = false; //boolean for whether the character is moving.
  16. public static boolean isJumping = false; //boolean for whether the character is jumping
  17. public static Level level; //creates the level.
  18. public static Character character; //creates the character
  19. private Image screen; //makes new Image variable called screen.
  20. public Component() { //Constructor for the Component class.
  21. setPreferredSize(size); //sets the size for the java applet.
  22. addKeyListener(new Listening()); //starts a new key listener.
  23. }
  24. public void start() { //Start method for game loop and defining objects
  25. //Defining objects etc.
  26. new Tile(); //loading images. //makes a new tile to load images.
  27. level = new Level(); //new level
  28. character = new Character(Tile.tileSize, Tile.tileSize * 2); //new character
  29. //Starting game loop.
  30. isRunning = true; //sets is running to true
  31. new Thread(this).start(); //game loop I think
  32. }
  33. public void stop() { //method to stop the game.
  34. isRunning = false; //sets isRunning to false
  35. }
  36. public static void main(String args[]){
  37. Component component = new Component(); //makes a new Component
  38. JFrame frame = new JFrame(); //calls a new frame
  39. frame.add(component); //adds component to frame
  40. frame.setVisible(true); //makes the frame visible
  41. frame.pack(); //sets the window to the proffered size
  42. frame.setTitle(name); //adds the name
  43. frame.setResizable(false); //stops it being resizable.
  44. frame.setLocationRelativeTo(null); //puts the window in the middle of the screen.
  45. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //closes the program when the window is closed.
  46. component.start(); //calls component.start()
  47. }
  48. public void tick() { //starts the refresh
  49. level.tick(); //calls the level tick
  50. character.tick(); //calls the character tick
  51. }
  52. public void render() { //renders graphics
  53. Graphics g = screen.getGraphics();
  54. //Drawing things.
  55. g.setColor(new Color(110, 110 ,255));
  56. g.fillRect(0, 0, pixel.width, pixel.height);
  57. level.render(g);
  58. character.render(g);
  59. g = getGraphics();
  60. g.drawImage(screen, 0, 0, size.width, size.height, 0, 0, pixel.width, pixel.height, null); //creates the screen
  61. g.dispose(); //disposes of the screen freeing up system resources
  62. }
  63. /*
  64. * Game Loop!
  65. *
  66. */
  67. public void run() {
  68. screen = createVolatileImage(pixel.width, pixel.height); //creates performance friendly image called screen
  69. while(isRunning) { //ticks and renders as long as the game is running
  70. tick();
  71. render();
  72. try { //makes the loop slower
  73. Thread.sleep(5);
  74. } catch(Exception e) { }
  75. }
  76. }
  77. }

comments powered by Disqus