Hi.
This is my first post. I have studied maths & CS at uni for a year. Had done no CS before this. I find it really tough but really wanna get better.
One of our assignments was to write a simple game in java. this was back in march. i did ok but never got the game to properly work. thought id try over summer but am having some trouble. its a simple ball and some yellow moving blocks.
i just can’t get the red ball to jump and have no idea where to go from here. i have 3 classes: Play, BackFrame and Jump. Play adds components to the frame and handles animation. BackFrame draws the background and adds components. And Jump handles the jump button. i’ve copied all the code down below. sorry if thats not what you’re supposed to do.
it’d mean SO much if anyone could have a look and point me in the write direction!
thanks!
james
import javax.swing.*;
import java.awt.*;
import java.net.*;
import javax.imageio.ImageIO;
import java.io.*;
class Play implements Runnable
{
private BackFrame back = new BackFrame();
private Jump jump = new Jump();
public static void main (String [] args)
{
Play program = new Play ();
SwingUtilities.invokeLater(program);
program.brickstart();
}
public void run ()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.add(back);
frame.pack ();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
void brickstart()
{
for (int i=0; i<100; i++)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException interruption)
{
}
back.move();
}
}
}
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
public class BackFrame extends JPanel
{
private int brickspeed = 3;
private int brickpos = 640;
private int brickpos2 = 890;
private Image background;
private JLabel gameover = new JLabel();
private JButton button = new JButton ("JUMP");
private Jump jump = new Jump();
private int yball = jump.newballpos();
BackFrame()
{
setPreferredSize(new Dimension(640, 480));
add(button);
button.addActionListener(jump);
//URL goveraddress = Play.class.getResource("GAMEOVERBACK.jpg");
//ImageIcon goverimage = new ImageIcon(goveraddress);
//gameover = new JLabel(goverimage);
}
public void move ()
{
while (true)
{
brickpos = brickpos - brickspeed;
brickpos2 = brickpos2 - brickspeed;
if (brickpos < -60)
{
brickpos = 640;
}
if (brickpos2 < -60)
{
brickpos2 = 640;
brickspeed = brickspeed + 1;
}
/*if (yball>310)
{
if ((brickpos<=104) || (brickpos2<=104))
{
System.exit(1);
}
}*/
try
{
Thread.sleep(1000 / 30);
}
catch (InterruptedException ex)
{
}
repaint();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
URL backaddress = Play.class.getResource("GAMEBACK.jpg");
ImageIcon backimage = new ImageIcon(backaddress);
background = backimage.getImage();
g2.drawImage(background,0,0,this);
g2.setColor(Color.RED);
g2.fillOval(20, yball, 80, 80);
g2.setColor(Color.YELLOW);
g2.fillRect(brickpos, 365, 60, 60);
g2.fillRect(brickpos2 , 365, 60, 60);
}
}
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Jump implements ActionListener
{
private int yball = 350;
public Jump ()
{
}
public void actionPerformed(ActionEvent e)
{
{
yball = yball - 10;
}
}
public int newballpos()
{
return yball;
}
}