線程
線程的概念
像以往我們做一個(gè)圖板或者做一個(gè)自動(dòng)運(yùn)行的程序時(shí)在一個(gè)方法進(jìn)行執(zhí)行時(shí)流纹,程序未執(zhí)行完成時(shí)是無法關(guān)閉以及進(jìn)行其他操作的薄霜,但是在繼承線程Thread后就可以實(shí)現(xiàn)在線程程序運(yùn)行時(shí)執(zhí)行其他操作。
線程的基本語法
首先將于要執(zhí)行線程的類繼承Thread
public class A extends Thread{
}
之后再重寫Thread中的run方法
public void run(){
while(true){
this.move();
}
}
最后在mian函數(shù)中創(chuàng)建A對(duì)象惰瓜,并調(diào)用A的start方法否副,注意這里不是通過調(diào)用run方法啟動(dòng),而是調(diào)用start方法啟動(dòng)崎坊。
public static void main(String args[]){
A a =new A();
a.start();
}
運(yùn)行程序后可以發(fā)現(xiàn)move方法在不斷被執(zhí)行但是與此同時(shí)我們可以進(jìn)行關(guān)閉等其他操作备禀。
大球吃小球
整體思路
這個(gè)游戲模仿大魚吃小魚的小游戲奈揍,通過創(chuàng)建不同大小的小球進(jìn)行吃與被吃的過程曲尸,玩家控制一個(gè)小球擁有可升級(jí)的選項(xiàng)男翰,其他小球擁有真?zhèn)蓽y(cè)其身邊小球的功能另患,當(dāng)其他小球比當(dāng)前小球級(jí)別高時(shí)反向逃離該小球,反之向該小球移動(dòng)蛾绎。
代碼實(shí)現(xiàn)
首先創(chuàng)建畫板對(duì)象昆箕,也是該游戲的主場(chǎng)景,設(shè)置背景為綠色租冠,創(chuàng)建鍵盤監(jiān)聽器控制玩家小球鹏倘,并監(jiān)聽畫布肺稀。
public class UI extends JFrame{
public static void main(String args[]){
UI ui = new UI();
ui.startUI();
}
public void startUI(){
this.setTitle("zoo");// 標(biāo)題
this.setSize(1200,700);// 窗口大小
this.setResizable(false);// 窗口是否可動(dòng)
this.setDefaultCloseOperation(3);// 關(guān)閉時(shí)退出程序
setLocationRelativeTo(null);//居中
this.setVisible(true);
Graphics g = this.getGraphics();
Listener l =new Listener(g);
this.addKeyListener(l);
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.green);
g.fillRect(0, 0, 1200, 750);
System.out.println(1);
}
}
創(chuàng)建小球類繼承Thread(但小球類不會(huì)用到,用于之后玩家類重寫run方法)话原,創(chuàng)建用于儲(chǔ)存小球?qū)ο蟮腁rrayList,并為小球設(shè)置等級(jí)0-4級(jí)繁仁,根據(jù)等級(jí)設(shè)定顏色大小以及速度,然后創(chuàng)建move方法設(shè)置小球的移動(dòng)規(guī)律黄虱,例如觸碰到邊界稚矿,遭遇其他小球進(jìn)行判斷能否被吃。再創(chuàng)建偵測(cè)的方法判斷偵測(cè)范圍內(nèi)小球的等級(jí)以及方位進(jìn)行移動(dòng)捻浦。
public class Animal extends Thread{
int id,speed,x,y,sx,sy,size;
ArrayList<Animal> list;
Graphics g;
Random m = new Random();
public Animal(int x,int y,int id,Graphics g,ArrayList<Animal> list){
this.x=x;
this.y= y;
if(id<5){
this.id =id;
}else{
this.id=4;
}
this.g =g;
this.speed = 10 - 2*id;
this.size = 20+10*id;
this.list =list;
sx = m.nextInt(speed+1);
sy = m.nextInt(speed+1);
}
public void move(){
int l,x1,y1,s;
if (x < 0) {
sx = m.nextInt(speed+1);
sy =speed-sx;
} else if (x > 1200) {
sx = -m.nextInt(speed+1);
sy = speed+sx;
} else if (y < 0) {
sy = m.nextInt(speed+1);
sx = speed -sy;
} else if (y > 650) {
sy = -m.nextInt(speed+1);
sx = speed +sy;
}
for (int i = 0; i < list.size(); i++) {
if(list.get(i)!=null){
x1 = list.get(i).x ;
y1 = list.get(i).y ;
l = (x1 - x) * (x1 - x ) + (y1 - y) * (y1 - y);
s =(list.get(i).size/2+size/2)*(list.get(i).size/2+size/2);
if(l < s&&x!=list.get(i).x){
if(id>list.get(i).id){
g.setColor(Color.green);
g.fillOval(list.get(i).x-list.get(i).size/2, list.get(i).y-list.get(i).size/2, list.get(i).size,list.get(i).size);
list.set(i, null);
}else if(id==list.get(i).id){
sx=-sx;
sy=-sy;
}
}
}
}
g.setColor(Color.green);
g.fillOval(x-size/2, y-size/2, size, size);
x += sx;
y += sy;
this.darw();
}
public void darw(){
Color c = null;
if(this.id==0){
c=Color.white;
}else if(this.id ==1){
c=Color.ORANGE;
}else if(this.id == 2){
c=Color.gray;
}else if(this.id == 3){
c=Color.yellow;
}else if(this.id == 4){
c=Color.black;
}
g.setColor(c);
g.fillOval(x-size/2, y-size/2, size, size);
}
public void Existence(){
int l,x1,y1;
for(int i=0;i<list.size();i++){
if(list.get(i)!=null){
x1=list.get(i).x;
y1= list.get(i).y;
l=(y-y1)*(y-y1)+(x-x1)*(x-x1);
if(l < 10000){
if(id > list.get(i).id){
this.a(list.get(i));
}else if(id < list.get(i).id){
this.b(list.get(i));
}
}
}
}
}
public void run() {
while (true) {
try {
Thread.sleep(80);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
for(int i=1;i<list.size();i++){
if(list.get(i)!=null){
list.get(i).Existence();
list.get(i).move();
}
}
}
}
public void a(Animal n){
int x1=n.x;
int y1= n.y;
if(x-x1>=0&&y-y1>=0){
if(x-x1>=y-y1){
sx =-speed;
sy = -((y-y1)/(x-x1))*speed;
}else{
sy =-speed;
sx = -((x-x1)/(y-y1))*speed;
}
}else if(x-x1<=0&&y-y1>=0){
if(x1-x>=y-y1){
sx = speed;
sy = -((y-y1)/(x-x1))*speed;
}else{
sy =speed;
sx = -((x-x1)/(y-y1))*speed;
}
}else if(x-x1>=0&&y-y1<=0){
if(x-x1>=y1-y){
sx = -speed;
sy = ((y-y1)/(x-x1))*speed;
}else{
sy =-speed;
sx = ((x-x1)/(y-y1))*speed;
}
}else if(x-x1<=0&&y-y1<=0){
if(x1-x>=y-y1){
sx = speed;
sy = ((y-y1)/(x-x1))*speed;
}else{
sy =speed;
sx = ((x-x1)/(y-y1))*speed;
}
}
}
public void b(Animal n){
int x1=n.x;
int y1= n.y;
if(x-x1>=0&&y-y1>=0){
if(x-x1>=y-y1){
sx =speed;
sy = ((y-y1)/(x-x1))*speed;
}else{
sy =speed;
sx =((x-x1)/(y-y1))*speed;
}
}else if(x-x1<=0&&y-y1>=0){
if(x1-x>=y-y1){
sx = -speed;
sy = ((y-y1)/(x-x1))*speed;
}else{
sy =-speed;
sx = ((x-x1)/(y-y1))*speed;
}
}else if(x-x1>=0&&y-y1<=0){
if(x-x1>=y1-y){
sx = speed;
sy = -((y-y1)/(x-x1))*speed;
}else{
sy =speed;
sx = -((x-x1)/(y-y1))*speed;
}
}else if(x-x1<=0&&y-y1<=0){
if(x1-x>=y-y1){
sx = -speed;
sy = -((y-y1)/(x-x1))*speed;
}else{
sy =-speed;
sx = -((x-x1)/(y-y1))*speed;
}
}
}
}
然后創(chuàng)建玩家小球繼承小球類朱灿,重寫屬性,重寫玩家自己的move方法盗扒,定義移動(dòng)方式缀去,以及升級(jí)的經(jīng)驗(yàn)條,以及玩家的等級(jí)(可大于小球的最高的等級(jí))缕碎,最后重寫run方法,讓小球數(shù)量等級(jí)根據(jù)玩家等級(jí)變化咏雌,且實(shí)現(xiàn)list中所有小球的移動(dòng)。
public class Player extends Animal {
int exp = 0;
public Player(int x, int y, int id, Graphics g, ArrayList<Animal> list) {
super(x, y, id, g, list);
// TODO Auto-generated constructor stub
if(id<6){
this.id = id;
}else{
this.id = 5;
}
sx = 0;
sy = 0;
}
public void move(){
int x1,y1,l,s;
if (x < 0&&sx<0) {
sx =0;
} else if (x > 1200&&sx>0) {
sx=0;
} else if (y < 0&&sy<0) {
sy=0;
} else if (y > 650&&sy>0) {
sy=0;
}
for (int i = 0; i < list.size(); i++) {
if(list.get(i)!=null){
x1 = list.get(i).x ;
y1 = list.get(i).y ;
l = (x1 - x) * (x1 - x ) + (y1 - y) * (y1 - y);
s =(list.get(i).size/2+size/2)*(list.get(i).size/2+size/2);
if(l < s&&x!=list.get(i).x){
if(id>list.get(i).id){
g.setColor(Color.green);
g.fillOval(list.get(i).x-list.get(i).size/2, list.get(i).y-list.get(i).size/2, list.get(i).size,list.get(i).size);
list.set(i, null);
size++;
this.up();
}
}
}
}
g.setColor(Color.green);
g.fillOval(x-size/2, y-size/2, size, size);
x += sx;
y += sy;
this.darw();
}
public void darw(){
Color c = null;
if(this.id==0){
c=Color.white;
}else if(this.id ==1){
c=Color.ORANGE;
}else if(this.id == 2){
c=Color.gray;
}else if(this.id == 3){
c=Color.yellow;
}else if(this.id == 4){
c=Color.black;
}else if(this.id ==5){
c=Color.red;
}
g.setColor(Color.red);
g.fillOval(x-size/2, y-size/2, size, size);
g.setColor(c);
g.fillOval(x-size/2+1, y-size/2+1, size-2, size-2);
}
public void up() {
exp++;
if (exp == 10) {
id++;
exp = 0;
}
g.setColor(Color.white);
g.fillRect(0, 680, 1200, 20);
g.setColor(Color.blue);
g.fillRect(0, 680, 120*this.exp, 20);
}
public void run() {
while (true) {
System.out.println(exp);
int m = 0;
if(id>3){
m=3;
}
int n = 0;
try {
Thread.sleep(80);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
for (int i = 0; i < list.size(); i++) {
if (list.get(i) != null) {
if (list.get(i).id > id) {
m++;
} else if (list.get(i).id < id) {
n++;
}
}
}
if (m < 3) {
Animal a = new Animal(ran(1200), ran(700), id + 1, g, list);
list.add(a);
}
if (n < 10) {
Animal b = new Animal(ran(1200), ran(700), id - 1, g, list);
list.add(b);
}
for (int i = 1; i < list.size(); i++) {
if (list.get(i) != null) {
list.get(i).Existence();
list.get(i).move();
}
}
}
}
public int ran(int o) {
Random r = new Random();
return r.nextInt(o + 1);
}
}
最后創(chuàng)建鍵盤監(jiān)聽器的類,并加入移動(dòng)指令以及啟動(dòng)一次玩家對(duì)象的start方法
public class Listener implements KeyListener {
Graphics g;
ArrayList<Animal> list = new ArrayList<Animal>();
int x=0;
Player p;
int time = 0;
public Listener(Graphics g) {
this.g = g;
p= new Player(600,350,1,g,list);
list.add(p);
}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
int key= e.getKeyCode();
if(key == e.VK_UP){
p.sy = -p.speed;
p.sx = 0;
p.move();
}else if(key == e.VK_DOWN){
p.sy = p.speed;
p.sx = 0;
p.move();
}else if(key ==e.VK_LEFT){
p.sx = -p.speed;
p.sy = 0;
p.move();
}else if(key ==e.VK_RIGHT){
p.sx = p.speed;
p.sy = 0;
p.move();
}
if(time==0){
time++;
p.start();
}
}
@Override
public void keyReleased(KeyEvent e) { }
}