1 package version21;
2
10
11
12 import java.awt.Canvas;
13 import java.awt.Color;
14 import java.awt.Dimension;
15 import java.awt.Font;
16 import java.awt.Graphics2D;
17 import java.awt.Rectangle;
18 import java.awt.event.KeyEvent;
19 import java.awt.event.KeyListener;
20 import java.awt.event.WindowAdapter;
21 import java.awt.event.WindowEvent;
22 import java.awt.image.BufferStrategy;
23 import java.awt.image.BufferedImage;
24 import java.util.ArrayList;
25
26 import javax.swing.JFrame;
27 import javax.swing.JPanel;
28
29 public class Invaders extends Canvas implements Stage, KeyListener {
30
31 private BufferStrategy strategy;
32 private long usedTime;
33
34 private SpriteCache spriteCache;
35 private ArrayList actors;
36 private Player player;
37
38 public Invaders() {
39 spriteCache = new SpriteCache();
40
41
42 JFrame ventana = new JFrame("Invaders");
43 JPanel panel = (JPanel)ventana.getContentPane();
44 setBounds(0,0,Stage.WIDTH,Stage.HEIGHT);
45 panel.setPreferredSize(new Dimension(Stage.WIDTH,Stage.HEIGHT));
46 panel.setLayout(null);
47 panel.add(this);
48 ventana.setBounds(0,0,Stage.WIDTH,Stage.HEIGHT);
49 ventana.setVisible(true);
50 ventana.addWindowListener( new WindowAdapter() {
51 public void windowClosing(WindowEvent e) {
52 System.exit(0);
53 }
54 });
55 ventana.setResizable(false);
56 createBufferStrategy(2);
57 strategy = getBufferStrategy();
58 requestFocus();
59 addKeyListener(this);
60 }
61
62 public void initWorld() {
63 actors = new ArrayList();
64 for (int i = 0; i < 10; i++){
65 Monster m = new Monster(this);
66 m.setX( (int)(Math.random()*Stage.WIDTH) );
67 m.setY( i*20 );
68 m.setVx( (int)(Math.random()*20-10) );
69
70 actors.add(m);
71 }
72
73 player = new Player(this);
74 player.setX(Stage.WIDTH/2);
75 player.setY(Stage.PLAY_HEIGHT - 2*player.getHeight());
76 }
77
78 public void addActor(Actor a) {
79 actors.add(a);
80 }
81
82 public void updateWorld() {
83 int i = 0;
84 while (i < actors.size()) {
85 Actor m = (Actor)actors.get(i);
86 if (m.isMarkedForRemoval()) {
87 actors.remove(i);
88 } else {
89 m.act();
90 i++;
91 }
92 }
93 player.act();
94 }
95
96 public void checkCollisions() {
97 Rectangle playerBounds = player.getBounds();
98 for (int i = 0; i < actors.size(); i++) {
99 Actor a1 = (Actor)actors.get(i);
100 Rectangle r1 = a1.getBounds();
101 if (r1.intersects(playerBounds)) {
102 player.collision(a1);
103 a1.collision(player);
104 }
105 for (int j = i+1; j < actors.size(); j++) {
106 Actor a2 = (Actor)actors.get(j);
107 Rectangle r2 = a2.getBounds();
108 if (r1.intersects(r2)) {
109 a1.collision(a2);
110 a2.collision(a1);
111 }
112 }
113 }
114 }
115
116 public void paintShields(Graphics2D g) {
117 g.setPaint(Color.red);
118 g.fillRect(280,Stage.PLAY_HEIGHT,Player.MAX_SHIELDS,30);
119 g.setPaint(Color.blue);
120 g.fillRect(280+Player.MAX_SHIELDS-player.getShields(),Stage.PLAY_HEIGHT,player.getShields(),30);
121 g.setFont(new Font("Arial",Font.BOLD,20));
122 g.setPaint(Color.green);
123 g.drawString("Shields",170,Stage.PLAY_HEIGHT+20);
124
125 }
126
127 public void paintScore(Graphics2D g) {
128 g.setFont(new Font("Arial",Font.BOLD,20));
129 g.setPaint(Color.green);
130 g.drawString("Score:",20,Stage.PLAY_HEIGHT + 20);
131 g.setPaint(Color.red);
132 g.drawString(player.getScore()+"",100,Stage.PLAY_HEIGHT + 20);
133 }
134
135 public void paintAmmo(Graphics2D g) {
136 int xBase = 280+Player.MAX_SHIELDS+10;
137 for (int i = 0; i < player.getClusterBombs();i++) {
138 BufferedImage bomb = spriteCache.getSprite("bombUL.gif");
139 g.drawImage( bomb ,xBase+i*bomb.getWidth(),Stage.PLAY_HEIGHT,this);
140 }
141 }
142
143 public void paintfps(Graphics2D g) {
144 g.setFont( new Font("Arial",Font.BOLD,12));
145 g.setColor(Color.white);
146 if (usedTime > 0)
147 g.drawString(String.valueOf(1000/usedTime)+" fps",Stage.WIDTH-50,Stage.PLAY_HEIGHT);
148 else
149 g.drawString("--- fps",Stage.WIDTH-50,Stage.PLAY_HEIGHT);
150 }
151
152
153 public void paintStatus(Graphics2D g) {
154 paintScore(g);
155 paintShields(g);
156 paintAmmo(g);
157 paintfps(g);
158 }
159
160 public void paintWorld() {
161 Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
162 g.setColor(Color.black);
163 g.fillRect(0,0,getWidth(),getHeight());
164 for (int i = 0; i < actors.size(); i++) {
165 Actor m = (Actor)actors.get(i);
166 m.paint(g);
167 }
168 player.paint(g);
169
170
171 paintStatus(g);
172 strategy.show();
173 }
174
175 public SpriteCache getSpriteCache() {
176 return spriteCache;
177 }
178
179 public void keyPressed(KeyEvent e) {
180 player.keyPressed(e);
181 }
182
183 public void keyReleased(KeyEvent e) {
184 player.keyReleased(e);
185 }
186 public void keyTyped(KeyEvent e) {}
187
188 public void game() {
189 usedTime=1000;
190 initWorld();
191 while (isVisible()) {
192 long startTime = System.currentTimeMillis();
193 updateWorld();
194 checkCollisions();
195 paintWorld();
196 usedTime = System.currentTimeMillis()-startTime;
197 try {
198 Thread.sleep(SPEED);
199 } catch (InterruptedException e) {}
200 }
201 }
202
203 public static void main(String[] args) {
204 Invaders inv = new Invaders();
205 inv.game();
206 }
207 }
208