/**这是用java awt写的一个类似于java像素游戏的一个地图,只写了一个赛道地图(copy的)怕是遗忘,所以就将内容粘贴到这个地方了*有兴趣的可以自己试着写一写,将不足之处再修改分享给大家。*/import javax.swing.*;import javax.swing.event.*;import java.awt.*;import java.awt.event.*;public class G1P1 extends JFrame {final int WIDTH=900,HEIGHT=650;//下面是待绘制的矩形Rectangle left=new Rectangle(0,0,WIDTH/9,HEIGHT);Rectangle right=new Rectangle((WIDTH/9)*9,0,WIDTH/9,HEIGHT);Rectangle top=new Rectangle(0,0,WIDTH,HEIGHT/9);Rectangle bottom=new Rectangle(0,(HEIGHT/9)*9,(WIDTH/9)*10,HEIGHT/9);Rectangle center=new Rectangle((int)((WIDTH/9)*2.5),(int)((HEIGHT/9)*2.5),(int)((WIDTH/9)*5),(int)((HEIGHT/9)*4));//下面的矩形用于绘制跑道上的障碍物,增加赛车的难度Rectangle obstacle1=new Rectangle(WIDTH/2,(int)(HEIGHT/9)*7,WIDTH/10,HEIGHT/9);Rectangle obstacle2=new Rectangle(WIDTH/3,(int)(HEIGHT/9)*5,WIDTH/10,HEIGHT/4);Rectangle obstacle3=new Rectangle(2*(WIDTH/3),(int)((HEIGHT/9)*5),WIDTH/10,HEIGHT/4);Rectangle obstacle4=new Rectangle(WIDTH/3,HEIGHT/9,WIDTH/30,HEIGHT/9);Rectangle obstacle5=new Rectangle(WIDTH/2,(int)((HEIGHT/9)*1.5),WIDTH/30,HEIGHT/4);//下面的矩形用于绘制内外跑道的终点线Rectangle finish=new Rectangle(WIDTH/9,HEIGHT/2-HEIGHT/9,(int)((WIDTH/9)*1.5),HEIGHT/70);Rectangle lineO=new Rectangle(WIDTH/9,HEIGHT/2,(int)((WIDTH/9)*1.5),HEIGHT/140);Rectangle lineI=new Rectangle((WIDTH/9)+((int)((WIDTH/9)*1.5)/2),(HEIGHT/2+HEIGHT/10),(int)((WIDTH/9)*1.5)/2,HEIGHT/140);//构造方法public G1P1(){ super("Racing"); setSize(WIDTH/9*10,HEIGHT/9*10); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true);}//该方法用于绘制赛车和赛场public void paint(Graphics g){ super.paint(g); g.setColor(Color.DARK_GRAY ); g.fillRect(0, 0, WIDTH, HEIGHT); //绘制时将场地设置为绿色 g.setColor(Color.green); //现在绘制各个矩形 g.fillRect(left.x,left.y,left.width,left.height); g.fillRect(right.x, right.y, right.width, right.height); g.fillRect(top.x,top.y,top.width,top.height); g.fillRect(bottom.x, bottom.y, bottom.width, bottom.height); g.fillRect(center.x,center. y, center.width,center. height); g.fillRect(obstacle1.x,obstacle1.y,obstacle1.width,obstacle1.height); g.fillRect(obstacle2.x,obstacle2.y,obstacle2.width,obstacle2.height); g.fillRect(obstacle3.x,obstacle3.y,obstacle3.width,obstacle3.height); g.fillRect(obstacle4.x,obstacle4.y,obstacle4.width,obstacle4.height); g.fillRect(obstacle5.x,obstacle5.y,obstacle5.width,obstacle5.height); //设置起跑线为白色 g.setColor(Color.white); //绘制起跑线 g.fillRect(lineO.x,lineO.y,lineO.width,lineO.height); g.fillRect(lineI.x, lineI.y, lineI.width, lineI.height); //设置终点线为黄色 g.setColor(Color.yellow); g.fillRect(finish.x, finish.y,finish. width, finish.height);}//主方法public static void main(String[] args){ new G1P1();}}