Guest User

Untitled

a guest
Jan 20th, 2020
76
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package gamemidlet;
  2.  
  3. import java.io.IOException;
  4. import java.util.Random;
  5. import javax.microedition.lcdui.*;
  6.  
  7. public class MyCanvas extends Canvas implements CommandListener {
  8.  
  9. Midlet midlet;
  10. boolean isAlive = false;
  11. int xPos = 0;
  12. int yPos = 0;
  13. int xMove = 0;
  14. int yMove = 0;
  15. int move = 0;
  16. int time = 0;
  17. int timeCounter = 0;
  18. int timeLimit;
  19. int poziom = 2;
  20. int[] tab = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  21. Graphics g;
  22. int menu = 1;
  23. Image img[] = new Image[9];
  24. Image temp;
  25.  
  26. public MyCanvas(Midlet _midlet) {
  27. midlet = _midlet;
  28. addCommand(new Command("Losowanie", Command.ITEM, 0));
  29. addCommand(new Command("Koniec", Command.EXIT, 0));
  30. setCommandListener(this);
  31.  
  32. switch (poziom) {
  33. case 0:
  34. timeLimit = 300;
  35. break;
  36. case 1:
  37. timeLimit = 200;
  38. break;
  39. case 2:
  40. timeLimit = 100;
  41. break;
  42. }
  43. try {
  44. for (int i = 0; i < 9; i++) {
  45. img[i] = Image.createImage("/" + i + ".png");
  46. }
  47.  
  48. } catch (IOException ex) {
  49. System.out.println("błąd wczytania obrazka");
  50. }
  51.  
  52. HeartBeat t = new HeartBeat();
  53. isAlive = true;
  54. t.start();
  55.  
  56. losowanie();
  57. }
  58.  
  59. protected void paint(Graphics g) {
  60.  
  61. int screenWidth = getWidth();
  62. int screenHeight = getHeight();
  63. g.setColor(0xffffff);
  64. g.fillRect(0, 0, screenWidth, screenHeight);
  65.  
  66.  
  67.  
  68. int xp = 40;
  69. int yp = 70;
  70. if (img[0] != null) {
  71. g.drawImage(img[0], xp + 0, yp + 0, Graphics.TOP | Graphics.LEFT);
  72. g.drawImage(img[1], xp + 55, yp + 0, Graphics.TOP | Graphics.LEFT);
  73. g.drawImage(img[2], xp + 110, yp + 0, Graphics.TOP | Graphics.LEFT);
  74.  
  75. g.drawImage(img[3], xp + 0, yp + 55, Graphics.TOP | Graphics.LEFT);
  76. g.drawImage(img[4], xp + 55, yp + 55, Graphics.TOP | Graphics.LEFT);
  77. g.drawImage(img[5], xp + 110, yp + 55, Graphics.TOP | Graphics.LEFT);
  78.  
  79. g.drawImage(img[6], xp + 0, yp + 110, Graphics.TOP | Graphics.LEFT);
  80. g.drawImage(img[7], xp + 55, yp + 110, Graphics.TOP | Graphics.LEFT);
  81. g.drawImage(img[8], xp + 110, yp + 110, Graphics.TOP | Graphics.LEFT);
  82.  
  83. }
  84.  
  85. g.setColor(0x000000);
  86. g.drawString("Moves: " + move, 50, 10, 0);
  87. g.drawString("Time: " + time, 50, 30, 0);
  88. g.setColor(0xffffff);
  89. }
  90.  
  91. protected void keyPressed(int keyCode) {
  92. int gameAction = getGameAction(keyCode);
  93. move = move + 1;
  94. switch (gameAction) {
  95. case UP:
  96. yMove = -1;
  97. break;
  98. case DOWN:
  99. yMove = +1;
  100. break;
  101. case LEFT:
  102. xMove = -1;
  103. break;
  104. case RIGHT:
  105. xMove = +1;
  106. break;
  107. }
  108. }
  109.  
  110. protected void keyReleased(int keyCode) {
  111. int gameAction = getGameAction(keyCode);
  112.  
  113. switch (gameAction) {
  114. case UP:
  115. case DOWN:
  116. yMove = 0;
  117. break;
  118. case LEFT:
  119. case RIGHT:
  120. xMove = 0;
  121. break;
  122. }
  123. }
  124.  
  125. protected void losowanie() {
  126.  
  127. Random r = new Random();
  128. for (int i = 1; i < 50; i++) {
  129. xMove = -1;
  130. if (r.nextInt(2) == 1) {
  131. xMove = 1;
  132. }
  133. yMove = -1;
  134. if (r.nextInt(2) == 1) {
  135. yMove = 1;
  136. }
  137. try {
  138. Thread.sleep(i * 2);
  139. } catch (Exception ex) {
  140. }
  141. update();
  142.  
  143. }
  144. move = 0;
  145. time = 0;
  146. }
  147.  
  148. protected void update() {
  149. int oldxPos = xPos;
  150. int oldyPos = yPos;
  151.  
  152. xPos += xMove;
  153. yPos += yMove;
  154.  
  155. if (xPos >= 2) {
  156. xPos = 2;
  157. }
  158. if (xPos <= 0) {
  159. xPos = 0;
  160. }
  161. if (yPos >= 2) {
  162. yPos = 2;
  163. }
  164. if (yPos <= 0) {
  165. yPos = 0;
  166. }
  167. temp = img[yPos * 3 + xPos];
  168. img[yPos * 3 + xPos] = img[oldyPos * 3 + oldxPos];
  169. img[oldyPos * 3 + oldxPos] = temp;
  170.  
  171. repaint();
  172. serviceRepaints();
  173. if (time == timeLimit) {
  174. ///Koniec///
  175. isAlive = false;
  176. Display.getDisplay(midlet).setCurrent(new EndCanvas(midlet));
  177. midlet.destroyApp(false);
  178. midlet.notifyDestroyed();
  179.  
  180. }
  181. }
  182.  
  183. public void commandAction(Command c, Displayable d) {
  184. switch (c.getCommandType()) {
  185. case Command.EXIT:
  186. isAlive = false;
  187. midlet.destroyApp(false);
  188. midlet.notifyDestroyed();
  189. losowanie();
  190. break;
  191. case Command.ITEM:
  192. losowanie();
  193. break;
  194. }
  195. }
  196.  
  197. private class HeartBeat extends Thread {
  198.  
  199. public void run() {
  200. while (isAlive == true) {
  201. try {
  202. sleep(100);
  203. timeCounter++;
  204. if (timeCounter == 10) {
  205. time++;
  206. timeCounter = 0;
  207. }
  208. update();
  209. } catch (Exception e) {
  210. isAlive = false;
  211. System.out.println("Exception on update thread");
  212. }
  213.  
  214. }
  215. System.out.println("Leaving update thread!");
  216. }
  217. }
  218. }
RAW Paste Data