Results 1 to 4 of 4
- 02-27-2011, 12:09 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 8
- Rep Power
- 0
Trouble with a mini game ( using KeyEvent)
Hi, I'm hoping someone may be able to assist with my woes?
I'm trying to write this mini game which essentially has a randomly positioned ball appearing at the top of the frame & then it decends, with a user controlled basket at the bottom. I've managed to get the graphics panel working & the ball randomly appears, decends & then repeats ( if it isn't caught). but now I'm really stuck.
My two main problems are the KeyList inner class - which I've managed to get to respond to user input & write out simple statements, but I cannot seem to get them to be able to update the position of myPicture (the basket). I know it's out of scope, but I can't workout how to get it in scope (without putting an argument in the drawingPanel.updatePictureState() method.
secondly, once I get the blasted thing moving, any advice on how to 'catch' the ball (utilising the respective object's boundaries?)
I apologise if the code below isn't perfect, I'm obviously a beginner and there may still be remnants of my different attempts to get things to work floating about. - Once I get it working I'll go back through & remove references to the irrelevant stuff!
Below is the code for my four separate classes....
(MyPointGame, Picture, PointBall & Main)
Thanks in advance,....-
PuppetJacks.
Java Code:package MyPointGame; import java.awt.*; import javax.swing.*; import javax.swing.WindowConstants; import java.awt.event.*; /** * * @author PuppetJacks */ public class MyPointGame extends JFrame { // instance variables provided public final int FRAME_WIDTH = 500; public final int FRAME_HEIGHT = 500; public DrawingPanel drawingPanel; public JPanel scorePanel; public JLabel scoreHeader, score; public JLabel levelHeader, level; public JLabel spacer, spacer2; public int currentScore, currentLevel; public PointBall pointball; public Point pos; /** * Creates a new instance of MyPointGame */ public MyPointGame(String title) { super(title); setSize(FRAME_WIDTH,FRAME_HEIGHT); this.setResizable(false); setLayout(new BorderLayout()); drawingPanel = new DrawingPanel(); scorePanel = new JPanel(); currentLevel = 1; currentScore = 0; scoreHeader = new JLabel(); scoreHeader.setFont(new Font("Tahoma",Font.BOLD,20)); scoreHeader.setText("Current Score:"); score = new JLabel(); score.setFont(new Font("Tahoma",Font.BOLD, 30)); score.setForeground(Color.RED); score.setText(" " + currentScore); levelHeader = new JLabel(); levelHeader.setFont(new Font("Tahoma",Font.BOLD,20)); levelHeader.setText("LEVEL:"); level = new JLabel(); level.setFont(new Font("Tahoma", Font.BOLD, 30)); level.setForeground(Color.RED); level.setText(" " + currentLevel); add(scorePanel,BorderLayout.NORTH); drawingPanel.setBackground(Color.BLACK); add(drawingPanel); scorePanel.setLayout(new FlowLayout()); scorePanel.add(scoreHeader); scorePanel.add(score); scorePanel.add(levelHeader); scorePanel.add(level); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); addKeyListener(new KeyList()); } public void update() { { while(true) { try { Thread.sleep(50); drawingPanel.updatePictureState(); } catch (InterruptedException e) { System.exit(0); } } } } private class DrawingPanel extends JPanel { public Picture myPicture; public PointBall myPointBall; public DrawingPanel() { myPicture = new Picture(FRAME_WIDTH, FRAME_HEIGHT); myPointBall = new PointBall(FRAME_WIDTH, FRAME_HEIGHT); } public void paintComponent(Graphics g) { super.paintComponent(g); myPicture.draw(g); myPointBall.draw(g); } public void updatePictureState() { //System.out.println("Moving!!"); myPointBall.fall(); if(currentScore==90) { myPointBall.scoreIncrease(currentScore); } if(currentScore!=myPointBall.returnScore()); { currentScore = myPointBall.returnScore(); score.setText(" " + currentScore); } repaint(); } } } private class KeyList extends KeyAdapter { public void keyPressed (KeyEvent k) { if (k.getKeyCode() == KeyEvent.VK_LEFT) { System.out.println("left"); } if (k.getKeyCode() == KeyEvent.VK_RIGHT) { System.out.println("Right"); //myPicture.moveRight(); } } } } package MyPointGame; import java.awt.Graphics; import java.util.Random; import java.awt.*; import javax.swing.*; import java.awt.event.*; /** * * @author PuppetJacks */ public class Picture { protected int xPos; protected int yPos; protected int basketWidth; protected int basketHeight; protected int width; protected int height; private boolean objectOnScreen; private final int move = 5; //TODO complete constructor public Picture (int frameWidth, int frameHeight) { width = frameWidth; height = frameHeight; basketWidth = 50; basketHeight = 10; xPos = (250); yPos = (height-100); } public void updatePictureState(int width, int height) { } public void draw(Graphics g) { g.setColor(Color.GRAY); g.fillRect(xPos, yPos, basketWidth, basketHeight); g.drawLine(xPos,yPos,xPos,yPos-10); g.drawLine(xPos+49,yPos,xPos+49,yPos-10); } public void moveRight() { if (this.xPos<499) { xPos = xPos + move; } } public void moveLeft() { if (this.xPos>0) { xPos = xPos - move; System.out.println("Moving!"); System.out.println("" + this.xPos); } } public int getYPos() { return this.yPos; } public int getXPos() { return this.xPos; } } package MyPointGame; import javax.swing.*; import java.awt.*; /** * * @author PuppetJacks */ public class PointBall { protected int dropPoint; private final int RADIUS = 5; private boolean ballOnScreen; private int xPos; private int yPos; private int speed; private int height; private int width; public int currentScore; private int currentLevel; /** Creates a new instance of PointBall */ public PointBall( int frameWidth, int frameHeight) { width = frameWidth; speed = 5; generateNewPointBallPosition(); height = frameHeight; } public void draw (Graphics g) { g.setColor(Color.RED); g.fillOval(xPos, yPos, RADIUS*2,RADIUS*2); } public void fall() { if(this.yPos>420) { generateNewPointBallPosition(); scoreDecrease(); } else { setYPos(getYPos() + speed); } } public void scoreIncrease(int thisScore) { if (this.getXPos() < myPicture.getXPos() && this.getXPos() > myPicture.getXPos() + BasketWidth { } currentScore = thisScore+10; } public void scoreDecrease() { currentScore = currentScore-5; } public int returnScore() { return this.currentScore; } public int scoreUpdate(int oldScore,int oldLevel) { if(oldLevel == 1) { currentLevel = ((oldScore/100)+1); } else { currentLevel = (oldScore/100); } return currentLevel; } public void setYPos(int newYPos) { this.yPos = newYPos; } public int getYPos() { return this.yPos; } public int getXPos() { return this.xPos; } private void generateNewPointBallPosition() { int dropPoint = (int) (Math.random()* width + 1); xPos = dropPoint; yPos = 0; } } Main Class package MyPointGame; public class Main { public static void main(String[] args) { MyPointGame myDrawingApp = new MyPointGame("my Game!"); myDrawingApp.setVisible(true); myDrawingApp.update();Last edited by PuppetJacks; 02-27-2011 at 12:16 PM.
- 02-27-2011, 01:00 PM #2
Member
- Join Date
- Feb 2011
- Posts
- 8
- Rep Power
- 0
Hello,
Please negate the second point.
I've managed to get the score to increment when the two pictures overlap.
It's just the KeyAdapter now, to get the basket moving.
Thanks again,...
PuppetJacks.
-
If what you want to do is perform an animation based on some key presses, such as the arrow keys, I recommend that you start small -- that you create an even smaller program than what you have now, and work out the bugs of doing this in this smaller simpler program. You may wish to use key bindings which are a bit more flexible than KeyListeners especially with regards to focus. There are some decent examples of using key bindings to do this in this forum if you search for it.
- 02-27-2011, 02:58 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 8
- Rep Power
- 0
Thanks,
I have actualy done some successful basic programs utilising KeyListeners.
it's just this one is for an assignment and is causing me particular problems due to the skeleton classes that we were provided to use in the first instance: MyPointGame (including the JPanel inner class), the Picture class & the Main class were all provided and half completed for us to complete which has placed me squarely out of my comfort zone as some of the methods and messgaes aren't doing whast ive been led to expect them to.
I shall look through the forum to see if I can see any hints there.
Many thanks.
- PuppetJacksLast edited by PuppetJacks; 02-27-2011 at 03:41 PM.
Similar Threads
-
First Game In Java: Having trouble with my try catch errors.
By Sparkx in forum Java GamingReplies: 2Last Post: 02-19-2011, 06:40 AM -
Java Mini-Game!
By Ehkx in forum New To JavaReplies: 3Last Post: 01-11-2011, 10:38 PM -
Help in KeyEvent
By chyrl in forum AWT / SwingReplies: 15Last Post: 05-26-2010, 08:12 AM -
JOptionPane KeyEvent
By mine0926 in forum NetBeansReplies: 5Last Post: 05-05-2010, 02:03 AM -
The 3 Word Story [Mini Game]
By Eku in forum Forum LobbyReplies: 90Last Post: 11-16-2008, 09:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks