Results 1 to 16 of 16
  1. #1
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Help with Java Game Code

    I'm making a maze game just so I can learn some practical uses for Java and have a problem with some code. The problem is that when you go on the cherry(point) tile keeps on adding points and won't stop. I have tried many ideas but have run out of them. Here's the source code to the relating classes.


    .................................................. .................................................. .................................................. .................................................. ...
    This is the board class
    .................................................. .................................................. .................................................. .................................................. ..
    Java Code:
    package boards;
    
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    import Core.code.PlayerClass;
    import Maps.Map1;
    
    public class Board extends JPanel implements ActionListener{
    
    	private boolean win = false;
    	
    	private Font font = new Font("Serif", Font.BOLD, 20);
    	
    	private String Message = "";
    	
    	private String fab = "Score";
    	
    	private Timer timer;
    	
    	private PlayerClass p;
    		
    	private Map1 map;
    
    	private int score = 0;
    
    	public Board() {
    
    		map = new Map1();
    		p = new PlayerClass();
    		addKeyListener(new Al());
    		setFocusable(true);
    		timer = new Timer(25, this);
    		timer.start();
    	}
    	
    	
    	public void actionPerformed(ActionEvent e) {
    		if(map.getMap(p.gettileX(), p.gettileY()).equals("f")){
    			Message = "Congrats You Have Beaten The Maze";
    			win = true;
    		}
    		if(map.getMap(p.gettileX(), p.gettileY()).equals("c")){
    			score++;
    		}
    		
    		
    		
    		repaint();
    		
    	}
    
    	public void paint(Graphics g) {
    		super.paint(g);
    		
    		
    		if (!win) {
    			for (int y = 0; y < 14; y++) {
    				for (int x = 0; x < 14; x++) {
    					if (map.getMap(x, y).equals("b")) {
    						g.drawImage(map.getBack(), x * 32, y * 32, null);
    					}
    					if (map.getMap(x, y).equals("m")) {
    						g.drawImage(map.getMud(), x * 32, y * 32, null);
    					}
    					if (map.getMap(x, y).equals("f")) {
    						g.drawImage(map.getFinish(), x * 32, y * 32, null);
    					}
    					if (map.getMap(x, y).equals("c")) {
    						g.drawImage(map.getCherry(), x * 32, y * 32, null);
    					}
    				}
    			}
    			
    			g.drawImage(p.getPlayer(), p.gettileX() * 32, p.gettileY() * 32, null);
    			
    		}
    
    		String s = fab + Integer.toString(score);
    		Font font = new Font("Serif", Font.BOLD, 32);
    		g.setFont(font);
    		g.setColor(Color.BLACK);
    		g.drawString(s, 20 + 1, 20 - 1);
    		g.setColor(new Color(  0, 255,   0));
    		g.drawString(s, 20, 20);
    		
    		if (win) {
    			g.setColor(Color.BLUE);
    
    			g.setFont(font);
    			g.drawString(Message, 32, 32);
    		}
    		
    	}
    	
    	public class Al extends KeyAdapter{
    		
    		public void keyPressed(KeyEvent e){
    			int keycode = e.getKeyCode();
    			
    			if (keycode == KeyEvent.VK_W) {
    				if (!map.getMap(p.gettileX(), p.gettileY() - 1).equals("m")) {
    					p.Move(0, -1);
    				}
    			}
    			if (keycode == KeyEvent.VK_A) {
    				if (!map.getMap(p.gettileX() - 1, p.gettileY()).equals("m")) {
    					p.Move(-1, 0);
    				}
    			}
    			if (keycode == KeyEvent.VK_S) {
    				if (!map.getMap(p.gettileX(), p.gettileY() + 1).equals("m")) {
    					p.Move(0, 1);
    				}
    			}
    			if (keycode == KeyEvent.VK_D) {
    				if (!map.getMap(p.gettileX() + 1, p.gettileY()).equals("m")) {
    					p.Move(1, 0);
    				}
    			}
    			if (keycode == KeyEvent.VK_UP) {
    				if (!map.getMap(p.gettileX(), p.gettileY() - 1).equals("m")) {
    					p.Move(0, -1);
    				}
    			}
    			if (keycode == KeyEvent.VK_LEFT) {
    				if (!map.getMap(p.gettileX() - 1, p.gettileY()).equals("m")) {
    					p.Move(-1, 0);
    				}
    			}
    			if (keycode == KeyEvent.VK_DOWN) {
    				if (!map.getMap(p.gettileX(), p.gettileY() + 1).equals("m")) {
    					p.Move(0, 1);
    				}
    			}
    			if (keycode == KeyEvent.VK_RIGHT) {
    				if (!map.getMap(p.gettileX() + 1, p.gettileY()).equals("m")) {
    					p.Move(1, 0);
    				}
    			}
    		}
    		public void keyRelased(KeyEvent e){
    			
    			
    		}
    	    public void keyTyped(KeyEvent e){
    	    	
    	    	
    	    }
    	
    	
    	}
    	
    }
    .................................................. .................................................. .................................................. .................................................. ...
    this is the class that i get the map and the cherry(or point ) info from
    .................................................. .................................................. .................................................. .................................................. ....
    Java Code:
    package Maps;
    
    import java.awt.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    
    
    public class Map1 {
    
    	private Scanner map;
    	private String Tiles[] = new String[14];
        public Image  back,
                      finish,
                      cherry,
                      mud;
        
        public Map1(){
        	//images
        	
        ImageIcon img = new ImageIcon(getClass().getResource("/Textures/map1.png"));
        back = img.getImage();
        img = new ImageIcon(getClass().getResource("/Textures/mud.png"));
        mud = img.getImage();
        img = new ImageIcon(getClass().getResource("/Textures/flag.png"));
        finish = img.getImage();
        img = new ImageIcon(getClass().getResource("/Textures/cherry.png"));
        cherry = img.getImage();
        
        
        
             // files
    		openFile();
    		readfile();
    		closeFile();
    
    	}
    
        public Image getBack(){
        	return back;
        }
        
        public Image getMud(){
        	return mud;
        }
        
        public Image getFinish(){
        	return finish;
        }
        public Image getCherry(){
        	return cherry;
        }
    
        
        	
        
        
        public String getMap(int x, int y){
    	String index = Tiles[y].substring(x, x + 1);
    	return index;
    }
    
    public void openFile() {
    	try{
    	map = new Scanner(getClass().getClassLoader().getResourceAsStream("Text/map1.txt"));
    	}catch(Exception e){
    	   System.out.print("Error Loading Map");
    	}
    	}
    
    public void readfile() {
    	while(map.hasNext()){
    		for(int i = 0; i < 14; i++){
    			Tiles[i] = map.next();
    		}
    	}
    }
    
    public void closeFile() {
    	map.close();
    	
    	
    }
    
    }
    .................................................. .................................................. .................................................. .................................................. ...

    How should i go about this?
    Last edited by JosAH; 05-19-2013 at 01:28 PM. Reason: added [code] ... [/code] tags

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Help with Java Game Code

    Instead on doing score++

    have a integer than represents the score of the cherry e.g private int cherryScore = 100;

    so something like:
    Java Code:
    setScore(getScore() + getCherryScore());
    you will to write:
    Java Code:
    getScore() //method for the score variable
    setScore(score: int)// method for score variable
    
    getCherryScore()// method to return the score of the cherry,
    Doing it this way can allow for extra fruits to be added with different point values

  3. #3
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: Help with Java Game Code

    Will this only add the score once as currently thats still my problem.

  4. #4
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Help with Java Game Code

    well every time you lang on the cherry it will update the score:
    Java Code:
    setScore(getScore() + getCherryScore())
    will update the score every time you step on a cherry.

  5. #5
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: Help with Java Game Code

    Is there a way to make the cherry tile convert into a normal walking tile so you cant spam it?

  6. #6
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Help with Java Game Code

    Quote Originally Posted by thelordposeidon View Post
    Is there a way to make the cherry tile convert into a normal walking tile so you cant spam it?
    What do you mean by that? What spaming means in this case?

  7. #7
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: Help with Java Game Code

    Spamming in this case means going onto the tile and it not disappearing so by that way you can get as much points as you want. Is there a way to get round that if so how?

  8. #8
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Help with Java Game Code

    just set the tile value to a normal board square after the score has been updated

  9. #9
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: Help with Java Game Code

    And how do I do this (sorry I new to java and this game is the first really project I'v ever tried)

  10. #10
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: Help with Java Game Code

    bump

  11. #11
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: Help with Java Game Code

    Could anybody help me with turning one tile into another i am very new to coding and this is a major problem to my game.

  12. #12
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Help with Java Game Code

    just set the board value to a normal board square.

    Do this:
    when i player steps on a cherry:
    1, update current score
    2, get the location of the cherry
    3, set the map value at this location to "m" (anything but "c" which represents a cherry
    4, repaint.

  13. #13
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: Help with Java Game Code

    Quote Originally Posted by monkeyjr97 View Post
    just set the board value to a normal board square.

    Do this:
    when i player steps on a cherry:
    1, update current score
    2, get the location of the cherry
    3, set the map value at this location to "m" (anything but "c" which represents a cherry
    4, repaint.

    Ihave done evrything mentioned above heres my code at the moment and it still won't work(I've added sound ssince last time so code slightly different)


    public void actionPerformed(ActionEvent e) {



    SoundManager s = new SoundManager(){
    @Override
    public void initSounds(){
    sounds.add(new sound("cherry", sound.getURL("cherry.wav")));
    }
    };
    if(health == 0){

    Message = "You have failed to beat the maze!";
    win = true;

    }

    if(map.getMap(p.gettileX(), p.gettileY()).equals("f")){
    Message = "Congrats You Have Beaten The Maze";
    win = true;
    }
    if(map.getMap(p.gettileX(), p.gettileY()).equals("c")){
    score++;
    s.playSound("cherry");
    s.stopSound("cherry");
    map.getMap(p.gettileX(), p.gettileY()).equals("m");

    }



    repaint();

    }

  14. #14
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: Help with Java Game Code

    please i need help with this

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Help with Java Game Code

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  16. #16
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: Help with Java Game Code

    Can anyone Help with this problem, I have tried multiple ways but none have worked. I should mention that I'm completely new to java so when you tell me do this I probably will get it wrong without some sort of sample code.

Similar Threads

  1. Replies: 2
    Last Post: 02-19-2014, 05:36 PM
  2. Java match-up game code 2D array
    By nevermind11111 in forum New To Java
    Replies: 2
    Last Post: 11-18-2012, 03:58 PM
  3. Replies: 4
    Last Post: 07-08-2012, 09:07 AM
  4. Need to code Tambola game in Java
    By leepikamukharji in forum Java Gaming
    Replies: 1
    Last Post: 04-11-2011, 01:30 PM
  5. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 08:06 AM

Tags for this Thread

Posting Permissions

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