Advertisement
xerpi

Balls.java

Oct 2nd, 2011
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. package com.xerpi.core;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.ListIterator;
  5. import java.util.Random;
  6.  
  7. import android.graphics.Bitmap;
  8. import android.graphics.Canvas;
  9. import android.graphics.Paint;
  10.  
  11. public class Balls{
  12.     Random random = new Random();
  13.     public float screenW = 854;
  14.     public float screenH = 480;
  15.     LinkedList <Ball> balls = new LinkedList<Ball>();
  16.     public int deleted;
  17.     public boolean checkCollision(float x, float y, float w, float h, float x2,
  18.             float y2, float w2, float h2) {
  19.         if (x + w >= x2 && x <= x2 + w2 && y + h >= y2 && y <= y2 + h2) {
  20.             return true;
  21.         }
  22.         return false;
  23.     }
  24.     public class Ball {
  25.         public int type;
  26.         public float x,y,inc_x,inc_y,w,h,speed,angle;
  27.         public Bitmap image;
  28.         boolean dead;
  29.        
  30.         public Ball(int type, Bitmap img){
  31.             this.type = type;
  32.             this.image = img;
  33.             this.speed = 2;
  34.             int num = random.nextInt(4);
  35.            
  36.             switch (num) {
  37.             case 0:
  38.                 this.x = -this.image.getWidth();
  39.                 this.y = random.nextInt((int) screenH+this.image.getHeight()*2)-this.image.getHeight();
  40.                 break;
  41.             case 1:
  42.                 this.x =  random.nextInt((int) screenW+this.image.getWidth()*2)-this.image.getWidth();
  43.                 this.y = -this.image.getHeight();
  44.                 break;
  45.             case 2:
  46.                 this.x = screenW+this.image.getWidth();
  47.                 this.y = random.nextInt((int) screenH+this.image.getHeight()*2)-this.image.getHeight();
  48.                 break;
  49.             case 3:
  50.                 this.x = random.nextInt((int) screenW+this.image.getWidth()*2)-this.image.getWidth();
  51.                 this.y = screenH+this.image.getHeight();
  52.                 break;
  53.             }
  54.             this.angle = (float) (Math.atan2(screenW/2-this.x,screenH/2-this.y)-1.57);
  55.             this.inc_x = (float) Math.cos(-this.angle);
  56.             this.inc_y = (float) Math.sin(-this.angle);
  57.            
  58.             if(type == 0){
  59.     //          this.image = BitmapFactory.decodeFile("res/drawable-hdpi/ball1.png");
  60.             }else if(type ==1){
  61.     //          this.image = BitmapFactory.decodeFile("res/drawable-hdpi/ball2.png");
  62.             }
  63.         }  
  64.     }
  65.    
  66.  
  67.     public void add(int type, Bitmap img){
  68.         this.balls.add(new Ball(type,img));
  69.     }
  70.    
  71.     public void blit(Canvas canvas, Paint paint){
  72.         for (int i = 0; i < this.balls.size(); i++){
  73.             canvas.drawBitmap(this.balls.get(i-deleted).image,this.balls.get(i-deleted).x,this.balls.get(i-deleted).y,paint);
  74.         }
  75.  
  76.     }
  77.    
  78.    
  79.    
  80.     public void move() {
  81. //      for (int i = 0; i < this.balls.size(); i++) {
  82. //          this.balls.get(i-deleted).y += this.balls.get(i-deleted).inc_y * this.balls.get(i-deleted).speed;
  83. //          this.balls.get(i-deleted).x += this.balls.get(i-deleted).inc_x * this.balls.get(i-deleted).speed;
  84. //      }  
  85.         for(  ListIterator<Ball> a=balls.listIterator(); a.hasNext(); ) {
  86.             Ball i = (Ball) a.next();
  87.            
  88.             if (checkCollision((float)i.x, (float)i.y,(float) i.image.getWidth(),(float)i.image.getHeight(),(float)screenW/2-10,(float)screenH/2-10,(float)20,(float)20)){
  89.                 a.remove();
  90.                 GameView.core.score++;
  91.  
  92.             }
  93.            
  94.             i.y += i.inc_y * i.speed;
  95.             i.x += i.inc_x * i.speed;
  96.         }
  97.     }
  98.    
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement