-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayer.java
More file actions
182 lines (140 loc) · 3.58 KB
/
Player.java
File metadata and controls
182 lines (140 loc) · 3.58 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Player extends Character{
private TwoPlayerShooterGamee game;
public final int WIDTH = 60;
public final int HEIGHT = 60;
int x = 0;
int xa = 0;
int y = 0;
int ya = 0;
int speed = 3;
File imgSource1 = new File("res/buckysFront.png");
File imgSource2 = new File("res/buckysBack.png");
File imgSource3 = new File("res/buckysLeft.png");
File imgSource4 = new File("res/buckysRight.png");
File imgSource5 = new File("res/ShooterBossHappy.png");
BufferedImage player;
boolean activeKey = true;
boolean speedBoost = false;
String name="Player";
public Player(TwoPlayerShooterGamee game) {
try {
player = ImageIO.read(this.getClass().getResource("/res/buckysFront.png"));
} catch (IOException e) {
e.printStackTrace();
}
this.game = game;
y = game.WINDOW_HEIGHT - 175;
x = game.WINDOW_WIDTH / 2 - (WIDTH / 2);
}
void setName(String s)
{
name = s;
}
String getName()
{
return name;
}
void move() {
if(game.playerScore % 2 == 0 && game.playerScore != 0 && !speedBoost){
speed++;
speedBoost = true;
}
if (x + xa > 0 && x + xa < game.getWidth()-WIDTH)
x = x + xa;
if (y + ya > game.getHeight()/2 && y + ya < game.getHeight()-HEIGHT)
y = y + ya;
}
public void paint(Graphics2D g) {
g.drawImage(player, x, y, WIDTH, HEIGHT, game);
}
public void keyReleased(KeyEvent e) {
xa = 0;
ya = 0;
activeKey = true;
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT && activeKey)
turnLeft();
if (e.getKeyCode() == KeyEvent.VK_RIGHT && activeKey)
turnRight();
if (e.getKeyCode() == KeyEvent.VK_UP && activeKey)
turnUp();
if (e.getKeyCode() == KeyEvent.VK_DOWN && activeKey)
turnDown();
if (e.getKeyCode() == KeyEvent.VK_ENTER)
shoot();
}
void shoot(){
if(game.playerBulletList.size() >= 5){
game.playerBulletList.remove(0);
}
Bullet bullet = new Bullet(game, this);
bullet.x = x + WIDTH/2;
bullet.y = y - bullet.HEIGHT;
}
void turnLeft(){
xa = -speed;
try {
player = ImageIO.read(this.getClass().getResource("/res/buckysLeft.png"));
} catch (IOException e) {
e.printStackTrace();
}
activeKey = false;
}
void turnRight(){
xa = speed;
try {
player = ImageIO.read(this.getClass().getResource("/res/buckysRight.png"));
} catch (IOException e) {
e.printStackTrace();
}
activeKey = false;
}
void turnUp(){
ya = -speed;
try {
player = ImageIO.read(this.getClass().getResource("/res/buckysBack.png"));
} catch (IOException e) {
e.printStackTrace();
}
activeKey = false;
}
void turnDown(){
ya = speed;
try {
player = ImageIO.read(this.getClass().getResource("/res/buckysFront.png"));
} catch (IOException e) {
e.printStackTrace();
}
activeKey = false;
}
public Rectangle getBounds() {
return new Rectangle(x, y, WIDTH, HEIGHT);
}
public void shot(Bullet bullet){
bullet.delete();
game.bossScore++;
game.boss.speedBoost = false;
try {
game.boss.boss = ImageIO.read(this.getClass().getResource("/res/ShooterBossHappy.png"));
} catch (IOException e) {
e.printStackTrace();
}
game.boss.count = 0;
}
public void reset(){
y = game.WINDOW_HEIGHT - 75;
x = game.WINDOW_WIDTH / 2 - (WIDTH / 2);
xa = 0;
ya = 0;
speed = 1;
}
}