import java.awt.*;
import java.awt.event.*;
class game0912 extends Frame implements Runnable{
int map[][]=new int[501][501];
int carx,cary;
int dx,dy;
int m,n;
int mode;
Image off;
Graphics offsc;
public static void main(String [] args) {
game0912 f=new game0912();
f.setTitle("game0912");
f.setSize(700,700);
f.setBackground(Color.white);
f.setVisible(true);
}
game0912(){
makemap();
Thread th=new Thread(this);
th.start();
addWindowListener(new stopwin());
}
class stopwin extends WindowAdapter{
public void windowClosing(WindowEvent we){System.exit(0);}
}
public void run(){
int t;
t=0;
mode=1;
carx=440;
cary=100;
while(t<500){
drive();
carx=carx+dx;
cary=cary+dy;
repaint();
try{
Thread.sleep(300);
}catch(InterruptedException e){}
t=t+1;
}
repaint();
}
public void paint(Graphics g){
int mx,nx;
off = createImage(500,500);
offsc = off.getGraphics();
for (m=-50;m<50;m++){
for (n=-50;n<50;n++){
mx=cary+m;
nx=carx+n;
if(mx>500)mx=500;
if(mx<0)mx=0;
if(nx>500)nx=500;
if(nx<0)nx=500;
offsc.setColor(Color.red);
if(map[mx][nx]==1)offsc.fillRect(5*m+250,5*n+250,5,5);
offsc.setColor(Color.blue);
if(map[mx][nx]==2)offsc.fillRect(5*m+250,5*n+250,5,5);
offsc.setColor(Color.green);
if(map[mx][nx]==3)offsc.fillRect(5*m+250,5*n+250,5,5);
}
}
offsc.setColor(Color.yellow);
offsc.fillRect(250,250,20,20);
g.drawImage(off,0,0,this);
}
void makemap(){
for(m=0;m<501;m++){
for(n=0;n<501;n++){
map[m][n]=1;
}
}
for(m=50;m<450;m++){
for(n=50;n<450;n++){
map[m][n]=2;
}
}
for(m=100;m<400;m++){
for(n=100;n<400;n++){
map[m][n]=3;
}
}
}
void drive(){
int j;
j=0;
if(mode==1)j=j+1;
if(cary<60)j=j+1;
if(j==2)mode=2;
j=0;
if(mode==2)j=j+1;
if(carx<60)j=j+1;
if(j==2)mode=3;
j=0;
if(mode==3)j=j+1;
if(cary>430)j=j+1;
if(j==2)mode=4;
j=0;
if(mode==4)j=j+1;
if(carx>430)j=j+1;
if(j==2)mode=1;
if(mode==1)dx=0;
if(mode==1)dy=-5;
if(mode==2)dx=-5;
if(mode==2)dy=0;
if(mode==3)dx=0;
if(mode==3)dy=5;
if(mode==4)dx=5;
if(mode==4)dy=0;
}
}
最終更新:2011年03月03日 19:11