版权声明:您好,欢迎来到我的技术博客。 https://blog.csdn.net/zwyjg/article/details/7862508
之前写过一个java版的,
学了android又写了android版的模拟交通灯
到了路口转弯的代码没添加,,,,,
还有一个问题,时间一长,有些车就不走了,不知道是啥问题,
如果你知道,告诉我一下.谢谢
import java.util.*;
import android.app.Activity;
import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.view.*;
public class Mian extends Activity implements Runnable {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyView my = new MyView(this);
setContentView(my);
new Thread(this).start();
}
public void run() {
// TODO Auto-generated method stub
MyCar myCar = null;
Random r = new Random();
int direct;
int end;
while (true) {
if (MyView.arrMyCar.size() <= 6) {
direct = r.nextInt(4);
switch (direct) {
case 0:
myCar = new MyCar(125, 0, 140, 35, 0,0);
MyView.arrMyCar.add(myCar);
break;
case 1:
myCar = new MyCar(0, 242, 35, 257, 1,1);
MyView.arrMyCar.add(myCar);
break;
case 2:
myCar = new MyCar(180, 395, 195, 430, 2,2);
MyView.arrMyCar.add(myCar);
break;
case 3:
myCar = new MyCar(285, 190, 320, 205, 3,3);
MyView.arrMyCar.add(myCar);
break;
}
Thread t = new Thread(myCar);
t.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
class MyView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
SurfaceHolder holder;
Rect rect;// 创建类
static Vector<Light> arrLight;
static Vector<MyCar> arrMyCar;
public MyView(Context context) {
super(context);
holder = this.getHolder();// 获取SurfaceHolder对象实例
holder.addCallback(this);// 添加callback接口
setFocusable(true);
arrLight = new Vector<Light>();// 放灯对象
arrMyCar = new Vector<MyCar>();// 放车对象
arrLight.add(new Light(115, 165, 150, 180, 0));
arrLight.add(new Light(95, 232, 110, 267, 1));
arrLight.add(new Light(170, 260, 205, 275, 2));
arrLight.add(new Light(210, 185, 225, 220, 3));
new Thread(new Light()).start();
}
@Override
// 当SurfaceView尺寸改变时调用
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
// 当SurfaceView创建时调用
public void surfaceCreated(SurfaceHolder holder) {
new Thread(this).start();// 启动新的线程
}
// 当SurfaceView销毁时调用
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public synchronized void run() {
// TODO Auto-generated method stub
while (true) {
Canvas canvas = holder.lockCanvas(null);// SurfaceHolder锁定并获得canvas对象
canvas.drawColor(Color.WHITE);
this.drawGraphics(canvas);
holder.unlockCanvasAndPost(canvas);// 解锁画布,提交画好的图像
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void drawGraphics(Canvas canvas) {
Paint mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL);// 设置为实心
mPaint.setAntiAlias(true);// 设置抗锯齿
this.draw(canvas, mPaint);
}
public void draw(Canvas canvas, Paint p) {
// 画车道
canvas.drawRect(110, 0, 115, 430, p);
canvas.drawRect(205, 0, 210, 430, p);
canvas.drawRect(0, 180, 320, 185, p);
canvas.drawRect(0, 275, 320, 280, p);
p.setColor(Color.RED);
canvas.drawRect(160, 0, 162, 430, p);
canvas.drawRect(0, 230, 320, 232, p);
// 画灯
p.setColor(Color.BLACK);
for (int i = 0; i < arrLight.size(); i++) {
Light light = arrLight.get(i);
canvas.drawRect(light.x1, light.y1, light.x2, light.y2, p);
if (light.direct % 2 == 0) {
if (light.redLight) {
p.setColor(Color.RED);
canvas.drawCircle(light.x1 + 7, light.y1 + 7, 7, p);
} else {
p.setColor(Color.GREEN);
canvas.drawCircle(light.x1 + 27, light.y1 + 7, 7, p);
}
} else {
if (light.redLight) {
p.setColor(Color.RED);
canvas.drawCircle(light.x1 + 7, light.y1 + 7, 7, p);
} else {
p.setColor(Color.GREEN);
canvas.drawCircle(light.x1 + 7, light.y1 + 27, 7, p);
}
}
p.setColor(Color.BLACK);
}
// 画车
for (int i = 0; i < MyView.arrMyCar.size(); i++) {
MyCar myCar = MyView.arrMyCar.get(i);
switch(myCar.color){
case 0:
p.setColor(Color.BLUE);
break;
case 1:
p.setColor(Color.CYAN);
break;
case 2:
p.setColor(Color.GRAY);
break;
case 3:
p.setColor(Color.GREEN);
break;
}
if (myCar.startDirect % 2 == 0) {
canvas.drawRect(myCar.x1, myCar.y1, myCar.x1 + 15,
myCar.y1 + 35, p);
} else {
canvas.drawRect(myCar.x1, myCar.y1, myCar.x1 + 35,
myCar.y1 + 15, p);
}
}
}
}
class Light implements Runnable {
int x1;
int y1;
int x2;
int y2;
boolean redLight;
boolean greenLight;
int direct;
public Light(int x1, int y1, int x2, int y2, int direct) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.direct = direct;
}
public Light() {
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
for (int i = 0; i < MyView.arrLight.size(); i++) {
Light light = MyView.arrLight.get(i);
if (direct % 2 == 0) {
if (light.direct % 2 == 0) {
light.greenLight = true;
light.redLight = false;
} else {
light.greenLight = false;
light.redLight = true;
}
} else {
if (light.direct % 2 == 0) {
light.greenLight = false;
light.redLight = true;
} else {
light.greenLight = true;
light.redLight = false;
}
}
}
direct++;
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Car{
int x1;
int y1;
int x2;
int y2;
int color;
int startDirect;
int speed = 8;
boolean stopCar = true;// 是否停车
boolean over=false;//退出线程
public Car(int x1, int y1, int x2, int y2, int startDirect, int color) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.startDirect = startDirect;
this.color=color;
}
}
class MyCar extends Car implements Runnable {
public MyCar(int x1, int y1, int x2, int y2, int startDirect, int color) {
super(x1, y1, x2, y2, startDirect, color);
// TODO Auto-generated constructor stub
}
public void panDuan(int x, int y, int direct) {
if (x < 0 || x > 320 || y < 0 || y > 430) {// 碰到边界,从arrAcr删除
MyView.arrMyCar.remove(this);
this.over=true;
}
// 是否碰到灯
for(int i=0;i<MyView.arrLight.size();i++){
Light light=MyView.arrLight.get(i);
if(direct%2==0){
if(y>=light.y1&&y<=light.y1+15){
if(light.redLight==true&&light.direct==direct){
stopCar=false;
}else{
stopCar=true;
}
}
}else{
if(x>=light.x1&&x<=light.x1+15){
if(light.redLight==true&&light.direct==direct){
stopCar=false;
}else{
stopCar=true;
}
}
}
}
//车碰车
for(int i=0;i<MyView.arrMyCar.size();i++){
MyCar car=MyView.arrMyCar.get(i);
if(car.startDirect%2==0){
if(y>=car.y1&&y<=car.y1+35){
if(car.stopCar==false&&car.startDirect==direct){
stopCar=false;
}else{
stopCar=true;
}
}
}else{
if(car.startDirect==direct){
if(x>=car.x1&&x<=car.x1+35){
if(car.stopCar==false&&car.startDirect==direct){
stopCar=false;
}else{
stopCar=true;
}
}
}
}
}
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
switch (startDirect) {
case 0:
if (stopCar) {
y1 += speed;
y2 += speed;
}
this.panDuan(x2, y2, startDirect);
break;
case 1:
if (stopCar) {
x1 += speed;
x2 += speed;
}
this.panDuan(x2, y2, startDirect);
break;
case 2:
if (stopCar) {
y1 -= speed;
y2 -= speed;
}
this.panDuan(x1, y1, startDirect);
break;
case 3:
if (stopCar) {
x1 -= speed;
x2 -= speed;
}
this.panDuan(x1, y1, startDirect);
break;
}
//退出线程
if(this.over==true){
break;
}
try {
Thread.sleep(60);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}