Hey all, I'm a 1st year CS student and I'm making my first pet project in Java, the idea is taken from this tutorial (http://www.edu4java.com/en/game/game6.html) but I didn't want to continue following the tutorial because I wanted to get my hands dirty and dive in trying to work it out myself.
Currently the code has three classes:
- Game which is the main class that sets up the KeyListener, game loop and painting of the screen
- Ball which creates the ball sprite and works out the position on screen when the move() and paint() methods are called
- Racquet which is the same as Ball but a different shape obviously.
My question is, can I separate the Game class and make a Board class that deals with creating the window and then leave the Game class to run the loop only? I've tried doing so but I've discovered circular dependencies between Board, Ball and Racquet if I do that. I can do it using setters in the Board class to reference the Ball and Racquet class but I can't get the KeyListener working in that case and I think there must be a slicker way of doing it The code is below:
Game:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* Created by HuwF on 24/04/2014.
*/
public class Game extends JPanel
{
//Create sprite objects
Ball ball = new Ball(this);
Racquet racquet = new Racquet(this);
public Game()
{
//Implement listener to move keyboard
addKeyListener(new KeyListener()
{
@Override
public void keyTyped(KeyEvent e)
{
}
@Override
public void keyPressed(KeyEvent e)
{
//Call racquet to move it
racquet.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e)
{
//Stops racquet movement
racquet.keyReleased(e);
}
});
setFocusable(true);
}
//Gets objects to update position and movement values
public void move()
{
ball.move();
racquet.move();
}
//Paints screen based on sprite's values
@Override
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
ball.paint(g2d);
racquet.paint(g2d);
}
public static void main(String[] args) throws InterruptedException
{
//Create and display frame
JFrame frame = new JFrame("Mini Tennis");
Game game = new Game();
frame.add(game);
frame.setSize(300, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Main game loop - I want everything apart from this in the Board class
while (true)
{
game.move();
game.repaint();
Thread.sleep(10);
}
}
}
import java.awt.Graphics2D;
/**
* Created by HuwF on 24/04/2014.
*/
public class Ball
{
//Ball coordinates
int x = 0;
int y = 0;
//1 = Right, 0 = Still, -1 = Left
int xDirection = 1;
//1 = Up, 0 = Still, -1 = Down
int yDirection = 1;
private Game game;
public Ball(Game game)
{
this.game= game;
}
void move()
{
//If ball is by left edge of screen, direction reverses
if (x + xDirection < 0)
{
xDirection = 1;
}
//If ball is by right edge of screen, direction reverses
if (x + xDirection > this.game.getWidth() - 30)
{
xDirection = -1;
}
//If ball is by the bottom of screen, direction reverses
if (y + yDirection < 0)
{
yDirection = 1;
}
//If ball is by the top of screen, direction reverses
if (y + yDirection > this.game.getHeight() - 30)
{
yDirection = -1;
}
//If ball is no where near screen border continue along path
x = x + xDirection;
y = y + yDirection;
}
public void paint(Graphics2D g)
{
//Draw sprite on screen
g.fillOval(x, y, 30, 30);
}
}
Racquet:
import java.awt.*;
import java.awt.event.KeyEvent;
/**
* Created by HuwF on 24/04/2014.
*/
public class Racquet
{
//Position, racquet can only move left or right
int x = 0;
//Direction, -1 = Left, 0 = Still, 1 = Right
int xDirection = 0;
private Game game;
public Racquet(Game game)
{
this.game = game;
}
public void move()
{
//If not near the left or right edge of the screen then move in current direction
if ((x + xDirection > 0) && (x + xDirection < this.game.getWidth() - 60))
{
x = x + xDirection;
}
}
public void paint(Graphics2D g)
{
//Draw sprite on screen
g.fillRect(x, 330, 60, 10);
}
//When a key is released the racquet stops moving
public void keyReleased(KeyEvent e)
{
xDirection = 0;
}
public void keyPressed(KeyEvent e)
{
//If left arrow is pressed direction is changed to left
if (e.getKeyCode() == KeyEvent.VK_LEFT)
{
xDirection = -1;
}
//If right arrow is pressed direction is changed to right
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
{
xDirection = 1;
}
}
}
Thanks in advance!