-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
156 lines (146 loc) · 5.96 KB
/
Player.java
File metadata and controls
156 lines (146 loc) · 5.96 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
//Kaan Cinar && Bogachan Arslan && Onder Soydal && Sinan Karabocuoglu
//Player
//24.04.2018
/*This class has variables and methods of the pacman, the player character. It has move() method
* to chage variables. It takes user input via getInput() to change directions of pacman. The draw()
* function of this class draws the pacman in component class.*/
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
public class Player {
//attitudes
int xpos;
int ypos;
int diameter;
int angle = 30; //used for mouth movement
int theta = 30; //used for mouth movement
int close = 5; //used for mouth movement
public Rectangle2D border; //these borders are used for wall detection, ghost chase, and food eating
public Rectangle2D upBor;
public Rectangle2D downBor;
public Rectangle2D leftBor;
public Rectangle2D rightBor;
int xvel = 0; //velocity for movement
int yvel = 0;
char direction = 'E'; //shows direction
public boolean up,down,right,left; //booleans to move the pacman
public boolean u,d,r,l = true; //these booleans check if directions are empty
//constructer, initializes the variables
public Player(int x, int y, int size) {
xpos = x;
ypos = y;
diameter = size;
border = new Rectangle2D.Double(x, y, diameter, diameter);
upBor = new Rectangle2D.Double(x-1, y-13, diameter+2, 12);
downBor = new Rectangle2D.Double(x-1, y+diameter+1, diameter+2, 12);
leftBor = new Rectangle2D.Double(x-13, y-1, 12, diameter+2);
rightBor = new Rectangle2D.Double(x+diameter+1, y-1, 12, diameter+2);
}
//draws pacman
public void draw(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.YELLOW);
/*g2.draw(upBor);
g2.draw(downBor);
g2.draw(leftBor);
g2.draw(rightBor);*/
g2.fillArc((int) border.getBounds2D().getX(), (int) border.getBounds2D().getY(), diameter, diameter,angle-theta,300+2*theta);
}
public int getX(){
return (int)border.getBounds2D().getX();
}
public int getY(){
return (int)border.getBounds2D().getY();
}
public char getDirection(){
return direction;
}
//used to get input for directions
public void getInput(int i){
up = false;
down = false;
right = false;
left = false;
if(i == 38){
up = true;
down = false;
}
if(i == 39){
right = true;
left = false;
}
if(i == 37){
left = true;
right = false;
}
if(i == 40){
down = true;
up = false;
}
}
public Rectangle2D getBorderOfPac(){
return border;
}
//changes variables
public void move(){
//if in left portal, teleports pacman to right portal
if((int) (border.getBounds2D().getX()+border.getBounds2D().getWidth())<0)
{
leftBor.setRect(988-13-3, (int) leftBor.getBounds2D().getY()+yvel, leftBor.getBounds2D().getWidth(), leftBor.getBounds2D().getHeight());
rightBor.setRect(988+diameter+1-3, (int) rightBor.getBounds2D().getY()+yvel, rightBor.getBounds2D().getWidth(), rightBor.getBounds2D().getHeight());
upBor.setRect(987-3, (int) upBor.getBounds2D().getY()+yvel, upBor.getBounds2D().getWidth(), upBor.getBounds2D().getHeight());
downBor.setRect(987-3, (int) downBor.getBounds2D().getY()+yvel, downBor.getBounds2D().getWidth(), downBor.getBounds2D().getHeight());
border.setRect(988-3, (int) border.getBounds2D().getY()+yvel, diameter, diameter);
}
//if in right portal, teleports pacman to left portal
if((int) (border.getBounds2D().getX())>992)
{
leftBor.setRect(0-13+3-border.getBounds2D().getWidth(), (int) leftBor.getBounds2D().getY()+yvel, leftBor.getBounds2D().getWidth(), leftBor.getBounds2D().getHeight());
rightBor.setRect(0+diameter+1+3-border.getBounds2D().getWidth(), (int) rightBor.getBounds2D().getY()+yvel, rightBor.getBounds2D().getWidth(), rightBor.getBounds2D().getHeight());
upBor.setRect(-1+3-border.getBounds2D().getWidth(), (int) upBor.getBounds2D().getY()+yvel, upBor.getBounds2D().getWidth(), upBor.getBounds2D().getHeight());
downBor.setRect(-1+3-border.getBounds2D().getWidth(), (int) downBor.getBounds2D().getY()+yvel, downBor.getBounds2D().getWidth(), downBor.getBounds2D().getHeight());
border.setRect(0+3-border.getBounds2D().getWidth(), (int) border.getBounds2D().getY()+yvel, diameter, diameter);
}
//sets variables to zero
xvel = 0;
yvel = 0;
if(theta<0||theta>30) close*=-1; //changes mouth movement
theta-=close; //changes mouth movement
//following if loops are used for movement in different directions
if((right||direction=='E')&&r) {
if(!u&&d) yvel=4;
if(!d&&u) yvel=-4;
direction = 'E';
xvel=4;
angle = 30;
}
if((left||direction =='W')&&l) {
if(!u&&d) yvel=4;
if(!d&&u) yvel=-4;
direction = 'W';
xvel=-4;
angle = 210;
}
if((up||direction=='N')&&u) {
if(!l&&r) xvel=4;
if(!r&&l) xvel=-4;
direction = 'N';
yvel=-4;
angle = 120;
}
if((down||direction=='S')&&d) {
if(!l&&r) xvel=4;
if(!r&&l) xvel=-4;
direction = 'S';
yvel=4;
angle = 300;
}
//in the end variables that store the positions are set
leftBor.setRect((int) leftBor.getBounds2D().getX()+xvel, (int) leftBor.getBounds2D().getY()+yvel, leftBor.getBounds2D().getWidth(), leftBor.getBounds2D().getHeight());
rightBor.setRect((int) rightBor.getBounds2D().getX()+xvel, (int) rightBor.getBounds2D().getY()+yvel, rightBor.getBounds2D().getWidth(), rightBor.getBounds2D().getHeight());
upBor.setRect((int) upBor.getBounds2D().getX()+xvel, (int) upBor.getBounds2D().getY()+yvel, upBor.getBounds2D().getWidth(), upBor.getBounds2D().getHeight());
downBor.setRect((int) downBor.getBounds2D().getX()+xvel, (int) downBor.getBounds2D().getY()+yvel, downBor.getBounds2D().getWidth(), downBor.getBounds2D().getHeight());
border.setRect((int) border.getBounds2D().getX()+xvel, (int) border.getBounds2D().getY()+yvel, diameter, diameter);
}
}