-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.java
77 lines (66 loc) · 1.83 KB
/
Game.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
import java.awt.*;
import java.util.*;
public class Game{
private boolean playing = false;
private int width, height;
private Player player;
private ArrayList<Guy> list = new ArrayList<Guy>();
private int score;
public Game( int w, int h ){
width = w;
height = h;
player = new Player( 5, height/2 - 10, width, height );
}
// sets up the game
public void init(){
score = 0;
player = new Player( 5, height/2 - 10, width, height );
list.clear();
}
public void updatePlayerMotion( int dx, int dy ){
player.setSpeed( dx, dy );
}
public void update(){
// update the player's position
player.move();
int y = (int) (5*Math.random()) + 1;
double x = Math.random();
if(y == 1){
if(x>0.5){
list.add(new Guy(710, (int)(Math.random()*500), true));
}else{
list.add(new Guy(710, (int)(Math.random()*500), false));
}
}
for(Guy g : list){
g.move();
}
Rectangle player_r = player.getShape();
for ( int k = list.size() - 1; k >= 0; k-- ){
Guy guy = list.get( k );
Rectangle guy_r = guy.getShape();
if ( player_r.intersects( guy_r ) ){
if(guy.isGood()){
score += 1;
}else{
player.hurt();
}
list.remove(guy);
}
if(guy.getX()<-20){
list.remove(guy);
}
}
}
public boolean keepPlaying(){
return player.isAlive();
}
public int getScore(){
return score;
}
public void draw( Graphics g ){
player.draw( g );
for ( Guy guy : list )
guy.draw( g );
}
}