Advertisement
xerpi

PongDroid

Sep 14th, 2011
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.25 KB | None | 0 0
  1. package com.pongdroid;
  2.  
  3. import java.util.Random;
  4.  
  5. import android.content.Context;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.graphics.Canvas;
  9. import android.graphics.Color;
  10. import android.graphics.Paint;
  11. import android.view.MotionEvent;
  12. import android.view.SurfaceHolder;
  13. import android.view.SurfaceView;
  14. import android.view.View;
  15. import android.view.SurfaceHolder.Callback;
  16. import android.view.View.OnTouchListener;
  17.  
  18. public class GameView extends SurfaceView implements OnTouchListener{
  19.             //Own methods
  20.                     public void print(Canvas canvas,Paint paint,float x, float y, String text,int size, int color){
  21.                         paint.setColor(color);
  22.                         paint.setTextSize(size);
  23.                         canvas.drawText(text,x,y,paint);
  24.                         paint.setColor(Color.BLACK);
  25.                     }
  26.                     public void clearScreen(Canvas canvas){
  27.                         canvas.drawColor(Color.BLACK);
  28.                     }
  29.                     public void drawRect(double x , double y, double x2, double y2, int color,Canvas canvas, Paint paint){
  30.                         paint.setColor(color);
  31.                         canvas.drawRect((float) x,(float) y,(float) x2,(float) y2,  paint);
  32.                     }
  33.                     public void drawLine(double x , double y, double x2, double y2, int color,Canvas canvas, Paint paint){
  34.                         paint.setColor(color);
  35.                         canvas.drawLine((float) x,(float) y,(float) x2,(float) y2,paint);
  36.                     }
  37.                    
  38.                     public boolean checkCollision(float x,float y,float w,float h,float x2,float y2,float w2,float h2){
  39.                         if (x +w >= x2 && x <= x2+w2 && y +h >= y2 && y <= y2+h2 ){
  40.                             return true;
  41.                         }
  42.                         return false;
  43.                     }
  44.         //Own classes
  45.             public class Ball{
  46.                 public Random random = new Random();
  47.                 public double ang,x,y,incX,incY,vel=8;
  48.                 public Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
  49.                 public int w = img.getWidth();
  50.                 public int h = img.getHeight();
  51.                 public double inc = 0.001;
  52.                 public void setRandomAngle(){
  53.                     this.ang = this.random.nextDouble()*Math.PI*2;
  54.                 }
  55.                 public void calcIncVars(){
  56.                     this.incX = Math.cos(this.ang);
  57.                     this.incY = Math.sin(this.ang);
  58.                 }
  59.                 public void serve(){
  60.                     this.x = 240-this.img.getWidth();
  61.                     this.y = 854/2-this.img.getHeight();
  62.                     this.setRandomAngle();
  63.                     this.calcIncVars();
  64.                 }
  65.                 public void move(){
  66.                     checkGoalCollisions();
  67.                     checkScreenCollisions();
  68.                     this.vel = (float) (this.vel + this.inc);
  69.                     this.x = this.x +this.incX*this.vel;
  70.                     this.y = this.y +this.incY*this.vel;
  71.                 }
  72.                 public void checkGoalCollisions(){
  73.                     if (checkCollision((float) ball.x,(float) ball.y,ball.w,ball.h,(float) goal1.x,(float) goal1.y,(float) goal1.w-goal1.x,(float) goal1.h-goal1.y)){
  74.                         racket2.score++;
  75.                         ball.serve();
  76.                     }else if (checkCollision((float) ball.x,(float) ball.y,ball.w,ball.h,(float) goal2.x,(float) goal2.y,(float) goal2.w-goal2.x,(float) goal2.h-goal2.y)){
  77.                         racket1.score++;
  78.                         ball.serve();
  79.                     }
  80.                 }
  81.                
  82.                 public void checkScreenCollisions(){
  83.                     if (this.x + this.w > limit.w){
  84.                         this.incX = -this.incX;
  85.                         this.x = limit.w-this.w;
  86.                     }
  87.                     if (this.x <limit.x){
  88.                         this.incX = -this.incX;
  89.                         this.x = limit.x;                      
  90.                     }
  91.                     if (this.y + this.h > limit.h){
  92.                         this.incY = -this.incY;
  93.                         this.y = limit.h-this.h;
  94.                     }
  95.                     if (this.y <limit.y){
  96.                         this.incY = -this.incY;
  97.                         this.y = limit.y;                      
  98.                     }
  99.                     //Check collision with racket 1
  100.                         if (checkCollision((float) this.x,(float) this.y,(float) this.w,(float) this.h,(float) racket1.x,(float) racket1.y,(float) racket1.w,(float) racket1.h)){
  101.                             this.incY = -this.incY;
  102.                             this.y = racket1.y+racket1.h+1;
  103.                         }
  104.                     //Check collision with racket 1
  105.                         if (checkCollision((float) this.x,(float) this.y,(float) this.w,(float) this.h,(float) racket2.x,(float) racket2.y,(float) racket2.w,(float) racket2.h)){
  106.                             this.incY = -this.incY;
  107.                             this.y = racket2.y-this.h-1;
  108.                         }
  109.                 }
  110.                 public void blit(Canvas canvas,Paint mPaint){
  111.                     canvas.drawBitmap(this.img, (float) this.x,(float) this.y,mPaint);
  112.                 }
  113.             }
  114.            
  115.             public class Limit{
  116.                 public int x = 30;
  117.                 public int y = 30;
  118.                 public int w = 450;
  119.                 public int h = 824;
  120.             }
  121.        
  122.             public class Racket{
  123.                 public double x,y;
  124.                 public int score = 0;
  125.                 public Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.racket);
  126.                 public int w = img.getWidth();
  127.                 public int h = img.getHeight();
  128.                 public double ballAng;
  129.                 public String who;
  130.                
  131.                 public void setWho(String who){
  132.                     this.who = who;
  133.                 }
  134.                 public String getWho(){
  135.                     return this.who;
  136.                 }
  137.                 public void setY(double y){
  138.                     this.y = y;
  139.                 }
  140.                 public void resetX(){
  141.                     this.x = 240-this.w/2;
  142.                 }
  143.                 public void move(){
  144.                     if (this.who == "cpu"){
  145.                         this.x = ball.x +ball.w/2-this.w/2;
  146.                     }else if(this.who == "human"){
  147.                        
  148.                     }      
  149.                 }      
  150.                 public void checkScreenCollisions(){
  151.                     if (this.x + this.w > limit.w){
  152.                         this.x = limit.w-this.w;
  153.                     }
  154.                     if (this.x <limit.x){
  155.                         this.x = limit.x;                      
  156.                     }
  157.                 }
  158.                 public void blit(Canvas canvas,Paint mPaint){
  159.                     canvas.drawBitmap(this.img, (float) this.x,(float) this.y,mPaint);
  160.                 }
  161.             }  
  162.  
  163.             public class Goal{
  164.                 float x,y,w,h;
  165.                 int color = Color.WHITE;
  166.                 public void blit(Canvas canvas,Paint paint){
  167.                     paint.setColor(this.color);
  168.                     canvas.drawRect(this.x, this.y,this.w,this.h,paint);
  169.                 }
  170.                 public void setColor(int color){
  171.                     this.color = color;
  172.                 }
  173.                 public void setVertex(float x, float y, float w, float h){
  174.                     this.x = x;
  175.                     this.y = y;
  176.                     this.w = w;
  177.                     this.h = h;
  178.                 }      
  179.             }
  180.                    
  181.     public Goal goal1 = new Goal();
  182.     public Goal goal2 = new Goal();
  183.     public Limit limit = new Limit();
  184.     public Racket racket1 = new Racket();  
  185.     public Racket racket2 = new Racket();
  186.     public Ball ball = new Ball();
  187.     private GameLoopThread gameLoopThread; 
  188.     private SurfaceHolder holder;
  189.     private Paint mPaint = new Paint();
  190.     public GameView(Context context) {
  191.         super(context);
  192.         gameLoopThread = new GameLoopThread(this);     
  193.         holder = getHolder();
  194.         holder.addCallback(new Callback() {
  195.             @Override
  196.             public void surfaceDestroyed(SurfaceHolder holder) {
  197.                 gameLoopThread.setRunning(false);
  198.                 gameLoopThread.stop();         
  199.             }  
  200.             @Override
  201.             public void surfaceCreated(SurfaceHolder holder) {
  202.                 gameLoopThread.setRunning(true);
  203.                 gameLoopThread.start();
  204.                 ball.serve();
  205.                 goal1.setVertex(140,0,340,30);
  206.                     goal1.setColor(Color.GREEN);
  207.                 goal2.setVertex(140,824,340,854);
  208.                     goal2.setColor(Color.GREEN);
  209.                 racket1.setY(30);
  210.                 racket1.setWho("human");
  211.                 racket1.resetX();
  212.                 racket2.setY(getHeight()-racket2.h*2);
  213.                 racket2.setWho("human");
  214.                 racket2.resetX();
  215.                
  216.             }  
  217.             @Override
  218.             public void surfaceChanged(SurfaceHolder holder, int format, int width,
  219.                     int height) {  
  220.             }
  221.         });
  222.     }
  223.     public void onDraw(Canvas canvas){
  224.         canvas.save();
  225.         this.setOnTouchListener(this);
  226.         clearScreen(canvas);
  227.         drawRect(30,30, 450,824, Color.WHITE,canvas, mPaint);
  228.         drawLine(30,854/2, 450,854/2, Color.BLACK,canvas, mPaint);
  229.         goal1.blit(canvas,mPaint);
  230.         goal2.blit(canvas,mPaint);
  231.         ball.blit(canvas, mPaint);
  232.         racket1.blit(canvas, mPaint);
  233.         racket2.blit(canvas, mPaint);
  234.         racket1.move();
  235.         racket2.move();
  236.         ball.move();    
  237.         mPaint.setColor(Color.RED);
  238.         mPaint.setTextSize(30);
  239.         canvas.rotate(90,240,427);
  240.         canvas.drawText("Score: "+racket1.score,-50,212,mPaint);
  241.         canvas.rotate(180,240,427);
  242.         canvas.drawText("Score: "+racket2.score,-50,212,mPaint);
  243. //      print(canvas,mPaint,5,15,(float)ball.vel+"",20, Color.GREEN);
  244.         canvas.restore();
  245.     }
  246.     public boolean onTouch(View v,MotionEvent event){      
  247.         for (int i = 0; i < event.getPointerCount();i++){
  248.             if ( checkCollision( (float) event.getX(i),(float) event.getY(i),0,0,  0, 0,480,90 ) ){
  249.                 if (racket1.who == "human"){
  250.                     racket1.x = (float) event.getX(i)-racket1.w/2;
  251.                     racket1.checkScreenCollisions();
  252.                 }
  253.             }else if ( checkCollision( (float) event.getX(i),(float) event.getY(i),0,0,  0, 764,480,90 ) ){
  254.                 if (racket2.who == "human"){
  255.                     racket2.x = (float) event.getX(i)-racket2.w/2;
  256.                     racket2.checkScreenCollisions();
  257.                 }
  258.             }  
  259.         }
  260.         return true;
  261.     }
  262.  
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement