learning 2d game programing
This commit is contained in:
79
DemoProjects/src/animation/swingTimer/Board.java
Normal file
79
DemoProjects/src/animation/swingTimer/Board.java
Normal file
@@ -0,0 +1,79 @@
|
||||
package animation.swingTimer;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Image;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.Timer;
|
||||
|
||||
public class Board extends JPanel
|
||||
implements ActionListener {
|
||||
|
||||
private final int B_WIDTH = 700;
|
||||
private final int B_HEIGHT = 700;
|
||||
private final int INITIAL_X = -40;
|
||||
private final int INITIAL_Y = -40;
|
||||
private final int DELAY = 5;
|
||||
|
||||
private Image star;
|
||||
private Timer timer;
|
||||
private int x, y;
|
||||
|
||||
public Board() {
|
||||
|
||||
initBoard();
|
||||
}
|
||||
|
||||
private void loadImage() {
|
||||
|
||||
ImageIcon ii = new ImageIcon("/home/bitecoding/Pictures/star.png");
|
||||
star = ii.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT);
|
||||
}
|
||||
|
||||
private void initBoard() {
|
||||
|
||||
setBackground(Color.BLACK);
|
||||
setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
|
||||
|
||||
loadImage();
|
||||
|
||||
x = INITIAL_X;
|
||||
y = INITIAL_Y;
|
||||
|
||||
timer = new Timer(DELAY, this);
|
||||
timer.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
|
||||
drawStar(g);
|
||||
}
|
||||
|
||||
private void drawStar(Graphics g) {
|
||||
|
||||
g.drawImage(star, x, y, this);
|
||||
Toolkit.getDefaultToolkit().sync();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
x += 10;
|
||||
y += 10;
|
||||
|
||||
if (y > B_HEIGHT) {
|
||||
|
||||
y = INITIAL_Y;
|
||||
x = INITIAL_X;
|
||||
}
|
||||
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
32
DemoProjects/src/animation/swingTimer/SwingTimer.java
Normal file
32
DemoProjects/src/animation/swingTimer/SwingTimer.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package animation.swingTimer;
|
||||
|
||||
import java.awt.EventQueue;
|
||||
import javax.swing.JFrame;
|
||||
|
||||
public class SwingTimer extends JFrame {
|
||||
|
||||
public SwingTimer() {
|
||||
|
||||
initUI();
|
||||
}
|
||||
|
||||
private void initUI() {
|
||||
|
||||
add(new Board());
|
||||
|
||||
setResizable(false);
|
||||
pack();
|
||||
|
||||
setTitle("Star");
|
||||
setLocationRelativeTo(null);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
EventQueue.invokeLater(() -> {
|
||||
SwingTimer ex = new SwingTimer();
|
||||
ex.setVisible(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
112
DemoProjects/src/animation/thread/Board.java
Normal file
112
DemoProjects/src/animation/thread/Board.java
Normal file
@@ -0,0 +1,112 @@
|
||||
package animation.thread;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Image;
|
||||
import java.awt.Toolkit;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
public class Board extends JPanel
|
||||
implements Runnable {
|
||||
|
||||
private final int B_WIDTH = 350;
|
||||
private final int B_HEIGHT = 350;
|
||||
private final int INITIAL_X = -40;
|
||||
private final int INITIAL_Y = -40;
|
||||
private final int DELAY = 25;
|
||||
|
||||
private Image star;
|
||||
private Thread animator;
|
||||
private int x, y;
|
||||
|
||||
public Board() {
|
||||
|
||||
initBoard();
|
||||
}
|
||||
|
||||
private void loadImage() {
|
||||
|
||||
ImageIcon ii = new ImageIcon("/home/bitecoding/Pictures/star.png");
|
||||
star = ii.getImage().getScaledInstance(50,50, Image.SCALE_DEFAULT);
|
||||
}
|
||||
|
||||
private void initBoard() {
|
||||
|
||||
setBackground(Color.BLACK);
|
||||
setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
|
||||
|
||||
loadImage();
|
||||
|
||||
x = INITIAL_X;
|
||||
y = INITIAL_Y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addNotify() {
|
||||
super.addNotify();
|
||||
|
||||
animator = new Thread(this);
|
||||
animator.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
|
||||
drawStar(g);
|
||||
}
|
||||
|
||||
private void drawStar(Graphics g) {
|
||||
|
||||
g.drawImage(star, x, y, this);
|
||||
Toolkit.getDefaultToolkit().sync();
|
||||
}
|
||||
|
||||
private void cycle() {
|
||||
|
||||
x += 1;
|
||||
y += 1;
|
||||
|
||||
if (y > B_HEIGHT) {
|
||||
|
||||
y = INITIAL_Y;
|
||||
x = INITIAL_X;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
long beforeTime, timeDiff, sleep;
|
||||
|
||||
beforeTime = System.currentTimeMillis();
|
||||
|
||||
while (true) {
|
||||
|
||||
cycle();
|
||||
repaint();
|
||||
|
||||
timeDiff = System.currentTimeMillis() - beforeTime;
|
||||
sleep = DELAY - timeDiff;
|
||||
|
||||
if (sleep < 0) {
|
||||
sleep = 2;
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(sleep);
|
||||
} catch (InterruptedException e) {
|
||||
|
||||
String msg = String.format("Thread interrupted: %s", e.getMessage());
|
||||
|
||||
JOptionPane.showMessageDialog(this, msg, "Error",
|
||||
JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
|
||||
beforeTime = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
}
|
||||
32
DemoProjects/src/animation/thread/ThreadAnimation.java
Normal file
32
DemoProjects/src/animation/thread/ThreadAnimation.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package animation.thread;
|
||||
|
||||
import java.awt.EventQueue;
|
||||
import javax.swing.JFrame;
|
||||
|
||||
public class ThreadAnimation extends JFrame {
|
||||
|
||||
public ThreadAnimation() {
|
||||
|
||||
initUI();
|
||||
}
|
||||
|
||||
private void initUI() {
|
||||
|
||||
add(new Board());
|
||||
|
||||
setResizable(false);
|
||||
pack();
|
||||
|
||||
setTitle("Star");
|
||||
setLocationRelativeTo(null);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
EventQueue.invokeLater(() -> {
|
||||
JFrame ex = new ThreadAnimation();
|
||||
ex.setVisible(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
81
DemoProjects/src/animation/utilityTimer/Board.java
Normal file
81
DemoProjects/src/animation/utilityTimer/Board.java
Normal file
@@ -0,0 +1,81 @@
|
||||
package animation.utilityTimer;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Image;
|
||||
import java.awt.Toolkit;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
public class Board extends JPanel {
|
||||
|
||||
private final int B_WIDTH = 350;
|
||||
private final int B_HEIGHT = 350;
|
||||
private final int INITIAL_X = -40;
|
||||
private final int INITIAL_Y = -40;
|
||||
private final int INITIAL_DELAY = 0;
|
||||
private final int PERIOD_INTERVAL = 25;
|
||||
|
||||
private Image star;
|
||||
private Timer timer;
|
||||
private int x, y;
|
||||
|
||||
public Board() {
|
||||
|
||||
initBoard();
|
||||
}
|
||||
|
||||
private void loadImage() {
|
||||
|
||||
ImageIcon ii = new ImageIcon("/home/bitecoding/Pictures/star.png");
|
||||
star = ii.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT);
|
||||
}
|
||||
|
||||
private void initBoard() {
|
||||
|
||||
setBackground(Color.BLACK);
|
||||
setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
|
||||
|
||||
loadImage();
|
||||
|
||||
x = INITIAL_X;
|
||||
y = INITIAL_Y;
|
||||
|
||||
timer = new Timer();
|
||||
timer.scheduleAtFixedRate(new ScheduleTask(),
|
||||
INITIAL_DELAY, PERIOD_INTERVAL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
|
||||
drawStar(g);
|
||||
}
|
||||
|
||||
private void drawStar(Graphics g) {
|
||||
|
||||
g.drawImage(star, x, y, this);
|
||||
Toolkit.getDefaultToolkit().sync();
|
||||
}
|
||||
|
||||
private class ScheduleTask extends TimerTask {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
x += 1;
|
||||
y += 1;
|
||||
|
||||
if (y > B_HEIGHT) {
|
||||
y = INITIAL_Y;
|
||||
x = INITIAL_X;
|
||||
}
|
||||
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
}
|
||||
32
DemoProjects/src/animation/utilityTimer/UtilityTimer.java
Normal file
32
DemoProjects/src/animation/utilityTimer/UtilityTimer.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package animation.utilityTimer;
|
||||
|
||||
import java.awt.EventQueue;
|
||||
import javax.swing.JFrame;
|
||||
|
||||
public class UtilityTimer extends JFrame {
|
||||
|
||||
public UtilityTimer() {
|
||||
|
||||
initUI();
|
||||
}
|
||||
|
||||
private void initUI() {
|
||||
|
||||
add(new Board());
|
||||
|
||||
setResizable(false);
|
||||
pack();
|
||||
|
||||
setTitle("Star");
|
||||
setLocationRelativeTo(null);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
EventQueue.invokeLater(() -> {
|
||||
JFrame ex = new UtilityTimer();
|
||||
ex.setVisible(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package minitennis1;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.geom.Ellipse2D;
|
||||
|
||||
public class Game2 extends JPanel {
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g) {
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
g2d.setColor(Color.RED);
|
||||
g2d.fillOval(0, 0, 30, 30);
|
||||
g2d.drawOval(0, 50, 30, 30);
|
||||
g2d.fillRect(50, 0, 30, 30);
|
||||
g2d.drawRect(50, 50, 30, 30);
|
||||
|
||||
g2d.draw(new Ellipse2D.Double(0, 100, 30, 30));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
JFrame frame = new JFrame("Mini Tennis");
|
||||
frame.add(new Game2());
|
||||
frame.setSize(300, 300);
|
||||
frame.setVisible(true);
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
package game.minitennis2;
|
||||
|
||||
public class Game {
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package painting;
|
||||
package painting.SwingPaintDemo;
|
||||
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.JFrame;
|
||||
@@ -1,4 +1,4 @@
|
||||
package painting;
|
||||
package painting.SwingPaintDemo;
|
||||
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.JFrame;
|
||||
@@ -1,4 +1,4 @@
|
||||
package painting;
|
||||
package painting.SwingPaintDemo;
|
||||
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.JFrame;
|
||||
@@ -8,10 +8,7 @@ import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseMotionListener;
|
||||
import java.awt.event.MouseMotionAdapter;
|
||||
|
||||
public class SwingPaintDemo3 {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package painting;
|
||||
package painting.SwingPaintDemo;
|
||||
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.JFrame;
|
||||
50
DemoProjects/src/painting/donut/Board.java
Normal file
50
DemoProjects/src/painting/donut/Board.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package painting.donut;
|
||||
|
||||
import java.awt.BasicStroke;
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.geom.Ellipse2D;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
public class Board extends JPanel {
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
|
||||
drawDonut(g);
|
||||
}
|
||||
|
||||
private void drawDonut(Graphics g) {
|
||||
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
|
||||
RenderingHints rh
|
||||
= new RenderingHints(RenderingHints.KEY_ANTIALIASING,
|
||||
RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
rh.put(RenderingHints.KEY_RENDERING,
|
||||
RenderingHints.VALUE_RENDER_QUALITY);
|
||||
|
||||
g2d.setRenderingHints(rh);
|
||||
|
||||
Dimension size = getSize();
|
||||
double w = size.getWidth();
|
||||
double h = size.getHeight();
|
||||
|
||||
Ellipse2D e = new Ellipse2D.Double(0, 0, 80, 130);
|
||||
g2d.setStroke(new BasicStroke(1));
|
||||
g2d.setColor(Color.gray);
|
||||
|
||||
for (double deg = 0; deg < 360; deg += 10) {
|
||||
AffineTransform at
|
||||
= AffineTransform.getTranslateInstance(w/2, h/2);
|
||||
at.rotate(Math.toRadians(deg));
|
||||
g2d.draw(at.createTransformedShape(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
31
DemoProjects/src/painting/donut/Donut.java
Normal file
31
DemoProjects/src/painting/donut/Donut.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package painting.donut;
|
||||
|
||||
import java.awt.EventQueue;
|
||||
import javax.swing.JFrame;
|
||||
|
||||
public class Donut extends JFrame {
|
||||
|
||||
public Donut() {
|
||||
|
||||
initUI();
|
||||
}
|
||||
|
||||
private void initUI() {
|
||||
|
||||
add(new Board());
|
||||
|
||||
setSize(330, 330);
|
||||
|
||||
setTitle("Donut");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setLocationRelativeTo(null);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
EventQueue.invokeLater(() -> {
|
||||
Donut ex = new Donut();
|
||||
ex.setVisible(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
38
DemoProjects/src/painting/image/Board.java
Normal file
38
DemoProjects/src/painting/image/Board.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package painting.image;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Image;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
public class Board extends JPanel {
|
||||
|
||||
private Image bardejov;
|
||||
|
||||
public Board() {
|
||||
|
||||
initBoard();
|
||||
}
|
||||
|
||||
private void initBoard() {
|
||||
|
||||
loadImage();
|
||||
|
||||
int w = bardejov.getWidth(this);
|
||||
int h = bardejov.getHeight(this);
|
||||
setPreferredSize(new Dimension(w, h));
|
||||
}
|
||||
|
||||
private void loadImage() {
|
||||
|
||||
ImageIcon ii = new ImageIcon("/home/bitecoding/Pictures/1182657.jpg");
|
||||
bardejov = ii.getImage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
|
||||
g.drawImage(bardejov, 0, 0, null);
|
||||
}
|
||||
}
|
||||
27
DemoProjects/src/painting/image/Image.java
Normal file
27
DemoProjects/src/painting/image/Image.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package painting.image;
|
||||
|
||||
import java.awt.EventQueue;
|
||||
import javax.swing.JFrame;
|
||||
|
||||
public class Image extends JFrame {
|
||||
|
||||
public Image() {
|
||||
initUI();
|
||||
}
|
||||
|
||||
private void initUI() {
|
||||
add(new Board());
|
||||
pack();
|
||||
setTitle("Bardejov");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setLocationRelativeTo(null);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
EventQueue.invokeLater(() -> {
|
||||
Image ex = new Image();
|
||||
ex.setVisible(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
109
DemoProjects/src/sprites/shootingMissles/Board.java
Normal file
109
DemoProjects/src/sprites/shootingMissles/Board.java
Normal file
@@ -0,0 +1,109 @@
|
||||
package sprites.shootingMissles;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.util.List;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.Timer;
|
||||
|
||||
public class Board extends JPanel implements ActionListener {
|
||||
|
||||
private final int ICRAFT_X = 40;
|
||||
private final int ICRAFT_Y = 60;
|
||||
private final int DELAY = 10;
|
||||
private Timer timer;
|
||||
private SpaceShip spaceShip;
|
||||
|
||||
public Board() {
|
||||
|
||||
initBoard();
|
||||
}
|
||||
|
||||
private void initBoard() {
|
||||
|
||||
addKeyListener(new TAdapter());
|
||||
setBackground(Color.BLACK);
|
||||
setFocusable(true);
|
||||
|
||||
spaceShip = new SpaceShip(ICRAFT_X, ICRAFT_Y);
|
||||
|
||||
timer = new Timer(DELAY, this);
|
||||
timer.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
|
||||
doDrawing(g);
|
||||
|
||||
Toolkit.getDefaultToolkit().sync();
|
||||
}
|
||||
|
||||
private void doDrawing(Graphics g) {
|
||||
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
|
||||
g2d.drawImage(spaceShip.getImage(), spaceShip.getX(),
|
||||
spaceShip.getY(), this);
|
||||
|
||||
List<Missile> missiles = spaceShip.getMissiles();
|
||||
|
||||
for (Missile missile : missiles) {
|
||||
|
||||
g2d.drawImage(missile.getImage(), missile.getX(),
|
||||
missile.getY(), this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
updateMissiles();
|
||||
updateSpaceShip();
|
||||
|
||||
repaint();
|
||||
}
|
||||
|
||||
private void updateMissiles() {
|
||||
|
||||
List<Missile> missiles = spaceShip.getMissiles();
|
||||
|
||||
for (int i = 0; i < missiles.size(); i++) {
|
||||
|
||||
Missile missile = missiles.get(i);
|
||||
|
||||
if (missile.isVisible()) {
|
||||
|
||||
missile.move();
|
||||
} else {
|
||||
|
||||
missiles.remove(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateSpaceShip() {
|
||||
|
||||
spaceShip.move();
|
||||
}
|
||||
|
||||
private class TAdapter extends KeyAdapter {
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
spaceShip.keyReleased(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
spaceShip.keyPressed(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
32
DemoProjects/src/sprites/shootingMissles/Executor.java
Normal file
32
DemoProjects/src/sprites/shootingMissles/Executor.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package sprites.shootingMissles;
|
||||
|
||||
import java.awt.EventQueue;
|
||||
import javax.swing.JFrame;
|
||||
|
||||
public class Executor extends JFrame {
|
||||
|
||||
public Executor() {
|
||||
|
||||
initUI();
|
||||
}
|
||||
|
||||
private void initUI() {
|
||||
|
||||
add(new Board());
|
||||
|
||||
setSize(400, 300);
|
||||
setResizable(false);
|
||||
|
||||
setTitle("Shooting missiles");
|
||||
setLocationRelativeTo(null);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
EventQueue.invokeLater(() -> {
|
||||
Executor ex = new Executor();
|
||||
ex.setVisible(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
27
DemoProjects/src/sprites/shootingMissles/Missile.java
Normal file
27
DemoProjects/src/sprites/shootingMissles/Missile.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package sprites.shootingMissles;
|
||||
|
||||
public class Missile extends Sprite {
|
||||
|
||||
private final int BOARD_WIDTH = 390;
|
||||
private final int MISSILE_SPEED = 2;
|
||||
|
||||
public Missile(int x, int y) {
|
||||
super(x, y);
|
||||
|
||||
initMissile();
|
||||
}
|
||||
|
||||
private void initMissile() {
|
||||
loadImage("/home/bitecoding/Pictures/laser.png",35,35);
|
||||
getImageDimensions();
|
||||
}
|
||||
|
||||
public void move() {
|
||||
|
||||
y -= MISSILE_SPEED;
|
||||
|
||||
if (y < 0) {
|
||||
visible = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
85
DemoProjects/src/sprites/shootingMissles/SpaceShip.java
Normal file
85
DemoProjects/src/sprites/shootingMissles/SpaceShip.java
Normal file
@@ -0,0 +1,85 @@
|
||||
package sprites.shootingMissles;
|
||||
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SpaceShip extends Sprite {
|
||||
|
||||
private int dx;
|
||||
private int dy;
|
||||
private List<Missile> missiles;
|
||||
|
||||
public SpaceShip(int x, int y) {
|
||||
super(x, y);
|
||||
|
||||
initSpaceShip();
|
||||
}
|
||||
|
||||
private void initSpaceShip() {
|
||||
|
||||
missiles = new ArrayList<>();
|
||||
|
||||
loadImage("/home/bitecoding/Pictures/spaceship.png",50,50);
|
||||
getImageDimensions();
|
||||
}
|
||||
|
||||
public void move() {
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
|
||||
public List<Missile> getMissiles() {
|
||||
return missiles;
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e) {
|
||||
|
||||
int key = e.getKeyCode();
|
||||
|
||||
if (key == KeyEvent.VK_SPACE) {
|
||||
fire();
|
||||
}
|
||||
|
||||
if (key == KeyEvent.VK_LEFT) {
|
||||
dx = -1;
|
||||
}
|
||||
|
||||
if (key == KeyEvent.VK_RIGHT) {
|
||||
dx = 1;
|
||||
}
|
||||
|
||||
if (key == KeyEvent.VK_UP) {
|
||||
dy = -1;
|
||||
}
|
||||
|
||||
if (key == KeyEvent.VK_DOWN) {
|
||||
dy = 1;
|
||||
}
|
||||
}
|
||||
|
||||
public void fire() {
|
||||
missiles.add(new Missile(x + width, y + height / 2));
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent e) {
|
||||
|
||||
int key = e.getKeyCode();
|
||||
|
||||
if (key == KeyEvent.VK_LEFT) {
|
||||
dx = 0;
|
||||
}
|
||||
|
||||
if (key == KeyEvent.VK_RIGHT) {
|
||||
dx = 0;
|
||||
}
|
||||
|
||||
if (key == KeyEvent.VK_UP) {
|
||||
dy = 0;
|
||||
}
|
||||
|
||||
if (key == KeyEvent.VK_DOWN) {
|
||||
dy = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
52
DemoProjects/src/sprites/shootingMissles/Sprite.java
Normal file
52
DemoProjects/src/sprites/shootingMissles/Sprite.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package sprites.shootingMissles;
|
||||
|
||||
import java.awt.*;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class Sprite {
|
||||
|
||||
protected int x;
|
||||
protected int y;
|
||||
protected int width;
|
||||
protected int height;
|
||||
protected boolean visible;
|
||||
protected Image image;
|
||||
|
||||
public Sprite(int x, int y) {
|
||||
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
visible = true;
|
||||
}
|
||||
|
||||
protected void loadImage(String imageName, int width, int height) {
|
||||
|
||||
ImageIcon ii = new ImageIcon(imageName);
|
||||
image = ii.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT);
|
||||
}
|
||||
|
||||
protected void getImageDimensions() {
|
||||
width = image.getWidth(null);
|
||||
height = image.getHeight(null);
|
||||
}
|
||||
|
||||
public Image getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public boolean isVisible() {
|
||||
return visible;
|
||||
}
|
||||
|
||||
public void setVisible(Boolean visible) {
|
||||
this.visible = visible;
|
||||
}
|
||||
}
|
||||
79
DemoProjects/src/sprites/spaceship/Board.java
Normal file
79
DemoProjects/src/sprites/spaceship/Board.java
Normal file
@@ -0,0 +1,79 @@
|
||||
package sprites.spaceship;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.Timer;
|
||||
|
||||
public class Board extends JPanel implements ActionListener {
|
||||
|
||||
private Timer timer;
|
||||
private SpaceShip spaceShip;
|
||||
private final int DELAY = 10;
|
||||
|
||||
public Board() {
|
||||
|
||||
initBoard();
|
||||
}
|
||||
|
||||
private void initBoard() {
|
||||
|
||||
addKeyListener(new TAdapter());
|
||||
setBackground(Color.black);
|
||||
setFocusable(true);
|
||||
|
||||
spaceShip = new SpaceShip();
|
||||
|
||||
timer = new Timer(DELAY, this);
|
||||
timer.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
|
||||
doDrawing(g);
|
||||
|
||||
Toolkit.getDefaultToolkit().sync();
|
||||
}
|
||||
|
||||
private void doDrawing(Graphics g) {
|
||||
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
|
||||
g2d.drawImage(spaceShip.getImage(), spaceShip.getX(),
|
||||
spaceShip.getY(), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
step();
|
||||
}
|
||||
|
||||
private void step() {
|
||||
|
||||
spaceShip.move();
|
||||
|
||||
repaint();
|
||||
}
|
||||
|
||||
private class TAdapter extends KeyAdapter {
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
spaceShip.keyReleased(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
spaceShip.keyPressed(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
32
DemoProjects/src/sprites/spaceship/MovingSprite.java
Normal file
32
DemoProjects/src/sprites/spaceship/MovingSprite.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package sprites.spaceship;
|
||||
|
||||
import java.awt.EventQueue;
|
||||
import javax.swing.JFrame;
|
||||
|
||||
public class MovingSprite extends JFrame {
|
||||
|
||||
public MovingSprite() {
|
||||
|
||||
initUI();
|
||||
}
|
||||
|
||||
private void initUI() {
|
||||
|
||||
add(new Board());
|
||||
|
||||
setTitle("Moving sprite");
|
||||
setSize(400, 300);
|
||||
|
||||
setLocationRelativeTo(null);
|
||||
setResizable(false);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
EventQueue.invokeLater(() -> {
|
||||
MovingSprite ex = new MovingSprite();
|
||||
ex.setVisible(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
105
DemoProjects/src/sprites/spaceship/SpaceShip.java
Normal file
105
DemoProjects/src/sprites/spaceship/SpaceShip.java
Normal file
@@ -0,0 +1,105 @@
|
||||
package sprites.spaceship;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.awt.event.KeyEvent;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class SpaceShip {
|
||||
|
||||
private int dx;
|
||||
private int dy;
|
||||
private int x = 40;
|
||||
private int y = 60;
|
||||
private int w;
|
||||
private int h;
|
||||
private Image image;
|
||||
|
||||
public SpaceShip() {
|
||||
|
||||
loadImage();
|
||||
}
|
||||
|
||||
private void loadImage() {
|
||||
|
||||
ImageIcon ii = new ImageIcon("/home/bitecoding/Pictures/spaceship.png");
|
||||
image = ii.getImage().getScaledInstance(50,50,1);
|
||||
|
||||
w = image.getWidth(null);
|
||||
h = image.getHeight(null);
|
||||
}
|
||||
|
||||
public void move() {
|
||||
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
|
||||
return w;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
public Image getImage() {
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e) {
|
||||
|
||||
int key = e.getKeyCode();
|
||||
|
||||
switch(key){
|
||||
case KeyEvent.VK_A:
|
||||
dx = -2;
|
||||
break;
|
||||
case KeyEvent.VK_D:
|
||||
dx = 2;
|
||||
break;
|
||||
case KeyEvent.VK_W:
|
||||
dy = -2;
|
||||
break;
|
||||
case KeyEvent.VK_S:
|
||||
dy = 2;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent e) {
|
||||
|
||||
int key = e.getKeyCode();
|
||||
|
||||
switch(key){
|
||||
case KeyEvent.VK_A:
|
||||
dx = 0;
|
||||
break;
|
||||
case KeyEvent.VK_D:
|
||||
dx = 0;
|
||||
break;
|
||||
case KeyEvent.VK_W:
|
||||
dy = 0;
|
||||
break;
|
||||
case KeyEvent.VK_S:
|
||||
dy = 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user