Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- =================================================================================================
- GIFT FOR 75TH
- =================================================================================================
- import java.awt.Color;
- import java.awt.Cursor;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Point;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionAdapter;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import javax.imageio.ImageIO;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.SwingUtilities;
- import javax.swing.Timer;
- public class SpaceGame implements Runnable, ActionListener {
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new SpaceGame());
- }
- private JFrame frame;
- private JPanel panel;
- private Timer timer;
- private BufferedImage iShip;
- private BufferedImage iRock;
- private GameObject ship;
- private List<Rock> rocks = new ArrayList<>();
- private List<Rock> newRocks = new ArrayList<>();
- @Override
- public void run() {
- try {
- loadImages();
- frame = new JFrame("Space game");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setCursor(noCursor());
- panel = new GamePanel();
- panel.setBackground(Color.GREEN);
- panel.setPreferredSize(new Dimension(640, 320));
- frame.add(panel);
- addMouseControl(panel);
- timer = new Timer(10, this);
- timer.start();
- ship = new GameObject(0, 0, iShip);
- rocks.add(new Rock(800, 200, iRock));
- frame.pack();
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private Cursor noCursor() {
- BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
- Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
- cursorImg, new Point(0, 0), "blank cursor");
- return blankCursor;
- }
- private void addMouseControl(JPanel panel) {
- panel.addMouseMotionListener(new MouseMotionAdapter() {
- @Override
- public void mouseMoved(MouseEvent e) {
- ship.setPosition(e.getX(), e.getY());
- }
- });
- }
- private void loadImages() throws IOException {
- iShip = ImageIO.read(new File("ship.png"));
- iRock = ImageIO.read(new File("rock.png"));
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- newRocks.clear();
- Iterator<Rock> it = rocks.iterator();
- while (it.hasNext()) {
- Rock rock = it.next();
- rock.move();
- if (!rock.hasSpawned() && rock.getX() < panel.getWidth()) {
- double newX = rock.getX() + 100 + Math.random() * 200;
- double newY = Math.random() * panel.getHeight();
- newRocks.add(new Rock(newX, newY, iRock));
- rock.setSpawned(true);
- }
- if (rock.getX() < -iRock.getWidth())
- it.remove();
- }
- rocks.addAll(newRocks);
- panel.repaint();
- }
- public class GamePanel extends JPanel {
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- //Draw space
- g.setColor(Color.BLACK);
- g.fillRect(0, 0, getWidth(), getHeight());
- //Draw objects
- ship.drawWith(g);
- for (GameObject rock : rocks)
- rock.drawWith(g);
- }
- }
- }
- ----------------------------
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- public class GameObject {
- private double x;
- private double y;
- private BufferedImage img;
- private double dx;
- private double dy;
- public GameObject(double x, double y, BufferedImage img) {
- setPosition(x, y);
- this.img = img;
- }
- public void setPosition(double x, double y) {
- this.x = x;
- this.y = y;
- }
- public void setSpeed(double dx, double dy) {
- this.dx = dx;
- this.dy = dy;
- }
- public void move() {
- x += dx;
- y += dy;
- }
- public void drawWith(Graphics g) {
- int imgX = (int) Math.round(x - img.getWidth() / 2);
- int imgY = (int) Math.round(y - img.getHeight() / 2);
- g.drawImage(img, imgX, imgY, null);
- }
- public double getX() {
- return x;
- }
- public double getY() {
- return y;
- }
- }
- -----------------------------------------------
- import java.awt.image.BufferedImage;
- public class Rock extends GameObject {
- private boolean spawned;
- public Rock(double x, double y, BufferedImage img) {
- super(x, y, img);
- setSpeed(-6, 0);
- }
- public boolean hasSpawned() {
- return spawned;
- }
- public void setSpawned(boolean spawned) {
- this.spawned = spawned;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement