troptrop

Project Code

Jun 5th, 2021
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.22 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3. import javax.swing.*;
  4. import java.util.Random;
  5. import java.awt.*;
  6.  
  7. import java.awt.event.*;
  8.  
  9. import java.util.ArrayList;
  10.  
  11. import java.awt.event.MouseListener;
  12. import java.awt.event.MouseMotionListener;
  13. import java.util.TimerTask;
  14.  
  15.  
  16. /*
  17.  
  18. * To change this license header, choose License Headers in Project Properties.
  19. * To change this template file, choose Tools | Templates
  20. * and open the template in the editor.
  21. */
  22. /**
  23. *
  24. * @author justi
  25. */
  26. public class test extends javax.swing.JFrame implements KeyListener, MouseListener, MouseMotionListener, Comparable, ActionListener {
  27. //Commands Guide: Press any key then type in commands. /create window to create a new window and /printwindows to print the properties of the windows.
  28.  
  29. public ArrayList<Window> list = new ArrayList<Window>();
  30. public Random rand = new Random();
  31. public ArrayList<Window> clicked = new ArrayList<Window>();
  32. public ArrayList<String> numbers = new ArrayList<String>();
  33. public boolean windowClicked;
  34. public int x = 250;
  35. public int y = 250;
  36. public int xSpeed = 0;
  37. public int ySpeed = 0;
  38. public Window w2;
  39. public Scanner sc = new Scanner(System.in);
  40. Timer t = new Timer(5, this);
  41. Timer t1 = new Timer(5, this);
  42. int score = 0;
  43. public Rectangle coin = new Rectangle(50, 50, 20, 20);
  44.  
  45. public ArrayList<Rectangle> b = new ArrayList<Rectangle>();
  46. public boolean dead = false;
  47.  
  48. /**
  49. * Creates new form MainFrame
  50. */
  51. public test() {
  52. t.start();
  53. initComponents();
  54. addKeyListener(this);
  55. addMouseListener(this);
  56. addMouseMotionListener(this);
  57. setFocusable(true);
  58. setFocusTraversalKeysEnabled(false);
  59. this.setLayout(null);
  60. Dimension d = new Dimension();
  61. d.setSize(500, 500);
  62. this.setSize(d);
  63.  
  64. b.add(new Rectangle(50, 50, 50, 50));
  65. b.add(new Rectangle(50, 150, 50, 50));
  66. b.add(new Rectangle(50, 250, 50, 50));
  67. b.add(new Rectangle(50, 350, 50, 50));
  68. b.add(new Rectangle(50, 450, 50, 50));
  69.  
  70. }
  71.  
  72. @Override
  73. public void paint(Graphics g) {
  74. Robot r = null;
  75. try {
  76. r = new Robot();
  77. } catch (AWTException e) {
  78. e.printStackTrace();
  79. }
  80.  
  81.  
  82. Color c = r.getPixelColor(x, y);
  83. super.paint(g);
  84. Graphics2D g2 = (Graphics2D) g;
  85. g2.setColor(Color.BLUE);
  86. g2.fillRect(x, y, 20, 20);
  87. g2.setColor(Color.YELLOW);
  88. g2.fillRect((int) coin.getX(),(int) coin.getY(), (int)(coin.getWidth()), (int)(coin.getHeight()));
  89. t1.start();
  90. for (int j = 0; j < b.size(); j++) {
  91. g2.setColor(Color.GREEN);
  92. g2.fillRect((int) b.get(j).getX(), (int) b.get(j).getY(), (int) b.get(j).getWidth(), (int) b.get(j).getHeight());
  93.  
  94.  
  95. }
  96.  
  97.  
  98. if(overlaps(coin)){
  99. coin.setLocation((int) (Math.random() * 450) + 1, (int) (Math.random() * 450) + 1);
  100. System.out.println(coin.getX());
  101. System.out.println(coin.getY());
  102. score++;
  103. System.out.println(score);
  104.  
  105. }
  106. for(int l = 0; l < b.size(); l++){
  107. if(overlaps(b.get(l))){
  108. System.out.println("Game Over!");
  109. System.out.println("Score:" +score);
  110. System.exit(0);
  111. }
  112. }
  113.  
  114. }
  115. /**
  116. * This method is called from within the constructor to initialize the form.
  117. * WARNING: Do NOT modify this code. The content of this method is always
  118. * regenerated by the Form Editor.
  119. */
  120. @SuppressWarnings("unchecked")
  121. // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  122. private void initComponents() {
  123.  
  124. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  125.  
  126. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  127. getContentPane().setLayout(layout);
  128. layout.setHorizontalGroup(
  129. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  130. .addGap(0, 400, Short.MAX_VALUE)
  131. );
  132. layout.setVerticalGroup(
  133. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  134. .addGap(0, 300, Short.MAX_VALUE)
  135. );
  136.  
  137. pack();
  138. }// </editor-fold>//GEN-END:initComponents
  139.  
  140. /**
  141. * @param args the command line arguments
  142. */
  143. public static void main(String args[]) {
  144. /* Set the Nimbus look and feel */
  145. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  146. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  147. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  148. */
  149. try {
  150. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  151. if ("Nimbus".equals(info.getName())) {
  152. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  153. break;
  154. }
  155. }
  156. } catch (ClassNotFoundException ex) {
  157. java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  158. } catch (InstantiationException ex) {
  159. java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  160. } catch (IllegalAccessException ex) {
  161. java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  162. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  163. java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  164. }
  165. //</editor-fold>
  166.  
  167. /* Create and display the form */
  168. java.awt.EventQueue.invokeLater(new Runnable() {
  169. public void run() {
  170. new test().setVisible(true);
  171.  
  172. }
  173. });
  174. }
  175. @Override
  176. public void actionPerformed(ActionEvent e) {
  177. x += xSpeed;
  178. y += ySpeed;
  179. int random = (int) (Math.random() * 5);
  180. for(int k = 0; k < b.size(); k++) {
  181.  
  182.  
  183. if(b.get(k).getX() >= 500){
  184. b.get(k).setLocation(0, (int)b.get(k).getY());
  185. random = (int) Math.random() * 5;
  186. }
  187. if(random != k) {
  188. b.get(k).setLocation((int) b.get(k).getX() + 4, (int) b.get(k).getY());
  189. }
  190. repaint();
  191.  
  192. }
  193.  
  194.  
  195. repaint();
  196. }
  197. public void up(){
  198. xSpeed = 0;
  199. ySpeed = -1;
  200. }
  201. public void down(){
  202. xSpeed = 0;
  203. ySpeed = 1;
  204. }
  205. public void left(){
  206. xSpeed = -1;
  207. ySpeed = 0;
  208. }
  209. public void right(){
  210. xSpeed = 1;
  211. ySpeed = 0;
  212. }
  213.  
  214. @Override
  215. public void keyPressed(KeyEvent e) {
  216. if(e.getKeyCode() == KeyEvent.VK_UP){
  217. up();
  218. }
  219. if(e.getKeyCode() == KeyEvent.VK_DOWN){
  220. down();
  221. }
  222. if(e.getKeyCode() == KeyEvent.VK_LEFT){
  223. left();
  224. }
  225. if(e.getKeyCode() == KeyEvent.VK_RIGHT){
  226. right();
  227. }
  228. }
  229.  
  230. @Override
  231. public void keyReleased(KeyEvent e) {
  232. // TODO Auto-generated method stub
  233. if(e.getKeyCode() == KeyEvent.VK_UP){
  234. xSpeed = 0;
  235. ySpeed = 0;
  236. }
  237. if(e.getKeyCode() == KeyEvent.VK_DOWN){
  238. xSpeed = 0;
  239. ySpeed = 0;
  240. }
  241. if(e.getKeyCode() == KeyEvent.VK_LEFT){
  242. xSpeed = 0;
  243. ySpeed = 0;
  244. }
  245. if(e.getKeyCode() == KeyEvent.VK_RIGHT){
  246. xSpeed = 0;
  247. ySpeed = 0;
  248. }
  249. }
  250.  
  251. @Override
  252. public void keyTyped(KeyEvent e) {
  253. // TODO Auto-generated method stub
  254.  
  255. }
  256.  
  257. @Override
  258. public void mouseClicked(MouseEvent e) {
  259. // TODO Auto-generated method stub
  260. // System.out.println("mouse clicked");
  261.  
  262. }
  263.  
  264. @Override
  265. public void mouseEntered(MouseEvent arg0) {
  266. // TODO Auto-generated method stub
  267.  
  268. }
  269.  
  270. @Override
  271. public void mouseExited(MouseEvent arg0) {
  272. // TODO Auto-generated method stub
  273.  
  274. }
  275.  
  276. @Override
  277. public void mousePressed(MouseEvent e) {
  278.  
  279.  
  280. }
  281.  
  282. @Override
  283. public void mouseReleased(MouseEvent arg0) {
  284. // TODO Auto-generated method stub
  285. windowClicked = false;
  286. }
  287.  
  288. @Override
  289. public void mouseMoved(MouseEvent e) {
  290.  
  291. }
  292.  
  293. @Override
  294. public void mouseDragged(MouseEvent e) {
  295.  
  296.  
  297. }
  298.  
  299.  
  300.  
  301. @Override
  302. public int compareTo(Object String) {
  303. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  304. }
  305. public boolean overlaps (Rectangle r) {
  306. return x < r.x + r.width && x + 20 > r.x && y < r.y + r.height && y + 20 > r.y;
  307. }
  308.  
  309.  
  310.  
  311. // Variables declaration - do not modify//GEN-BEGIN:variables
  312. // End of variables declaration//GEN-END:variables
  313. }
  314.  
  315.  
Advertisement
Add Comment
Please, Sign In to add comment