import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/**
* Created by ... on 19.02.2017.
*/
public class gameplay extends JPanel implements ActionListener , KeyListener {
Timer tm= new Timer(5, this);
boolean play = false;
int score = 0;
int totalBricks = 28;
// Paddle
int xbat = 375 , velX = 0;
//Image img;
//Ball
int xball = 440, yball = 600;
int ballXdir = 5;
int ballYdir = -3;
public brickMap map;
public gameplay()
{
map = new brickMap(4,7);
tm.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
public void paint(Graphics g)
{
//------Hintergrundbild----------------------------------------------------------------------------------------------------------------
//ImageIcon bgpic = new ImageIcon("javagame\\src\\Menu_spielfenster\\rsc\\wallpaper.png");
//img = bgpic.getImage();
// g.drawImage(img, 0, 0, null);
//------------------------------------------------------------
g.setColor(Color.gray);
g.fillRect(0,0,900,900);
// Paddle
g.setColor(Color.blue);
g.fillRect(xbat, 700, 130, 20);
// Ball
g.setColor(Color.green);
g.fillOval(xball, yball, 26,26);
//Won
if(totalBricks <= 0){
play = false;
ballXdir = 0;
ballYdir = 0;
g.setColor(Color.YELLOW);
g.setFont(new Font("serif", Font.BOLD, 25));
g.drawString("You Won " + score,350 ,600);
g.setFont(new Font("serif", Font.BOLD, 25));
g.drawString("Mit Enter neustarten",350 ,860);
}
// Game Over
if(yball > 720){
play = false;
ballXdir = 0;
ballYdir = 0;
g.setColor(Color.YELLOW);
g.setFont(new Font("serif", Font.BOLD, 25));
g.drawString("Game Over, Score: " + score,350 ,600);
g.setFont(new Font("serif", Font.BOLD, 25));
g.drawString("Mit Enter neustarten",350 ,860);
}
// Bricks
map.draw((Graphics2D)g);
// score
g.setColor(Color.YELLOW);
g.setFont(new Font("serif", Font.BOLD, 25));
g.drawString(""+score,5 ,860);
g.dispose();
}
@Override
public void actionPerformed(ActionEvent e)
{
// Paddle----------------------------
if (xbat < 0) {
velX =0;
xbat = 0;
}
if (xbat > 770) {
velX = 0;
xbat = 770;
}
xbat = xbat + velX;
//----------------------------------
// Ball-----------------------------
//Paddle und ball detection
if(play) {
if(new Rectangle(xball, yball, 26, 26).intersects(new Rectangle(xbat, 700, 130, 20))) {
ballXdir = (int) (Math.random()*5);
if(ballXdir <3){
}
else{
ballXdir = -ballXdir;
}
ballYdir = -ballYdir;
}
//ball und bricks
A: for(int i = 0; i < map.map.length; i++){
for(int j = 0; j < map.map[0].length; j++){
if(map.map[i][j] > 0){
int brickX = j * map.brickWigth + 80;
int brickY = i * map.brickHeight + 50;
int brickWidth = map.brickWigth;
int brickHeight = map.brickHeight;
Rectangle rect = new Rectangle(brickX, brickY, brickWidth, brickHeight);
Rectangle ballRect = new Rectangle(xball, yball, 26, 26);
Rectangle brickRect = rect;
// Der Ball trifft auf die Bricks. Ein Brick wird abgezogen und 10 Punkte zur Score hinzugefügt
if(ballRect.intersects(brickRect)){
map.setBrickValue(0 , i , j);
totalBricks --;
score += 10;
if(xball + 26 <= brickRect.x || xball + 1 >= brickRect.x + brickRect.width){
ballXdir = -ballXdir;
} else {
ballYdir = -ballYdir;
}
break A;
}
}
}
}
xball += ballXdir;
yball += ballYdir;
if(xball < 0){
ballXdir = -ballXdir;
}
if(yball < 0){
ballYdir = -ballYdir;
}
if(xball > 874){
ballXdir = -ballXdir;
}
}
//----------------------------------
repaint();
}
@Override
public void keyPressed(KeyEvent e)
{
//Paddle------------------------------------------
int c = e.getKeyCode();
if (c == KeyEvent.VK_RIGHT){
play = true;
velX = +6;
}
if (c == KeyEvent.VK_LEFT){
play = true;
velX = -6;
}
//-----------------------------------------------
//Wenn Enter gedrückt wird
if(c == KeyEvent.VK_ENTER){
if(!play){
play = true;
xball = 440;
yball = 600;
ballXdir = 5;
ballYdir = -3;
xbat = 375;
score = 0;
totalBricks = 28;
map = new brickMap(4, 7);
repaint();
}
}
}
@Override
public void keyReleased(KeyEvent e)
{
//Paddle--------------------------------------
velX= 0;
//--------------------------------------------
}
@Override
public void keyTyped(KeyEvent e)
{
}
}