-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWinScreen.java
More file actions
239 lines (205 loc) · 8.61 KB
/
WinScreen.java
File metadata and controls
239 lines (205 loc) · 8.61 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
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
package com.angrybirds;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
public class WinScreen implements Screen {
private SpriteBatch batch;
private Texture backgroundTexture;
private Texture levelsButtonTexture;
private Texture nextLevelButtonTexture;
private Stage stage;
private Main game;
private int currentLevel;
private int score;
private Music backgroundMusic;
private BitmapFont scoreFont;
private float scoreX;
private float scoreY;
// Constants for button dimensions and positioning
private static final float BUTTON_WIDTH = 150f;
private static final float BUTTON_HEIGHT = 150f;
private static final float BOTTOM_PADDING = 20f; // Space from bottom of screen
private static final float SIDE_PADDING = 20f; // Space from sides of screen
private static final int MAX_LEVELS = 3; // Maximum number of levels
public WinScreen(Main game, int completedLevel, int score) {
this.game = game;
this.currentLevel = completedLevel;
this.score = score;
batch = new SpriteBatch();
// Initialize font
scoreFont = new BitmapFont();
scoreFont.setColor(Color.WHITE);
scoreFont.getData().setScale(2f); // Adjust size as needed
}
// Method to set custom score display coordinates
public void setScoreDisplayCoordinates(float x, float y) {
this.scoreX = x;
this.scoreY = y;
}
@Override
public void show() {
// Choose background and music based on score
if (score > 2300) {
backgroundTexture = new Texture(Gdx.files.internal("victory_silver_screen.jpeg"));
backgroundMusic = Gdx.audio.newMusic(Gdx.files.internal("myinstants.mp3"));
// Default coordinates for gold screen
setScoreDisplayCoordinates(
Gdx.graphics.getWidth() * 0.45f,
Gdx.graphics.getHeight() * 0.7f
);
} else if (score > 2100) {
backgroundTexture = new Texture(Gdx.files.internal("victory_gold_screen.jpeg"));
backgroundMusic = Gdx.audio.newMusic(Gdx.files.internal("myinstants.mp3"));
// Default coordinates for silver screen
setScoreDisplayCoordinates(
Gdx.graphics.getWidth() * 0.45f,
Gdx.graphics.getHeight() * 0.7f
);
} else {
backgroundTexture = new Texture(Gdx.files.internal("victory_bronze_screen.jpeg"));
backgroundMusic = Gdx.audio.newMusic(Gdx.files.internal("myinstants.mp3"));
// Default coordinates for bronze screen
setScoreDisplayCoordinates(
Gdx.graphics.getWidth() * 0.45f,
Gdx.graphics.getHeight() * 0.7f
);
}
// Configure music playback
backgroundMusic.setLooping(false);
backgroundMusic.setVolume(0.5f);
backgroundMusic.play();
levelsButtonTexture = new Texture(Gdx.files.internal("backbuttonremoved.png"));
nextLevelButtonTexture = new Texture(Gdx.files.internal("nextbutton.png"));
stage = new Stage();
Gdx.input.setInputProcessor(stage);
// Levels button (bottom left)
ImageButton levelsButton = new ImageButton(new TextureRegionDrawable(levelsButtonTexture));
levelsButton.setSize(BUTTON_WIDTH, BUTTON_HEIGHT);
levelsButton.setPosition(SIDE_PADDING, BOTTOM_PADDING);
levelsButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
stopMusic();
System.out.println("LEVELS button clicked");
game.setScreen(new LevelSelectionBirds(game));
}
@Override
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
levelsButton.setSize(BUTTON_WIDTH - 10, BUTTON_HEIGHT - 10);
levelsButton.setPosition(levelsButton.getX() + 5, levelsButton.getY() + 5);
}
@Override
public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
levelsButton.setSize(BUTTON_WIDTH, BUTTON_HEIGHT);
levelsButton.setPosition(levelsButton.getX() - 5, levelsButton.getY() - 5);
}
});
ImageButton.ImageButtonStyle style = new ImageButton.ImageButtonStyle();
style.up = null;
style.down = null;
style.checked = null;
// Next Level button (centered)
ImageButton nextLevelButton = new ImageButton(style); // Remove the texture
nextLevelButton.setSize(BUTTON_WIDTH+50, BUTTON_HEIGHT+50);
nextLevelButton.setPosition(
(Gdx.graphics.getWidth() - BUTTON_WIDTH) / 2f,
BOTTOM_PADDING
);
nextLevelButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
stopMusic();
System.out.println("NEXT LEVEL button clicked");
// Navigate to the next level
Screen nextLevelScreen;
switch (currentLevel) {
case 1:
nextLevelScreen = new Level_2_Birds(game);
break;
case 2:
nextLevelScreen = new Level_3_birds(game);
break;
default:
nextLevelScreen = new Level_1_birds(game);
break;
}
game.setScreen(nextLevelScreen);
}
@Override
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
nextLevelButton.setSize(BUTTON_WIDTH - 10, BUTTON_HEIGHT - 10);
nextLevelButton.setPosition(
(Gdx.graphics.getWidth() - (BUTTON_WIDTH - 10)) / 2f,
BOTTOM_PADDING + 5
);
}
@Override
public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
nextLevelButton.setSize(BUTTON_WIDTH, BUTTON_HEIGHT);
nextLevelButton.setPosition(
(Gdx.graphics.getWidth() - BUTTON_WIDTH) / 2f,
BOTTOM_PADDING
);
}
});
stage.addActor(levelsButton);
stage.addActor(nextLevelButton);
}
// Method to stop music
private void stopMusic() {
if (backgroundMusic != null) {
backgroundMusic.stop();
backgroundMusic.dispose();
}
}
@Override
public void render(float delta) {
ScreenUtils.clear(0, 0, 0, 1);
batch.begin();
// Draw background
batch.draw(backgroundTexture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
// Draw score
scoreFont.draw(batch, "Score: " + score, scoreX, scoreY);
batch.end();
stage.act(delta);
stage.draw();
}
@Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
@Override
public void pause() {
stopMusic();
}
@Override
public void resume() {
}
@Override
public void hide() {
stopMusic();
dispose();
}
@Override
public void dispose() {
batch.dispose();
stopMusic(); // Ensure music is stopped and resources are freed
if (backgroundTexture != null) backgroundTexture.dispose();
if (levelsButtonTexture != null) levelsButtonTexture.dispose();
if (nextLevelButtonTexture != null) nextLevelButtonTexture.dispose();
if (scoreFont != null) scoreFont.dispose();
stage.dispose();
}
}