Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.pongdroid;
- import java.util.Random;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.view.MotionEvent;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.View;
- import android.view.SurfaceHolder.Callback;
- import android.view.View.OnTouchListener;
- public class GameView extends SurfaceView implements OnTouchListener{
- //Own methods
- public void print(Canvas canvas,Paint paint,float x, float y, String text,int size, int color){
- paint.setColor(color);
- paint.setTextSize(size);
- canvas.drawText(text,x,y,paint);
- paint.setColor(Color.BLACK);
- }
- public void clearScreen(Canvas canvas){
- canvas.drawColor(Color.BLACK);
- }
- public void drawRect(double x , double y, double x2, double y2, int color,Canvas canvas, Paint paint){
- paint.setColor(color);
- canvas.drawRect((float) x,(float) y,(float) x2,(float) y2, paint);
- }
- public void drawLine(double x , double y, double x2, double y2, int color,Canvas canvas, Paint paint){
- paint.setColor(color);
- canvas.drawLine((float) x,(float) y,(float) x2,(float) y2,paint);
- }
- 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;
- }
- //Own classes
- public class Ball{
- public Random random = new Random();
- public double ang,x,y,incX,incY,vel=8;
- public Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
- public int w = img.getWidth();
- public int h = img.getHeight();
- public double inc = 0.001;
- public void setRandomAngle(){
- this.ang = this.random.nextDouble()*Math.PI*2;
- }
- public void calcIncVars(){
- this.incX = Math.cos(this.ang);
- this.incY = Math.sin(this.ang);
- }
- public void serve(){
- this.x = 240-this.img.getWidth();
- this.y = 854/2-this.img.getHeight();
- this.setRandomAngle();
- this.calcIncVars();
- }
- public void move(){
- checkGoalCollisions();
- checkScreenCollisions();
- this.vel = (float) (this.vel + this.inc);
- this.x = this.x +this.incX*this.vel;
- this.y = this.y +this.incY*this.vel;
- }
- public void checkGoalCollisions(){
- 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)){
- racket2.score++;
- ball.serve();
- }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)){
- racket1.score++;
- ball.serve();
- }
- }
- public void checkScreenCollisions(){
- if (this.x + this.w > limit.w){
- this.incX = -this.incX;
- this.x = limit.w-this.w;
- }
- if (this.x <limit.x){
- this.incX = -this.incX;
- this.x = limit.x;
- }
- if (this.y + this.h > limit.h){
- this.incY = -this.incY;
- this.y = limit.h-this.h;
- }
- if (this.y <limit.y){
- this.incY = -this.incY;
- this.y = limit.y;
- }
- //Check collision with racket 1
- 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)){
- this.incY = -this.incY;
- this.y = racket1.y+racket1.h+1;
- }
- //Check collision with racket 1
- 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)){
- this.incY = -this.incY;
- this.y = racket2.y-this.h-1;
- }
- }
- public void blit(Canvas canvas,Paint mPaint){
- canvas.drawBitmap(this.img, (float) this.x,(float) this.y,mPaint);
- }
- }
- public class Limit{
- public int x = 30;
- public int y = 30;
- public int w = 450;
- public int h = 824;
- }
- public class Racket{
- public double x,y;
- public int score = 0;
- public Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.racket);
- public int w = img.getWidth();
- public int h = img.getHeight();
- public double ballAng;
- public String who;
- public void setWho(String who){
- this.who = who;
- }
- public String getWho(){
- return this.who;
- }
- public void setY(double y){
- this.y = y;
- }
- public void resetX(){
- this.x = 240-this.w/2;
- }
- public void move(){
- if (this.who == "cpu"){
- this.x = ball.x +ball.w/2-this.w/2;
- }else if(this.who == "human"){
- }
- }
- public void checkScreenCollisions(){
- if (this.x + this.w > limit.w){
- this.x = limit.w-this.w;
- }
- if (this.x <limit.x){
- this.x = limit.x;
- }
- }
- public void blit(Canvas canvas,Paint mPaint){
- canvas.drawBitmap(this.img, (float) this.x,(float) this.y,mPaint);
- }
- }
- public class Goal{
- float x,y,w,h;
- int color = Color.WHITE;
- public void blit(Canvas canvas,Paint paint){
- paint.setColor(this.color);
- canvas.drawRect(this.x, this.y,this.w,this.h,paint);
- }
- public void setColor(int color){
- this.color = color;
- }
- public void setVertex(float x, float y, float w, float h){
- this.x = x;
- this.y = y;
- this.w = w;
- this.h = h;
- }
- }
- public Goal goal1 = new Goal();
- public Goal goal2 = new Goal();
- public Limit limit = new Limit();
- public Racket racket1 = new Racket();
- public Racket racket2 = new Racket();
- public Ball ball = new Ball();
- private GameLoopThread gameLoopThread;
- private SurfaceHolder holder;
- private Paint mPaint = new Paint();
- public GameView(Context context) {
- super(context);
- gameLoopThread = new GameLoopThread(this);
- holder = getHolder();
- holder.addCallback(new Callback() {
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- gameLoopThread.setRunning(false);
- gameLoopThread.stop();
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- gameLoopThread.setRunning(true);
- gameLoopThread.start();
- ball.serve();
- goal1.setVertex(140,0,340,30);
- goal1.setColor(Color.GREEN);
- goal2.setVertex(140,824,340,854);
- goal2.setColor(Color.GREEN);
- racket1.setY(30);
- racket1.setWho("human");
- racket1.resetX();
- racket2.setY(getHeight()-racket2.h*2);
- racket2.setWho("human");
- racket2.resetX();
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- }
- });
- }
- public void onDraw(Canvas canvas){
- canvas.save();
- this.setOnTouchListener(this);
- clearScreen(canvas);
- drawRect(30,30, 450,824, Color.WHITE,canvas, mPaint);
- drawLine(30,854/2, 450,854/2, Color.BLACK,canvas, mPaint);
- goal1.blit(canvas,mPaint);
- goal2.blit(canvas,mPaint);
- ball.blit(canvas, mPaint);
- racket1.blit(canvas, mPaint);
- racket2.blit(canvas, mPaint);
- racket1.move();
- racket2.move();
- ball.move();
- mPaint.setColor(Color.RED);
- mPaint.setTextSize(30);
- canvas.rotate(90,240,427);
- canvas.drawText("Score: "+racket1.score,-50,212,mPaint);
- canvas.rotate(180,240,427);
- canvas.drawText("Score: "+racket2.score,-50,212,mPaint);
- // print(canvas,mPaint,5,15,(float)ball.vel+"",20, Color.GREEN);
- canvas.restore();
- }
- public boolean onTouch(View v,MotionEvent event){
- for (int i = 0; i < event.getPointerCount();i++){
- if ( checkCollision( (float) event.getX(i),(float) event.getY(i),0,0, 0, 0,480,90 ) ){
- if (racket1.who == "human"){
- racket1.x = (float) event.getX(i)-racket1.w/2;
- racket1.checkScreenCollisions();
- }
- }else if ( checkCollision( (float) event.getX(i),(float) event.getY(i),0,0, 0, 764,480,90 ) ){
- if (racket2.who == "human"){
- racket2.x = (float) event.getX(i)-racket2.w/2;
- racket2.checkScreenCollisions();
- }
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement