SlideShare a Scribd company logo
1 of 13
Download to read offline
This is Java,
I am currently stumped on how to add a scoreboard for my game that I am making. I have
inclued my code and classes so far. Any help with a working scoreboard would be greatly
apperiacted.
Game.java
import javax.swing.JFrame;
public class Game {
public static void main(String[] args)
{
// create the frame
JFrame myFrame = new JFrame("Platformer");
// set up the close operation
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create panel
Mainpanel myPanel = new Mainpanel();
// add panel
myFrame.getContentPane().add(myPanel);
// pack
myFrame.pack();
// set visibility to true
myFrame.setVisible(true);
}
}
Mainpanel.java
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Mainpanel extends JPanel implements KeyListener, ActionListener{
private final int boardWidth =1000;
private final int boardHeight =1000;
int x = 0;
int y = 0;
int i= 0;
int goldCount=11;
int score = 0;
ImageIcon myIcon = new ImageIcon("./src/TreasureChest.png");
Timer mainTimer;
player player1;
player player2;
static ArrayList treasure = new ArrayList();
Random rand = new Random();
public String ScoreCount = "Score: " + score;
public Mainpanel()
{
setPreferredSize(new Dimension(boardWidth,boardHeight));
addKeyListener(this);
setFocusable(true);
player1= new player (100,100);
player2= new player (200,200);
addKeyListener(new move(player1));
addKeyListener(new move(player2));
mainTimer = new Timer(10,this);
mainTimer.start();
startGame();
}
JLabel scoreLabel = new JLabel("Score: 0");
public void paintComponent(Graphics page)
{
super.paintComponent(page);
Graphics2D g2d =(Graphics2D) page;
player1.draw(g2d);
player2.draw(g2d);
g2d.
g2d.setColor(new Color(128, 128, 128));
g2d.fillRect(0, 0, 50, 1000);
g2d.setColor(new Color(128, 128, 128));
g2d.fillRect(950, 0, 50, 1000);
g2d.setColor(new Color(128, 128, 128));
g2d.fillRect(50, 0, 900, 50);
g2d.setColor(new Color(128, 128, 128));;
g2d.fillRect(50, 950, 900, 50);
for (int i=0 ; i < treasure.size(); i++){
Gold tempGold = treasure.get(i);
tempGold.draw(g2d);
}
}
public void actionPerformed (ActionEvent arg0){
player1.update();
repaint();
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
public void addGold(Gold g){
treasure.add(g);
}
public static void removeGold (Gold g) {
treasure.remove(g);
}
public static ArrayList getGoldList() {
return treasure;
}
public void startGame() {
for (int i=0; i < goldCount; i++){
addGold(new Gold(rand.nextInt(boardWidth), rand.nextInt(boardHeight)));
}
}
public void someoneScored()
{
score++;
scoreLabel.setBounds(200, 200, 100, 100);
scoreLabel.setText("Score: " + score);
}
}
Player.java
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;
public class player extends Entity{
int velX=0;
int vely=0;
public player(int x, int y){
super(x, y);
}
public void update(){
y+= vely;
x+= velX;
checkCollisons();
checkOOB();
}
public void draw(Graphics2D g2d) {
g2d.drawImage(getPlayerImg(), x, y, null);
//g2d.draw(getBounds());
}
public Image getPlayerImg(){
ImageIcon ic = new ImageIcon("./src/TreasureChest.png");
return ic.getImage();
}
public void keyPressed(KeyEvent arg0){
int keyCode = arg0.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT)
{
velX=-10;
}
else if (keyCode == KeyEvent.VK_RIGHT)
{
velX=10;
}
else if (keyCode == KeyEvent.VK_UP)
{
vely=-10;
}
else if (keyCode == KeyEvent.VK_DOWN)
{
vely=10;
}
}
public void keyReleased(KeyEvent arg0){
int keyCode = arg0.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT)
{
velX=0;
}
else if (keyCode == KeyEvent.VK_RIGHT)
{
velX=0;
}
else if (keyCode == KeyEvent.VK_UP)
{
vely=0;
}
else if (keyCode == KeyEvent.VK_DOWN)
{
vely=0;
}
}
public void checkCollisons(){
ArrayList treasure = Mainpanel.getGoldList();
for (int i=0; i < treasure.size(); i++){
Gold tempGold= treasure.get(i);
if (getBounds().intersects(tempGold.getBounds())) {
Mainpanel.removeGold(tempGold);
}
}
}
public Rectangle getBounds(){
return new Rectangle(x,y, getPlayerImg().getWidth(null), getPlayerImg().getHeight(null));
}
private void checkOOB() {
if(x < 50) x = 50;
if(x > 900) x = 900;
if(y < 50) y = 50;
if(y > 915) y = 9;
}
}
Entity.java
import java.awt.Graphics2D;
public class Entity {
int x,y;
public Entity(int x, int y){
this.x= x;
this.y=y;
}
public void update(){
}
public void draw(Graphics2D g2d){
}
}
Move.java
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class move extends KeyAdapter {
player p;
public move(player player1){
p= player1;
}
public void keyPressed(KeyEvent e){
p.keyPressed(e);
}
public void keyReleased(KeyEvent e){
p.keyReleased(e);
}
}
Gold.java
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
public class Gold extends Entity {
public Gold(int x, int y){
super(x, y);
}
public void update(){
}
public void draw(Graphics2D g2d) {
g2d.drawImage(getGoldImg(), x, y, null);
//g2d.draw(getBounds());
}
public Image getGoldImg(){
ImageIcon ic = new ImageIcon("./src/TreasureChest.png");
return ic.getImage();
}
public Rectangle getBounds(){
return new Rectangle(x,y, getGoldImg().getWidth(null), getGoldImg().getHeight(null));
}
}
Solution
import java.applet.*;
import java.awt.*;
public class gamescore extends Applet implements Runnable
{
//instance fields
int x_pos=350;
int y_pos=250;
int p1_x=0;
int p1_y=200;
int p2_x=490;
int p2_y=200;
int radius=10;
int x_speed=1;
int y_speed=1;
final int paddle_width=20;
final int paddle_height=80;
final int ball_speed=25;
int p1_score=0;
int p2_score=0;
int p1_scoreKeeper=0;
int p2_scoreKeeper=0;
String score = p1_score+" "+p2_score;;
//handles mouse down events
public boolean mouseDown(Event e, int x, int y)
{
x_speed*=-1;
y_speed*=-1;
return true;
}
//handles keyboard events
public boolean keyDown(Event e, int key)
{
//p1 up control
if(key == Event.UP)
{
p1_y+=-6;
}
//p1 down control
if(key == Event.DOWN)
{
p1_y+=6;
}
return true;
}
//called the first time you enter the HTML site with the applet
public void init() {}
//called EVERY time you enter the HTML site with the applet
public void start()
{
//define a new thread
Thread th = new Thread(this);
//start thread
th.start();
}
//called if you leave the site with the applet
public void stop() {}
//called if you leave the page finally (e. g. closing browser)
public void destroy() {}
public void run()
{
//lower thread priority
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while(true)
{
//if ball leaves applet area, reset
if(y_pos>500+radius)
{
y_pos=100;
}
//ball bounces if it reaches bottom edge of applet
if(y_pos>308-radius)
{
y_speed+=-1;
}
//ball bounces if it reaches top edge of applet
if(y_pos<radius)
{
y_speed=1;
}
if(p1_score<20&&p2_score<20)
{
//move ball along x-axis
x_pos+=x_speed;
y_pos+=y_speed;
}
//SCORING
if(x_pos>600)
{
p1_scoreKeeper++;
//reset ball
x_pos=350;
y_pos=150;
}
//player 2 scores if ball goes past left edge
if(x_pos<0)
{
p2_scoreKeeper++;
//reset ball
x_pos=350;
y_pos=150;
}
//update score
p1_score+=p1_scoreKeeper;
p2_score+=p2_scoreKeeper;
//repaint the applet
repaint();
try
{
//stop thread for specified time
Thread.sleep(ball_speed);
}
catch(InterruptedException ex)
{
//do nothing
}
//set thread to max priority
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void paint (Graphics g)
{
//set color
g.setColor(Color.green);
//set text font
g.setFont (new Font ("Monospaced",Font.PLAIN,24));
//draw score
g.drawString(score, 400, 40);
//paint a filled colored circle
g.fillOval(x_pos-radius, y_pos-radius,radius*2,radius*2);
g.setColor(Color.pink);
//player 1 paddle
g.fillRect(p1_x, p1_y, paddle_width, paddle_height);
g.setColor(Color.white);
//player 2 paddle
g.fillRect(p2_x, p2_y, paddle_width, paddle_height);
}
}

More Related Content

Similar to This is Java,I am currently stumped on how to add a scoreboard for.pdf

Whenever I run my application my Game appears with the pict.pdf
Whenever I run my application my Game appears with the pict.pdfWhenever I run my application my Game appears with the pict.pdf
Whenever I run my application my Game appears with the pict.pdfaarthitimesgd
 
write a prgoram that displays four images or objects in a 2 x 2 grid.pdf
write a prgoram that displays four images or objects in a 2 x 2 grid.pdfwrite a prgoram that displays four images or objects in a 2 x 2 grid.pdf
write a prgoram that displays four images or objects in a 2 x 2 grid.pdfPRATIKSINHA7304
 
Games, AI, and Research - Part 2 Training (FightingICE AI Programming)
Games, AI, and Research - Part 2 Training (FightingICE AI Programming)Games, AI, and Research - Part 2 Training (FightingICE AI Programming)
Games, AI, and Research - Part 2 Training (FightingICE AI Programming)Pujana Paliyawan
 
Lec 10 10_sept [compatibility mode]
Lec 10 10_sept [compatibility mode]Lec 10 10_sept [compatibility mode]
Lec 10 10_sept [compatibility mode]Palak Sanghani
 
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docxCodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docxmary772
 
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docxCodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docxmccormicknadine86
 
Task Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdfTask Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdfcronkwurphyb44502
 
Connecting your phone and home with firebase and android things - James Cogga...
Connecting your phone and home with firebase and android things - James Cogga...Connecting your phone and home with firebase and android things - James Cogga...
Connecting your phone and home with firebase and android things - James Cogga...DroidConTLV
 
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdfImport java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdfapexcomputer54
 
import java.awt.FlowLayout;import java.awt.event.KeyEvent;import.pdf
import java.awt.FlowLayout;import java.awt.event.KeyEvent;import.pdfimport java.awt.FlowLayout;import java.awt.event.KeyEvent;import.pdf
import java.awt.FlowLayout;import java.awt.event.KeyEvent;import.pdfgalagirishp
 
Creating an Uber Clone - Part XXIV.pdf
Creating an Uber Clone - Part XXIV.pdfCreating an Uber Clone - Part XXIV.pdf
Creating an Uber Clone - Part XXIV.pdfShaiAlmog1
 
HTML5って必要?
HTML5って必要?HTML5って必要?
HTML5って必要?GCS2013
 
Making Games in JavaScript
Making Games in JavaScriptMaking Games in JavaScript
Making Games in JavaScriptSam Cartwright
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...DroidConTLV
 
The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189Mahmoud Samir Fayed
 
DAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docx
DAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docxDAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docx
DAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docxtheodorelove43763
 

Similar to This is Java,I am currently stumped on how to add a scoreboard for.pdf (20)

Whenever I run my application my Game appears with the pict.pdf
Whenever I run my application my Game appears with the pict.pdfWhenever I run my application my Game appears with the pict.pdf
Whenever I run my application my Game appears with the pict.pdf
 
J slider
J sliderJ slider
J slider
 
write a prgoram that displays four images or objects in a 2 x 2 grid.pdf
write a prgoram that displays four images or objects in a 2 x 2 grid.pdfwrite a prgoram that displays four images or objects in a 2 x 2 grid.pdf
write a prgoram that displays four images or objects in a 2 x 2 grid.pdf
 
Java oops features
Java oops featuresJava oops features
Java oops features
 
Games, AI, and Research - Part 2 Training (FightingICE AI Programming)
Games, AI, and Research - Part 2 Training (FightingICE AI Programming)Games, AI, and Research - Part 2 Training (FightingICE AI Programming)
Games, AI, and Research - Part 2 Training (FightingICE AI Programming)
 
Lec 10 10_sept [compatibility mode]
Lec 10 10_sept [compatibility mode]Lec 10 10_sept [compatibility mode]
Lec 10 10_sept [compatibility mode]
 
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docxCodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
 
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docxCodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
 
Android workshop
Android workshopAndroid workshop
Android workshop
 
Task Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdfTask Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdf
 
Connecting your phone and home with firebase and android things - James Cogga...
Connecting your phone and home with firebase and android things - James Cogga...Connecting your phone and home with firebase and android things - James Cogga...
Connecting your phone and home with firebase and android things - James Cogga...
 
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdfImport java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
 
import java.awt.FlowLayout;import java.awt.event.KeyEvent;import.pdf
import java.awt.FlowLayout;import java.awt.event.KeyEvent;import.pdfimport java.awt.FlowLayout;import java.awt.event.KeyEvent;import.pdf
import java.awt.FlowLayout;import java.awt.event.KeyEvent;import.pdf
 
Creating an Uber Clone - Part XXIV.pdf
Creating an Uber Clone - Part XXIV.pdfCreating an Uber Clone - Part XXIV.pdf
Creating an Uber Clone - Part XXIV.pdf
 
applet.docx
applet.docxapplet.docx
applet.docx
 
HTML5って必要?
HTML5って必要?HTML5って必要?
HTML5って必要?
 
Making Games in JavaScript
Making Games in JavaScriptMaking Games in JavaScript
Making Games in JavaScript
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
 
The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189
 
DAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docx
DAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docxDAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docx
DAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docx
 

More from anjandavid

I am exploring the basic components of advanced broadband networks..pdf
I am exploring the basic components of advanced broadband networks..pdfI am exploring the basic components of advanced broadband networks..pdf
I am exploring the basic components of advanced broadband networks..pdfanjandavid
 
How many different instances of the ls command are installed on .pdf
How many different instances of the ls command are installed on .pdfHow many different instances of the ls command are installed on .pdf
How many different instances of the ls command are installed on .pdfanjandavid
 
How are the Allen Bradley SLC 500 program files organizedSolutio.pdf
How are the Allen Bradley SLC 500 program files organizedSolutio.pdfHow are the Allen Bradley SLC 500 program files organizedSolutio.pdf
How are the Allen Bradley SLC 500 program files organizedSolutio.pdfanjandavid
 
Hi, I need help please, this is about KaseyaWich o the following .pdf
Hi, I need help please, this is about KaseyaWich o the following .pdfHi, I need help please, this is about KaseyaWich o the following .pdf
Hi, I need help please, this is about KaseyaWich o the following .pdfanjandavid
 
How did WWI and its aftermath provide African Americans with opportu.pdf
How did WWI and its aftermath provide African Americans with opportu.pdfHow did WWI and its aftermath provide African Americans with opportu.pdf
How did WWI and its aftermath provide African Americans with opportu.pdfanjandavid
 
For problems 3 and 4, consider the following functions that implemen.pdf
For problems 3 and 4, consider the following functions that implemen.pdfFor problems 3 and 4, consider the following functions that implemen.pdf
For problems 3 and 4, consider the following functions that implemen.pdfanjandavid
 
Give an example of a system. What are the Components, Attributes,.pdf
Give an example of a system. What are the Components, Attributes,.pdfGive an example of a system. What are the Components, Attributes,.pdf
Give an example of a system. What are the Components, Attributes,.pdfanjandavid
 
Discuss the attribution process and attribution errorsSolution.pdf
Discuss the attribution process and attribution errorsSolution.pdfDiscuss the attribution process and attribution errorsSolution.pdf
Discuss the attribution process and attribution errorsSolution.pdfanjandavid
 
E, an individual, received $40,000 of non-eligible dividends from Ca.pdf
E, an individual, received $40,000 of non-eligible dividends from Ca.pdfE, an individual, received $40,000 of non-eligible dividends from Ca.pdf
E, an individual, received $40,000 of non-eligible dividends from Ca.pdfanjandavid
 
Describe one (1) example in which laws granting freedom of the press.pdf
Describe one (1) example in which laws granting freedom of the press.pdfDescribe one (1) example in which laws granting freedom of the press.pdf
Describe one (1) example in which laws granting freedom of the press.pdfanjandavid
 
Briefly discuss 3–5 key trends in the modern health care operation.pdf
Briefly discuss 3–5 key trends in the modern health care operation.pdfBriefly discuss 3–5 key trends in the modern health care operation.pdf
Briefly discuss 3–5 key trends in the modern health care operation.pdfanjandavid
 
A student obtained the following data Mass of water in calorimeter 3.pdf
A student obtained the following data Mass of water in calorimeter 3.pdfA student obtained the following data Mass of water in calorimeter 3.pdf
A student obtained the following data Mass of water in calorimeter 3.pdfanjandavid
 
All answers must be in your own words.What is importance of the Wa.pdf
All answers must be in your own words.What is importance of the Wa.pdfAll answers must be in your own words.What is importance of the Wa.pdf
All answers must be in your own words.What is importance of the Wa.pdfanjandavid
 
Can someone please fix my code for a hashtable frequencey counter I.pdf
Can someone please fix my code for a hashtable frequencey counter I.pdfCan someone please fix my code for a hashtable frequencey counter I.pdf
Can someone please fix my code for a hashtable frequencey counter I.pdfanjandavid
 
A polycationic mRNA contains two or more promoter sequences. True Fal.pdf
A polycationic mRNA contains two or more promoter sequences. True Fal.pdfA polycationic mRNA contains two or more promoter sequences. True Fal.pdf
A polycationic mRNA contains two or more promoter sequences. True Fal.pdfanjandavid
 
Why are electrons shared in molecular compoundsWhy are electron.pdf
Why are electrons shared in molecular compoundsWhy are electron.pdfWhy are electrons shared in molecular compoundsWhy are electron.pdf
Why are electrons shared in molecular compoundsWhy are electron.pdfanjandavid
 
Which of these isare true of UDPa. It provides reliability, flow-c.pdf
Which of these isare true of UDPa. It provides reliability, flow-c.pdfWhich of these isare true of UDPa. It provides reliability, flow-c.pdf
Which of these isare true of UDPa. It provides reliability, flow-c.pdfanjandavid
 
Which liquid would BaCl Which liquid would BaCl 4. Which liquid.pdf
Which liquid would BaCl Which liquid would BaCl 4. Which liquid.pdfWhich liquid would BaCl Which liquid would BaCl 4. Which liquid.pdf
Which liquid would BaCl Which liquid would BaCl 4. Which liquid.pdfanjandavid
 
What is employee involvement What are some of the benefits of invol.pdf
What is employee involvement What are some of the benefits of invol.pdfWhat is employee involvement What are some of the benefits of invol.pdf
What is employee involvement What are some of the benefits of invol.pdfanjandavid
 
What are the pros and cons of technological leader versus technologi.pdf
What are the pros and cons of technological leader versus technologi.pdfWhat are the pros and cons of technological leader versus technologi.pdf
What are the pros and cons of technological leader versus technologi.pdfanjandavid
 

More from anjandavid (20)

I am exploring the basic components of advanced broadband networks..pdf
I am exploring the basic components of advanced broadband networks..pdfI am exploring the basic components of advanced broadband networks..pdf
I am exploring the basic components of advanced broadband networks..pdf
 
How many different instances of the ls command are installed on .pdf
How many different instances of the ls command are installed on .pdfHow many different instances of the ls command are installed on .pdf
How many different instances of the ls command are installed on .pdf
 
How are the Allen Bradley SLC 500 program files organizedSolutio.pdf
How are the Allen Bradley SLC 500 program files organizedSolutio.pdfHow are the Allen Bradley SLC 500 program files organizedSolutio.pdf
How are the Allen Bradley SLC 500 program files organizedSolutio.pdf
 
Hi, I need help please, this is about KaseyaWich o the following .pdf
Hi, I need help please, this is about KaseyaWich o the following .pdfHi, I need help please, this is about KaseyaWich o the following .pdf
Hi, I need help please, this is about KaseyaWich o the following .pdf
 
How did WWI and its aftermath provide African Americans with opportu.pdf
How did WWI and its aftermath provide African Americans with opportu.pdfHow did WWI and its aftermath provide African Americans with opportu.pdf
How did WWI and its aftermath provide African Americans with opportu.pdf
 
For problems 3 and 4, consider the following functions that implemen.pdf
For problems 3 and 4, consider the following functions that implemen.pdfFor problems 3 and 4, consider the following functions that implemen.pdf
For problems 3 and 4, consider the following functions that implemen.pdf
 
Give an example of a system. What are the Components, Attributes,.pdf
Give an example of a system. What are the Components, Attributes,.pdfGive an example of a system. What are the Components, Attributes,.pdf
Give an example of a system. What are the Components, Attributes,.pdf
 
Discuss the attribution process and attribution errorsSolution.pdf
Discuss the attribution process and attribution errorsSolution.pdfDiscuss the attribution process and attribution errorsSolution.pdf
Discuss the attribution process and attribution errorsSolution.pdf
 
E, an individual, received $40,000 of non-eligible dividends from Ca.pdf
E, an individual, received $40,000 of non-eligible dividends from Ca.pdfE, an individual, received $40,000 of non-eligible dividends from Ca.pdf
E, an individual, received $40,000 of non-eligible dividends from Ca.pdf
 
Describe one (1) example in which laws granting freedom of the press.pdf
Describe one (1) example in which laws granting freedom of the press.pdfDescribe one (1) example in which laws granting freedom of the press.pdf
Describe one (1) example in which laws granting freedom of the press.pdf
 
Briefly discuss 3–5 key trends in the modern health care operation.pdf
Briefly discuss 3–5 key trends in the modern health care operation.pdfBriefly discuss 3–5 key trends in the modern health care operation.pdf
Briefly discuss 3–5 key trends in the modern health care operation.pdf
 
A student obtained the following data Mass of water in calorimeter 3.pdf
A student obtained the following data Mass of water in calorimeter 3.pdfA student obtained the following data Mass of water in calorimeter 3.pdf
A student obtained the following data Mass of water in calorimeter 3.pdf
 
All answers must be in your own words.What is importance of the Wa.pdf
All answers must be in your own words.What is importance of the Wa.pdfAll answers must be in your own words.What is importance of the Wa.pdf
All answers must be in your own words.What is importance of the Wa.pdf
 
Can someone please fix my code for a hashtable frequencey counter I.pdf
Can someone please fix my code for a hashtable frequencey counter I.pdfCan someone please fix my code for a hashtable frequencey counter I.pdf
Can someone please fix my code for a hashtable frequencey counter I.pdf
 
A polycationic mRNA contains two or more promoter sequences. True Fal.pdf
A polycationic mRNA contains two or more promoter sequences. True Fal.pdfA polycationic mRNA contains two or more promoter sequences. True Fal.pdf
A polycationic mRNA contains two or more promoter sequences. True Fal.pdf
 
Why are electrons shared in molecular compoundsWhy are electron.pdf
Why are electrons shared in molecular compoundsWhy are electron.pdfWhy are electrons shared in molecular compoundsWhy are electron.pdf
Why are electrons shared in molecular compoundsWhy are electron.pdf
 
Which of these isare true of UDPa. It provides reliability, flow-c.pdf
Which of these isare true of UDPa. It provides reliability, flow-c.pdfWhich of these isare true of UDPa. It provides reliability, flow-c.pdf
Which of these isare true of UDPa. It provides reliability, flow-c.pdf
 
Which liquid would BaCl Which liquid would BaCl 4. Which liquid.pdf
Which liquid would BaCl Which liquid would BaCl 4. Which liquid.pdfWhich liquid would BaCl Which liquid would BaCl 4. Which liquid.pdf
Which liquid would BaCl Which liquid would BaCl 4. Which liquid.pdf
 
What is employee involvement What are some of the benefits of invol.pdf
What is employee involvement What are some of the benefits of invol.pdfWhat is employee involvement What are some of the benefits of invol.pdf
What is employee involvement What are some of the benefits of invol.pdf
 
What are the pros and cons of technological leader versus technologi.pdf
What are the pros and cons of technological leader versus technologi.pdfWhat are the pros and cons of technological leader versus technologi.pdf
What are the pros and cons of technological leader versus technologi.pdf
 

Recently uploaded

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 

Recently uploaded (20)

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 

This is Java,I am currently stumped on how to add a scoreboard for.pdf

  • 1. This is Java, I am currently stumped on how to add a scoreboard for my game that I am making. I have inclued my code and classes so far. Any help with a working scoreboard would be greatly apperiacted. Game.java import javax.swing.JFrame; public class Game { public static void main(String[] args) { // create the frame JFrame myFrame = new JFrame("Platformer"); // set up the close operation myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // create panel Mainpanel myPanel = new Mainpanel(); // add panel myFrame.getContentPane().add(myPanel); // pack myFrame.pack(); // set visibility to true myFrame.setVisible(true); } } Mainpanel.java import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList; import java.util.Random; import java.awt.Color;
  • 2. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class Mainpanel extends JPanel implements KeyListener, ActionListener{ private final int boardWidth =1000; private final int boardHeight =1000; int x = 0; int y = 0; int i= 0; int goldCount=11; int score = 0; ImageIcon myIcon = new ImageIcon("./src/TreasureChest.png"); Timer mainTimer; player player1; player player2; static ArrayList treasure = new ArrayList(); Random rand = new Random(); public String ScoreCount = "Score: " + score; public Mainpanel() { setPreferredSize(new Dimension(boardWidth,boardHeight)); addKeyListener(this); setFocusable(true); player1= new player (100,100); player2= new player (200,200); addKeyListener(new move(player1));
  • 3. addKeyListener(new move(player2)); mainTimer = new Timer(10,this); mainTimer.start(); startGame(); } JLabel scoreLabel = new JLabel("Score: 0"); public void paintComponent(Graphics page) { super.paintComponent(page); Graphics2D g2d =(Graphics2D) page; player1.draw(g2d); player2.draw(g2d); g2d. g2d.setColor(new Color(128, 128, 128)); g2d.fillRect(0, 0, 50, 1000); g2d.setColor(new Color(128, 128, 128)); g2d.fillRect(950, 0, 50, 1000); g2d.setColor(new Color(128, 128, 128)); g2d.fillRect(50, 0, 900, 50); g2d.setColor(new Color(128, 128, 128));; g2d.fillRect(50, 950, 900, 50); for (int i=0 ; i < treasure.size(); i++){ Gold tempGold = treasure.get(i); tempGold.draw(g2d); }
  • 4. } public void actionPerformed (ActionEvent arg0){ player1.update(); repaint(); } @Override public void keyPressed(KeyEvent arg0) { // TODO Auto-generated method stub } @Override public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } public void addGold(Gold g){ treasure.add(g); } public static void removeGold (Gold g) { treasure.remove(g); } public static ArrayList getGoldList() { return treasure; } public void startGame() { for (int i=0; i < goldCount; i++){ addGold(new Gold(rand.nextInt(boardWidth), rand.nextInt(boardHeight)));
  • 5. } } public void someoneScored() { score++; scoreLabel.setBounds(200, 200, 100, 100); scoreLabel.setText("Score: " + score); } } Player.java import java.awt.Graphics2D; import java.awt.Image; import java.awt.Rectangle; import java.awt.event.KeyEvent; import java.util.ArrayList; import javax.swing.ImageIcon; public class player extends Entity{ int velX=0; int vely=0; public player(int x, int y){ super(x, y); } public void update(){ y+= vely; x+= velX; checkCollisons(); checkOOB(); } public void draw(Graphics2D g2d) { g2d.drawImage(getPlayerImg(), x, y, null);
  • 6. //g2d.draw(getBounds()); } public Image getPlayerImg(){ ImageIcon ic = new ImageIcon("./src/TreasureChest.png"); return ic.getImage(); } public void keyPressed(KeyEvent arg0){ int keyCode = arg0.getKeyCode(); if (keyCode == KeyEvent.VK_LEFT) { velX=-10; } else if (keyCode == KeyEvent.VK_RIGHT) { velX=10; } else if (keyCode == KeyEvent.VK_UP) { vely=-10; } else if (keyCode == KeyEvent.VK_DOWN) { vely=10; } } public void keyReleased(KeyEvent arg0){ int keyCode = arg0.getKeyCode(); if (keyCode == KeyEvent.VK_LEFT) { velX=0;
  • 7. } else if (keyCode == KeyEvent.VK_RIGHT) { velX=0; } else if (keyCode == KeyEvent.VK_UP) { vely=0; } else if (keyCode == KeyEvent.VK_DOWN) { vely=0; } } public void checkCollisons(){ ArrayList treasure = Mainpanel.getGoldList(); for (int i=0; i < treasure.size(); i++){ Gold tempGold= treasure.get(i); if (getBounds().intersects(tempGold.getBounds())) { Mainpanel.removeGold(tempGold); } } } public Rectangle getBounds(){ return new Rectangle(x,y, getPlayerImg().getWidth(null), getPlayerImg().getHeight(null)); } private void checkOOB() { if(x < 50) x = 50; if(x > 900) x = 900; if(y < 50) y = 50; if(y > 915) y = 9; }
  • 8. } Entity.java import java.awt.Graphics2D; public class Entity { int x,y; public Entity(int x, int y){ this.x= x; this.y=y; } public void update(){ } public void draw(Graphics2D g2d){ } } Move.java import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; public class move extends KeyAdapter { player p; public move(player player1){ p= player1; } public void keyPressed(KeyEvent e){ p.keyPressed(e); } public void keyReleased(KeyEvent e){
  • 9. p.keyReleased(e); } } Gold.java import java.awt.Graphics2D; import java.awt.Image; import java.awt.Rectangle; import javax.swing.ImageIcon; public class Gold extends Entity { public Gold(int x, int y){ super(x, y); } public void update(){ } public void draw(Graphics2D g2d) { g2d.drawImage(getGoldImg(), x, y, null); //g2d.draw(getBounds()); } public Image getGoldImg(){ ImageIcon ic = new ImageIcon("./src/TreasureChest.png"); return ic.getImage(); } public Rectangle getBounds(){ return new Rectangle(x,y, getGoldImg().getWidth(null), getGoldImg().getHeight(null)); } } Solution import java.applet.*;
  • 10. import java.awt.*; public class gamescore extends Applet implements Runnable { //instance fields int x_pos=350; int y_pos=250; int p1_x=0; int p1_y=200; int p2_x=490; int p2_y=200; int radius=10; int x_speed=1; int y_speed=1; final int paddle_width=20; final int paddle_height=80; final int ball_speed=25; int p1_score=0; int p2_score=0; int p1_scoreKeeper=0; int p2_scoreKeeper=0; String score = p1_score+" "+p2_score;; //handles mouse down events public boolean mouseDown(Event e, int x, int y) { x_speed*=-1; y_speed*=-1; return true; } //handles keyboard events public boolean keyDown(Event e, int key) { //p1 up control if(key == Event.UP) { p1_y+=-6; }
  • 11. //p1 down control if(key == Event.DOWN) { p1_y+=6; } return true; } //called the first time you enter the HTML site with the applet public void init() {} //called EVERY time you enter the HTML site with the applet public void start() { //define a new thread Thread th = new Thread(this); //start thread th.start(); } //called if you leave the site with the applet public void stop() {} //called if you leave the page finally (e. g. closing browser) public void destroy() {} public void run() { //lower thread priority Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while(true) { //if ball leaves applet area, reset if(y_pos>500+radius) { y_pos=100; } //ball bounces if it reaches bottom edge of applet if(y_pos>308-radius) {
  • 12. y_speed+=-1; } //ball bounces if it reaches top edge of applet if(y_pos<radius) { y_speed=1; } if(p1_score<20&&p2_score<20) { //move ball along x-axis x_pos+=x_speed; y_pos+=y_speed; } //SCORING if(x_pos>600) { p1_scoreKeeper++; //reset ball x_pos=350; y_pos=150; } //player 2 scores if ball goes past left edge if(x_pos<0) { p2_scoreKeeper++; //reset ball x_pos=350; y_pos=150; } //update score p1_score+=p1_scoreKeeper; p2_score+=p2_scoreKeeper; //repaint the applet repaint(); try
  • 13. { //stop thread for specified time Thread.sleep(ball_speed); } catch(InterruptedException ex) { //do nothing } //set thread to max priority Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } public void paint (Graphics g) { //set color g.setColor(Color.green); //set text font g.setFont (new Font ("Monospaced",Font.PLAIN,24)); //draw score g.drawString(score, 400, 40); //paint a filled colored circle g.fillOval(x_pos-radius, y_pos-radius,radius*2,radius*2); g.setColor(Color.pink); //player 1 paddle g.fillRect(p1_x, p1_y, paddle_width, paddle_height); g.setColor(Color.white); //player 2 paddle g.fillRect(p2_x, p2_y, paddle_width, paddle_height); } }