Results 1 to 3 of 3
  1. #1
    emukherji is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default 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_PRIORITY);
    		
    	
    		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_PRIORITY);
    			
    		}	
    		}
    		
    	
    	
    	
    	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 pbrockway2; 12-16-2011 at 01:53 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Java Score Not Working

    Whenever the bullet hits the disc the score is supposed to go up 1
    But in your code you increment the score each time around the loop whenever the bullet and the disk intersect. The bullet may hit once, but the condition

    Java Code:
    bullet_y <= disc2_y && bullet_x > disc2_x && bullet_x < disc2_x + disc2_width
    may be true for a number of cycles.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Java Score Not Working

    This thread is a duplicate. Reply here: Java Score Not Working

    @OP: Please don't do this. I want to know what Norm is saying, and perhaps Norm wants to know what I'm saying. The same with everyone else who participates in the discussion. We can't do this if the discussion is fragmented like this.

Similar Threads

  1. Score Counter In Java GUI.
    By UnAccomplishedJavaPerson in forum New To Java
    Replies: 9
    Last Post: 11-18-2011, 01:47 PM
  2. Replies: 4
    Last Post: 02-15-2011, 11:05 PM
  3. solr score
    By belsen in forum Lucene
    Replies: 0
    Last Post: 11-16-2010, 03:04 PM
  4. Replies: 8
    Last Post: 05-28-2008, 07:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •