JAVA入门到精通-第39讲-线程.坦克大战7
![](https://img2018.cnblogs.com/blog/1167156/201811/1167156-20181130180418068-988553275.png)
如果没有做要求,用实现接口的方法写进程;
至少有继承的机会;
实际上,更多的情况下是多线程计算;
![](https://img2018.cnblogs.com/blog/1167156/201811/1167156-20181130180418544-1729992427.png)
两个线程,t1/t2,同时启动;
创建了一只猪,创建了一只鸟;
第一个线程承载猪,第二个线程承载鸟;
很可能是交替进行,但也未必;和休眠时间相关;
![](https://img2018.cnblogs.com/blog/1167156/201811/1167156-20181130180418866-141941523.png)
同时去争夺一种资源,线程同步的问题;
![](https://img2018.cnblogs.com/blog/1167156/201811/1167156-20181130180419530-1682813252.png)
![](https://img2018.cnblogs.com/blog/1167156/201811/1167156-20181130180420537-1280001387.png)
Thread类本身就实现了Runnable接口,本身是一家人;
启动的方式不太一样;
坦克的子弹自动跑(肯定用到线程):
子弹也是一个对象:有坐标,有速度;
可以当做线程的一个类,可以自动跑动;
子弹也是属于某个坦克的;
![](https://img2018.cnblogs.com/blog/1167156/201811/1167156-20181130180421536-568780928.png)
![](https://img2018.cnblogs.com/blog/1167156/201811/1167156-20181130180421763-1364790676.png)
![](https://img2018.cnblogs.com/blog/1167156/201811/1167156-20181130180421986-1251668.png)
![](https://img2018.cnblogs.com/blog/1167156/201811/1167156-20181130180422584-335560603.png)
![](https://img2018.cnblogs.com/blog/1167156/201811/1167156-20181130180422978-1472196165.png)
这个坦克永远只能有一颗子弹;
当用户按下J的时候,子弹发射:KeyPressed
else if 的致命毛病是找到一个入口,
不会去判断另外的入口;
按了W键,子弹就可能出不来了;
![](https://img2018.cnblogs.com/blog/1167156/201811/1167156-20181130180423254-1790858.png)
绘制子弹:
其实是一个点:
![](https://img2018.cnblogs.com/blog/1167156/201811/1167156-20181130180423517-1810770089.png)
怎么让子弹跑起来?
让子弹的坐标不停的得到修改。
把子弹做成一个线程,不停地修改坐标达到目的;
传方向进去:
![](https://img2018.cnblogs.com/blog/1167156/201811/1167156-20181130180423795-107467692.png)
![](https://img2018.cnblogs.com/blog/1167156/201811/1167156-20181130180424049-1312624335.png)
传进速度:
![](https://img2018.cnblogs.com/blog/1167156/201811/1167156-20181130180424317-1452533806.png)
![](https://img2018.cnblogs.com/blog/1167156/201811/1167156-20181130180424521-846888518.png)
//子弹何时死亡?
启动子弹:
![](https://img2018.cnblogs.com/blog/1167156/201811/1167156-20181130180424840-1869692692.png)
虽然子弹坐标在变化,但子弹不变;
paint函数需要不停地重绘,子弹才能跑;
把MyPanel也做成一个线程;
屏幕本身也在不停的刷新:
![](https://img2018.cnblogs.com/blog/1167156/201811/1167156-20181130180425299-1405242422.png)
还得启动MyPanel线程
![](https://img2018.cnblogs.com/blog/1167156/201811/1167156-20181130180425662-1493160262.png)
但是,打一颗子弹,这子弹永远在跑:
当子弹碰到敌人坦克/边框的时候子弹死亡;
boolean isLive=true;
----------------------------------------------------------
多线程实例演示[Thread03.java]
x
1
/**
2
*多线程Thread使用
3
*1、一个线程通过接收n来执行1+..+n得到和
4
*2、另一线程每隔1秒输出一次hello world!
5
*/
6
public class Thread03 {
7
public static void main(String[] args) {
8
Pig pig=new Pig(10);
9
Bird bird=new Bird(10);
10
//建立线程
11
Thread t1=new Thread(pig);
12
Thread t2=new Thread(bird);
13
//启动线程
14
t1.start();
15
t2.start();
16
}
17
}
18
19
//打印hello world!
20
class Pig implements Runnable{
21
int n=0;
22
int times=0;
23
public Pig(int n){
24
this.n=n;
25
}
26
27
public void run(){
28
while(true){
29
try {
30
Thread.sleep(1000);
31
} catch (Exception e) {
32
e.printStackTrace();
33
}
34
times++;
35
System.out.println("我是一个线程,正在输出第"+times+"个hello world!");
36
if(times==n){
37
break;
38
}
39
}
40
}
41
}
42
43
//算数学题
44
class Bird implements Runnable{//多线程时应使用implements接口来实现,不要使用extends继承
45
int n=0;
46
int res=0;
47
int times=0;
48
public Bird(int n){
49
this.n=n;
50
}
51
52
public void run() {
53
while(true){
54
try {
55
Thread.sleep(1000);
56
} catch (Exception e) {
57
e.printStackTrace();
58
}
59
res+=(++times);
60
System.out.println("当前结果是:"+res);
61
if(times==n){
62
System.out.println("最后的结果是:"+res);
63
break;
64
}
65
}
66
}
67
}
68
![](https://img2018.cnblogs.com/blog/1167156/201811/1167156-20181130180426427-879138229.png)
MyTank03: