Jump to content

  • Log In with Google      Sign In   
  • Create Account


HuwFulcher

Member Since 31 Mar 2011
Offline Last Active Apr 27 2014 06:12 AM

Topics I've Started

Any way to make this simple tennis game more OO?

24 April 2014 - 08:21 AM

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);
        }
    }
}
 
Ball:
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!


C, C# or C++?

10 April 2011 - 08:04 AM

Okay I want to learn a few game programming languages to get a head start in Uni but I'm not sure which of the three in the topic is best to start out with. Once I have a good knowledge of one or two of the above languages I will move onto Python and Java to cover all the bases :P

Problem writing string array's to text file.

03 April 2011 - 09:48 AM

Okay then basically I'm making a low grade character sheet for the game Elder Scrolls IV: Oblivion. I have text boxes to fill out name, race etc and a text box to input the directory for the .txt file to be written to. It worked fine till I decided to change it to a string array. Now when I want to write the contents of the whole string array to .txt format it will write it but the only text in the file is System.String[]

Here is the full code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Oblivion_Character_Sheet
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            string[] contents = new string[5];
            contents[0] = "Name: " + txtBoxName.Text;
            contents[1] = "Race: " + txtBoxRace.Text;
            contents[2] = "Birthsign: " + txtBoxBirth.Text;
            contents[3] = "Class: " + txtBoxClass.Text;
            contents[4] = "Other info: " + rchtxtBoxOther.Text;
            
            File.WriteAllText(txtBoxPath.Text, contents.ToString());
        }
    }
}

Planning for the future

31 March 2011 - 10:01 AM

Okay so I'm currently 15 (soon to be 16 ^^) and am looking at taking a degree in Computer Games Programming. So planning ahead I have researched job oppourtunites and University's etc. So far I think this is the right course for me. I love how they make games and will take a look at learning a few of the basics of C#, Java, Python and maybe C++ to get a head start at uni. I've basically trimmed the uni options down to 2 or 3 based on how close they are (3 hours and 30 mins away from me max) so here they are

  • Staffordshire University (has the highest rating out of 3 on thecompleteuniversityguide.com)
  • De Montfort University (has the lowest " ")
  • Derby University (has the second best " ")
Any advice would be helpful. I'm looking at how good the courses are not how good the Uni life is (although it is a bonus) ;). If you have any feedback for me please say so :).

Thanks in advance.

PARTNERS