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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
| import java.awt.*; import java.applet.*; import java.awt.event.*; import java.io.*; import java.awt.image.*;
public class VideoGame extends Applet implements Runnable, KeyListener {
Thread animation; Graphics offscreen; static final int REFRESH_RATE = 35; int width, height; AudioClip music; MediaTracker t; Image image,images[][]; boolean[][] grid; Layer2 layer2; Hero hero; int amtSprites=8; Sprite[] sprite=new Sprite[amtSprites]; public String debugString; int spriteNumber;
public void init() { this.addKeyListener(this); showStatus("Loading Images"); width=bounds().width; height=bounds().height; setBackground(Color.black); image = createImage(width,height); offscreen = image.getGraphics(); Font timesNew = new Font("TimesRoman",Font.PLAIN,18); offscreen.setFont(timesNew); grid=new boolean[width][height]; music = getAudioClip(getDocumentBase(),"Music/Tsz_02.mid"); loadImages(); music.play(); for (int j=0; j<width-1; j++) {grid[j][0]=true; grid[j][height-1]=true;} for (int j=0; j<height-1; j++) {grid[0][j]=true; grid[width-1][j]=true;} for (int i=0; i<amtSprites; i++){ sprite[i].updateMap(grid, amtSprites); sprite[i].updatePosition();} hero.updateMap(grid, amtSprites); layer2.setObstruction(grid); }
public void start() { showStatus("Starting Game!"); animation = new Thread(this); if (animation != null) { animation.start(); } } public void update(Graphics g) { paint(g); }
public void paint(Graphics g) { offscreen.setColor(Color.black); offscreen.fillRect(0,0,width,height); updateImages(); layer2.paint(); hero.paint(offscreen); for (int i=0; i<amtSprites; i++){ sprite[i].paint(); if (sprite[i].messageNumber!=-4) spriteNumber=i; } sprite[spriteNumber].say(" The completed argument to the end method tells whether or not the I/O operation actually completed, that is, whether it had any effect that would be visible to the invoker. In the case of an operation that reads bytes, for example, this argument should be true if, and only if, some bytes were actually transferred into the invoker's target buffer.A concrete channel class must also implement the implCloseChannel method in such a way that if it is invoked while another thread is blocked in a native I/O operation upon the channel then that operation will immediately return, either by throwing an exception or by returning normally. If a thread is interrupted or the channel upon which it is blocked is asynchronously closed then the channel's end method will throw the appropriate exception. "); g.drawImage(image,0,0,this); }
public void run() { while (true) { repaint(); try { Thread.sleep (REFRESH_RATE); } catch (Exception exc) { }; } } public void stop() { showStatus("Game Stopped"); if (animation != null) { animation.stop(); animation = null; } }
public void updateImages(){
for (int i=0; i<amtSprites; i++) { if(sprite[i].messageNumber==-4) sprite[i].update(sprite, i); sprite[i].updatePosition(); } hero.update(sprite); }
public void extractImages(Image strip,Image images[][],int num_images,int croppedWidth,int croppedHeight, int spriteSet) { ImageProducer producer; ImageProducer source = strip.getSource(); int startingY=0; if (spriteSet>4){ spriteSet=spriteSet%4; startingY=4*croppedHeight;} int startingX=(spriteSet*(3*croppedWidth))-(3*croppedWidth); for (int i = 0; i<num_images/4; i++) { for (int j=0;j<num_images/3; j++){ ImageFilter extractFilter = new CropImageFilter(startingX+(i*croppedWidth),startingY+(j*croppedHeight),croppedWidth,croppedHeight); producer = new FilteredImageSource(source,extractFilter); images[i][j] = createImage(producer); t.addImage(images[i][j], 0); } } } public void loadImages() { t = new MediaTracker(this); Image strip; int num_images=12; strip=getImage(getCodeBase(), "sprites/layer.png"); t.addImage(strip, 0); layer2=new Layer2(this, strip, offscreen); strip = getImage(getCodeBase(),"sprites/Chara1.png"); t.addImage(strip, 0); images = new Image[num_images/4][num_images/3]; extractImages(strip,images,num_images,24,32, 1); hero = new Hero(600,400,images,this); for (int i=0; i<amtSprites; i++){ strip = getImage(getCodeBase(),"sprites/Chara1.png"); t.addImage(strip, 0); images = new Image[num_images/4][num_images/3]; extractImages(strip,images,num_images,24,32, (int)(Math.random()*7+1)); sprite[i] = new Sprite(10,300,images,this, offscreen, hero); Image face=getImage(getCodeBase(), "Faces/hero1.png"); ImageProducer producer; ImageProducer source = face.getSource(); ImageFilter extractFilter = new CropImageFilter(0,0,47,47); producer = new FilteredImageSource(source,extractFilter); face = createImage(producer); t.addImage(face, 0); sprite[i].face=face; } try { t.waitForAll(); } catch (InterruptedException e) {} if (t.isErrorAny()) { showStatus("Error Loading Images!"); } else if (t.checkAll()) { showStatus("Images successfully loaded"); } } public int[] spritex=new int[amtSprites]; public int[] spritey=new int[amtSprites]; public void keyTyped(KeyEvent e) {} public void keyReleased(KeyEvent e) { hero.key[e.getKeyCode()]=false; hero.enableActionKey=true; } public void keyPressed(KeyEvent e) {hero.key[e.getKeyCode()]=true;} } |