-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.java
More file actions
93 lines (83 loc) · 2.21 KB
/
Block.java
File metadata and controls
93 lines (83 loc) · 2.21 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
import java.awt.*;
import javax.swing.*;
/**
* The class for all block objects
*
* Useful Methods:
* draw: creates each block
* setColor: allows one to change the
* color of block to be drawn
*
* @Avi and Amar Ruthen
* @12.22.2020
*/
public class Block
{
int xLoc, yLoc, squareSize, numMoves;
boolean user, immovable;
Color blockColor;
/**
* Constructor for all blocks created
*/
public Block(int xLoc, int yLoc, int squareSize, boolean user,
Color blockColor, boolean immovable)
{
this.xLoc = xLoc;
this.yLoc = yLoc;
this.squareSize = squareSize;
this.user = user;
this.blockColor = blockColor;
this.immovable = immovable;
numMoves = 0;
}
/**
* Moves the blocks in 4 directions
* @param moveCode 0-3 code, in order: up, down, left, right
*/
public void move(int moveCode)
{
numMoves++;
switch(moveCode)
{
case 0: //move up
yLoc--;
break;
case 1: //move down
yLoc++;
break;
case 2: // move left
xLoc--;
break;
case 3: //moves right
xLoc++;
break;
default: //Don't move
break;
}
}
/**
* Draws each block, which is represented as a colored square
*
* @param g allows for drawing to be displayed on Graphics Window
*/
public void draw(Graphics g)
{
// location that block should be drawn
int x = (int) (squareSize * xLoc);
int y = (int) (squareSize * yLoc);
//Sets color of block depending on the type of block
if(immovable)
g.setColor(Color.GRAY);
g.setColor(blockColor);
g.fillRect(x, y, squareSize, squareSize);
}
/**
* Allows other classes to set the color of the block to be drawn
*
* @param c is the desired color
*/
public void setColor(Color c)
{
blockColor = c;
}
}