-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCannon.pde
155 lines (140 loc) · 3.24 KB
/
Cannon.pde
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
public class Cannon
{
private int xCoord1;
private int yCoord1;
private int xCoord2;
private int yCoord2;
private int xCoord3;
private int yCoord3;
private int cannonBottomWidth; //variable setting the bottom width of the triangle, i.e. the distance between the x coordinates
public Cannon() //default constructor method setting default values of fields
{
cannonBottomWidth = 100;
xCoord1 = 0;
xCoord2 = cannonBottomWidth;
xCoord3 = cannonBottomWidth/2;
yCoord1 = height;
yCoord2 = height;
yCoord3 = height-50;
}
//getter methods
public int getXCoord1()
{
return xCoord1;
}
public int getYCoord1()
{
return yCoord1;
}
public int getXCoord2()
{
return xCoord2;
}
public int getYCoord2()
{
return yCoord2;
}
public int getXCoord3()
{
return xCoord3;
}
public int getYCoord3()
{
return yCoord3;
}
public int getCannonBottomWidth()
{
return cannonBottomWidth;
}
//setter methods
//method with validation so that cannon does not move outside the output window
public void setXCoord1(int xCoord1)
{
if (xCoord1<0)
{
xCoord1 = 0;
} else if (xCoord1 >width-100)
{
xCoord1 = width-100;
} else
{
this.xCoord1 = xCoord1;
}
}
//method with validation so that cannon does not move outside the output window
public void setXCoord2(int xCoord2)
{
if (xCoord2 <100)
{
xCoord2 = 100;
} else if (xCoord2 >=width)
{
xCoord2 = width;
} else
{
this.xCoord2 = xCoord2;
}
}
//method with validation so that width of cannon is constant
public void setXCoord3(int xCoord3)
{
if (xCoord3 != xCoord1 + cannonBottomWidth/2)
xCoord3 = xCoord1 + cannonBottomWidth/2;
else
{
this.xCoord3 = xCoord3;
}
}
public void setYCoord1(int yCoord1)
{
if (yCoord1 != height)
{
yCoord1 = height;
}
}
public void setYCoord2(int yCoord2)
{
if (yCoord2 != height)
{
yCoord2 = height;
}
}
public void setYCoord3(int yCoord3)
{
if (yCoord3 != height-50)
{
yCoord3 = height-50;
}
}
public void setCannonBottomWidth(int cannonBottomWidth)
{
if (cannonBottomWidth != 100)
{
cannonBottomWidth = 100;
}
}
/*
public method with no return which displays the cannon - initially a triangle shape with circle and rectangles attached to the same coordinates.
*/
public void display()
{
fill(0);
noStroke();
triangle(xCoord1, yCoord1, xCoord2, yCoord2, xCoord3, yCoord3);
circle(xCoord3, yCoord3, 50);
rect(xCoord3-25, yCoord3, 50, 50);
rect(xCoord3-45, yCoord3+20, 20, 30);
rect(xCoord3+25, yCoord3+20, 20, 30);
}
/*
public method with no return that takes in 1 parameter allowing for movement of the cannon object when called in the keyPressed method
in main program
calling setter methods to envoke validation
*/
public void moveCannon(int amount)
{
this.setXCoord1(xCoord1 +amount);
this.setXCoord2(xCoord2 +amount);
this.setXCoord3(xCoord3 +amount);
}
}