?? carprotected.java
字號:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Menu;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
public class Carprotected extends JWindow{
public static void main(String[] args) {
new Loadframe();
}
}
//加載頁面
class Loadframe extends JWindow implements Runnable
{
JProgressBar jprogress=null;
Image flashimg=null;
int i=0;
public Loadframe()
{
jprogress=new JProgressBar(JProgressBar.HORIZONTAL,1,300);//初始化進度條
Toolkit tk=Toolkit.getDefaultToolkit();
flashimg=tk.createImage("Cg0430.jpg");
this.add(new JLabel(new ImageIcon(flashimg)),BorderLayout.CENTER);
this.setSize(400,300);
//this.pack();
jprogress.setString("now loading....");
jprogress.setStringPainted(true);
jprogress.setForeground(Color.gray);
jprogress.setBackground(Color.GREEN);
this.add(jprogress,BorderLayout.SOUTH);
this.setLocation((1024-400)/2,(768-300)/2);
this.setVisible(true);
new Thread(this).start();
}
public void run() {
while(i<=300)
{
try {
Thread.sleep(8);
} catch (InterruptedException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
jprogress.setValue(i);
i++;
}
this.dispose();
new Menudemo();
}
}
//菜單頁面
class Menudemo extends JFrame implements KeyListener,Runnable
{
int gamestate=0;//0:菜單;1:主場景;2:關于游戲
String s1[]={"開始游戲","關于游戲","游戲幫助","退出游戲"};
int y=80;
int y1=70;
int num = 200;
BufferedImage bg=null;
Graphics g1=null;
Image imgred=null;
Image imgblue=null;
Image imggreen=null;
Image imgS = null;
Image eximg[] = new Image[8];
Boom ex = new Boom(eximg,this);
Car redcar=null;
Car enemy[];
boolean isE;
boolean isR = true;
int index=0;
int road=-100;
int ly[]={-500,-200,-600,-100,-550,-650,-700,-200,-500};
public Menudemo()
{
MediaTracker mt= new MediaTracker(this);
bg=new BufferedImage(250,300,BufferedImage.TYPE_INT_RGB);
g1=bg.getGraphics();
Toolkit t1=Toolkit.getDefaultToolkit();
imgred=t1.createImage("Red_Car.gif");
imgblue=t1.createImage("Blue_Car.gif");
imggreen=t1.createImage("Green_Car.gif");
imgS = t1.createImage("stop.gif");
mt.addImage(imgS, 0);
mt.addImage(imgred, 0);
mt.addImage(imggreen, 0);
mt.addImage(imgblue, 0);
for(int i = 0; i < eximg.length; i++)
{
eximg[i] = t1.createImage("z"+(i+1)+".png");
mt.addImage(eximg[i],0);
}
try {
mt.waitForAll();
} catch (InterruptedException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
redcar=new Car(100,250,imgred,this);
enemy=new Car[6];
for(int i=0;i<3;i++)
{
enemy[i]=new Car(i*60,ly[i],imgblue);
enemy[i+3]=new Car(i*40,ly[i+3],imggreen);
}
this.setSize(250,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation((1024-250)/2,(768-300)/2);
new Thread(this).start();
this.addKeyListener(this);
this.setVisible(true);
}
public void paint(Graphics g)
{
g1.clearRect(0,0,250,300);
if(gamestate==0)//菜單
{
drawMenu(g1);
}
else if(gamestate==1)
{
drawMain(g1);
}
else if(gamestate==2)
{
drawAbout(g1);
}
else if(gamestate==3)
{
drawHelp(g1);
}
g.drawImage(bg,0,0,this);
}
//繪制菜單
public void drawMenu(Graphics g)
{
g.setColor(Color.RED);
g.setFont(new Font("幼圓",Font.BOLD,20));
y=80;
for(int i=0;i<s1.length;i++)
{
g.drawString(s1[i],80,y);
y+=50;
}
g.setColor(Color.orange);
g.fillOval(55,y1,15,15);
}
//繪主場景
public void drawMain(Graphics g)
{
for(int i=0;i<enemy.length;i++)
{
enemy[i].setDir(2);
enemy[i].drawCar(g);
enemy[i].move();
if(enemy[i].y>300)
this.setEnemy(i);
if(enemy[i].isP(redcar))
{
int tmp1 = enemy[i].x;
int tmp2 = enemy[i].y;
ex.setLoc(tmp1, tmp2);
ex.setE(true);
ex.start1();
enemy[i].setLocation(-500,-100);
num-=20;
if(num <= 0)
{
num = 0;
isE = false;
isR = false;
}
}
}
if(ex.getE())
ex.drawE(g1);
redcar.drawCar(g);
this.drawRoad(g);
if(isR==false)
{
g.drawImage(imgS, (250-imgS.getWidth(null))/2,(300-imgS.getHeight(null))/2,this);
}
}
public void setEnemy(int en)
{
int x=(int)(Math.random()*212);
int y=0;
if(en>0)
y=(int)(Math.random()*(-500)+enemy[en-1].y+(-200));
else if(en==0)
y=(int)(Math.random()*(-500)+enemy[5].y+(-200));
enemy[en].setLocation(x,y);
}
//繪制關于界面
public void drawAbout(Graphics g)
{
g.setColor(Color.WHITE);
g.drawString("關于游戲",30,50);
}
//繪制幫助界面
public void drawHelp(Graphics g)
{
g.setFont(new Font("宋體",Font.BOLD,10));
g.setColor(Color.YELLOW);
g.drawString("游戲幫助",10,50);
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
int key=e.getKeyCode();
//主菜單
if(gamestate==0)
{
switch(key)
{
case KeyEvent.VK_UP:
y1-=50;
if(y1<70)
y1=220;
break;
case KeyEvent.VK_DOWN:
y1+=50;
if(y1>=270)
y1=70;
break;
case KeyEvent.VK_ENTER:
if(y1==70)
gamestate=1;
if(y1==120)
gamestate=2;
if(y1==170)
gamestate=3;
if(y1==220)
System.exit(0);
break;
}
repaint();
}//游戲界面
else if(gamestate==1)
{
if(key==KeyEvent.VK_ENTER)
gamestate=0;
if(key==KeyEvent.VK_LEFT)
redcar.setDir(0);
if(key==KeyEvent.VK_RIGHT)
redcar.setDir(1);
redcar.move();
}//關于界面
else if(gamestate==2)
{
if(key==KeyEvent.VK_ENTER)
gamestate=0;
repaint();
}
//幫助界面
else if(gamestate==3)
{
if(key==KeyEvent.VK_ENTER)
gamestate=0;
repaint();
}
}
public void keyReleased(KeyEvent e)
{
}
public void drawRoad(Graphics g)
{
g.setColor(Color.WHITE);
g.fillRect(120,road,10,60);
g.setColor(Color.blue);
g.drawRect(30,40,201,10);
g.setColor(Color.red);
g.fillRect(31,41,num,9);
}
public void run()
{
while(isR)
{
road+=60;
if(gamestate==1)
{
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
repaint();
if(road>=350)
road=-100;
}
}
//汽車類
class Car
{
int x;
int y;
Menudemo game;
Image img;
int dir=0;
public Car(int x,int y,Image img,Menudemo game)
{
this.x=x;
this.y=y;
this.img=img;
this.game=game;
}
public Car(int x,int y,Image img)
{
this.x=x;
this.y=y;
this.img=img;
}
public void setLocation(int x,int y)
{
this.x=x;
this.y=y;
}
public void drawCar(Graphics g)
{
g.drawImage(img,x,y,game);
}
public void setDir(int dir)
{
this.dir=dir;
}
public void move()
{
switch(dir)
{
case 0:
x-=8;
if(x<=0)
x=0;
break;
case 1:
x+=8;
if(x>=210)
x=210;
break;
case 2:
y+=12;
break;
}
}
public Rectangle2D getR()
{
return new Rectangle2D.Float(x,y,img.getWidth(game),img.getHeight(game));
}
public boolean isP(Car c)
{
return getR().intersects(c.getR());
}
}
class Boom implements Runnable
{
Menudemo game;
Image img[];
int x;
int y;
boolean isE;
public Boom(Image img[], Menudemo game)
{
this.img = img;
this.game = game;
}
public void setLoc(int x, int y)
{
this.x = x;
this.y = y;
}
public void setE(boolean isE)
{
this.isE = isE;
}
public boolean getE()
{
return isE;
}
public void start1()
{
new Thread(this).start();
}
public void run()
{
while(isE)
{
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
index++;
if(index > 6)
{
index = 0;
isE = false;
}
}
}
public void drawE(Graphics g)
{
g.drawImage(img[index],x,y,game);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -