JAVA模拟赛车游戏

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
// TODO Auto-generated catch block e.printStackTrace(); } } }
}
//MyPanel
import java.applet.Applet; import java.applet.AudioClip; import java.awt.Graphics; import java.io.File; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.List;
public class Racer extends JFrame{ static MyPanel drawPanel;
public static final int LINE_LEN=20; //方格的边长 public static final int NUM=20; //方格的个数为 NUM*NUM private static final int EDGE_WIDTH=25; //边距 static int x=0; static JLabel label=new JLabel("你的分数是: "); public static AudioClip music;
});
//添加按钮事件 exit.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent e) {
System.exit(0); }
});
gameHelp.addActionListener(new ActionListener() {
switch (e.getKeyCode()){ case KeyEvent.VK_LEFT : {
if(MyPanel.x>=60){
MyPanel.x-=20; } }; break;
case KeyEvent.VK_RIGHT: { if(MyPanel.x<=330){ MyPanel.x+=20; } }; break;
Car.Stop=!Car.Stop; }; break; } }
@Override public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub } }); }
}
//MyThread 类
import javax.swing.JOptionPane;
Baidu Nhomakorabea
// public void gameover(Graphics g){
//
for(int i=0;i<15;i+=2){
public static void playRain() throws MalformedURLException{ music = Applet.newAudioClip(new File("雨的印记.wav").toURL()); music.loop(); }
//构造函数初始化面板 Racer() throws MalformedURLException{
(car.getX1()<(MyPanel.x-Racer.LINE_LEN+1)&&(MyPanel.x-Racer.LI NE_LEN+1)<car.getX2()&&car.getY1()<(MyPanel.y+1+4*Racer.LINE_L EN)&&(MyPanel.y+1+4*Racer.LINE_LEN)<car.getY2())||
(car.getX1()<(MyPanel.x+2*Racer.LINE_LEN)&&(MyPanel.x+2*Racer. LINE_LEN)<car.getX2()&&car.getY1()<(MyPanel.y+1+Racer.LINE_LEN )&&(MyPanel.y+1+Racer.LINE_LEN)<car.getY2())||
{ win=false;
JOptionPane.showMessageDialog(car.panel, " 游戏结束! ");
car.panel.cars.remove(car);
break; } try {
Thread.sleep(50); } catch (InterruptedException e) {
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel;
//对 Start 菜单添加按钮 start.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent e) {
MyThread.win=true; MyPanel.x=(Racer.LINE_LEN*(int)((Racer.NUM-3)/2)); MyPanel.y=(Racer.NUM-4)*Racer.LINE_LEN; Racer.x=0; Car.stepY=10; new birth().start(); }
public class MyThread extends Thread{ static boolean win=true; Car car; public MyThread(Car car){ this.car =car; }
public void run() { while(true){ if(Car.Stop){ car.move(); } if(car.getY2()>=600){ car.panel.cars.remove(car); }
@Override public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub JOptionPane.showMessageDialog(drawPanel, "模拟赛车游戏 V 1.1 版权私有!"); } });
if((car.getX1()<(MyPanel.x-Racer.LINE_LEN+1)&&(MyPanel.x-Ra cer.LINE_LEN+1)<car.getX2()&&car.getY1()<(MyPanel.y+1+Racer.LI NE_LEN)&&(MyPanel.y+1+Racer.LINE_LEN)<car.getY2())||
Toolkit tk = Toolkit.getDefaultToolkit(); Image image = tk.createImage("Car.jpg"); this.setIconImage(image); JMenuBar bar=new JMenuBar(); JMenu file=new JMenu("游戏"); JMenu help =new JMenu("关于"); JMenuItem start=new JMenuItem("重新开始"); JMenuItem exit=new JMenuItem("退出"); JMenuItem gameHelp=new JMenuItem("about"); bar.add(file); bar.add(help); file.add(start); file.add(exit); help.add(gameHelp); JPanel Mainpanel=new JPanel(new BorderLayout()); drawPanel = new MyPanel(); this.setTitle("模拟赛车游戏"); this.setContentPane(Mainpanel); Mainpanel.add(drawPanel,BorderLayout.CENTER); Mainpanel.add(bar,BorderLayout.NORTH); Mainpanel.add(label,BorderLayout.EAST); this.setSize((LINE_LEN+1)*NUM+EDGE_WIDTH+70, (LINE_LEN+1)*NUM+EDGE_WIDTH+25); this.setVisible(true); playRain();
new birth().start();
//添加键盘事件 this.addKeyListener(new KeyListener() {
@Override public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override public void keyPressed(KeyEvent e) {
case KeyEvent.VK_UP: { if(MyPanel.y>=10){ MyPanel.y-=20; } }; break;
case KeyEvent.VK_DOWN: { if(MyPanel.y<=310){ MyPanel.y+=10; } }; break;
//暂停键 case KeyEvent.VK_SPACE: {
//Racer 类
import java.applet.Applet; import java.applet.AudioClip; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.peer.SystemTrayPeer; import java.io.File; import java.net.MalformedURLException;
import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel;
class MyPanel extends JPanel {
static List<Car> cars = new ArrayList<Car>(); private static boolean flag=false; //画路时,交替地画出黑白方块 private int num=Racer.NUM; private int len=Racer.LINE_LEN; static int x=(Racer.LINE_LEN*(int)((Racer.NUM-3)/2)); static int y=(Racer.NUM-4)*Racer.LINE_LEN;
car.getX1()<(MyPanel.x+2*Racer.LINE_LEN)&&(MyPanel.x+2*Racer.L INE_LEN)<car.getX2()&&car.getY1()<(MyPanel.y+1+4*Racer.LINE_LE N)&&(MyPanel.y+1+4*Racer.LINE_LEN)<car.getY2())
相关文档
最新文档