-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameManager.java
More file actions
133 lines (129 loc) · 4.58 KB
/
Copy pathGameManager.java
File metadata and controls
133 lines (129 loc) · 4.58 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GameManager {
Game game;
Scores.ManipScores scores;
boolean gameStarted = false;
boolean selectionEnabled = true;
Timer timer;
Sounds.timerSound timerSound = new Sounds.timerSound();
int timeLeft;
int matches = 0;
int attempts = 0;
int score = 1000;
Card lastSelected;
public GameManager(Game game) {
this.game = game;
this.timeLeft = (game.nbcards+1) * 10;
}
private void initializeTimer() {
timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
setTimeLeft(timeLeft - 1);
score-= 5;
if(timeLeft < 10)
game.time.setForeground(Color.RED);
if(timeLeft == 0) {
timer.stop();
timerSound.stop();
Sounds.playGameOverSound();
game.endGame.setForeground(Color.RED);
game.showEndGameMessage("GAME OVER", "You Lost");
}
}
});
}
public void startGame(String player) {
initializeTimer();
scores = new Scores.ManipScores(player, game.mode);
game.myHighestScore.setText(String.valueOf(scores.myHighestScore));
game.highestScore.setText(String.valueOf(scores.highestScore));
lastSelected = null;
gameStarted = true;
timerSound.play();
timer.start();
game.startbtn.setText("new game");
}
public void endGame() {
gameStarted = false;
timer.stop();
timerSound.stop();
setTimeLeft((1+game.nbcards)*10);
setAttempts(0);
setMatches(0);
score = 1000;
game.time.setForeground(Color.BLACK);
game.startbtn.setText("start game");
}
public void selectCard(Card card) {
if(!card.isFlipped() && selectionEnabled) {
setAttempts(attempts + 1);
score-= 10;
card.setFlipped(true);
Sounds.playFlipSound();
if (lastSelected != null) {
if (card.equals(lastSelected)) {
Sounds.playMatchingCardSound();
setMatches(matches + 1);
lastSelected = null;
if(matches == game.nbcards*2) {
timer.stop();
timerSound.stop();
Sounds.playWinSound();
String msg1 = "YOU WON";
String msg2 = "score : " + score;
game.endGame.setForeground(Color.orange);
if(score > scores.myHighestScore) {
Sounds.playNewScoreSound();
scores.updateScores(scores.player + "," + score + "," + attempts + "," + timeLeft);
game.gf.scores.loadScores();
if (score > scores.highestScore)
msg2 = "New highest score : " + score;
else
msg2 = "Your new highest score : " + score;
}
game.showEndGameMessage(msg1, msg2);
}
} else {
selectionEnabled = false;
Timer timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
Sounds.playFlipSound();
card.setFlipped(false);
lastSelected.setFlipped(false);
lastSelected = null;
selectionEnabled = true;
}
});
timer.setRepeats(false);
timer.start();
}
} else {
lastSelected = card;
}
}
}
public void setMatches(int sc) {
this.matches = sc;
game.matches.setText(getMatches());
}
public void setAttempts(int n) {
this.attempts = n;
game.attempts.setText(getAttempts());
}
public void setTimeLeft(int t) {
this.timeLeft = t;
game.time.setText(getTimeLeft());
}
public String getMatches() {
return String.valueOf(matches);
}
public String getAttempts() {
return String.valueOf(attempts);
}
public String getTimeLeft() {
return String.valueOf(timeLeft);
}
}