赛车游戏

重点
1, 多线程 - 两辆赛车
2, 按键监听 - KeyListener
3, 碰撞检测 -
Rectangle p1=new Rectangle(W/9,H/2,H/30,H/30);
p1.intersects(left);

package chapter1;

package chapter1;

import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JApplet;
import javax.swing.JFrame;

public class G1P1 extends JFrame{
	int W =900, H = 650;
	double p1Speed=.5,p2Speed=.5;
	
	final int UP = 0, RIGHT=1, DOWN =2, LEFT=3;
	int p1Direction = UP;
	int p2Direction = UP;
	
    //以下是边框模块
    Rectangle left=new Rectangle(0,0,W/9,H);
    Rectangle right=new Rectangle((W/9)*8,0,W/9,H);
    Rectangle top=new Rectangle(0,0,W,H/9);
    Rectangle bottom=new Rectangle(0,(H/9)*8,W,H/9);
    Rectangle center=new Rectangle((int)((W/9)*2.5),(int)((H/9)*2.5),(int)((W/9)*5),(int)((H/9)*4));
    
    //以下是障碍模块
    Rectangle obstacle=new Rectangle(W/2,(int)((H/9)*7),W/10,H/9);
    Rectangle obstacle2=new Rectangle(W/3,(int)((H/9)*5),W/10,H/4);
    Rectangle obstacle3=new Rectangle(2*(W/3),(int)((H/9)*5),W/10,H/4);
    Rectangle obstacle4=new Rectangle(W/3,H/9,W/30,H/9);
    Rectangle obstacle5=new Rectangle(W/2,(int)((H/9)*1.5),W/30,H/4);
    
    //一下是线模块
    Rectangle finish=new Rectangle(W/9,(H/2)-(H/9),(int)((W/9)*1.5),H/70);
    Rectangle line0=new Rectangle(W/9,H/2,(int)((W/9)*1.5)/2,H/140);
    Rectangle line1=new Rectangle((W/9)+((int)((W/9)*1.5)/2),(H/2)+(H/10),(int)((W/9)*1.5)/2,H/140);
    
    //移动模块
    Rectangle p1=new Rectangle(W/9,H/2,H/30,H/30);
    Rectangle p2=new Rectangle(((W/9)+((int)((W/9)*1.5)/2)),(H/2)+(H/10),W/30,W/30);
    
    public G1P1(){
        super();
        setSize(W,H);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(false);
        Move1 m1=new Move1();
        Move2 m2=new Move2();
        m1.start();
        m2.start();
        
    }
    public void paint(Graphics g){
        super.paint(g);
        g.setColor(Color.DARK_GRAY);
        g.fillRect(0,0,W,H);
        g.setColor(Color.GREEN);
        g.fillRect(left.x,left.y,left.width,left.height);
        g.fillRect(right.x,right.y,right.width,right.height);
        g.fillRect(top.x,top.y,top.width,top.height);
        g.fillRect(bottom.x,bottom.y,bottom.width,bottom.height);
        g.fillRect(center.x,center.y,center.width,center.height);
        g.fillRect(obstacle.x,obstacle.y,obstacle.width,obstacle.height);
        g.fillRect(obstacle2.x,obstacle2.y,obstacle2.width,obstacle2.height);
        g.fillRect(obstacle3.x,obstacle3.y,obstacle3.width,obstacle3.height);
        g.fillRect(obstacle4.x,obstacle4.y,obstacle4.width,obstacle4.height);
        g.fillRect(obstacle5.x,obstacle5.y,obstacle5.width,obstacle5.height);
        g.setColor(Color.WHITE);
        g.fillRect(line0.x,line0.y,line0.width,line0.height);
        g.fillRect(line1.x,line1.y,line1.width,line1.height);
        g.setColor(Color.YELLOW);
        g.fillRect(finish.x,finish.y,finish.width,finish.height);
        g.setColor(Color.BLUE);
        //g.fill3DRect(p1.x,p1.y, p1.width,p1.height,true);
        
        URL url = this.getClass().getResource("ship2.png");
		Image img1 = Toolkit.getDefaultToolkit().getImage(url);
		g.drawImage(img1, p1.x, p1.y, this);
		
        g.setColor(Color.RED);
        g.fill3DRect(p2.x,p2.y,p2.width,p2.height,true);
        
       
    }
    
