-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameScene.java
More file actions
152 lines (103 loc) · 4.14 KB
/
GameScene.java
File metadata and controls
152 lines (103 loc) · 4.14 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
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
public class GameScene extends Scene{
Rectangle bg, fg;
Graphics2D graphic2;
static Scene pausedScene;
static Snake snake;
static MusicPlayer gameMusic;
KListener kListener;
public Food food;
public static int score = 0;
public static String currentScore = "Your Score: ";
public static int snakeSize = 2;
public static int highScore;
public static boolean paused;
public GameScene(KListener kListener) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
Window.getWindow().setTitle(Constants.SCREEN_TITLE);
this.kListener = kListener;
bg = new Rectangle(0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);
fg = new Rectangle(24, 48, Constants.TILE_WIDTH*31, Constants.TILE_WIDTH*22);
snake = new Snake(2, 48, 48+24, 24, 24, fg);
gameMusic = new MusicPlayer("assets/2020-04-24_-_Arcade_Kid_-_FesliyanStudios.com_-_David_Renda.wav");
gameMusic.setVolume(0.5f);
food = new Food(fg, snake, 12, 12, Color.GREEN);
food.spawn();
highScore = ScoreWriter.readScores();
}
@Override
public void update(double deltaTime) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
if (kListener.isKeyPressed(KeyEvent.VK_UP) || kListener.isKeyPressed(KeyEvent.VK_W)){
snake.changeDirection(Directions.UP);
} else if(kListener.isKeyPressed(KeyEvent.VK_DOWN) || kListener.isKeyPressed(KeyEvent.VK_S)){
snake.changeDirection(Directions.DOWN);
}else if(kListener.isKeyPressed(KeyEvent.VK_LEFT) || kListener.isKeyPressed(KeyEvent.VK_A)){
snake.changeDirection(Directions.LEFT);
}else if(kListener.isKeyPressed(KeyEvent.VK_RIGHT) || kListener.isKeyPressed(KeyEvent.VK_D)){
snake.changeDirection(Directions.RIGHT);
} else if (kListener.isKeyPressed(KeyEvent.VK_ESCAPE)) {
paused = true;
gameMusic.pause();
pausedScene = Window.getWindow().getCurrentScene();
Window.getWindow().changeState(3);
}
if(!food.foodIsSpawned){
food.spawn();
}
snake.update(deltaTime);
food.update(deltaTime);
}
@Override
public void draw(Graphics graphic) {
graphic2 = (Graphics2D)graphic;
graphic2.setColor(Color.BLUE);
graphic2.fill(new Rectangle2D.Double(bg.x, bg.y, bg.width, bg.height));
graphic2.setColor(Color.BLACK);
graphic2.fill(new Rectangle2D.Double(fg.x, fg.y, fg.width, fg.height));
graphic.setFont(Constants.MY_FONT);
graphic.setColor(Color.WHITE);
graphic.drawString("HIGHSCORE: " + highScore,28,43);
snake.draw(graphic2);
food.draw(graphic2);
}
public static MusicPlayer getGameMusic() {
return gameMusic;
}
public static void count() throws UnsupportedAudioFileException, LineUnavailableException, IOException {
if (snakeSize <= 5) {
score += 5;
} else if (snakeSize <= 10) {
score += 10;
}else if (snakeSize <= 15) {
score += 12;
} else if (snakeSize <= 20) {
score += 20;
} else if (snakeSize <= 35) {
score += 30;
} else if (snakeSize > 36){
score += 50;
}
if(score == 106){
snake.setOgWaitBetweenUpdate(0.09);
} else if (score == 230) {
snake.setOgWaitBetweenUpdate(0.08);
} else if (score == 380) {
snake.setOgWaitBetweenUpdate(0.07);
}
snakeSize +=1;
Window.getWindow().setTitle( currentScore + score);
}
public static void setSnakeSize(int snakeSize) {
GameScene.snakeSize = snakeSize;
}
public static void setPaused(boolean paused) {
GameScene.paused = paused;
}
public static Scene getPausedScene() {
return pausedScene;
}
}