2012-12-09 08:36:34Morris
[JAVA][碰撞] 失敗作擴充失敗作
demo.jpg
Demo影片 : http://www.youtube.com/watch?v=tCgr-17XL4Q
實際上你可以到這裡玩玩看 http://140.115.216.220/test/GameJava/
如果我電腦關了,你當然玩不到囉。
一般 Java 版本。
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.File;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class GameFrame extends JFrame {
boolean init = true;
int cx, cy, cr;
int nex, ney;
double dx, dy, dr, dirx, diry;
int[][] board = new int[700/6][700/6];
public GameFrame() {
this.setSize(700, 700);
this.setResizable(false);
this.setTitle("INORDER TO ORDER");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseMotionListener(new MouseMotion());
cx = 700/6/2;
cy = 700/6/2;
cr = 3;
nex = 700/2;
ney = 700/2;
File f = new File("hide.bmp");
if(f.canRead()) {
System.out.println("Read");
try {
BufferedImage buf = ImageIO.read(f);
System.out.println(buf.getTileWidth() + " " + buf.getTileHeight());
for(int i = 0; i < buf.getTileWidth(); i++)
for(int j = 0; j < buf.getTileHeight(); j++) {
int k = buf.getRGB(i, j);
if(k < -100) {
board[i][j] = 1;
}
}
} catch(Exception e) {}
}
Timer timer = new Timer(30, new TimerListener());
timer.start();
}
public void paint(Graphics g) {
if(init) {
g.setColor(Color.gray);
for(int i = 0; i < 700; i++)
g.drawLine(0, i, 700, i);
g.setColor(Color.black);
for(int i = 0; i < 700; i += 6) {
for(int j = 0; j < 700; j += 6) {
g.fillRect(i, j, 5, 5);
}
}
init = false;
g.setColor(Color.green);
for(int i = cx-cr; i <= cx+cr; i++) {
for(int j = cy-cr; j <= cy+cr; j++) {
if((i-cx)*(i-cx) + (j-cy)*(j-cy) <= cr*cr) {
g.fillRect(i*6, j*6, 5, 5);
}
}
}
} else {
if(dragFlag == false && moveFlag == false)
return;
g.setColor(Color.black);
for(int i = cx-cr; i <= cx+cr; i++) {
for(int j = cy-cr; j <= cy+cr; j++) {
if((i-cx)*(i-cx) + (j-cy)*(j-cy) <= cr*cr) {
if(i >= 0 && j >= 0 && i < 700/6 && j < 700/6 && board[i][j] != 1)
g.fillRect(i*6, j*6, 5, 5);
}
}
}
if(dragFlag == true && moveFlag == false) {
cx = nex/6;
cy = ney/6;
dx = nex;
dy = ney;
}
if(moveFlag == true) {
dx += dirx;
dy += diry;
cx = (int)((dx)/6);
cy = (int)((dy)/6);
if(dx-cr*6 < 10 || dx+cr*6 >= 680) {
dirx = -dirx;
if(dirx < 0) dirx = -Math.abs(dirx)*0.91;
else dirx = Math.abs(dirx)*0.91;
}
if(dy-cr*6 < 45 || dy+cr*6 >= 680) {
diry = -diry;
if(diry < 0) diry = -Math.abs(diry)*0.91;
else diry = Math.abs(diry)*0.91;
}
if(dx-cr < 0) dx = 10+cr;
if(dx+cr > 680) dx = 680-cr;
if(dy-cr < 0) dy = 45+cr;
if(dy+cr > 680) dy = 680-cr;
dx += dirx;
dy += diry;
cx = (int)((dx)/6);
cy = (int)((dy)/6);
if(Math.abs(dirx) < 1 || Math.abs(diry) < 1)
moveFlag = false;
}
if(cx-cr < 0) cx = cr;
if(cx+cr > 680/6)cx = 680/6-cr;
if(cy < 0) cy = 0;
if(cy+cr > 680/6)cy = 680/6-cr;
g.setColor(Color.green);
for(int i = cx-cr; i <= cx+cr; i++) {
for(int j = cy-cr; j <= cy+cr; j++) {
if((i-cx)*(i-cx) + (j-cy)*(j-cy) <= cr*cr) {
g.fillRect(i*6, j*6, 5, 5);
}
}
}
}
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
repaint();
}
}
boolean dragFlag = false, moveFlag = false;
int[] qX = new int[10], qY = new int[10];
int qIdx = 0;
class MouseMotion extends MouseMotionAdapter {
public void mouseDragged(MouseEvent e) {
nex = e.getX();
ney = e.getY();
if((nex-cx*6)*(nex-cx*6)+(ney-cy*6)*(ney-cy*6) <= 36*cr*cr) {
dragFlag = true;
moveFlag = false;
qX[qIdx] = nex;
qY[qIdx] = ney;
qIdx = (qIdx+1)%5;
repaint();
}
/*System.out.printf("dragged %d %d\n", nex, ney);*/
}
public void mouseMoved(MouseEvent e) {
nex = e.getX();
ney = e.getY();
if(dragFlag == true) {
dragFlag = false;
moveFlag = true;
dirx = nex - qX[(qIdx+1)%5];
diry = ney - qY[(qIdx+1)%5];
dx = nex;
dy = ney;
dr = Math.sqrt(dirx*dirx + diry*diry);
dirx /= 10;
diry /= 10;
if(dirx > 100) dirx /= 100;
if(diry > 100) diry /= 100;
System.out.println("move "+dirx+" "+diry);
repaint();
}
/*System.out.printf("moved %d %d\n", nex, ney);*/
}
}
public static void main(String[] args) {
GameFrame gui = new GameFrame();
gui.setVisible(true);
}
}
網頁 JApplet 版本,也就是 jar 版本
讀圖片必須比較特殊一點,否則會看不到
URL url = getClass().getClassLoader().getResource("hide.bmp");
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.File;
import javax.imageio.ImageIO;
import java.net.URL;
import java.awt.image.BufferedImage;
public class GameFrameNet extends JApplet {
public void init() {
GameFrame p = new GameFrame();
this.getContentPane().add(p);
this.setSize(700, 700);
repaint();
}
boolean init = true;
int cx, cy, cr;
int nex, ney;
double dx, dy, dr, dirx, diry;
int[][] board = new int[700/6][700/6];
boolean dragFlag = false, moveFlag = false;
int[] qX = new int[10], qY = new int[10];
int qIdx = 0;
public void paint(Graphics g) {
if(init) {
g.setColor(Color.gray);
for(int i = 0; i < 700; i++)
g.drawLine(0, i, 700, i);
g.setColor(Color.black);
for(int i = 0; i < 700; i += 6) {
for(int j = 0; j < 700; j += 6) {
g.fillRect(i, j, 5, 5);
}
}
init = false;
g.setColor(Color.green);
for(int i = cx-cr; i <= cx+cr; i++) {
for(int j = cy-cr; j <= cy+cr; j++) {
if((i-cx)*(i-cx) + (j-cy)*(j-cy) <= cr*cr) {
g.fillRect(i*6, j*6, 5, 5);
}
}
}
} else {
if(dragFlag == false && moveFlag == false)
return;
g.setColor(Color.black);
for(int i = cx-cr; i <= cx+cr; i++) {
for(int j = cy-cr; j <= cy+cr; j++) {
if((i-cx)*(i-cx) + (j-cy)*(j-cy) <= cr*cr) {
if(i >= 0 && j >= 0 && i < 700/6 && j < 700/6 && board[i][j] != 1)
g.fillRect(i*6, j*6, 5, 5);
}
}
}
if(dragFlag == true && moveFlag == false) {
cx = nex/6;
cy = ney/6;
dx = nex;
dy = ney;
}
if(moveFlag == true) {
dx += dirx;
dy += diry;
cx = (int)((dx)/6);
cy = (int)((dy)/6);
if(dx-cr*6 < 10 || dx+cr*6 >= 680) {
dirx = -dirx;
if(dirx < 0) dirx = -Math.abs(dirx)*0.91;
else dirx = Math.abs(dirx)*0.91;
}
if(dy-cr*6 < 10 || dy+cr*6 >= 680) {
diry = -diry;
if(diry < 0) diry = -Math.abs(diry)*0.91;
else diry = Math.abs(diry)*0.91;
}
if(dx-cr < 0) dx = 10+cr;
if(dx+cr > 680) dx = 680-cr;
if(dy-cr < 0) dy = 45+cr;
if(dy+cr > 680) dy = 680-cr;
dx += dirx;
dy += diry;
cx = (int)((dx)/6);
cy = (int)((dy)/6);
if(Math.abs(dirx) < 1 || Math.abs(diry) < 1)
moveFlag = false;
}
if(cx-cr < 0) cx = cr;
if(cx+cr > 680/6)cx = 680/6-cr;
if(cy < 0) cy = 0;
if(cy+cr > 680/6)cy = 680/6-cr;
g.setColor(Color.green);
for(int i = cx-cr; i <= cx+cr; i++) {
for(int j = cy-cr; j <= cy+cr; j++) {
if((i-cx)*(i-cx) + (j-cy)*(j-cy) <= cr*cr) {
g.fillRect(i*6, j*6, 5, 5);
}
}
}
}
}
class GameFrame extends JPanel {
public GameFrame() {
this.setSize(700, 700);
this.setBounds(0, 0, 700, 700);
this.addMouseMotionListener(new MouseMotion());
cx = 700/6/2;
cy = 700/6/2;
cr = 3;
nex = 700/2;
ney = 700/2;
URL url = getClass().getClassLoader().getResource("hide.bmp");
try {
BufferedImage buf = ImageIO.read(url);
for(int i = 0; i < buf.getTileWidth(); i++)
for(int j = 0; j < buf.getTileHeight(); j++) {
int k = buf.getRGB(i, j);
if(k < -100) {
board[i][j] = 1;
}
}
} catch(Exception e) {}
Timer timer = new Timer(30, new TimerListener());
timer.start();
}
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
repaint();
}
}
class MouseMotion extends MouseMotionAdapter {
public void mouseDragged(MouseEvent e) {
nex = e.getX();
ney = e.getY();
if((nex-cx*6)*(nex-cx*6)+(ney-cy*6)*(ney-cy*6) <= 36*cr*cr) {
dragFlag = true;
moveFlag = false;
qX[qIdx] = nex;
qY[qIdx] = ney;
qIdx = (qIdx+1)%5;
repaint();
}
/*System.out.printf("dragged %d %d\n", nex, ney);*/
}
public void mouseMoved(MouseEvent e) {
nex = e.getX();
ney = e.getY();
if(dragFlag == true) {
dragFlag = false;
moveFlag = true;
dirx = nex - qX[(qIdx+1)%5];
diry = ney - qY[(qIdx+1)%5];
dx = nex;
dy = ney;
dr = Math.sqrt(dirx*dirx + diry*diry);
dirx /= 10;
diry /= 10;
if(dirx > 100) dirx /= 100;
if(diry > 100) diry /= 100;
repaint();
}
/*System.out.printf("moved %d %d\n", nex, ney);*/
}
}
}
上一篇:[JAVA][模仿失敗作] 隨機