Skip to content

Instantly share code, notes, and snippets.

@nyuichi
Created April 4, 2011 15:23
Show Gist options
  • Save nyuichi/901817 to your computer and use it in GitHub Desktop.
Save nyuichi/901817 to your computer and use it in GitHub Desktop.
Spriteライブラリを自作してみた。
package sprite;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class Event {
private static Queue<AWTEvent> queue = new LinkedList<AWTEvent>();
public static AWTEvent peek() {
return queue.peek();
}
public static AWTEvent poll() {
return queue.poll();
}
public static AWTEvent[] get() {
AWTEvent[] e = (AWTEvent[])queue.toArray(new AWTEvent[queue.size()]);
queue.clear();
return e;
}
static void post(AWTEvent e) {
queue.offer(e);
}
}
package sprite;
import java.util.*;
import sprite.Sprite;
public class Group {
private final ArrayList<Sprite> sprites;
public Group(Sprite... sprites) {
this.sprites = new ArrayList<Sprite>(Arrays.asList(sprites));
}
public Sprite[] getSprites() {
return (Sprite[])sprites.toArray(new Sprite[sprites.size()]);
}
public void remove(Sprite... sprites) {
for (Sprite s : sprites)
this.sprites.remove(s);
}
void add(Sprite... sprites) {
this.sprites.addAll(Arrays.asList(sprites));
}
public void update() {
for (Sprite s : sprites)
s.update();
}
}
package sprite;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import static java.awt.image.BufferedImage.*;
import javax.swing.*;
import sprite.*;
public class Screen extends JPanel{
private int width;
private int height;
private JFrame frame;
private Image[] surf;
private int c;
public Screen(int width, int height) {
this.width = width;
this.height = height;
this.setFocusable(true);
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
Event.post(e);
}
});
this.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
Event.post(e);
}
});
surf = new Image[2];
surf[0] = new BufferedImage(width, height, TYPE_INT_ARGB);
surf[1] = new BufferedImage(width, height, TYPE_INT_ARGB);
c = 0;
frame = new JFrame();
frame.add(this);
frame.setSize(new Dimension(width, height));
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void clear() {
Graphics g = surf[c].getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,width,height);
}
public void draw(Sprite... sprites) {
for (Sprite s : sprites) {
Graphics g = surf[c].getGraphics();
g.drawImage(s.image,
s.p.x-s.size.width/2,
s.p.y-s.size.height/2, this);
}
}
public void draw(Group... groups) {
for (Group g : groups) {
draw(g.getSprites());
}
}
protected void paintComponent(Graphics g) {
g.drawImage(surf[c], 0, 0, this);
c = (c+1)&1;
}
}
package sprite;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import sprite.*;
abstract public class Sprite {
protected final ArrayList<Group> groups;
protected boolean alive;
public Image image;
public Dimension size;
public Point p;
public Point v;
public Point a;
protected Sprite(Group... groups) {
this.groups = new ArrayList<Group>(Arrays.asList(groups));
add(groups);
this.alive = true;
this.p = new Point();
this.v = new Point();
this.a = new Point();
}
public void add(Group... groups) {
this.groups.addAll(Arrays.asList(groups));
for (Group g : groups)
g.add(this);
}
public void remove(Group... groups) {
for (Group g : groups)
this.groups.remove(g);
}
public void kill() {
for (Group g : groups)
g.remove(this);
}
public boolean isAlive() {
return this.alive;
}
abstract public void update();
}
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import static java.awt.image.BufferedImage.*;
import java.util.*;
import javax.swing.*;
import sprite.Sprite;
import sprite.Group;
import sprite.Event;
import sprite.Screen;
class Ball extends Sprite {
public Ball(int x, int y, Group... groups) {
super(groups);
this.p = new Point(x,y);
this.size = new Dimension(10, 10);
this.image = new BufferedImage(size.width, size.height, TYPE_INT_ARGB);
this.v = new Point(0, -3);
// Init image;
Graphics g = this.image.getGraphics();
g.setColor(Color.RED);
g.fillOval(0, 0, size.width, size.height);
}
public void update() {
p.x += v.x;
p.y += v.y;
}
}
class Ship extends Sprite {
public Ship(int x, int y, Group... groups) {
super(groups);
this.p = new Point(x,y);
this.size = new Dimension(50, 50);
this.image = new BufferedImage(size.width, size.height, TYPE_INT_ARGB);
// Init image
Graphics g = this.image.getGraphics();
g.setColor(Color.BLUE);
g.fillOval(0, 0, size.width, size.height);
}
public void update() {
v.x += a.x;
v.y += a.y;
p.x += v.x;
p.y += v.y;
if (v.x >= 10) v.x = 10;
if (v.x <= -10) v.x = -10;
if (v.y >= 10) v.y = 10;
if (v.y <= -10) v.y = -10;
}
}
public class Test {
public static void main(String[] args) throws Exception {
int width = 500;
int height = 750;
Screen screen = new Screen(width, height);
Ship ship = new Ship(width/2, height/2);
Group balls = new Group();
while(true) {
Thread.sleep(1000/60);
// Input
for (AWTEvent e : Event.get()) {
if (e instanceof KeyEvent) {
int c = ((KeyEvent)e).getKeyCode();
if (c == KeyEvent.VK_UP)
ship.a.y = -1;
if (c == KeyEvent.VK_DOWN)
ship.a.y = 1;
if (c == KeyEvent.VK_RIGHT)
ship.a.x = 1;
if (c == KeyEvent.VK_LEFT)
ship.a.x = -1;
if (c == KeyEvent.VK_Z) {
Ball b = new Ball(ship.p.x, ship.p.y, balls);
System.out.println("Fire");
}
}
}
// Update
ship.update();
balls.update();
// Draw
screen.clear();
screen.draw(ship);
screen.draw(balls);
// Reflect
screen.repaint();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment