-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpaceGameView
249 lines (180 loc) · 7.36 KB
/
SpaceGameView
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
243
244
245
246
247
248
249
package android.example.spaceinvproject;
import android.content.Context; //gives android environment info - allows use of android sys functionality
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas; //canvas, colour and paint allow to draw to the screen
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF; //easy collision detection for game objs
import android.util.Log; //error detection in thread
import android.view.MotionEvent;
import android.view.SurfaceHolder; //surface for drawing
import android.view.SurfaceView; //thread use and implementation of a runnable interface
//import android.content.res.AssetFileDescriptor;
//import android.content.res.AssetManager;
//import android.media.AudioManager;
//import android.media.SoundPool;
import java.io.IOException; //error handling
//runnable interface, need to implement run method - 'runs' the game
public class SpaceGameView extends SurfaceView implements Runnable {
private Context context;
private Thread gameThread = null;
//thread
private SurfaceHolder ourHolder;
//surfaceHolder locks surface before graphics are drawn
private volatile boolean playing;
//boolean to be set/unset depending on whether the game is running or not
private boolean paused = true;
//game paused at start
private Canvas canvas;
private Paint paint;
//canvas and paint obj - drawing to UI thread would cause performance issues, Surace vies allows to draw on
//custom view from a separate thread it controls
private long fps;
//tracks fps
private long timeThisFrame;
//to calculate fps
private int screenX;
private int screenY;
//size of screen in pixels
private int score = 0;
//Score
private int lives = 3;
//lives
private Spaceship spaceShip;
private Bullet bullet;
public SpaceGameView(Context context, int x, int y) { //constructor
super(context); //globally available superclass attributes which can be used in other methods
this.context = context;
ourHolder = getHolder(); //ourHolder = drawing surface
paint = new Paint();
screenX = x;
screenY = y;
initLevel(); //calls initLevel to initialise game screen
}
private void initLevel() { //method used to initialise all game objs when new game created
bullet = new Bullet(screenY, screenX);
spaceShip = new Spaceship(context, screenX, screenY, bullet);
}
@Override
public void run() {
while (playing) {
//score = 0;
//checks the time, does updat and draws (1 time cycle)
//then calculates elapsed time and assigns to fps
long startTime = System.currentTimeMillis();
//updates frame
if (!paused) {
update();
}
//draws frame
draw();
long timeThis = System.currentTimeMillis() - startTime;
if (timeThis >= 1) {
fps = 1000 / timeThis;
}
}
}
private void update() {
//will call update methods of each obj that would change in a time cycle and then check for collisions
spaceShip.update(fps);
if(bullet.getStatus())
bullet.update(fps);
//checkCollisions();
}
private void draw() {
if (ourHolder.getSurface().isValid()) {
canvas = ourHolder.lockCanvas(); //lock canvas for drawing
//draw background colour and text on screen
//canvas.drawColor(Color.argb(255, 26, 128, 182));
canvas.drawBitmap(spaceShip.getBackgroundBitmap(), 0, 0, paint);
paint.setColor(Color.argb(255, 249, 129, 0));
paint.setTextSize(40);
canvas.drawText("Score: " + score + " Lives: " +
lives, 10, 50, paint);
canvas.drawBitmap(spaceShip.currentBitmap, spaceShip.getX(), spaceShip.getY() - 50, paint);
if(bullet.getStatus()) //draws bullet based on rectangular positioning
canvas.drawRect(bullet.getRect(), paint);
//draw the game objects
ourHolder.unlockCanvasAndPost(canvas); //unlock the surface and show on screen
}
}
public void pause() {
//if SpaceGameActivity app activity is paused - shutdown thread
playing = false;
try {
gameThread.join();
} catch (InterruptedException e) {
Log.e("Error:", "joining thread");
}
}
public void resume() {
//if app activity is started then start thread
playing = true;
gameThread = new Thread(this);
gameThread.start();
}
//allows the app to detect screen touch inputs
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
//motion detected
case MotionEvent.ACTION_DOWN:
//spaceShip.fireBullet(bullet);
//break;
/*spaceShip.fireBullet(bullet.DOWN);
break;
spaceShip.fireBullet(bullet.LEFT);
break;
spaceShip.fireBullet(bullet.RIGHT);
break;
return true;*/
paused = false;
//set the spaceship movement to the direction pressed
//cross through the screen
if (motionEvent.getX() / screenX > motionEvent.getY() / screenY) {
if (1 - motionEvent.getX() / screenX > motionEvent.getY() / screenY) {
spaceShip.setMovementState(spaceShip.UP);
} else {
spaceShip.setMovementState(spaceShip.RIGHT);
}
} else {
if (1 - motionEvent.getX() / screenX > motionEvent.getY() / screenY) {
spaceShip.setMovementState(spaceShip.LEFT);
}else{
spaceShip.setMovementState(spaceShip.DOWN);
}
}
if(spaceShip.fireBullet()) {
//only play sound when bullet fired
// soundPool.play(shootID, 1, 1, 0, 0, 1);
}
break;
//for when player removes finger from screen
case MotionEvent.ACTION_UP:
//if(motionEvent.getY()>screenY - screenY /10){
spaceShip.setMovementState(spaceShip.STOPPED);
break;
}
return true;
}
private void checkCollisions(){ //sets bullet to inactive if no longer present on screen
if(bullet.getImpactPointY()<0)
bullet.setInactive();
if(bullet.getImpactPointY()>screenY)
bullet.setInactive();
if(bullet.getImpactPointX()<0)
bullet.setInactive();
if(bullet.getImpactPointX()>screenX)
bullet.setInactive();
//if(spaceShip.getX()>screenX - spaceShip.getLength())
// spaceShip.setX(0);
//if(spaceShip.getX()<0+spaceShip.getLength())
// spaceShip.setX(screenX);
//
// if(spaceShip.getY()>screenY - getLength())
// spaceShip.setY(0);
// if(spaceShip.getY()<0+spaceShip.getLength())
// spaceShip.setY(screenY);
}
} // end class