📄 carsource.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>CarSource</TITLE>
</HEAD>
<BODY >
<P>import java.awt.*; </P>
<P>import java.applet.*;</P>
<P>import java.awt.image.*;</P>
<P>/**This Original Java Code is written by me, Using double buffering.
</P>
<P>*I haven't actually loaded any GIF images for the car, but I had </P>
<P>*drawn it by myself. It would be great looking if I had Included </P>
<P>* GIF's but Since it may even slow down the game I had not used </P>
<P>* them. I had used MultiThreading and I am running two threads. </P>
<P>* One for having control over the user CAR and the other for the racing
</P>
<P>* cars. This program has some bugs. I will remove in my later versions.
</P>
<P>* for best vision use applet width and height as</P>
<P>* <applet code=Car.class width=640 height=400> </applet></P>
<P>* You can modify this code and distribute among your friends </P>
<P>* Original Programmed by : A. LEVI.</P>
<P>* My e-mail address is levi57@hotmail.com * -still unemployed- */</P>
<P>public class Car extends Applet implements Runnable </P>
<P>{</P>
<UL>
<P>Image img,car1,cup; </P>
<P>// This Field is to Trace out the User's Car position </P>
<P>static int position=235; </P>
<P>static int points = 0; </P>
<P>// You can change this delay to any value which effects in the speed
of the game </P>
<P>static int delay = 50; </P>
<P>road rd; </P>
<P>Thread thr; </P>
<P>static int pts=50; </P>
<P>boolean msg=true; </P>
<P>// If this field is true then the 'road' thread will be stopped </P>
<P>static boolean kill=false; </P>
<P>public void init() </P>
<P>{ </P>
<UL>
<P>int x[] = { 15, 15, 0, 60, 45, 45 }; </P>
<P>int y[] = { 45, 50, 58, 58, 50, 45 }; </P>
<P>setBackground(Color.black);</P>
<P>// Drawing the Car Image</P>
<P>img = createImage(60,60); </P>
<P>Graphics g = img.getGraphics(); </P>
<P>g.setColor(Color.black); </P>
<P>g.fillRect(0,0,60,60); </P>
<P>g.setColor(Color.green); </P>
<P>g.fillRect(12,20,36,7); </P>
<P>g.fillRect(8,15,4,17); </P>
<P>g.fillRect(48,15,4,17); </P>
<P>g.fillRect(5,40,50,7); </P>
<P>g.fillRect(0,35,5,17); </P>
<P>g.fillRect(55,35,5,17);</P>
<P>g.setColor(Color.red); </P>
<P>g.fillRect(20,0,20,15); </P>
<P>g.fillRect(15,15,30,40); </P>
<P>g.setColor(Color.blue); </P>
<P>g.fillRect(20,20,7,10); </P>
<P>g.fillRect(33,20,7,10); </P>
<P>g.setColor(Color.red); </P>
<P>g.fillRect(22,22,3,6); </P>
<P>g.fillRect(35,22,3,6); </P>
<P>g.setFont(new Font("TimesRoman",Font.PLAIN,7)); </P>
<P>g.setColor(Color.white); </P>
<P>g.fillPolygon(x,y,6); </P>
<P>g.setColor(Color.black); </P>
<P>g.drawString("YAMAHA",15,52);</P>
<P>// Drawing the Car Image</P>
<P>car1 = createImage(60,60); </P>
<P>Graphics g1 = car1.getGraphics(); </P>
<P>g1.setColor(Color.black); </P>
<P>g1.fillRect(0,0,60,60); </P>
<P>g1.setColor(Color.green); </P>
<P>g1.fillRect(12,20,36,7); </P>
<P>g1.fillRect(8,15,4,17); </P>
<P>g1.fillRect(48,15,4,17); </P>
<P>g1.fillRect(5,40,50,7); </P>
<P>g1.fillRect(0,35,5,17); </P>
<P>g1.fillRect(55,35,5,17);</P>
<P>g1.setColor(Color.blue); </P>
<P>g1.fillRect(20,0,20,15); </P>
<P>g1.fillRect(15,15,30,40); </P>
<P>g1.setColor(Color.red); </P>
<P>g1.fillRect(20,20,7,10); </P>
<P>g1.fillRect(33,20,7,10); </P>
<P>g1.setColor(Color.blue); </P>
<P>g1.fillRect(22,22,3,6); </P>
<P>g1.fillRect(35,22,3,6); </P>
<P>g1.setFont(new Font("TimesRoman",Font.PLAIN,7)); </P>
<P>g1.setColor(Color.white); </P>
<P>g1.fillPolygon(x,y,6); </P>
<P>g1.setColor(Color.black); </P>
<P>g1.drawString(" B.M.W ",15,52);</P>
<P>thr = new Thread(this); thr.start(); rd = new road(getGraphics(),car1,this);
rd.start();</P>
<P>// Cup Image </P>
<P>int a[] = {20,5,35}; </P>
<P>int b[] = {150,160,160}; </P>
<P>cup = createImage(50,165); </P>
<P>Graphics handle = cup.getGraphics(); </P>
<P>handle.setColor(Color.black); </P>
<P>handle.fillRect(0,0,50,165); </P>
<P>handle.setColor(Color.red); </P>
<P>handle.fillArc(0,40,40,30,0,180); </P>
<P>handle.setColor(Color.yellow); </P>
<P>handle.fillArc(0,15,40,80,180,180); </P>
<P>handle.setColor(Color.red); </P>
<P>handle.drawLine(20,95,20,150); </P>
<P>handle.fillPolygon(a,b,3); </P>
</UL>
<P>} </P>
<P>public void run() </P>
<P>{</P>
<UL>
<P>// If you cross the 50 mark you will get a Cup of Java </P>
<P>while(points <= 52) </P>
<P>{ </P>
<UL>
<P>if(points == 52 || kill == true) </P>
<P>{ </P>
<UL>
<P>rd.stop(); </P>
<P>repaint(); </P>
<P>thr.stop(); </P>
</UL>
<P>} </P>
<P>if((points%4)==0) </P>
<P>{ </P>
<UL>
<P>rd.j = 0; </P>
<P>pts = points; </P>
<P>points++; </P>
<P>delay--; </P>
<P>if(delay <= 0) </P>
<UL>
<P>delay = 0; </P>
</UL>
<P>rd.flag=1; </P>
<P>repaint(); </P>
</UL>
<P>}</P>
<P>try </P>
<P>{ </P>
<UL>
<P>Thread.sleep(delay); </P>
</UL>
<P>}catch(InterruptedException exp){}</P>
</UL>
<P>} </P>
</UL>
<P>} </P>
<P>public void destroy() </P>
<P>{ </P>
<UL>
<P>thr.stop(); </P>
<P>rd.stop(); </P>
</UL>
<P>}</P>
<P>//If User presses mouse the the Car is shifted to opposite side of the
road</P>
<P>public boolean mouseDown(Event e , int x, int y) </P>
<P>{ </P>
<UL>
<P>if(e.id == Event.MOUSE_DOWN) </P>
<P>{ </P>
<UL>
<P>if(position == 235) </P>
<P>position = 355; </P>
<P>else </P>
<P>position = 235; </P>
<P>repaint(); </P>
</UL>
<P>}</P>
<P>return true; </P>
</UL>
<P>} </P>
<P>public void paint(Graphics gr) </P>
<P>{ </P>
<UL>
<P>if(!kill) </P>
<P>{ </P>
<UL>
<P>if(msg) </P>
<P>{ </P>
<UL>
<P>// This is the opening message</P>
<P>gr.setColor(Color.black); </P>
<P>gr.fillRect(0,0,640,400); </P>
<P>gr.setColor(Color.yellow); </P>
<P>gr.setFont(new Font("TimesRoman",Font.BOLD,16)); </P>
<P>gr.drawString("TO PLAY THE GAME CLICK ANY MOUSE BUTTON",140,100);
</P>
<P>gr.drawString("THE CAR WILL MOVE IN OPPOSITE DIRECTION",140,150);
</P>
<P>gr.drawString("WAIT A MINUTE......",230,200); </P>
<P>msg = false; </P>
<P>try{ </P>
<UL>
<P>Thread.sleep(3000); </P>
</UL>
<P>}catch(Exception exp){} </P>
<P>gr.setColor(Color.black); </P>
<P>gr.fillRect(0,0,640,400); </P>
</UL>
<P>} </P>
<P>gr.setColor(Color.white); </P>
<P>gr.fillRect(200,0,10,400); </P>
<P>gr.fillRect(440,0,10,400); </P>
<P>gr.drawImage(img,position,300,this); </P>
<P>gr.setColor(Color.yellow); </P>
<P>gr.fillRect(550,5,637,25); </P>
<P>gr.setColor(Color.blue); </P>
<P>gr.setFont(new Font("TimesRoman",Font.BOLD,20)); </P>
<P>gr.drawString("Score :"+pts,557,22); </P>
<P>if(points >= 52) </P>
<P>{</P>
<UL>
<P>for(int xyz=0;xyz<3;xyz++) </P>
<P>{ </P>
<UL>
<P>gr.setColor(Color.yellow); </P>
<P>gr.drawString("Have a Cuppa Java",240,100); </P>
<P>gr.drawImage(cup,300,100,this); </P>
<P>gr.setColor(Color.yellow); </P>
<P>gr.fillRect(550,5,637,25); </P>
<P>gr.setColor(Color.blue); </P>
<P>gr.setFont(new Font("TimesRoman",Font.BOLD,20)); </P>
<P>gr.drawString("Score :50",557,22); </P>
<P>try </P>
<P>{ </P>
<UL>
<P>Thread.sleep(500); </P>
</UL>
<P>}catch(InterruptedException exp){} </P>
</UL>
<P>} </P>
</UL>
<P>} </P>
</UL>
<P>} </P>
<P>else </P>
<P>{ </P>
<UL>
<P>gr.setColor(Color.yellow); </P>
<P>gr.drawString("YOU HAVE LOST THE GAME",250,200); </P>
</UL>
<P>} </P>
</UL>
<P>} </P>
</UL>
<P>}</P>
<P>class road extends Thread </P>
<P>{ </P>
<UL>
<P>int i; </P>
<P>public static int j = 0; </P>
<P>Graphics g; </P>
<P>Image car2; </P>
<P>ImageObserver io; </P>
<P>public static int flag = 0; </P>
<P>boolean msg=true; </P>
<P>road(Graphics g,Image car2,ImageObserver io) </P>
<P>{ </P>
<UL>
<P>this.g = g; </P>
<P>this.io = io; </P>
<P>this.car2 = car2; </P>
</UL>
<P>} </P>
<P>public void run() </P>
<P>{ </P>
<UL>
<P>drawRoad(g); </P>
</UL>
<P>}</P>
<P>// The actual logic i.e Moving of cars is here</P>
<P>public void drawRoad(Graphics gr) </P>
<P>{ </P>
<UL>
<P>if(msg) </P>
<P>{ </P>
<UL>
<P>gr.setColor(Color.black); </P>
<P>gr.fillRect(0,0,640,400); </P>
<P>gr.setColor(Color.yellow); </P>
<P>gr.setFont(new Font("TimesRoman",Font.BOLD,16)); </P>
<P>gr.drawString("TO PLAY THE GAME CLICK ANY MOUSE BUTTON",140,100);
</P>
<P>gr.drawString("THE CAR WILL MOVE IN OPPOSITE DIRECTION",140,150);
</P>
<P>gr.drawString("WAIT A MINUTE......",230,200); </P>
<P>msg = false; </P>
<P>try</P>
<P>{</P>
<UL>
<P>Thread.sleep(3000); </P>
</UL>
<P>}catch(Exception exp){} </P>
<P>gr.setColor(Color.black); </P>
<P>gr.fillRect(0,0,640,400); </P>
</UL>
<P>} </P>
<P>for(;j<=1000;j+=10) </P>
<P>{ </P>
<UL>
<P>for(i=-1000;i<=479;i+=60) </P>
<P>{ </P>
<UL>
<P>gr.setColor(Color.black); </P>
<P>gr.fillRect(320,i+j,10,i+j+50); </P>
<P>gr.setColor(Color.white); </P>
<P>gr.fillRect(320,i+j+10,10,i+j+60); </P>
</UL>
<P>} </P>
<P>gr.clearRect(235,j-10,60,60); </P>
<P>gr.drawImage(car2,235,0+j,io); </P>
<P>gr.clearRect(355,-150+(j-10),60,60); </P>
<P>gr.drawImage(car2,355,-150+j,io); </P>
<P>gr.clearRect(235,-300+(j-10),60,60); </P>
<P>gr.drawImage(car2,235,-300+j,io); </P>
<P>gr.clearRect(355,-450+(j-10),60,60); </P>
<P>gr.drawImage(car2,355,-450+j,io); </P>
<P>if( (Car.position == 235 && (j >= 250 && j <=
360)) || (Car.position == 355 && (j >= 400 && j <=
510)) || (Car.position == 235 && (j >= 550 && j <=
660)) || (Car.position == 355 && (j >= 700 && j <=
810)) ) </P>
<P>{ </P>
<UL>
<P>try </P>
<P>{ </P>
<UL>
<P>Thread.sleep(2000); </P>
<P>Car.kill = true; </P>
</UL>
<P>}catch(InterruptedException exp){} </P>
</UL>
<P>} </P>
<P>if (j >= 360 ) { if( (( j - 360 ) % 150 ) == 0 ) </P>
<P>{ </P>
<UL>
<P>if(flag == 1) </P>
<P>{ </P>
<UL>
<P>Car.points--; </P>
<P>flag = 0; </P>
</UL>
<P>} </P>
<P>Car.points++; </P>
<P>gr.setColor(Color.yellow); </P>
<P>gr.fillRect(550,5,637,25); </P>
<P>gr.setColor(Color.blue); </P>
<P>gr.setFont(new Font("TimesRoman",Font.BOLD,20)); </P>
<P>gr.drawString("Score :"+Car.points,557,22); </P>
</UL>
<P>} </P>
</UL>
<P>} </P>
<P>try </P>
<P>{ </P>
<UL>
<P>Thread.sleep(Car.delay); </P>
</UL>
<P>}catch(InterruptedException exp){} </P>
<P>} </P>
</UL>
<P>} </P>
</UL>
<P>} </P>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -