Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.xerpi.core;
- import java.util.LinkedList;
- import java.util.ListIterator;
- import java.util.Random;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- public class Balls{
- Random random = new Random();
- public float screenW = 854;
- public float screenH = 480;
- LinkedList <Ball> balls = new LinkedList<Ball>();
- public int deleted;
- public boolean checkCollision(float x, float y, float w, float h, float x2,
- float y2, float w2, float h2) {
- if (x + w >= x2 && x <= x2 + w2 && y + h >= y2 && y <= y2 + h2) {
- return true;
- }
- return false;
- }
- public class Ball {
- public int type;
- public float x,y,inc_x,inc_y,w,h,speed,angle;
- public Bitmap image;
- boolean dead;
- public Ball(int type, Bitmap img){
- this.type = type;
- this.image = img;
- this.speed = 2;
- int num = random.nextInt(4);
- switch (num) {
- case 0:
- this.x = -this.image.getWidth();
- this.y = random.nextInt((int) screenH+this.image.getHeight()*2)-this.image.getHeight();
- break;
- case 1:
- this.x = random.nextInt((int) screenW+this.image.getWidth()*2)-this.image.getWidth();
- this.y = -this.image.getHeight();
- break;
- case 2:
- this.x = screenW+this.image.getWidth();
- this.y = random.nextInt((int) screenH+this.image.getHeight()*2)-this.image.getHeight();
- break;
- case 3:
- this.x = random.nextInt((int) screenW+this.image.getWidth()*2)-this.image.getWidth();
- this.y = screenH+this.image.getHeight();
- break;
- }
- this.angle = (float) (Math.atan2(screenW/2-this.x,screenH/2-this.y)-1.57);
- this.inc_x = (float) Math.cos(-this.angle);
- this.inc_y = (float) Math.sin(-this.angle);
- if(type == 0){
- // this.image = BitmapFactory.decodeFile("res/drawable-hdpi/ball1.png");
- }else if(type ==1){
- // this.image = BitmapFactory.decodeFile("res/drawable-hdpi/ball2.png");
- }
- }
- }
- public void add(int type, Bitmap img){
- this.balls.add(new Ball(type,img));
- }
- public void blit(Canvas canvas, Paint paint){
- for (int i = 0; i < this.balls.size(); i++){
- canvas.drawBitmap(this.balls.get(i-deleted).image,this.balls.get(i-deleted).x,this.balls.get(i-deleted).y,paint);
- }
- }
- public void move() {
- // for (int i = 0; i < this.balls.size(); i++) {
- // this.balls.get(i-deleted).y += this.balls.get(i-deleted).inc_y * this.balls.get(i-deleted).speed;
- // this.balls.get(i-deleted).x += this.balls.get(i-deleted).inc_x * this.balls.get(i-deleted).speed;
- // }
- for( ListIterator<Ball> a=balls.listIterator(); a.hasNext(); ) {
- Ball i = (Ball) a.next();
- 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)){
- a.remove();
- GameView.core.score++;
- }
- i.y += i.inc_y * i.speed;
- i.x += i.inc_x * i.speed;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement