Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Font;
  4. import java.awt.FontMetrics;
  5. import java.awt.Graphics;
  6. import java.awt.Image;
  7. import java.awt.Toolkit;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.KeyAdapter;
  11. import java.awt.event.KeyEvent;
  12.  
  13. import javax.swing.Timer;
  14. import javax.swing.ImageIcon;
  15. import javax.swing.JPanel;
  16.  
  17. public class Plansza extends JPanel implements ActionListener {
  18.  
  19. private final int WIDTH = 800;
  20. private final int HEIGHT = 800;
  21. private final int PUNKT_ROZMIAR = 10;
  22. private final int P_PUNKTY = 900;
  23. private final int DELAY = 100;
  24.  
  25. private final int x[] = new int[P_PUNKTY];
  26. private final int y[] = new int[P_PUNKTY];
  27.  
  28. private int punkty;
  29. private boolean lewo = false;
  30. private boolean prawo = true;
  31. private boolean gora = false;
  32. private boolean dol = false;
  33. private boolean wGrze = true;
  34.  
  35. private Timer timer;
  36. private Image ogon;
  37. private Image glowa;
  38. private Image logo;
  39. private Image logo1;
  40.  
  41. private int jabłko_x;
  42. private int jabłko_y;
  43. private int RAND=29;
  44.  
  45. public Plansza() {
  46. tworzPlansza();
  47. }
  48.  
  49. private void tworzPlansza() {
  50. setBackground(Color.white);
  51. setFocusable(true);
  52.  
  53. setPreferredSize(new Dimension(WIDTH, HEIGHT));
  54. loadImages();
  55. tworzGra();
  56. addKeyListener(new TAdapter());
  57. }
  58.  
  59. private void loadImages() {
  60.  
  61. ImageIcon io = new ImageIcon("src/icons/ogon.png");
  62. ogon = io.getImage();
  63.  
  64. ImageIcon ig = new ImageIcon("src/icons/glowa.png");
  65. glowa = ig.getImage();
  66.  
  67. ImageIcon il = new ImageIcon("src/icons/logo.png");
  68. logo = il.getImage();
  69.  
  70. ImageIcon ill = new ImageIcon("src/icons/logo1.jpg");
  71.  
  72. }
  73.  
  74. private void tworzGra() {
  75.  
  76. punkty = 3;
  77.  
  78. for (int z = 0; z < punkty; z++) {
  79. x[z] = 50 - z * 10;
  80. y[z] = 50;
  81. }
  82.  
  83. Apple();
  84.  
  85. timer = new Timer(DELAY, this);
  86. timer.start();
  87. }
  88. @Override
  89. public void paintComponent(Graphics g) {
  90. super.paintComponent(g);
  91.  
  92. doDrawing(g);
  93. }
  94.  
  95. private void doDrawing(Graphics g) {
  96.  
  97. if (wGrze) {
  98.  
  99. g.drawImage(logo, jabłko_x, jabłko_y, this);
  100. for (int z = 0; z < punkty; z++) {
  101. if (z == 0) {
  102. g.drawImage(glowa, x[z], y[z], this);
  103. } else {
  104. g.drawImage(ogon, x[z], y[z], this);
  105. }
  106. }
  107. }
  108. else {
  109. gameOver(g);
  110. }
  111.  
  112.  
  113. }
  114. private void ruch() {
  115.  
  116. for (int z = punkty; z > 0; z--) {
  117. x[z] = x[(z - 1)];
  118. y[z] = y[(z - 1)];
  119. }
  120.  
  121. if (lewo) {
  122. x[0] -= PUNKT_ROZMIAR;
  123. }
  124.  
  125. if (prawo) {
  126. x[0] += PUNKT_ROZMIAR;
  127. }
  128.  
  129. if (gora) {
  130. y[0] -= PUNKT_ROZMIAR;
  131. }
  132.  
  133. if (dol) {
  134. y[0] += PUNKT_ROZMIAR;
  135. }
  136. }
  137.  
  138.  
  139. @Override
  140. public void actionPerformed(ActionEvent e) {
  141.  
  142. if (wGrze) {
  143. checkApple();
  144. ruch();
  145. checkCollision();
  146. }
  147.  
  148. repaint();
  149. }
  150. private class TAdapter extends KeyAdapter {
  151.  
  152. @Override
  153. public void keyPressed(KeyEvent e) {
  154.  
  155. int key = e.getKeyCode();
  156.  
  157. if ((key == KeyEvent.VK_LEFT) && (!prawo)) {
  158. lewo = true;
  159. gora = false;
  160. dol = false;
  161. }
  162.  
  163. if ((key == KeyEvent.VK_RIGHT) && (!lewo)) {
  164. prawo = true;
  165. gora = false;
  166. dol = false;
  167. }
  168.  
  169. if ((key == KeyEvent.VK_UP) && (!dol)) {
  170. gora = true;
  171. prawo = false;
  172. lewo = false;
  173. }
  174.  
  175. if ((key == KeyEvent.VK_DOWN) && (!gora)) {
  176. dol = true;
  177. prawo = false;
  178. lewo = false;
  179. }
  180. }
  181. }
  182. private void Apple() {
  183.  
  184. int r = (int) (Math.random() * RAND);
  185. jabłko_x = ((r * PUNKT_ROZMIAR));
  186.  
  187. r = (int) (Math.random() * RAND);
  188. jabłko_y = ((r * PUNKT_ROZMIAR));
  189. }
  190.  
  191. private void checkApple() {
  192.  
  193. if ((x[0] == jabłko_x) && (y[0] == jabłko_y)) {
  194.  
  195. punkty++;
  196. Apple();
  197. }
  198. }
  199.  
  200. private void checkCollision() {
  201.  
  202. for (int z = punkty; z > 0; z--) {
  203.  
  204. if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
  205. wGrze = false;
  206. }
  207. }
  208.  
  209. if (y[0] >= HEIGHT) {
  210. wGrze = false;
  211. }
  212.  
  213. if (y[0] < 0) {
  214. wGrze = false;
  215. }
  216.  
  217. if (x[0] >= WIDTH) {
  218. wGrze = false;
  219. }
  220.  
  221. if (x[0] < 0) {
  222. wGrze = false;
  223. }
  224.  
  225. if (!wGrze) {
  226. timer.stop();
  227. }
  228. }
  229.  
  230. private void gameOver(Graphics g) {
  231.  
  232. String msg = "Koniec gry";
  233. Font small = new Font("Arial", Font.BOLD, 25);
  234. FontMetrics metr = getFontMetrics(small);
  235. Image logo1 = Toolkit.getDefaultToolkit().getImage("src/icons/logo1.jpg");
  236. g.drawImage(logo1, 50, 100, this);
  237. g.setColor(Color.black);
  238. g.setFont(small);
  239. g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2, HEIGHT / 2);
  240. }
  241.  
  242.  
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement