I need some help with my Java breakout program. I need to know how to create a s
ID: 3569310 • Letter: I
Question
I need some help with my Java breakout program. I need to know how to create a scoreboard, and create a start and pause button. ANY HELP WOULD BE APPRECIATED!
public class GameGUI extends JFrame implements ActionListener, KeyListener{
public BufferedImage offScreenImage;
Graphics2D g2;
public static GamePanel GP;
private Ball ball;
private Paddle paddle;
private Timer timer;
private ArrayList<Brick> bricks = new ArrayList<>();
static JLabel score, lives;
static JButton restart;
static JFrame frame;
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GameGUI();
}
});
}
public GameGUI() {
initFrameComponents();
initGameComponents();
offScreenImage = new BufferedImage(GP.getWidth(), GP.getHeight(), BufferedImage.TYPE_INT_RGB);
g2 = (Graphics2D) offScreenImage.getGraphics();
g2.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
this.addKeyListener(this);
System.out.println("width ["+GP.getWidth()+"] : height ["+GP.getHeight()+"]");
timer = new Timer(10,this);
timer.setInitialDelay(0);
timer.start();
refreshScreen();
}
private void initFrameComponents() {
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Breakout!");
GP = new GamePanel();
GP.setPreferredSize(new Dimension(900, 700));
this.add(GP);
this.pack();
}
private void initGameComponents() {
ball = new Ball(GP.getWidth()/2 -10, GP.getHeight()/2 -10, 20);
paddle = new Paddle(GP.getWidth()/2 -50, 650, 100,15);
for(int i = 0; i < 900; i+=100) {
for(int j = 50; j < 50+(25*5); j+=25) {
bricks.add(new Brick(i+10, j, 80, 15));
}
}
}
private void refreshScreen() {
g2.setColor(Color.BLACK);
g2.fillRect(0, 0, GP.getWidth(), GP.getHeight());
ball.drawBall(g2);
paddle.drawPaddle(g2);
for(Brick b: bricks) {
b.drawBrick(g2);
}
//draw other components
GP.repaint();
}
public void gameOverScreen()
{
g2.setColor(Color.BLACK);
g2.fillRect(0, 0, GP.getWidth(), GP.getHeight());
g2.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 40));
g2.setColor(Color.WHITE);
g2.drawString("You Lose", GP.getWidth()/2-60, GP.getHeight()/2);
GP.repaint();
}
@Override
public void keyPressed(KeyEvent event) {
int direction = 0;
if(event.getKeyCode() == KeyEvent.VK_LEFT)direction = -1;
if(event.getKeyCode() == KeyEvent.VK_RIGHT)direction = 1;
paddle.movePaddle(direction*50);
refreshScreen();
}
public void keyReleased2(KeyEvent arg0) {}
public void keyTyped2(KeyEvent arg0) {}
@Override
public void actionPerformed(ActionEvent arg0) {
ball.move();
if(ball.intersects(paddle))
{
if(ball.x+(ball.width/2) < paddle.x+(paddle.width/6))
{
ball.setXspeed(-3);
}
else if(ball.x+(ball.width/2) < paddle.x+(paddle.width/3))
{
ball.setXspeed(-2);
}
else if(ball.x+(ball.width/2) < paddle.x+(paddle.width/2))
{
ball.setXspeed(-1);
}
else if(ball.x+(ball.width/2) < paddle.x+(2*paddle.width/3))
{
ball.setXspeed(1);
}
else if(ball.x+(ball.width/2) < paddle.x+(2*paddle.width/3)+(paddle.width/6))
{
ball.setXspeed(2);
}
else
{
ball.setXspeed(3);
}
ball.reverseSpeed('y');
while(ball.intersects(paddle))
{
ball.move();
}
}
for(int i = 0; i < bricks.size(); i++)
{
Brick b = bricks.get(i);
if(ball.intersects(b))
{
ball.reverseSpeed('y');
while(ball.intersects(b))
{
ball.move();
}
if(b.getHitNum() != 0)bricks.remove(i);
else b.hitNumUP();
}
}
if(ball.x < 0 || ball.x +ball.getWidth() > GP.getWidth())
{
ball.reverseSpeed('x');
}
if(ball.y < 0 )
{
ball.reverseSpeed('y');
}
if(ball.y + ball.getHeight() > GP.getHeight())
{
this.removeKeyListener(this);
timer.stop();
gameOverScreen();
}
else
{
refreshScreen();
}
}
public class GamePanel extends JPanel {
public void paintComponent(Graphics g) {
g.drawImage(offScreenImage, 0, 0, null);
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
Explanation / Answer
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
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#include<alloc.h>
void Push(int, node **);
void Display(node **);
int Pop(node **);
int Sempty(node *);
typedef struct stack {
int data;
struct stack *next;
} node;
void main() {
node *top;
int data, item, choice;
char ans, ch;
clrscr();
top = NULL;
printf(" Stack Using Linked List : nn");
do {
printf(" The main menu");
printf(" 1.Push 2.Pop 3.Display 4.Exit");
printf(" Enter Your Choice");
scanf("%d", &choice);
switch (choice) {
case 1:
printf(" Enter the data");
scanf("%d", &data);
Push(data, &top);
break;
case 2:
if (Sempty(top))
printf(" Stack underflow!");
else {
item = Pop(&top);
printf(" The popped node is%d", item);
}
break;
case 3:
Display(&top);
break;
case 4:
printf(" Do You want To Quit?(y/n)");
ch = getche();
if (ch == 'y')
exit(0);
else
break;
}
printf(" Do you want to continue?");
ans = getche();
getch();
clrscr();
} while (ans == 'Y' || ans == 'y');
getch();
}
void Push(int Item, node **top) {
node *New;
node * get_node(int);
New = get_node(Item);
New->next = *top;
*top = New;
}
node * get_node(int item) {
node * temp;
temp = (node *) malloc(sizeof(node));
if (temp == NULL)
printf(" Memory Cannot be allocated");
temp->data = item;
temp->next = NULL;
return (temp);
}
int Sempty(node *temp) {
if (temp == NULL)
return 1;
else
return 0;
}
int Pop(node **top) {
int item;
node *temp;
item = (*top)->data;
temp = *top;
*top = (*top)->next;
free(temp);
return (item);
}
void Display(node **head) {
node *temp;
temp = *head;
if (Sempty(temp))
printf(" The stack is empty!");
else {
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
}
getch();
}
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
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#include<alloc.h>
void Push(int, node **);
void Display(node **);
int Pop(node **);
int Sempty(node *);
typedef struct stack {
int data;
struct stack *next;
} node;
void main() {
node *top;
int data, item, choice;
char ans, ch;
clrscr();
top = NULL;
printf(" Stack Using Linked List : nn");
do {
printf(" The main menu");
printf(" 1.Push 2.Pop 3.Display 4.Exit");
printf(" Enter Your Choice");
scanf("%d", &choice);
switch (choice) {
case 1:
printf(" Enter the data");
scanf("%d", &data);
Push(data, &top);
break;
case 2:
if (Sempty(top))
printf(" Stack underflow!");
else {
item = Pop(&top);
printf(" The popped node is%d", item);
}
break;
case 3:
Display(&top);
break;
case 4:
printf(" Do You want To Quit?(y/n)");
ch = getche();
if (ch == 'y')
exit(0);
else
break;
}
printf(" Do you want to continue?");
ans = getche();
getch();
clrscr();
} while (ans == 'Y' || ans == 'y');
getch();
}
void Push(int Item, node **top) {
node *New;
node * get_node(int);
New = get_node(Item);
New->next = *top;
*top = New;
}
node * get_node(int item) {
node * temp;
temp = (node *) malloc(sizeof(node));
if (temp == NULL)
printf(" Memory Cannot be allocated");
temp->data = item;
temp->next = NULL;
return (temp);
}
int Sempty(node *temp) {
if (temp == NULL)
return 1;
else
return 0;
}
int Pop(node **top) {
int item;
node *temp;
item = (*top)->data;
temp = *top;
*top = (*top)->next;
free(temp);
return (item);
}
void Display(node **head) {
node *temp;
temp = *head;
if (Sempty(temp))
printf(" The stack is empty!");
else {
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
}
getch();
}