Advertisement
PSN_CR33P

Old Space Game Template

Mar 16th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. =================================================================================================
  2. GIFT FOR 75TH
  3. =================================================================================================
  4.  
  5.  
  6.  
  7.  
  8. import java.awt.Color;
  9. import java.awt.Cursor;
  10. import java.awt.Dimension;
  11. import java.awt.Graphics;
  12. import java.awt.Point;
  13. import java.awt.Toolkit;
  14. import java.awt.event.ActionEvent;
  15. import java.awt.event.ActionListener;
  16. import java.awt.event.MouseEvent;
  17. import java.awt.event.MouseMotionAdapter;
  18. import java.awt.image.BufferedImage;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.util.ArrayList;
  22. import java.util.Iterator;
  23. import java.util.List;
  24.  
  25. import javax.imageio.ImageIO;
  26. import javax.swing.JFrame;
  27. import javax.swing.JPanel;
  28. import javax.swing.SwingUtilities;
  29. import javax.swing.Timer;
  30.  
  31. public class SpaceGame implements Runnable, ActionListener {
  32.  
  33.     public static void main(String[] args) {
  34.         SwingUtilities.invokeLater(new SpaceGame());
  35.     }
  36.  
  37.     private JFrame frame;
  38.     private JPanel panel;
  39.     private Timer timer;
  40.     private BufferedImage iShip;
  41.     private BufferedImage iRock;
  42.     private GameObject ship;
  43.     private List<Rock> rocks = new ArrayList<>();
  44.     private List<Rock> newRocks = new ArrayList<>();
  45.  
  46.  
  47.     @Override
  48.     public void run() {
  49.         try {
  50.             loadImages();
  51.        
  52.             frame = new JFrame("Space game");
  53.             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54.             frame.setCursor(noCursor());
  55.    
  56.             panel = new GamePanel();
  57.             panel.setBackground(Color.GREEN);
  58.             panel.setPreferredSize(new Dimension(640, 320));
  59.             frame.add(panel);
  60.             addMouseControl(panel);
  61.            
  62.             timer = new Timer(10, this);
  63.             timer.start();
  64.            
  65.             ship = new GameObject(0, 0, iShip);
  66.             rocks.add(new Rock(800, 200, iRock));
  67.            
  68.             frame.pack();
  69.             frame.setLocationRelativeTo(null);
  70.             frame.setVisible(true);
  71.         } catch (IOException e) {
  72.             e.printStackTrace();
  73.         }
  74.     }
  75.  
  76.     private Cursor noCursor() {
  77.         BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
  78.  
  79.         Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
  80.             cursorImg, new Point(0, 0), "blank cursor");
  81.        
  82.         return blankCursor;
  83.     }
  84.  
  85.     private void addMouseControl(JPanel panel) {
  86.         panel.addMouseMotionListener(new MouseMotionAdapter() {
  87.            
  88.             @Override
  89.             public void mouseMoved(MouseEvent e) {
  90.                 ship.setPosition(e.getX(), e.getY());
  91.             }
  92.         });
  93.     }
  94.  
  95.     private void loadImages() throws IOException {
  96.         iShip = ImageIO.read(new File("ship.png"));
  97.         iRock = ImageIO.read(new File("rock.png"));
  98.     }
  99.  
  100.     @Override
  101.     public void actionPerformed(ActionEvent e) {
  102.         newRocks.clear();
  103.         Iterator<Rock> it = rocks.iterator();
  104.         while (it.hasNext()) {
  105.             Rock rock = it.next();
  106.             rock.move();
  107.             if (!rock.hasSpawned() && rock.getX() < panel.getWidth()) {
  108.                 double newX = rock.getX() + 100 + Math.random() * 200;
  109.                 double newY = Math.random() * panel.getHeight();
  110.                 newRocks.add(new Rock(newX, newY, iRock));
  111.                 rock.setSpawned(true);
  112.             }
  113.             if (rock.getX() < -iRock.getWidth())
  114.                 it.remove();
  115.         }
  116.         rocks.addAll(newRocks);
  117.         panel.repaint();
  118.     }
  119.  
  120.     public class GamePanel extends JPanel {
  121.  
  122.         @Override
  123.         protected void paintComponent(Graphics g) {
  124.             super.paintComponent(g);
  125.            
  126.             //Draw space
  127.             g.setColor(Color.BLACK);
  128.             g.fillRect(0, 0, getWidth(), getHeight());
  129.            
  130.             //Draw objects
  131.             ship.drawWith(g);
  132.             for (GameObject rock : rocks)
  133.                 rock.drawWith(g);
  134.         }
  135.        
  136.     }  
  137. }
  138.  
  139. ----------------------------
  140.  
  141. import java.awt.Graphics;
  142. import java.awt.image.BufferedImage;
  143.  
  144. public class GameObject {
  145.     private double x;
  146.     private double y;
  147.     private BufferedImage img;
  148.     private double dx;
  149.     private double dy;
  150.  
  151.     public GameObject(double x, double y, BufferedImage img) {
  152.         setPosition(x, y);
  153.         this.img = img;
  154.     }
  155.    
  156.     public void setPosition(double x, double y) {
  157.         this.x = x;
  158.         this.y = y;
  159.     }
  160.    
  161.     public void setSpeed(double dx, double dy) {
  162.         this.dx = dx;
  163.         this.dy = dy;
  164.     }
  165.    
  166.     public void move() {
  167.         x += dx;
  168.         y += dy;
  169.     }
  170.    
  171.     public void drawWith(Graphics g) {
  172.         int imgX = (int) Math.round(x - img.getWidth() / 2);
  173.         int imgY = (int) Math.round(y - img.getHeight() / 2);
  174.         g.drawImage(img, imgX, imgY, null);
  175.     }
  176.  
  177.     public double getX() {
  178.         return x;
  179.     }
  180.    
  181.     public double getY() {
  182.         return y;
  183.     }
  184. }
  185.  
  186. -----------------------------------------------
  187.  
  188. import java.awt.image.BufferedImage;
  189.  
  190. public class Rock extends GameObject {
  191.  
  192.     private boolean spawned;
  193.    
  194.     public Rock(double x, double y, BufferedImage img) {
  195.         super(x, y, img);
  196.         setSpeed(-6, 0);
  197.     }
  198.    
  199.     public boolean hasSpawned() {
  200.         return spawned;
  201.     }
  202.  
  203.     public void setSpawned(boolean spawned) {
  204.         this.spawned = spawned;
  205.     }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement