2014.11.20简单赛车游戏

本文介绍了一个基于Java实现的赛车游戏框架,包括车辆动态行为、赛道绘制、计时器管理和用户交互功能。游戏通过多个车辆实例在赛道上进行竞速,最终根据车辆位置判断胜负并展示结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
	}

	@Override
	public 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() {
			
			@Override
			public 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() {
			
			@Override
			public 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() {
			
			@Override
			public 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();
			}
		});
		
	}
	
	@Override
	public 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();
		} 
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值