Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Simple Game - Need Help

  1. #1
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Simple Game - Need Help

    This is a simple game I'm trying to make. I'm trying to get it to where it will shoot a bullet and once you release the space bar i want it to keep going for a small amount of time but it won't. And occasionally it will crash

    Java Code:
    package game.practice;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import javax.swing.JFrame;
    
    @SuppressWarnings("serial")
    public class GamePractice extends JFrame implements Runnable {
    	
    	int x, y, xDirection, yDirection;
    	static int bulletX;
    	static int bulletY;
    	Image dbImage;
    	Graphics dbg;
    	static boolean isShooting = false;
    
    	public void run(){
    		try{
    			while(true){
    				move();
    				
    				Thread.sleep(5);
    			}
    			}catch(Exception e){
    				System.err.println(e.getMessage());
    			}
    		}
    	
    	public void move(){
    		x += xDirection;
    		y += yDirection;
    		
    		if(x <= 0){
    			x = 0;
    		}
    		if(x >= 800){
    			x = 800;
    		}
    		if(y <= 0){
    			y = 0;
    		}
    		if(y >= 600){
    			y = 600;
    		}
    	}
    	
    	public void setXDirection(int xdir){
    		xDirection = xdir;
    	}
    	public void setYDirection(int ydir){
    		yDirection = ydir;
    	}
    	
    	public class AL extends KeyAdapter{
    		public void keyPressed(KeyEvent e){
    			int keyCode = e.getKeyCode();
    			if(keyCode == KeyEvent.VK_LEFT){
    				setXDirection(-1);
    			}
    			if(keyCode == KeyEvent.VK_RIGHT){
    				setXDirection(+1);
    			}
    			if(keyCode == KeyEvent.VK_UP){
    				setYDirection(-1);
    			}
    			if(keyCode == KeyEvent.VK_DOWN){
    				setYDirection(+1);
    			}
    			if(keyCode == KeyEvent.VK_SPACE){
    				isShooting = true;
    			}
    		}
    		
    		public void keyReleased(KeyEvent e){
    			int keyCode = e.getKeyCode();
    			if(keyCode == KeyEvent.VK_LEFT){
    				setXDirection(0);
    			}
    			if(keyCode == KeyEvent.VK_RIGHT){
    				setXDirection(0);
    			}
    			if(keyCode == KeyEvent.VK_UP){
    				setYDirection(0);
    			}
    			if(keyCode == KeyEvent.VK_DOWN){
    				setYDirection(0);
    			}
    			if(keyCode == KeyEvent.VK_SPACE){
    				try {
    					Thread.sleep(3000);
    				} catch (InterruptedException e1) {
    					e1.printStackTrace();
    				}
    				bulletX = x;
    				bulletY = y;
    				isShooting = false;
    			}
    		}
    	}
    	
    	public GamePractice(){
    		addKeyListener(new AL());
    		setTitle("Mediocre Game -- Pre-Alpha -- Version 2.0 -- Created By: Mitchell Butterfield");
    		setSize(800, 600);
    		setLocationRelativeTo(null);
    		setResizable(false);
    		setVisible(true);
    		setBackground(Color.BLUE);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		x = 400;
    		y = 400;
    	}
    	
    	public void paint(Graphics g){
    		dbImage = createImage(getWidth(), getHeight());
    		dbg = dbImage.getGraphics();
    		paintComponent(dbg);
    		if(isShooting == true){
    			paintBullet(dbg);
    		}
    		g.drawImage(dbImage, 0, 0, this);
    	}
    	
    	public void paintComponent(Graphics g){
    		g.setColor(Color.DARK_GRAY);
    		g.fillRect(x, y, 30, 30);
    		g.setColor(Color.WHITE);
    		g.drawString("X: " + x, 50, 50);
    		g.drawString("Y: " + y, 50, 60);
    		g.drawString("X Direction: " + xDirection, 50, 70);
    		g.drawString("Y Direction: " + yDirection, 50, 80);
    		g.drawString("Is Shooting: " + isShooting, 50, 90);
    		g.drawString("Bullet X: " + bulletX, 50, 100);
    		g.drawString("Bullet Y: " + bulletY, 50, 110);
    		
    		repaint();
    	}
    	
    	public void paintBullet(Graphics g){
    		g.fillOval(bulletX, bulletY, 5, 5);
    		bulletY--;
    	}
    	
    	public static void main(String[] args){
    		GamePractice p = new GamePractice();
    		Thread t1 = new Thread(p);
    		t1.start();
    	}
    	}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Simple Game - Need Help

    Why are you calling paintComponent directly? And why does paintComponent have a repaint call in it? Both are dangerous things to do. Also you shouldn't even be coding in the paint method. It appears that you haven't gone through the Swing graphics tutorials yet, and I recommend that you do this first. Then look into using a Swing Timer.

  3. #3
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Re: Simple Game - Need Help

    Thank you but the link doesn't seem to be working.

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

    Default Re: Simple Game - Need Help

    Something seems to have gone wrong with Fubarable's link. Here it is again: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

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

Similar Threads

  1. Simple Java Game -- HELP :)
    By Gamersunited22 in forum New To Java
    Replies: 7
    Last Post: 04-04-2012, 06:41 PM
  2. need a simple java game
    By Shashwat in forum New To Java
    Replies: 1
    Last Post: 01-03-2011, 02:42 PM
  3. simple game programming help
    By b.m in forum New To Java
    Replies: 0
    Last Post: 11-25-2010, 05:16 PM
  4. Simple Game example
    By mayuresh34 in forum CLDC and MIDP
    Replies: 1
    Last Post: 10-08-2009, 01:21 PM
  5. Simple Dodging Game
    By Tb0h in forum New To Java
    Replies: 1
    Last Post: 07-18-2009, 02:30 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
  •