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
| * Copyright 2011 Alan Waddington * * Controls * Up: S * Down: X * Start: SPACE */
import java.applet.Applet; import java.awt.Color; import java.awt.Event; import java.awt.image.BufferedImage; import java.awt.Graphics;
public class P extends Applet implements Runnable {
private final static int SCREENWIDTH = 800; private final static int SCREENHEIGHT = 600; private final static int BATWIDTH = 10; private final static int BATHEIGHT = 30; private final static int BALLSIZE = 10; private final static int BATX = 20; private final static float PLAYERSPEED = 0.0002f; private final static float COMPUTERSPEED = 0.000186f; private final static float BALLSPEED = 0.0002f; private final static int WIN = 3; private static boolean input[] = new boolean[0x10000]; @Override public void start() { new Thread(this).start(); }
public void run() {
float playerX0 = 0, playerY0 = 0; float playerX1 = 0, playerY1 = 0; float ballX = 0, ballY = 0;
float ballSpeedX = BALLSPEED; float ballSpeedY = BALLSPEED;
int playerScore = 0; int computerScore = 0;
boolean gameOver = true; boolean reset = true;
long time, lastTime; long deltaTime = 0; lastTime = time = System.nanoTime()/1000;
BufferedImage screen = new BufferedImage( SCREENWIDTH, SCREENHEIGHT, BufferedImage.TYPE_INT_RGB); while(!isActive()) Thread.yield(); Graphics g = screen.getGraphics(); Graphics gs = getGraphics(); do { lastTime = time; time = System.nanoTime()/1000; deltaTime = time - lastTime;
if (reset) { reset = false; playerScore = 0; computerScore = 0; playerX0 = BATX-BATWIDTH; playerY0 = SCREENHEIGHT/2; playerX1 = SCREENWIDTH-BATX; playerY1 = SCREENHEIGHT/2; ballX = SCREENWIDTH/2; ballY = SCREENHEIGHT/2; }
if (input['s'] && playerY0>0) playerY0-=PLAYERSPEED*deltaTime; if (input['x'] && playerY0<SCREENHEIGHT-BATHEIGHT) playerY0+=PLAYERSPEED*deltaTime;
if (playerY1+BATHEIGHT/2>ballY+BALLSIZE/2) { playerY1-=COMPUTERSPEED*deltaTime; } if (playerY1+BATHEIGHT/2<ballY+BALLSIZE/2) { playerY1+=COMPUTERSPEED*deltaTime; }
if (!gameOver) { ballX += ballSpeedX*deltaTime; ballY += ballSpeedY*deltaTime; } if (ballY<0) { ballY = - ballY; ballSpeedY *=-1; } if (ballY>SCREENHEIGHT-BALLSIZE) { ballY = 2*(SCREENHEIGHT-BALLSIZE) - ballY; ballSpeedY *=-1; } if (ballX<playerX0+BATWIDTH && ballY-playerY0<BATHEIGHT+BALLSIZE && playerY0-ballY<BALLSIZE) { ballX = 2*(playerX0+BATWIDTH) - ballX; ballSpeedX *=-1; } if (ballX+BALLSIZE>playerX1 && ballY-playerY1<BATHEIGHT+BALLSIZE && playerY1-ballY<BALLSIZE) { ballX = 2*(playerX1-BALLSIZE) - ballX; ballSpeedX *=-1; } if (ballX<-BATWIDTH) { computerScore++; ballX = SCREENWIDTH/2; ballY = SCREENHEIGHT/2; ballSpeedX *=-1; } if (ballX>SCREENWIDTH) { playerScore++; ballX = SCREENWIDTH/2; ballY = SCREENHEIGHT/2; ballSpeedX *=-1; }
if (playerScore>=WIN || computerScore>=WIN) { gameOver = true; }
g.setColor(Color.BLACK); g.fillRect(0,0,SCREENWIDTH, SCREENHEIGHT); g.setColor(Color.GREEN); g.fillRect((int)playerX0, (int)playerY0, BATWIDTH, BATHEIGHT); g.fillRect((int)playerX1, (int)playerY1, BATWIDTH, BATHEIGHT); g.fillRect((int)ballX, (int)ballY, BALLSIZE, BALLSIZE);
g.drawString("["+String.valueOf(playerScore+"]"), SCREENWIDTH/4, 20); if (playerScore==WIN) { g.drawString("You Win",SCREENWIDTH/2-20,100); } g.drawString("["+String.valueOf(computerScore)+"]", SCREENWIDTH*3/4, 20); if (computerScore==WIN) { g.drawString("I Win",SCREENWIDTH/2-10,100); } if (gameOver) { g.drawString("Press SPACE",SCREENWIDTH/2-35,130); if (input[' ']) { gameOver = false; reset = true; } }
gs.drawImage(screen, 0,0,null);
Thread.yield(); } while (isActive());
}
@Override public boolean handleEvent(Event e) { input[e.key] = (e.id&1)==1; return false; }
} |