Results 1 to 17 of 17
Thread: Java Score Not Working
- 12-16-2011, 01:52 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Java Score Not Working
Hello, I am working on a school project, this is my first semester doing Java. I have managed to do everything without a problem except for the score of my game. Whenever the bullet hits the disc the score is supposed to go up 1, but it has been going up by 40 each time the bullet hits. Any help would be appreciated here is my code:
Java Code:import java.applet.*; import java.awt.*; import java.awt.event.*; public class Cannon_Finished extends Applet implements Runnable { // Variables for application int appwidth = 700; int appheight = 400; int skyheight = 300; // Variables for gun int width = 6; int height = 50; int top_x = 350; int top_y = 280; int speed = 13; // gun base int base_width = 36; int base_height = 50; int base_x = top_x - 15; int base_y = top_y + (height / 2); // bullet boolean bullet = false; int bullet_x = top_x; int bullet_y = top_y; int bullet_speed = 6; int bullet_height = 2; // disc boolean disc = false; int disc_height = 15; int disc_width = 50; int disc_speed = 4; int disc_x = 15; int disc_y = 30; //disc 2 boolean disc2 = false; int disc2_height = 15; int disc2_width = 50; int disc2_speed = 3; int disc2_x = appwidth - 15; int disc2_y = 50; boolean disc3 = false; int disc3_height = 15; int disc3_width = 50; int disc3_speed = 3; int disc3_x = appwidth - 15; int disc3_y = 50; int lives = 15; int score = 0; AudioClip bang; public void init() { setSize(appwidth, appheight); bang = getAudioClip(getCodeBase(), "explosion.au"); } public void start() { Thread th = new Thread(this); th.start(); } public void run() { Thread.currentThread().setPriority(Thread.MIN_PRIO RITY); while(true) { if(disc) disc_x = disc_x +(int)(Math.random()*3); if (disc_x>=appwidth){ disc_x = disc_x -1; lives = lives-1; disc = false; } if (bullet_y <= disc_y && bullet_x > disc_x && bullet_x < disc_x + disc_width) { disc = false; score = score+1; } if(disc2){ disc2_x = disc2_x -2; if (disc2_x <=appwidth-appwidth){ lives = lives-1; disc2 = false; } } if (bullet_y <= disc2_y && bullet_x > disc2_x && bullet_x < disc2_x + disc2_width) { disc2 = false; score = score+1; } if(disc3){ disc3_x = disc3_x -1; if (disc3_x <=appwidth-appwidth){ lives = lives-1; disc3 = false; } } if (bullet_y <= disc3_y && bullet_x > disc3_x && bullet_x < disc3_x + disc3_width) { disc3 = false; score = score+1; } draw_bullet(); repaint(); try { Thread.sleep(5); } catch(InterruptedException x) { } Thread.currentThread().setPriority(Thread.MAX_PRIO RITY); } } private void draw_bullet() { if(bullet) { bullet_y = bullet_y - bullet_speed; bullet_x = top_x; if(bullet_y < 0) { bullet_y = top_y; bullet = false; } } } public void paint(Graphics g) { // Draw sky g.setColor(Color.CYAN); g.fillRect(0, 0, appwidth, skyheight); // Draw grass g.setColor(Color.GREEN); g.fillRect(0, skyheight, appwidth, appheight-skyheight); // Draw gun g.setColor(new Color(126, 34, 23)); g.fillRect(top_x, top_y, width, height); // gun base g.fillArc(base_x, base_y, base_width, base_height, 0, 180); // Draw bullet if(bullet) { g.setColor(Color.BLACK); g.fillOval(bullet_x, bullet_y, width, width); } //Draw score g.drawString("Score " +(int) score, appwidth - 70, top_y - 20); g.drawString("Lives " + (int) lives, 10, top_y - 20); // Draw disc if(disc) { g.setColor(Color.RED); g.fillRect(disc_x, disc_y, disc_width, disc_height); } //draw disc2 if(disc2) { g.setColor(Color.BLACK); g.fillRect(disc2_x, disc2_y, disc2_width, disc2_height); } //draw disc3 if(disc3) { g.setColor(Color.GRAY); g.fillRect(disc3_x, disc3_y, disc3_width, disc3_height); } if(lives <=0){ g.setColor(Color.RED); g.fillRect(0, 0, appwidth, appheight); g.setColor(Color.BLACK); g.drawString("You Lose!!", 150, 150); } } // Keyboard // ************************ KeyDown ************************************* public boolean keyDown(Event evt, int key) { if (key == Event.LEFT) { top_x = top_x - speed; if(top_x <= 0) top_x = 0; } if (key == Event.RIGHT) { top_x = top_x + speed; if(top_x >= appwidth - width) top_x = appwidth - width; } if(key == Event.UP) top_x = 0; if(key == Event.DOWN) top_x = appwidth - width; if (key == Event.ESCAPE) { disc = true; disc2 = true; disc3 = true; disc_x = 0; disc_y = (int)(Math.random()*200); disc2_y = (int)(Math.random()*200); disc2_x = appwidth; disc3_x = appwidth; disc3_y = (int)(Math.random()*200); } // base position base_x = top_x - 15; base_y = top_y + (height / 2); if(key == 32) { bullet = true; bang.play(); } return true; } // ************************ KeyUp ************************************* public boolean keyUp(Event evt, int key) { return true; } // Double buffering private Image dbImage; private Graphics dbg; /** Update - Method, implements double buffering */ public void update(Graphics g) { // initialize buffer if (dbImage == null) { dbImage = createImage(this.getSize().width, this.getSize().height); dbg = dbImage.getGraphics(); } // clear screen in background dbg.setColor(getBackground()); dbg.fillRect(0, 0, this.getSize().width, this.getSize().height); // draw elements in background dbg.setColor(getForeground()); paint(dbg); // draw image on the screen g.drawImage(dbImage, 0, 0, this); } }Last edited by Norm; 12-16-2011 at 01:56 AM. Reason: added code tags
- 12-16-2011, 01:58 AM #2
Re: Java Score Not Working
Try debugging your code by adding printlns every where the score is changed. Print out the new value.it has been going up by 40 each time the bullet hits.
The print out should help you figure out what is wrong with your code.
- 12-16-2011, 02:02 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Re: Java Score Not Working
I realized its not exactly 40 each time its like a random number between 10-70. I'm very new to java I'm sorry I'm not even sure how to add printlns, if you could tell me how to do that I would. Sorry for my idiocy once again, and thank for your help sir.
- 12-16-2011, 02:07 AM #4
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Re: Java Score Not Working
I have found that the statement:
if (bullet_y <= disc_y && bullet_x > disc_x && bullet_x < disc_x + disc_width)
has made the bullet go through it multiple times and the score would therefore increase by more than 1.
Does anyone know how to change this so the score will only increase once when the bullet and disc hit?
- 12-16-2011, 02:11 AM #5
Re: Java Score Not Working
What are the conditions that controls whether you increase the score? How many times can the bullet continue to hit the target? Is the first time enough?Does anyone know how to change this so the score will only increase once when the bullet and disc hit?
Then remove/destroy the bullet after its first contact with the target.
- 12-16-2011, 02:14 AM #6
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Re: Java Score Not Working
I typed:
if (bullet_y <= disc_y && bullet_x > disc_x && bullet_x < disc_x + disc_width) {
bullet = false;
score = score+1;
disc = false;
This made the score go up almost infinitely, and then the cannon stopped shooting the bullets. Am I doing something more wrong?
- 12-16-2011, 02:25 AM #7
Re: Java Score Not Working
You need to look at the program's logic to see what it is doing.Am I doing something more wrong?
Remember the computer will do EXACTLY what you tell it to do. So check your code to make sure you are telling it what you want it to do. It is not very good are reading minds, so you must be very specific with your instructions to tell it what to do.
- 12-16-2011, 02:34 AM #8
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Re: Java Score Not Working
This is the logic I put in, (or at least think i did):
When esc is pressed all the discs become true, and begin going across the screen, some from left to right, others from left to right, this works.
When the bullet hits any disc, the disc disappears. (Works)
When the bullet reached the top of the applet, the bullet = false. (Works)
Now, from what I understand when the bullet hits the disc, the bullet = false, and should go away.
When this happens, I say the score should increase 1.
That is what I think I did.
- 12-16-2011, 02:35 AM #9
Re: Java Score Not Working
What makes the score continue to increase after you set bullet = false?
- 12-16-2011, 02:40 AM #10
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Re: Java Score Not Working
I'm not sure, I set it so when I press esc the bullet=true again, (line 254)
but the bullet does not shoot after the first time the bullet hits the disc.
Could it be that although the bullet and disc does not show up on the screen anymore they are still interacting with each other?
This is the one thing I cannot figure out, I did everything without really running into any issues.
- 12-16-2011, 02:42 AM #11
Re: Java Score Not Working
What makes the score continue to increase?
You have an if test to control when the score will increase.
When will that if test not be true?
- 12-16-2011, 02:45 AM #12
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Re: Java Score Not Working
Hmm...yeah I'm sorry I feel like a complete fool but I'm not quite sure I follow what you're saying, I need to make a test to see when the score won't increase?
- 12-16-2011, 02:52 AM #13
Re: Java Score Not Working
Your code has a problem. It continues to increase the value of score.
WHY????
You need to change something so that the program does not execute the: score = score+1; statement.
That statement's execution is controlled by an if statement.
Look at the conditions the if statement tests and see why it becomes true when you do NOT want it to be true.
- 12-16-2011, 02:55 AM #14
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Re: Java Score Not Working
It is because this statement...
if (bullet_y <= disc3_y && bullet_x > disc3_x && bullet_x < disc3_x + disc3_width) {
happens multiple times, so each time this statement happens the score increases by 1, so is there any way I can change this statement to only occur once?
- 12-16-2011, 03:15 AM #15
Re: Java Score Not Working
Add the test of a boolean variable to the if condition.
Set the boolean false when the if statement is true.
Somewhere else you will have to set it true to for that if condition to ever be true again.
- 12-16-2011, 03:20 AM #16
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Re: Java Score Not Working
First of all I want to thank you for all the time you've spent helping me it means a lot.
Secondly, my java idiocy shines brightly once again, I'm really new and don't quite understand how to do this.
Could you go into a little more detail?
Sorry once again.
- 12-16-2011, 03:31 AM #17
Similar Threads
-
Java Score Not Working
By emukherji in forum Java AppletsReplies: 2Last Post: 12-16-2011, 02:02 AM -
Score Counter In Java GUI.
By UnAccomplishedJavaPerson in forum New To JavaReplies: 9Last Post: 11-18-2011, 01:47 PM -
I need a java program that takes scores and ouputs average, highest, and lowest score
By TheSweetHelloKitty in forum New To JavaReplies: 4Last Post: 02-15-2011, 11:05 PM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks