Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Ball.java
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Ball {
public static BoxBall ball;
//public static Game.board board;
private int xDirection, yDirection;
private int ballSpeed;
public Ball(){
//here is where we set the ball starting point - random
on Y direction
Random randY = new Random();
int Y = randY.nextInt(400) + 100;
ball = new BoxBall(570,Y);
ballSpeed = 2;// The ball speed
xDirection = -ballSpeed;
yDirection = -ballSpeed;
}
public void drawBall(Graphics g){
BoxBall box = ball;
g.setColor(Color.black);
g.fillOval(box.x, box.y, 20,20);
}
public void tick(Bricks bricks_h){
//while(Game.gameRunning){
GameMessages msg = new GameMessages(); // messages
int boardx = Game.board.getBoardX();
//The ball hits the board
if(ball.x>=boardx &&
ball.x<=boardx+100&&ball.y>Game.HEIGHT-40){
yDirection = -ballSpeed;
}
//The ball hits to the right
else if(ball.x >= Game.WIDTH-20){
xDirection = -ballSpeed;// The ball speed is 1
}
//The ball hits to the left
else if(ball.x == 0){
xDirection = ballSpeed;
}
//The ball hits to bottom
else if(ball.y >= Game.HEIGHT-20){
//yDirection = -1;
msg.string(Game.globalGraphics, "YOU DIED", "red");
Game.gameRunning = false;
}
//The ball hits to the top
else if(ball.y <=50){ // 50 is the size of the brick
boolean foundBrick = false;
for (Brick brick : bricks_h.body) {
if (brick.x == (ball.x/50)) {
foundBrick = true;
bricks_h.body.remove(brick);
Game.score += 10; // add score on each
hit with a brick
if (bricks_h.body.isEmpty()) {
msg.string(Game.globalGraphics, "YOU
WIN", "green");
Game.gameRunning = false;
}
break;
}
}
if(foundBrick){//break the brick
yDirection = ballSpeed;
} else if(ball.y <= 0) {
yDirection = ballSpeed;
}
}
//Board v = Game.board;
BoxBall nextPos = new BoxBall(ball.x+xDirection,
ball.y+yDirection);
ball = nextPos;
//}
}
public int getxDirection() {
return xDirection;
}
public void setxDirection(int xDirection) {
this.xDirection = xDirection;
}
public int getyDirection() {
return yDirection;
}
public void setyDirection(int yDirection) {
this.yDirection = yDirection;
}
}
Game.java
import
import
import
import
java.awt.Canvas;
java.awt.Color;
java.awt.Graphics;
java.awt.image.BufferStrategy;
@SuppressWarnings("serial")
public class Game extends Canvas implements Runnable{
public static final int ROWS = 30;
public static final int COLS = 30;
public static final int SIZE = 20;
public static final int WIDTH = COLS*SIZE;
public static final int HEIGHT = ROWS*SIZE;
private Ball ball;
public static Board board;
public static int score;
//add bricks
public static Bricks bricks;
private Thread runThread;
public static Graphics globalGraphics;
public static GameMessages msg; // for all messages
static boolean gameRunning = false;
private BufferStrategy bs;
public void paint(Graphics g){
globalGraphics = g.create();
// Check is the game is already running
if(runThread == null){
runThread = new Thread(this);
runThread.start();
gameRunning = true;
}
}
public Game(){
ball = new Ball();
board = new Board();
msg = new GameMessages();
score = 0;
bricks = new Bricks();
board.setSize(score);
}
@SuppressWarnings("static-access")
public void run(){
//msg.drawNewGame(globalGraphics);
while (gameRunning){
render(globalGraphics);
board.move();
ball.tick(bricks);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
board.setSize(score);
}
this.drawDeath(globalGraphics);// the text when you dead
//
}
public void render(Graphics g){
//Setting the bufferStrategy to be the one used in our
canvas
this.bs = this.getBufferStrategy();
if (bs == null) {
//Create 2 buffers
this.createBufferStrategy(2);
//returns out of the method to prevent errors
return;
}
//Instantiates the graphics related to the bufferStrategy
g = bs.getDrawGraphics();
// Start drawing things on the screen
this.drawScore(g);
g.clearRect(0, 0, WIDTH, HEIGHT);
ball.drawBall(g);
board.drawBoard(g);
bricks.drawBricks(g);
// end of drawing.
//Enables the buffer
bs.show();
//Shows everything stored in the Graphics object
g.dispose();
}
//
public void drawScore(Graphics g){
// set the score board background color and paint it.
g.setColor(Color.gray);
g.fillRect(Game.WIDTH, 0, 100, Game.HEIGHT);
// set the scoreboard borders
g.setColor(Color.darkGray);
g.drawRect(Game.WIDTH, 0, 100, Game.HEIGHT);
// draw the score on the score board.
g.setColor(Color.black);
g.drawString("SCORE = " + score, 610, 100);
}
private static void drawDeath(Graphics g){
//
//
//
//
g.setColor(Color.red);
g.drawString("YOU DIED", Game.WIDTH/2, Game.HEIGHT/2);
// drawing death
}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
GameMessages.java
import java.awt.Color;
import java.awt.Graphics;
public class GameMessages {
public static void string (Graphics g, String str, String color){
switch (color) {
case "green":
g.setColor(Color.green);
break;
case "red":
g.setColor(Color.red);
break;
default:
break;
}
g.drawString(str, Game.WIDTH/2, Game.HEIGHT/2);
}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Board.java
import java.awt.Color;
import java.awt.Graphics;
public class Board {
private static final int WIDTH = 600;
private static final int initialBoardSize = 100;
private int boardX = 300;
private int nextMove = 0;
private int boardSize= initialBoardSize;
public Board (){
//this.game = game;
}
public void move(){
if ((boardX+nextMove*5)>1&&(boardX+nextMove*2)< WIDTH-boardSize){
boardX=boardX+nextMove*5;
}
}
public void drawBoard (Graphics g){
g.setColor(Color.darkGray);
g.fillRect(boardX, 580, boardSize, 15);
}
public void setNextMove (int nextMove){
this.nextMove=nextMove;
}
public void setSize (int score){
if(this.boardSize>30){
this.boardSize=Board.initialBoardSize-5*(score/20);
}
}
public int getBoardX (){
return this.boardX;
}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
BoxBall.java
public class BoxBall {
public int x,y;
public static final int BOX_SIZE = 20;
public BoxBall(int x, int y){
this.x = x;
this.y = y;
}
public boolean equals(Object obj){
if(obj instanceof BoxBall){
BoxBall b = (BoxBall) obj;
return (this.x==b.x && this.y==b.y);
}
return false;
}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Brick.java
public class Brick {
public int x, y;
public static final int BRICK_SIZE = 50;
public Brick(int x, int y){
this.x = x;
this.y = y;
}
public String toString(){
return "[x=" + x + ", y=" + y + "]";
}
public boolean equals(Object obj){
if (obj instanceof Brick) {
Brick b = (Brick)obj;
return(this.x == b.x && this.y == b.y );
}
return false;
}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Bricks.java
import java.awt.Color;
import java.awt.Graphics;
import java.util.Collections;
import java.util.LinkedList;
public class Bricks {
public static LinkedList<Brick> body;
public Bricks(){
body = new LinkedList<>();
//WIDTH = 600, 600/12=50 for each brick.
Collections.addAll(body,
new Brick(0, 0),
new Brick(1, 0),
new Brick(2, 0),
new Brick(3, 0),
new Brick(4, 0),
new Brick(5, 0),
new Brick(6, 0),
new Brick(7, 0),
new Brick(8, 0),
new Brick(9, 0),
new Brick(10, 0),
new Brick(11, 0)
);
}
@SuppressWarnings("static-access")
public void drawBricks(Graphics g){
for (Brick brick : body) {
//Draw each brick [initial color was BLUE]
g.setColor(new Color(150,25,14));
g.fillRect(brick.x * brick.BRICK_SIZE, brick.y * brick.BRICK_SIZE,
brick.BRICK_SIZE, brick.BRICK_SIZE);
//Draw the border of each brick [initial color was BLACK]
g.setColor(new Color(174,137,118));
g.drawRect(brick.x * brick.BRICK_SIZE, brick.y * brick.BRICK_SIZE,
brick.BRICK_SIZE, brick.BRICK_SIZE);
}
}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
GameApp.java
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
@SuppressWarnings("serial")
public class GameApp extends Applet{
private Game game;
InputHandler keyReader;
public void init(){
game = new Game();
// Set the size of the canvas (game field).
// Shouldn't be bigger than applet size
//WIDTH+100 for the scoreboard
game.setPreferredSize(new Dimension(Game.WIDTH+100, Game.HEIGHT));
game.setBackground(Color.getHSBColor(0.56f, 0.3f, 0.8f));
// make the window visible
game.setVisible(true);
// make the window on focus
game.setFocusable(true);
this.add(game);
this.setVisible(true);
keyReader= new InputHandler(game);
}
public void paint(Graphics g){
// set the size of the applet (game window)
this.setSize(new Dimension(800, 650));
}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
InputHandler.java
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class InputHandler implements KeyListener {
public InputHandler(Game game)
{
game.addKeyListener(this);
}
@Override
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_LEFT){
Game.board.setNextMove (-1);//The board speed is 1
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
Game.board.setNextMove (1);
}
}
@Override
public void keyReleased(KeyEvent e) {
Game.board.setNextMove (0);
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}