/
GameView.java
242 lines (194 loc) · 7.42 KB
/
GameView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package com.lud.root.jetfighter;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.util.ArrayList;
import java.util.Arrays;
public class GameView extends SurfaceView implements Runnable{
volatile boolean playing;
private Thread gameThread = null;
//Adding the player to this class
private Player player;
//Objects used for drawing
private Paint paint;
private Canvas canvas;
private SurfaceHolder surfaceHolder;
//Add stars list
private ArrayList<Star> stars = new ArrayList<Star>();
//Adding Enemies
private Enemy[] enemies; // only one enemy to decrease the difficulty
private int enemyCount = 3; // Number Of Enemies
private Boom boom;
int screenX;
private boolean isGameOver;
int score;
int highScore[] = new int[6];
float distance[] = new float[enemyCount];
public SharedPreferences sharedPreferences;
static MediaPlayer gameOnSound;
final MediaPlayer gameOverSound;
//Context to be used in onTouchEvent on GameOver Screen , for transition from
//GameOver Screen to Main Activity
Context context;
public GameView(Context context, int screenX, int screenY) {
super(context);
player = new Player(context, screenX, screenY);
this.context = context;
//initialize drawing objects
surfaceHolder = getHolder();
paint = new Paint();
int starNums = 50;
for (int i = 0; i < starNums; i++){
Star s = new Star(screenX,screenY);
stars.add(s);
}
enemies = new Enemy[enemyCount];
enemies[0] = new Enemy(context, screenX, screenY); // This needs to be created so that the next enemies
// created can be kept apart and not overlapping
for (int i=1; i<enemyCount; i++)
enemies[i] = new Enemy(context, screenX, screenY, enemies[i-1].getY(),enemies[i-1].getRadius());
boom = new Boom(context);
this.screenX = screenX;
isGameOver = false;
score = 0;
sharedPreferences = context.getSharedPreferences("Scores", Context.MODE_PRIVATE);
//initialize the array of high scores
highScore[0] = sharedPreferences.getInt("score1",0);
highScore[1] = sharedPreferences.getInt("score2",0);
highScore[2] = sharedPreferences.getInt("score3",0);
highScore[3] = sharedPreferences.getInt("score4",0);
highScore[4] = sharedPreferences.getInt("score5",0);
gameOnSound = MediaPlayer.create(context,R.raw.gameon);
gameOverSound = MediaPlayer.create(context,R.raw.gameover);
gameOnSound.setLooping(true);
gameOnSound.start();
}
@Override
public void run() {
while (playing){
update();
draw();
control();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_UP:
player.stopSlowing();
break;
case MotionEvent.ACTION_DOWN:
player.startSlowing();
break;
}
if(isGameOver){
if(event.getAction() == MotionEvent.ACTION_DOWN)
context.startActivity(new Intent(context,MyActivity.class));
}
return true;
}
private void update() {
//score based on the time passed
score++;
player.update();
boom.setX(-250);
boom.setY(-250);
for(Star s : stars)
s.update();
// Below Code is for multiple enemy
for(int i=0; i < enemyCount; i++){
enemies[i].update((int) player.getSpeed() + 10 );
distance[i] = (float) Math.sqrt((player.getX()-enemies[i].getX())*(player.getX()-enemies[i].getX()) + (player.getY()-enemies[i].getY())*(player.getY()-enemies[i].getY()));
if(distance[i] < (30 + enemies[i].getRadius())){
boom.setX(player.getX());
boom.setY(player.getY());
player.setX(-200);
isGameOver = true;
playing = false;
gameOnSound.stop();
gameOverSound.start();
highScore[5] = score;
Arrays.sort(highScore);
Log.d("score","Score : "+score+"\nHighScores : "+highScore[0]+"\n"+highScore[1]+"\n"+highScore[2]+"\n"+highScore[3]+"\n"+highScore[4]+"\n"+highScore[5]);
SharedPreferences pref;
pref = context.getSharedPreferences("Scores", Context.MODE_PRIVATE);
SharedPreferences.Editor e = pref.edit();
for(int j=0;j<5;j++){
e.putInt("score"+(j+1),highScore[5-j]);
e.apply();
Log.d("score","score"+(j+1)+"\nHighScores : "+highScore[5-j]);
}
}
}
}
private void draw() {
if(surfaceHolder.getSurface().isValid()){
//lock the canvas
canvas = surfaceHolder.lockCanvas();
canvas.drawColor(Color.BLACK);
//setting the paint color to white to draw the stars
paint.setColor(Color.WHITE);
//drawing all the stars
for (Star s : stars){
paint.setStrokeWidth(s.getStarWidth());
canvas.drawPoint(s.getX(), s.getY(), paint);
}
paint.setTextSize(30);
canvas.drawText("Score : "+score,100,50,paint);
paint.setColor(Color.CYAN);
for(int i = 0; i< enemyCount; i++)
canvas.drawCircle(enemies[i].getX(), enemies[i].getY(), enemies[i].getRadius(), paint);
canvas.drawBitmap(boom.getBitmap(),boom.getX(),boom.getY(),paint);
paint.setColor(Color.RED);
canvas.drawCircle(player.getX(), player.getY(), player.getRadius(), paint);
if(isGameOver){
paint.setTextSize(150);
paint.setTextAlign(Paint.Align.CENTER);
int yPos = (int)((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()))/2);
canvas.drawText("GAME OVER",canvas.getWidth()/2, yPos,paint);
}
//Unlocking the canvas
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
private void control() {
try {
gameThread.sleep(30); //creating the frame rate to around 33fps
canvas = null;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void pause(){
//pausing the game , set the variable to false
playing = false;
try {
gameThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void resume(){
playing = true;
gameThread = new Thread(this);
gameThread.start();
}
public static void stopMusic(){
gameOnSound.stop();
gameOnSound.release();
}
public static void pauseMusic(){
gameOnSound.pause();
}
public static void resumeMusic(){
gameOnSound.start();
}
}