Java Applet ile yapılmış tuğla kırma oyunu (DX-BALL)

package aaaa;


import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;




public class projeOdevi extends Applet implements Runnable,KeyListener{

	
	int [][] kareler = new int[10][2];
	boolean [] karelerB = new boolean[10];
	int x,y,cap,hareketx,harekety,artisx,artisy,karex,karey,can;
	Thread t;
	boolean calisiyormu = true,yukari = false,bittimi=false; 
	@Override
	public void init(){
	
		for(int i=0;i<karelerB.length;i++){
			karelerB[i]=false;
		}
		
		for(int i=0;i<5;i++){
			kareler[i][0]=(i+1)*100;
			kareler[i][1]=10;
			
	
		}
		
		for(int i=0;i<5;i++){
			kareler[i+5][0]=(i+1)*100;
			kareler[i+5][1]=50;
			
	
		}
	
		setSize(650,500);
	
		cap=20;;
		x=getWidth()/2-(cap/2);
		y=getHeight()-41;
		artisx=5;
		artisy=-5;
		hareketx=getWidth()/2-40;
		harekety=getHeight()-21;
		can=3;
		t= new Thread(this);
		addKeyListener(this);
		
	}
	
	
	@Override
	public void paint(Graphics g){
		super.paint(g);
	
		for(int i=0;i<karelerB.length;i++){
			if(karelerB[i]==false){
				bittimi=false;
				break;
			}
			bittimi=true;
		}
		
		
		g.drawString("Can = "+can, 5, 20);
			 	
		if(!bittimi && can>0){
		
		for(int i=0;i<kareler.length;i++){
			if(!karelerB[i])
			g.drawRect(kareler[i][0], kareler[i][1], 40, 20);

		}
		
		g.fillOval(x, y, cap,  cap);
		
		
		g.fillRect(hareketx, harekety, 80, 5);
		g.drawRect(hareketx, harekety, 80, 20);
	}else{
		t.stop();
		g.drawString("Oyun Bitti", getWidth()/2-30, getHeight()/2-10);
	}
	}

	@Override
	public void run() {
		
		while(true){
			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		
			
			
			
			if(calisiyormu){  //thread calıstıgında
				if(x>=hareketx&&x<=(hareketx+80)&&(y+30)>=getHeight()&&(y+30)<=(getHeight()+21)){  // hareketeden carptıgında
					artisx=(int)(Math.random()*10-5);
					artisy=-5;
					
				}
				
			
				y+=artisy;
				x+=artisx;
			
				
		for(int i=0;i<kareler.length;i++){
			if(x>=kareler[i][0]&&x<=(kareler[i][0]+40) && y>=kareler[i][1]&&y<=(kareler[i][1]+20)){
				if(!karelerB[i]){
				System.out.println("vurdu"+ y +" "+(kareler[i][1]+20)+"vurdu"+ x +" "+(kareler[i][0]+40));
				karelerB[i]=true;
				artisy =5;
		    	artisx = -artisx;
			}
			}
			
		}		
			
	
		
		if(y>=0&&y<=getHeight()-20-artisy){ //e sorun var
			
			
		
		}else if(y>=getHeight()-20-artisy){ //asagı indigind
			System.out.println("oyun bitti");
			calisiyormu=false;
		x=getWidth()/2-(cap/2);
			y=getHeight()-41;
			artisx=5;
			artisy=-5;
			repaint();
			yukari=false;
			can-=1;
		}
		else{
			if(y<=0 && artisy<=0){
			  y=0;
			  artisy =5;
			}else{
			artisy = (int)(Math.random()*10-5);
			artisy=-artisy;
			System.out.println(artisy);
			}
		}
		
		if(x>=0&&x<=getWidth()-20-artisx){ //köşeler carptıgında

		}else{
		
			artisx=-artisx;
			System.out.println("x"+artisx);
		}
		
		
		
		
		
		
		System.out.println("çalışıyor");
		
			}else{  //thread durdugunda
				
				System.out.println("durdu");
		
				x=getWidth()/2-(cap/2);
				y=getHeight()-41;
				hareketx=getWidth()/2-40;
				harekety=getHeight()-21;
				
				
			}
			repaint();
		}
		
	}


	@Override
	public void keyPressed(KeyEvent arg0) {
		if(arg0.getKeyCode() ==KeyEvent.VK_UP){
			if(!yukari){
				yukari=true;
	    if(!t.isAlive()){
	    	artisy =-5;
	    	artisx = (int)(Math.random()*10-5);
	    	t.start();
	    }  else{
	    	calisiyormu=true;
	    	artisx=(int)(Math.random()*10-5);
			artisy=-5;
	    }	
		}
		}
		if(arg0.getKeyCode() == KeyEvent.VK_RIGHT){
			if((hareketx-65)<=getHeight()){
				if(!yukari)
				x+=10;
			hareketx+=10;
			repaint();
			}
		}
		if(arg0.getKeyCode() == KeyEvent.VK_LEFT){
			if((hareketx-5)>=0){
				if(!yukari)
				x-=10;
				hareketx-=10;
				repaint();
		}}
		
	}


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


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

Ekran Görüntüsü;

 

 

Yorumlar

Bu gönderi için yorum yapılmadı.