-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shooter.pde
72 lines (64 loc) · 1.44 KB
/
Shooter.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
Player guy;
ArrayList<Bullet> bullets = new ArrayList<Bullet>();
ArrayList<Bullet> bulletsToDelete = new ArrayList<Bullet>();
void setup(){
size(1280, 800);
guy = new Player();
}
void draw(){
background(0);
guy.show();
guy.move();
for(Bullet b:bullets){
b.update();
if( b.outOfRange() ){
bulletsToDelete.add(b);
}
}
bullets.removeAll(bulletsToDelete);
}
void keyPressed(){
switch(key){
case 'w':
guy.setYVelocity(-1);
break;
case 's':
guy.setYVelocity(1);
break;
case 'a':
guy.setXVelocity(-1);
break;
case 'd':
guy.setXVelocity(1);
break;
}
}
void keyReleased(){
switch(key){
case 'w':
guy.setYVelocity(0);
break;
case 's':
guy.setYVelocity(0);
break;
case 'a':
guy.setXVelocity(0);
break;
case 'd':
guy.setXVelocity(0);
break;
}
}
void mouseClicked(){
float x = guy.getX();
float y = guy.getY();
float angle = guy.getAngle();
float speed = guy.gun.getBulletSpeed();
float dx = mouseX - x;
float dy = mouseY - y;
float len = sqrt(pow(dx, 2) + pow(dy, 2));
if(len == 0) len = 0.0000001;
float vx = dx/len * speed;
float vy = dy/len * speed;
bullets.add( new Bullet(x, y, vx, vy, angle) );
}