Академический Документы
Профессиональный Документы
Культура Документы
Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;
/* CONSTANTS */
/* GAME VARIABLES */
double x, y;
double sizeX;
double sizeY;
double left() {
return x - sizeX / 2.0;
}
double right() {
return x + sizeX / 2.0;
}
double top() {
return y - sizeY / 2.0;
}
double bottom() {
return y + sizeY / 2.0;
}
class ScoreBoard {
int score = 0;
int lives = PLAYER_LIVES;
boolean win = false;
boolean gameOver = false;
String text = "";
Font font;
ScoreBoard() {
font = new Font(FONT, Font.PLAIN, 12);
text = "Welcome to Arkanoid Java version";
}
void increaseScore() {
score++;
if (score == (COUNT_BLOCKS_X * COUNT_BLOCKS_Y)) {
win = true;
text = "You have won! \nYour score was: " + score
+ "\n\nPress Enter to restart";
} else {
updateScoreboard();
}
}
void die() {
lives--;
if (lives == 0) {
gameOver = true;
text = "You have lost! \nYour score was: " + score
+ "\n\nPress Enter to restart";
} else {
updateScoreboard();
}
}
void updateScoreboard() {
text = "Score: " + score + " Lives: " + lives;
}
void draw(Graphics g) {
if (win || gameOver) {
font = font.deriveFont(50f);
FontMetrics fontMetrics = g.getFontMetrics(font);
g.setColor(Color.WHITE);
g.setFont(font);
int titleHeight = fontMetrics.getHeight();
int lineNumber = 1;
for (String line : text.split("\n")) {
int titleLen = fontMetrics.stringWidth(line);
g.drawString(line, (SCREEN_WIDTH / 2) - (titleLen /
2),
(SCREEN_HEIGHT / 4) + (titleHeight *
lineNumber));
lineNumber++;
}
} else {
font = font.deriveFont(34f);
FontMetrics fontMetrics = g.getFontMetrics(font);
g.setColor(Color.WHITE);
g.setFont(font);
int titleLen = fontMetrics.stringWidth(text);
int titleHeight = fontMetrics.getHeight();
g.drawString(text, (SCREEN_WIDTH / 2) - (titleLen / 2),
titleHeight + 5);
}
}
void update() {
x += velocity * FT_STEP;
}
void stopMove() {
velocity = 0.0;
}
void moveLeft() {
if (left() > 0.0) {
velocity = -PADDLE_VELOCITY;
} else {
velocity = 0.0;
}
}
void moveRight() {
if (right() < SCREEN_WIDTH) {
velocity = PADDLE_VELOCITY;
} else {
velocity = 0.0;
}
}
void draw(Graphics g) {
g.setColor(Color.RED);
g.fillRect((int) (left()), (int) (top()), (int) sizeX, (int)
sizeY);
}
Brick(double x, double y) {
this.x = x;
this.y = y;
this.sizeX = BLOCK_WIDTH;
this.sizeY = BLOCK_HEIGHT;
}
void draw(Graphics g) {
g.setColor(Color.YELLOW);
g.fillRect((int) left(), (int) top(), (int) sizeX, (int) sizeY);
}
}
double x, y;
double radius = BALL_RADIUS;
double velocityX = BALL_VELOCITY;
double velocityY = BALL_VELOCITY;
Ball(int x, int y) {
this.x = x;
this.y = y;
}
void draw(Graphics g) {
g.setColor(Color.RED);
g.fillOval((int) left(), (int) top(), (int) radius * 2,
(int) radius * 2);
}
if (left() < 0)
velocityX = BALL_VELOCITY;
else if (right() > SCREEN_WIDTH)
velocityX = -BALL_VELOCITY;
if (top() < 0) {
velocityY = BALL_VELOCITY;
} else if (bottom() > SCREEN_HEIGHT) {
velocityY = -BALL_VELOCITY;
x = paddle.x;
y = paddle.y - 50;
scoreBoard.die();
}
double left() {
return x - radius;
}
double right() {
return x + radius;
}
double top() {
return y - radius;
}
double bottom() {
return y + radius;
}
mBrick.destroyed = true;
scoreboard.increaseScore();
public Arkanoid() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setUndecorated(false);
this.setResizable(false);
this.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
this.setVisible(true);
this.addKeyListener(this);
this.setLocationRelativeTo(null);
this.createBufferStrategy(2);
initializeBricks(bricks);
void run() {
BufferStrategy bf = this.getBufferStrategy();
Graphics g = bf.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, getWidth(), getHeight());
running = true;
while (running) {
} else {
if (tryAgain) {
tryAgain = false;
initializeBricks(bricks);
scoreboard.lives = PLAYER_LIVES;
scoreboard.score = 0;
scoreboard.win = false;
scoreboard.gameOver = false;
scoreboard.updateScoreboard();
ball.x = SCREEN_WIDTH / 2;
ball.y = SCREEN_HEIGHT / 2;
paddle.x = SCREEN_WIDTH / 2;
}
}
lastFt = elapsedTime;
currentSlice += lastFt;
ball.update(scoreboard, paddle);
paddle.update();
testCollision(paddle, ball);
Iterator<Brick> it = bricks.iterator();
while (it.hasNext()) {
Brick brick = it.next();
testCollision(brick, ball, scoreboard);
if (brick.destroyed) {
it.remove();
}
}
}
}
try {
g = bf.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, getWidth(), getHeight());
ball.draw(g);
paddle.draw(g);
for (Brick brick : bricks) {
brick.draw(g);
}
scoreboard.draw(g);
} finally {
g.dispose();
}
bf.show();
Toolkit.getDefaultToolkit().sync();
@Override
public void keyPressed(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.VK_ESCAPE) {
running = false;
}
if (event.getKeyCode() == KeyEvent.VK_ENTER) {
tryAgain = true;
}
switch (event.getKeyCode()) {
case KeyEvent.VK_LEFT:
paddle.moveLeft();
break;
case KeyEvent.VK_RIGHT:
paddle.moveRight();
break;
default:
break;
}
}
@Override
public void keyReleased(KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.VK_LEFT:
paddle.stopMove();
case KeyEvent.VK_RIGHT:
paddle.stopMove();
break;
default:
break;
}
}
@Override
public void keyTyped(KeyEvent arg0) {