2014.11.20简单赛车游戏
来源:互联网 发布:mac预览编辑pdf 编辑:程序博客网 时间:2024/05/16 02:04
1.package com.hechao;import java.awt.Color;import java.awt.Graphics;public class Car implements Runnable{private String name;private int speed;private int x = 50;public Car(String name) {this.name = name;this.speed = (int) (Math.random() * 8 + 1);}public void move(){x += speed;}public void draw(Graphics g, int y) {Color color = g.getColor();g.setColor(Color.BLUE);g.fillRect(x, y, 50, 40);g.setColor(Color.BLACK);g.drawString(name, x + 15, y + 25);g.setColor(color);}@Overridepublic void run() {this.move();}public int getX() {return x;}public String getName() {return name;}}
2.package com.hechao;import java.awt.Font;import java.awt.Graphics;import java.awt.Image;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.image.BufferedImage;import java.util.ArrayList;import java.util.List;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.Timer;@SuppressWarnings("serial")public class CarGameFrame extends JFrame {private JButton startButton, stopButton;private List<Car> list = new ArrayList<Car>();private Image offImage = new BufferedImage(1000, 600, 1);private Track myTrack = new Track();private Timer timer = null;private ExecutorService service = Executors.newFixedThreadPool(5);private JLabel isOverLabel;public CarGameFrame() {this.setTitle("赛车游戏");this.setSize(1000, 600);this.setResizable(false);this.setLocationRelativeTo(null);this.setLayout(null);this.setDefaultCloseOperation(EXIT_ON_CLOSE);initialize();}private void initialize(){//标签初始化isOverLabel = new JLabel();isOverLabel.setBounds(200, 530, 100, 30);isOverLabel.setFont(new Font("微软雅黑", 1, 14));this.add(isOverLabel);//初始化按钮startButton = new JButton("开始");startButton.setBounds(400, 530, 60, 30);startButton.setEnabled(true);startButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {timer.start();startButton.setEnabled(false);stopButton.setEnabled(true);}});this.add(startButton);stopButton = new JButton("暂停");stopButton.setBounds(500, 530, 60, 30);stopButton.setEnabled(true);stopButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {timer.stop();stopButton.setEnabled(false);startButton.setEnabled(true);}});this.add(stopButton);//初始化车队list.add(new Car("01"));list.add(new Car("02"));list.add(new Car("03"));list.add(new Car("04"));list.add(new Car("05"));//添加时钟timer = new Timer(5, new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {for (int i = 0; i < list.size(); i++) {service.execute(list.get(i));if (list.get(i).getX() >= 850) {isOverLabel.setText(list.get(i).getName() + "号选手获胜!");timer.stop();}}repaint();}});}@Overridepublic void paint(Graphics g) {Graphics myG = offImage.getGraphics();super.paint(myG);myTrack.draw(myG);for(int i = 0; i < list.size(); i++){list.get(i).draw(myG, 80 + i * 100);}g.drawImage(offImage, 0, 0, null);}}
3.package com.hechao;import java.awt.Color;import java.awt.Graphics;/** * 赛道 * @author hechao * */public class Track {public Track(){}public void draw(Graphics g){Color color = g.getColor();g.setColor(Color.GREEN);g.fillRect(50, 50, 50, 500);g.drawRect(50, 50, 900, 500);g.setColor(Color.RED);g.fillRect(900, 50, 50, 500);g.setColor(color);}}
4.package com.hechao;import javax.swing.UIManager;public class Test01 {public static void main(String[] args) {new CarGameFrame().setVisible(true);try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch (Exception e) {e.printStackTrace();} }}
0 0
- 2014.11.20简单赛车游戏
- 赛车游戏
- 用threejs制作一款简单的赛车游戏
- 赛车游戏开发总结
- 赛车游戏开发总结
- Ogre(NxOgre)赛车游戏
- java游戏--赛车模拟
- 手势玩儿赛车游戏
- j2me简易赛车游戏
- ubuntu 游戏 赛车
- 赛车游戏的悖论
- 赛车游戏算法笔记
- 赛车游戏开发总结
- 《天天赛车》游戏介绍
- unity3d赛车游戏视频教程
- supertuxkart_android赛车游戏移植
- 【bzoj2328】[HNOI2011]赛车游戏
- CQYZ_Vijos_P3090 赛车游戏
- 写出 文件 iOS
- 从零开始学习HyBrid App开发(一)What's HyBrid App?
- Android开发之实现锁屏功能
- 为什么 JavaScript仍将是程序员的首选
- bit-GAVUP-试一试这个博客,以后就拿来写技术文档了
- 2014.11.20简单赛车游戏
- UVA - 10911 Forming Quiz Teams 状态压缩
- 使用lucene,IKAnalyzer提取敏感词
- Android GridView(九宫格)
- Coursera台大机器学习课程笔记4 -- Training versus Testing
- 手动创建库后,sys用户查询dba_tablespaces报错:ORA-00942: table or view does not exist
- 《Java虚拟机原理图解》1.4 class文件中的字段表集合--field字段在class文件中是怎样组织的
- 的副书记和的疯狂了士大夫三大街
- Coursera台大机器学习课程笔记5 -- Theory of Generalization