-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameScreen.java
More file actions
222 lines (210 loc) · 8.79 KB
/
GameScreen.java
File metadata and controls
222 lines (210 loc) · 8.79 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
package Maze;
//////////////////////////////////////////////
// CMSC 495 - Fall 2021 //
// Professor Mark Munoz //
//------------------------------------------//
// Maze.Maze Game //
// Written in Java: //
//------------------------------------------//
// Class: Maze.GameScreen.java //
// The class displays the game screen for //
// the maze game. //
//////////////////////////////////////////////
/*********************************************
* All imports Used *
*********************************************/
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
/*********************************************
* Enumeration for mouth state *
*********************************************/
enum Mouth {SMILE,CAUTIOUS,MAD}
/*********************************************
* The GameScreen class contains the *
* information for the display *
*********************************************/
public class GameScreen extends Canvas {
int BLOCK_SIZE = 50;
int xTotalDimensions;
int yTotalDimensions;
boolean mazeIsDrawn;
/**************************************************
* GameScreen constructor *
* @param blockSize *
* @param mItems *
**************************************************/
GameScreen ( int blockSize, MazeItems[][] mItems) {
this.BLOCK_SIZE = blockSize;
this.xTotalDimensions = BLOCK_SIZE * mItems.length;
this.yTotalDimensions = BLOCK_SIZE * mItems[0].length;
mazeIsDrawn = false;
Toolkit t=Toolkit.getDefaultToolkit();
}
/***************************************************
* The paint method has all of the graphics *
* information. This is ran whenever the screen *
* refreshes *
* @param g *
***************************************************/
public void paint(Graphics g) {
g.setColor(Color.GRAY);
g.drawRect(0, 0, xTotalDimensions, yTotalDimensions);
g.fillRect(0, 0, xTotalDimensions, yTotalDimensions);
try {
fillColumn(g, 0);
} catch (IOException e) {
e.printStackTrace();
}
mazeIsDrawn = true;
}
private void fillColumn (Graphics g, int column) throws IOException {
fillRow (g,column,0);
if (column < Maze.mazeLevelData.mazeGrid.length - 1) {
fillColumn(g,column + 1);
}
}
private void fillRow (Graphics g, int column, int row) throws IOException {
Toolkit t=Toolkit.getDefaultToolkit();
int i = column, j = row;
switch (Maze.mazeLevelData.mazeGrid[column][row]) {
case FLOOR: {
g.setColor(Color.BLACK);
g.drawRect((i * BLOCK_SIZE), (j * BLOCK_SIZE), BLOCK_SIZE, BLOCK_SIZE);g.fillRect((i * BLOCK_SIZE), (j * BLOCK_SIZE), BLOCK_SIZE, BLOCK_SIZE);
break;
}
case EXIT: {
g.setColor(Color.GREEN);
g.drawRect((i * BLOCK_SIZE), (j * BLOCK_SIZE), BLOCK_SIZE, BLOCK_SIZE);
g.fillRect((i * BLOCK_SIZE), (j * BLOCK_SIZE), BLOCK_SIZE, BLOCK_SIZE);
g.drawImage(t.getImage("Exit.png"),(i * BLOCK_SIZE), (j * BLOCK_SIZE),this);
break;
}
case PLAYER_POSITION:
case PLAYER_POSITION_UP: {
drawCharacter(g,i,j, Color.BLUE, Directions.UP,Mouth.CAUTIOUS);
break;
}
case PLAYER_POSITION_DOWN: {
drawCharacter(g,i,j, Color.BLUE, Directions.DOWN,Mouth.CAUTIOUS);
break;
}
case PLAYER_POSITION_LEFT: {
drawCharacter(g,i,j, Color.BLUE, Directions.LEFT,Mouth.CAUTIOUS);
break;
}
case PLAYER_POSITION_RIGHT: {
drawCharacter(g,i,j, Color.BLUE, Directions.RIGHT,Mouth.CAUTIOUS);
break;
}
case ENEMY_POSITION_UP: {
drawCharacter(g,i,j, Color.RED, Directions.UP,Mouth.MAD);
break;
}
case ENEMY_POSITION:
case ENEMY_POSITION_DOWN: {
drawCharacter(g,i,j, Color.RED, Directions.DOWN,Mouth.MAD);
break;
}
case ENEMY_POSITION_LEFT: {
drawCharacter(g,i,j, Color.RED, Directions.LEFT,Mouth.MAD);
break;
}
case ENEMY_POSITION_RIGHT: {
drawCharacter(g,i,j, Color.RED, Directions.RIGHT,Mouth.MAD);
break;
}
case PLAYER_WIN: {
drawCharacter(g,i,j,Color.BLUE, Directions.UP,Mouth.SMILE);
break;
}
case PLAYER_DEAD: {
drawCharacter(g,i,j,Color.GRAY, Directions.UP,Mouth.MAD);
break;
}
}
if (row < Maze.mazeLevelData.mazeGrid[column].length - 1) {
fillRow(g,column,row + 1);
}
}
private void drawCharacter (Graphics g, int i, int j, Color characterColor, Directions directionFacing, Mouth mouthMode) {
Color mouthTop,mouthBottom;
if (mouthMode == Mouth.CAUTIOUS) {
mouthTop = Color.BLACK;
mouthBottom = characterColor;// This will not be used below. just to fix an initialization
}
else if (mouthMode == Mouth.SMILE) {
mouthTop = characterColor;
mouthBottom = Color.BLACK;
}
else {
mouthBottom = characterColor;
mouthTop = Color.BLACK;
}
// Background Color and Square
g.setColor(Color.BLACK);
g.fillRect((i * BLOCK_SIZE), (j * BLOCK_SIZE), BLOCK_SIZE, BLOCK_SIZE);
// CharacterColor
g.setColor(characterColor);
g.fillOval((i * BLOCK_SIZE), (j * BLOCK_SIZE), BLOCK_SIZE, BLOCK_SIZE);
// Eye justification
int leftRightJustifierConstant;
switch (directionFacing) {
case UP:
case DOWN: {
leftRightJustifierConstant = BLOCK_SIZE / 10;
break;
}
case LEFT: {
leftRightJustifierConstant = 0;
break;
}
default: {
leftRightJustifierConstant = (BLOCK_SIZE / 10) * 2;
break;
}
}
g.setColor(Color.WHITE);
g.fillOval(((i * BLOCK_SIZE) + ((BLOCK_SIZE / 10) * 2)) , (j * BLOCK_SIZE + BLOCK_SIZE/5), (BLOCK_SIZE/ 10) * 3, (BLOCK_SIZE / 10) * 3);
g.fillOval(((i * BLOCK_SIZE) + ((BLOCK_SIZE / 10) * 5)) , (j * BLOCK_SIZE + BLOCK_SIZE/5), (BLOCK_SIZE/ 10) * 3, (BLOCK_SIZE / 10) * 3);
g.setColor(Color.BLACK);
g.fillOval(((i * BLOCK_SIZE) + ((BLOCK_SIZE / 10) * 2)+(leftRightJustifierConstant)), (j * BLOCK_SIZE + BLOCK_SIZE/3), BLOCK_SIZE/ 8, BLOCK_SIZE / 8);
g.fillOval(((i * BLOCK_SIZE) + ((BLOCK_SIZE / 10) * 5)+(leftRightJustifierConstant)), (j * BLOCK_SIZE + BLOCK_SIZE/3), BLOCK_SIZE/ 8, BLOCK_SIZE / 8);
g.setColor(mouthTop);
g.fillOval((i * BLOCK_SIZE + (BLOCK_SIZE / 4))+ (BLOCK_SIZE/10 ), (j * BLOCK_SIZE+ (BLOCK_SIZE /2) + 10), BLOCK_SIZE / 3, BLOCK_SIZE/5);
if (mouthMode != Mouth.CAUTIOUS) {
g.setColor(mouthBottom);
g.fillOval((i * BLOCK_SIZE + (BLOCK_SIZE / 4))+ (BLOCK_SIZE/10 ), (j * BLOCK_SIZE+ (BLOCK_SIZE /2) + 15), BLOCK_SIZE / 3, BLOCK_SIZE/5);
}
if (mouthMode == Mouth.SMILE) {
g.setColor(mouthTop);
g.fillOval((i * BLOCK_SIZE + (BLOCK_SIZE / 4))+ (BLOCK_SIZE/10 ), (j * BLOCK_SIZE+ (BLOCK_SIZE /2) + 10), BLOCK_SIZE / 3, BLOCK_SIZE/5);
}
}
}
class TitleScreen {
Frame titleFrame;
JLabel Title;
JButton startButton;
JButton instructionsButton;
JButton highScoreButton;
TitleScreen () {
titleFrame = new JFrame("The Maze Game!");
titleFrame.setLayout(null);
Title = new JLabel ("Maze Game");
startButton = new JButton ("Start");
instructionsButton = new JButton ("Instructions");
//highScoreButton = new JButton ("High Scores");
titleFrame.setSize(300,300);
Title.setBounds (5,5,190,60);
Title.setFont( new Font("Comic Sans", Font.PLAIN, 24) );
startButton.setBounds(50,70,200,40);
instructionsButton.setBounds(50,120,200,40);
//highScoreButton.setBounds(50,170,200,40);
titleFrame.add(Title);
titleFrame.add(startButton);
titleFrame.add(instructionsButton);
//titleFrame.add(highScoreButton);
titleFrame.setVisible(true);
}
}