Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Difficulty Level Included #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Difficulty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

public enum Difficulty {
EASY,
MEDIUM,
HARD
}
141 changes: 81 additions & 60 deletions src/Gameplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Gameplay extends JPanel implements KeyListener, ActionListener
private int ballYdir = -2;

private MapGenerator map;
private Difficulty difficulty;

public Gameplay()
{
Expand All @@ -35,111 +36,131 @@ public Gameplay()
setFocusTraversalKeysEnabled(false);
timer=new Timer(delay,this);
timer.start();
setDifficulty(Difficulty.EASY);
}

public void setDifficulty(Difficulty difficulty) {
this.difficulty = difficulty;
switch (difficulty) {
case EASY:
ballXdir = -1;
ballYdir = -2;
break;
case MEDIUM:
ballXdir = -2;
ballYdir = -3;
break;
case HARD:
ballXdir = -3;
ballYdir = -4;
break;
}
}

public void paint(Graphics g)
{
// background
g.setColor(Color.black);
g.fillRect(1, 1, 692, 592);

// drawing map
map.draw((Graphics2D) g);

// borders
g.setColor(Color.yellow);
g.fillRect(0, 0, 3, 592);
g.fillRect(0, 0, 692, 3);
g.fillRect(691, 0, 3, 592);
// the scores

// the scores
g.setColor(Color.white);
g.setFont(new Font("serif",Font.BOLD, 25));
g.drawString(""+score, 590,30);

g.setFont(new Font("serif", Font.BOLD, 25));
g.drawString("Score: " + score, 590, 30);

// display difficulty level
g.drawString("Difficulty: " + difficulty, 20, 30);

// the paddle
g.setColor(Color.green);
g.fillRect(playerX, 550, 100, 8);

// the ball
g.setColor(Color.yellow);
g.fillOval(ballposX, ballposY, 20, 20);

// when you won the game
if(totalBricks <= 0)
{
play = false;
ballXdir = 0;
ballYdir = 0;
g.setColor(Color.RED);
g.setFont(new Font("serif",Font.BOLD, 30));
g.drawString("You Won", 260,300);

g.setColor(Color.RED);
g.setFont(new Font("serif",Font.BOLD, 20));
g.drawString("Press (Enter) to Restart", 230,350);
if (totalBricks <= 0) {
play = false;
ballXdir = 0;
ballYdir = 0;
g.setColor(Color.RED);
g.setFont(new Font("serif", Font.BOLD, 30));
g.drawString("You Won", 260, 300);

g.setColor(Color.RED);
g.setFont(new Font("serif", Font.BOLD, 20));
g.drawString("Press (Enter) to Restart", 230, 350);
}

// when you lose the game
if(ballposY > 570)
{
play = false;
ballXdir = 0;
ballYdir = 0;
g.setColor(Color.RED);
g.setFont(new Font("serif",Font.BOLD, 30));
g.drawString("Game Over, Scores: "+score, 190,300);

g.setColor(Color.RED);
g.setFont(new Font("serif",Font.BOLD, 20));
g.drawString("Press (Enter) to Restart", 230,350);
}

if (ballposY > 570) {
play = false;
ballXdir = 0;
ballYdir = 0;
g.setColor(Color.RED);
g.setFont(new Font("serif", Font.BOLD, 30));
g.drawString("Game Over, Scores: " + score, 190, 300);

g.setColor(Color.RED);
g.setFont(new Font("serif", Font.BOLD, 20));
g.drawString("Press (Enter) to Restart", 230, 350);
}

g.dispose();
}

public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
{
if(playerX >= 600)
{
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
if (playerX >= 600) {
playerX = 600;
}
else
{
} else {
moveRight();
}
}
}

if (e.getKeyCode() == KeyEvent.VK_LEFT)
{
if(playerX < 10)
{
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
if (playerX < 10) {
playerX = 10;
}
else
{
} else {
moveLeft();
}
}
if (e.getKeyCode() == KeyEvent.VK_ENTER)
{
if(!play)
{
}

if (e.getKeyCode() == KeyEvent.VK_ENTER) {
if (!play) {
play = true;
ballposX = 120;
ballposY = 350;
ballXdir = -1;
ballYdir = -2;
setDifficulty(difficulty); // Reset ball speed to the current difficulty level
playerX = 310;
score = 0;
totalBricks = 21;
map = new MapGenerator(3, 7);

repaint();
}
}
}

if (e.getKeyCode() == KeyEvent.VK_1) {
setDifficulty(Difficulty.EASY);
}

if (e.getKeyCode() == KeyEvent.VK_2) {
setDifficulty(Difficulty.MEDIUM);
}

if (e.getKeyCode() == KeyEvent.VK_3) {
setDifficulty(Difficulty.HARD);
}
}

public void keyReleased(KeyEvent e) {}
Expand Down
2 changes: 1 addition & 1 deletion src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static void main(String[] args) {
obj.setVisible(true);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.add(gamePlay);
obj.setVisible(true);
obj.setVisible(true);

}

Expand Down