-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpampedeDisplay.java
More file actions
131 lines (111 loc) · 3.79 KB
/
Copy pathSpampedeDisplay.java
File metadata and controls
131 lines (111 loc) · 3.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
package com.gradescope.spampede;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
/**
* The "view" in MVC that is responsible for drawing the board on the screen.
*
* @author CS60 instructors
*/
class SpampedeDisplay {
/** The board/spampede data being drawn. */
private final SpampedeData theData;
/** The display where the board is drawn. */
private final Graphics theScreen;
/** The width of the display in pixels. */
private final int width;
/** The height of the display in pixels. */
private final int height;
/** A picture of a can of spam. */
public static Image imageSpam;
/**
* Creates a new SpampedeDisplay.
*
* @param theBoard the data being displayed
* @param theScreen the display on which to draw the board
* @param width the width of the display (in pixels)
* @param height the height of the display (in pixels)
*/
public SpampedeDisplay(SpampedeData theBoard, Graphics theScreen, int width, int height) {
this.theScreen = theScreen;
this.theData = theBoard;
this.height = height;
this.width = width;
}
/* -------------------- */
/* Displaying the board */
/* -------------------- */
/**
* Re-draws the board, spam, and snake (but not the buttons).
*/
public void updateGraphics() {
// Draw the background -- DO NOT REMOVE!
this.clear();
// Draw the title
this.displayTitle();
// Draw the board
int width = 500;
int height = 300;
//YShifts the board by 2/7 to make it in middle of screen
int yshift = 2 * (Preferences.GAMEBOARDHEIGHT - height) / 7;
int xshift = Preferences.NUM_CELLS_WIDE;
//loops through board cells according to shift location
for (int x = xshift; x < width + xshift; x = x + Preferences.CELL_SIZE) {
for (int y = yshift; y < height + yshift; y = y + Preferences.CELL_SIZE) {
//gets color for each cell based on CellType
Color cell = this.theData.getCellColor((y - yshift) / Preferences.CELL_SIZE,
(x - xshift) / Preferences.CELL_SIZE);
this.drawSquare(x, y, cell);
}
}
// Display an image, just for fun
if (SpampedeDisplay.imageSpam != null) {
this.theScreen.drawImage(SpampedeDisplay.imageSpam, 200, 370, null);
}
// Draw the game-over message, if appropriate
if (this.theData.getGameOver()) {
this.displayGameOver();
}
}
/**
* Draws a cell-sized square with its upper-left corner at the specified pixel
* coordinates (i.e. x pixels to the right and y pixels below the upper-left
* corner) on the display.
*
* @param x the x-coordinate, between 0 and this.width-1 inclusive
* @param y the y-coordinate, between 0 and this.height-1 inclusive
* @param cellColor the color of the square being drawn
*/
private void drawSquare(int x, int y, Color cellColor) {
this.theScreen.setColor(cellColor);
this.theScreen.fillRect(x, y, Preferences.CELL_SIZE, Preferences.CELL_SIZE);
}
/**
* Draws the background. DO NOT MODIFY!
*/
private void clear() {
this.theScreen.setColor(Preferences.COLOR_BACKGROUND);
this.theScreen.fillRect(0, 0, this.width, this.height);
this.theScreen.setColor(Preferences.TITLE_COLOR);
this.theScreen.drawRect(0, 0, this.width - 1, Preferences.GAMEBOARDHEIGHT - 1);
}
/* ------------ */
/* Text Display */
/* ------------ */
/**
* Displays the title of the game.
*/
private void displayTitle() {
this.theScreen.setFont(Preferences.TITLE_FONT);
this.theScreen.setColor(Preferences.TITLE_COLOR);
this.theScreen.drawString(Preferences.TITLE, Preferences.TITLE_X, Preferences.TITLE_Y);
}
/**
* Displays the game-over message.
*/
private void displayGameOver() {
this.theScreen.setFont(Preferences.GAME_OVER_FONT);
this.theScreen.setColor(Preferences.GAME_OVER_COLOR);
this.theScreen.drawString(Preferences.GAME_OVER_TEXT, Preferences.GAME_OVER_X, Preferences.GAME_OVER_Y);
}
}