Repository navigation
Expand file tree
/
Copy pathplayer.pde
More file actions
111 lines (103 loc) · 1.97 KB
/
Copy pathplayer.pde
File metadata and controls
111 lines (103 loc) · 1.97 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
//player class
class Player {
//image
PImage img;
//velocity
int xv;
int yv;
//position
int x;
int y;
//speed
int step = 5;
//size
int size = 30;
// constructor
public Player() {
//put player in the middle of the screen
x = width/2;
y = height/2;
//load image
img = loadImage("player.png");
}
public void display() {
//move by velocity
x = x+xv;
y = y+yv;
//if you are go past the left of the screen
if(x<(2*size)){
//move back into the screen
x=(2*size);
//stop moving left
xv = 0;
}
//if you go above the top of the screen
if(y<size){
//move back into the screen
y=size;
//stop moving vertically
yv = 0;
}
//if you leave to the right
if(x>width-(2*size)){
//come back
x=width-(2*size);
xv = 0;
}
//if you leave the bottom
if(y>height-size){
//come back in
y=height-size;
//stop trying to leave
yv = 0;
}
//draw the player image
imageMode(CENTER);
image(img,x,y,size*4,size*3);
}
//if a key is pressed
public void move(char k) {
//set velocity to left
if ( k == 'a') {
xv = -step;
}
//set velocity to right
if ( k == 'd') {
xv = step;
}
//set velocity to up
if( k == 'w') {
yv = -step;
}
//set velocity to down
if (k == 's') {
yv = step;
}
//shoot
if(k == ' '){
//add a new bullet on each side
//with opposite velocities
bullets.add(new Bullet(x+(size*2),y,1));
bullets.add(new Bullet(x-(size*2),y,-1));
}
}
//key is released
public void noMove(char k) {
//stop moving left
if ( k == 'a' && xv<0) {
xv = 0;
}
//stop moving right
if ( k == 'd' && xv > 0) {
xv = 0;
}
//stop moving up
if ( k == 'w' && yv<0) {
yv = 0;
}
//stop moving down
if ( k == 's' && yv > 0) {
yv = 0;
}
}
}