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.
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();