-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.java
179 lines (159 loc) · 3.96 KB
/
Player.java
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
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class Player extends Entity{
BufferedImage up1,up2, up3, up4, down1, down2, down3, down4, left1,left2,left3,left4,right1,right2,right3,right4;
int velx=0, vely=0;
public Rectangle hitBox;
public int playerWidth=100, playerHeight = 100;
int spriteCounter, spriteNum;
String direction="";
public Player(int x, int y) {
super(x, y);
setPlayerImg();
direction="down";
spriteNum=1;
hitBox = new Rectangle(x, y, playerWidth, playerHeight);
update();
// TODO Auto-generated constructor stub
}
public void update(){
//y+=vely;
//x+=velx;
spriteCounter++;
if(spriteCounter>30) {
if(spriteNum==1) {
spriteNum=2;
}else if (spriteNum==2)
spriteNum=3;
else if(spriteNum==3)
spriteNum=4;
else if(spriteNum==4)
spriteNum=1;
spriteCounter=0;
}
}
public void draw(Graphics2D g2d){
BufferedImage image=null;
switch(direction) {
case "up":
if(spriteNum==1) {
image=up1;
}else if(spriteNum==2)
image = up2;
else if(spriteNum==3)
image=up3;
else if(spriteNum==4)
image=up4;
break;
case "down":
if(spriteNum==1) {
image=down1;}
else if(spriteNum==2) {
image = down2;}
else if(spriteNum==3) {
image=down3;}
else if(spriteNum==4) {
image=down4;}
break;
case "left":
if(spriteNum==1) {
image =left1;}
if(spriteNum==2) {
image = left2;}
if(spriteNum==3) {
image=left3;}
if(spriteNum==4) {
image=left4;}
break;
case "right":
if(spriteNum==1) {
image= right1;}
if(spriteNum==2) {
image = right2;}
if(spriteNum==3) {
image=right3;}
if(spriteNum==4) {
image=right4;}
break;
}
if(vely==0&&velx==0&&direction.equals("up"))
image=up1;
else if(vely==0&&velx==0&&direction.equals("down"))
image=down1;
else if(vely==0&&velx==0&&direction.equals("right"))
image=right1;
else if(vely==0&&velx==0&&direction.equals("left"))
image=left1;
g2d.setColor(Color.red);
g2d.drawImage(image, x, y, 100, 100, null);
g2d.draw(hitBox);;
}
public void setPlayerImg(){
try {
up1=ImageIO.read(new File("up1.png"));
up2=ImageIO.read(new File("up2.png"));
up3=ImageIO.read(new File("up3.png"));
up4=ImageIO.read(new File("up4.png"));
down1=ImageIO.read(new File("down1.png"));
down2=ImageIO.read(new File("down2.png"));
down3=ImageIO.read(new File("down3.png"));
down4=ImageIO.read(new File("down4.png"));
right1=ImageIO.read(new File("right1.png"));
right2=ImageIO.read(new File("right2.png"));
right3=ImageIO.read(new File("right3.png"));
right4=ImageIO.read(new File("right4.png"));
left1=ImageIO.read(new File("left1.png"));
left2=ImageIO.read(new File("left2.png"));
left3=ImageIO.read(new File("left3.png"));
left4=ImageIO.read(new File("left4.png"));
}catch(IOException e) {
e.printStackTrace();
}
}
public int getXVel() {
return velx;
}
public int getYVel() {
return vely;
}
public void keyPressed(KeyEvent e) {
int key=e.getKeyCode();
if(key==KeyEvent.VK_W) {
vely=-2;
direction="up";
}else if (key==KeyEvent.VK_S) {
vely=2;
direction="down";
}else if (key==KeyEvent.VK_A) {
velx=-2;
direction="left";
}else if(key==KeyEvent.VK_D) {
velx=2;
direction="right";
}
}
public void keyReleased(KeyEvent e) {
int key=e.getKeyCode();
if(key==KeyEvent.VK_W) {
vely=0;
//direction="sup";
}else if (key==KeyEvent.VK_S) {
vely=0;
//direction="sdown";
}else if (key==KeyEvent.VK_A) {
velx=0;
//direction="sleft";
}else if(key==KeyEvent.VK_D) {
velx=0;
//direction="sright";
}
}
}