Advertisement
Guest User

Untitled

a guest
Jun 29th, 2013
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. package net.flodro.minigame;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.awt.Point;
  9.  
  10. import javax.swing.JFrame;
  11.  
  12. import net.flodro.minigame.graphics.Images;
  13.  
  14. @SuppressWarnings("serial")
  15. public class MiniGame extends Canvas implements Runnable {
  16.  
  17. public static int score = 0;
  18. public static int pixelSize = 2;
  19. public static int WIN_WIDTH = 400;
  20. public static int WIN_HEIGHT = 250;
  21.  
  22. public Dimension size = new Dimension(WIN_WIDTH, WIN_HEIGHT);
  23. public Dimension pixel = new Dimension(size.width / 2, size.height / 2);
  24.  
  25. private Image screen;
  26. private Thread thread;
  27. private boolean running = false;
  28.  
  29. public static int currentScreen = 1;
  30.  
  31. public static Player player;
  32. public static boolean playerJumping = false;
  33. public static Point mse = new Point(0, 0);
  34.  
  35. public MiniGame() {
  36. new Images();
  37. player = new Player(20, 0, Images.player.getWidth(), Images.player.getHeight());
  38. addKeyListener(player);
  39. }
  40.  
  41. private void start() {
  42. requestFocus();
  43. running = true;
  44. thread = new Thread(this);
  45. thread.start();
  46. }
  47.  
  48. @SuppressWarnings("unused")
  49. private void stop() {
  50. if (!running)
  51. return;
  52. running = false;
  53. try {
  54. thread.join();
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. System.exit(0);
  58. }
  59. }
  60.  
  61. public void run() {
  62. screen = createVolatileImage(pixel.width, pixel.height);
  63. while (running) {
  64. tick();
  65. render();
  66. }
  67. }
  68.  
  69. private void tick() {
  70. player.tick();
  71. }
  72.  
  73. private void render() {
  74. Graphics g = screen.getGraphics();
  75. g.setColor(Color.BLACK);
  76.  
  77. if(currentScreen == 1) {
  78. g.drawImage(Images.screen1, 0, 0, Images.screen1.getWidth(), Images.screen1.getHeight(), null);
  79. g.setColor(Color.BLACK);
  80. g.fillRect(150, 220, 150, 30);
  81. } else if(player.x >= 370) {
  82. g.drawImage(Images.screen2, 0, 0, Images.screen2.getWidth(), Images.screen2.getHeight(), null);
  83. }
  84.  
  85. player.render(g);
  86.  
  87. // X Position, Y Position, X Width, Y Height
  88. // g.setColor(Color.CYAN);
  89. // g.fillRect(150, 220, 150, 30);
  90.  
  91. g = getGraphics();
  92. g.drawImage(screen, 0, 0, size.width, size.height, 0, 0, pixel.width, pixel.height, null);
  93. g.dispose();
  94. }
  95.  
  96. public static void main(String[] args) {
  97. String OS = System.getProperty("os.name");
  98. if(OS.startsWith("Mac")) {
  99. WIN_WIDTH = 800; WIN_HEIGHT = 500;
  100. } else {
  101. WIN_WIDTH = 806; WIN_HEIGHT = 506;
  102. }
  103.  
  104. MiniGame game = new MiniGame();
  105. JFrame frame = new JFrame();
  106.  
  107. frame.add(game);
  108. frame.pack();
  109. frame.setResizable(false);
  110. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  111. frame.setVisible(true);
  112. frame.setSize(WIN_WIDTH, WIN_HEIGHT);
  113. frame.setTitle("Mini Game");
  114. frame.setLocationRelativeTo(null);
  115.  
  116. game.start();
  117. }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement