-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackground.java
More file actions
119 lines (107 loc) · 3.51 KB
/
Background.java
File metadata and controls
119 lines (107 loc) · 3.51 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
/*
* File: Background.java
* ------------------------------
* This class defines the background, and its related methods,
* particularly, adding other classes of objects to the canvas.
*/
import acm.graphics.GCanvas;
import acm.graphics.GImage;
import acm.util.RandomGenerator;
public class Background extends GCanvas implements Constants {
RandomGenerator rg = new RandomGenerator();
// The background will have an image to it.
private GImage img;
public void init() {
addBackground();
}
// Set the background.
private void addBackground() {
img = new GImage(CVS_FILENAME);
img.setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
add(img);
}
/*
* Method addFace puts the flying object onto the center of the screen.
*/
public void addFace(Face face) {
double x = this.getWidth() * 0.5 - face.getImg().getWidth() * 0.5;
double y = this.getHeight() * 0.5 - face.getImg().getHeight() * 0.5;
this.add(face.getImg(), x, y);
}
/*
* Method addBlockPairs puts a GCompound pair onto the screen. The position
* will vary, therefore a parameter is passed into this method.
*/
public Pairs addBlockPairs(double x) {
Pairs pairs = new Pairs();
this.add(pairs, x, 0);
return pairs;
}
/*
* Method addDisplay gets a certain type of Displays object and add them
* onto the screen.
*/
public Displays addDisplay(String str, int pts) {
switch (str) {
case "Title": {
Displays title = new Displays("Title", 0);
double x = this.getWidth() * 0.5 - title.getImg().getWidth() * 0.5;
this.add(title.getImg(), x, TITLE_HEIGHT);
return title;
}
case "ClickToStart": {
Displays clickToStart = new Displays("ClickToStart", 0);
double x = this.getWidth() * 0.5 - clickToStart.getImg().getWidth() * 0.5;
this.add(clickToStart.getImg(), x, CLICK_START_HEIGHT);
return clickToStart;
}
case "GameOver": {
Displays gameOver = new Displays("GameOver", 0);
double x = this.getWidth() * 0.5 - gameOver.getImg().getWidth() * 0.5;
this.add(gameOver.getImg(), x, GAMEOVER_HEIGHT);
return gameOver;
}
case "Score": {
Displays youVeScored = new Displays("Score", 0);
double x = this.getWidth() * 0.5 - youVeScored.getImg().getWidth() * 0.5;
this.add(youVeScored.getImg(), x, SCORELABEL_HEIGHT);
return youVeScored;
}
case "Points": {
Displays points = new Displays("Points", pts);
double x = this.getWidth() * 0.5 - points.getLbl().getWidth() * 0.5;
this.add(points.getLbl(), x, PTS_HEIGHT);
return points;
}
case "PointsOnScreen": {
Displays pointsOnScreen = new Displays("PointsOnScreen", pts);
double y = this.getHeight() * 0.5 + pointsOnScreen.getLbl().getAscent() * 0.5;
// The position is an initial one. It will be updated.
this.add(pointsOnScreen.getLbl(), POINTS_SCREEN_X, y);
return pointsOnScreen;
}
case "InstructionLabel": {
Displays instructionLabel = new Displays("InstructionLabel", 0);
double x = this.getWidth() * 0.5 - instructionLabel.getImg().getWidth() * 0.5;
this.add(instructionLabel.getImg(), x, INFO_LABEL_HEIGHT);
return instructionLabel;
}
case "Instructions": {
Displays instruction = new Displays("Instructions", 0);
this.add(instruction.getImg(), INFO_X, INFO_Y);
return instruction;
}
case "ClickToQuit": {
Displays clickToQuit = new Displays("ClickToQuit", 0);
double x = this.getWidth() * 0.5 - clickToQuit.getImg().getWidth() * 0.5;
this.add(clickToQuit.getImg(),x,CLICK_QUIT_HEIGHT);
return clickToQuit;
}
}
return null;
}
// Image getter.
public GImage getImg() {
return img;
}
}