-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathSubHunter.java
More file actions
146 lines (123 loc) · 3.86 KB
/
SubHunter.java
File metadata and controls
146 lines (123 loc) · 3.86 KB
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
// This is our package
// If you are copy & pasting the code then this line will probably be different to yours
// If so, keep YOUR line- not this one
package com.gamecodeschool.c3subhunter;
// These are all the classes of other people's
// (Android) code that we use in Sub Hunt
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.util.Log;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.Display;
import android.widget.ImageView;
import java.util.Random;
public class SubHunter extends Activity {
// These variables can be "seen"
// throughout the SubHunter class
int numberHorizontalPixels;
int numberVerticalPixels;
int blockSize;
int gridWidth = 40;
int gridHeight;
float horizontalTouched = -100;
float verticalTouched = -100;
int subHorizontalPosition;
int subVerticalPosition;
boolean hit = false;
int shotsTaken;
int distanceFromSub;
boolean debugging = true;
/*
Android runs this code just before
the app is seen by the player.
This makes it a good place to add
the code that is needed for
the one-time setup.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the current device's screen resolution
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
// Initialize our size based variables based on the screen resolution
numberHorizontalPixels = size.x;
numberVerticalPixels = size.y;
blockSize = numberHorizontalPixels / gridWidth;
gridHeight = numberVerticalPixels / blockSize;
Log.d("Debugging", "In onCreate");
newGame();
draw();
}
/*
This code will execute when a new
game needs to be started. It will
happen when the app is first started
and after the player wins a game.
*/
void newGame(){
Log.d("Debugging", "In newGame");
}
/*
Here we will do all the drawing.
The grid lines, the HUD and
the touch indicator
*/
void draw() {
Log.d("Debugging", "In draw");
printDebuggingText();
}
/*
This part of the code will
handle detecting that the player
has tapped the screen
*/
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
Log.d("Debugging", "In onTouchEvent");
takeShot();
return true;
}
/*
The code here will execute when
the player taps the screen It will
calculate distance from the sub'
and determine a hit or miss
*/
void takeShot(){
Log.d("Debugging", "In takeShot");
draw();
}
// This code says "BOOM!"
void boom(){
}
// This code prints the debugging text
public void printDebuggingText(){
Log.d("numberHorizontalPixels",
"" + numberHorizontalPixels);
Log.d("numberVerticalPixels",
"" + numberVerticalPixels);
Log.d("blockSize", "" + blockSize);
Log.d("gridWidth", "" + gridWidth);
Log.d("gridHeight", "" + gridHeight);
//Log.d("horizontalTouched",
//"" + horizontalTouched);
Log.d("verticalTouched",
"" + verticalTouched);
Log.d("subHorizontalPosition",
"" + subHorizontalPosition);
Log.d("subVerticalPosition",
"" + subVerticalPosition);
Log.d("hit", "" + hit);
Log.d("shotsTaken", "" + shotsTaken);
Log.d("debugging", "" + debugging);
Log.d("distanceFromSub",
"" + distanceFromSub);
}
}