    private class Move1 extends Thread implements KeyListener{
        public void run(){
        	
        	addKeyListener(this);
        	
            while(true){
                try{
                    repaint();
                    
                    if(p1.intersects(left) || p1.intersects(right) || 
                    		p1.intersects(top) || p1.intersects(obstacle) ||
                    		p1.intersects(obstacle2) || p1.intersects(p2) ||
                    		p1.intersects(obstacle3) || p1.intersects(obstacle4) ||
                    		p1.intersects(obstacle5) ){
                    	p1Speed = -4;
                    }
                    
                    if(p1.intersects(center)){
                    	p1Speed = -2.5;
                    }
                    
                    if(p1Speed<=5){
                        p1Speed+=.2;
                    }
                    
                    //p1.y-=p1Speed;
                    
                    if(p1Direction == UP){
                    	p1.y -= (int)p1Speed;
                    }
                    if(p1Direction == DOWN){
                    	p1.y += (int)p1Speed;
                    }
                    if(p1Direction == LEFT){
                    	p1.x -= (int)p1Speed;
                    }
                    if(p1Direction == RIGHT){
                    	p1.x += (int)p1Speed;
                    }
                    
                    Thread.sleep(75);
                }catch(Exception e){
                    break;
                }
            }
        }

		@Override
		public void keyPressed(KeyEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void keyReleased(KeyEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void keyTyped(KeyEvent event) {
			if(event.getKeyChar() == 'a'){
				p1Direction = LEFT;
			}
			if(event.getKeyChar() == 's'){
				p1Direction = DOWN;
			}
			if(event.getKeyChar() == 'd'){
				p1Direction = RIGHT;
			}
			if(event.getKeyChar() == 'w'){
				p1Direction = UP;
			}
		}
    }
    private class Move2 extends Thread implements KeyListener{
        public void run(){
        	
        	addKeyListener(this);
        	
            while(true){
                try{
                    repaint();
                    
                    if(p2.intersects(left) || p2.intersects(right) || 
                    		p2.intersects(top) || p2.intersects(obstacle) ||
                    		p2.intersects(obstacle2) || p2.intersects(p1) ||
                    		p2.intersects(obstacle3) || p2.intersects(obstacle4) ||
                    		p2.intersects(obstacle5) ){
                    	p2Speed = -4;
                    }
                    
                    if(p2.intersects(center)){
                    	p2Speed = -2.5;
                    }
                    
                    if(p2Speed<=5){
                        p2Speed+=.2;
                    }
                    
                    
                    if(p2Direction == UP){
                    	p2.y -= (int)p2Speed;
                    }
                    if(p2Direction == DOWN){
                    	p2.y += (int)p2Speed;
                    }
                    if(p2Direction == LEFT){
                    	p2.x -= (int)p2Speed;
                    }
                    if(p2Direction == RIGHT){
                    	p2.x += (int)p2Speed;
                    }
                    
                    Thread.sleep(75);
                }catch(Exception e){
                    break;
                }
            }
        }

		@Override
		public void keyPressed(KeyEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void keyReleased(KeyEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void keyTyped(KeyEvent event) {
			if(event.getKeyChar() == 'j'){
				p2Direction = LEFT;
			}
			if(event.getKeyChar() == 'k'){
				p2Direction = DOWN;
			}
			if(event.getKeyChar() == 'l'){
				p2Direction = RIGHT;
			}
			if(event.getKeyChar() == 'i'){
				p2Direction = UP;
			}
		}
    }
    
    public static void main(String[] args) {
		new G1P1();
	}
}

你可能感兴趣的:(多线程,thread,游戏,swing,UP)