Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Help with simple java game.  (Read 1247 times)
0 Members and 1 Guest are viewing this topic.
Offline jamesovine

Junior Newbie





« Posted 2011-08-17 15:12:58 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
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();
    }
  }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
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);
           
  }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
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;
  }

}
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2011-08-17 18:18:47 »

Integers are primitives, so by assigning yball in BackFrame on initialization, it will be given the initial value of jump.newballpos() but will not continue to change along with the return value of newballpos(). This is because any primitive is just a copy of another primitive. Therefore once you call jump.newballpos(), you never have any connection to that function again, you have only its return value at that moment in time.

You have two options to fix this:

1) Continually update the value of yball every frame.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
public void move ()
  {
    while (true)
    {
      yball = jump.newballpos();
      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();
    }
  }

2) Do things the "proper" object-oriented way and just get the value from an accessor.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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, jump.newballpos(), 80, 80);
    g2.setColor(Color.YELLOW);
    g2.fillRect(brickpos, 365, 60, 60);
    g2.fillRect(brickpos2 , 365, 60, 60);
  }


Objects are passed by reference, primitives are not. Let me know if this doesn't make sense to you.

In the future, you can debug this sort of thing by setting breakpoints or using System.out.println() to discover that yball never actually gets changed. That would end up leading you to figure out what I've told you.

See my work:
OTC Software
Offline jamesovine

Junior Newbie





« Reply #2 - Posted 2011-08-18 00:13:41 »

ah great!
thanks so much for the reply!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BoBear2681

JGO Coder


Medals: 18



« Reply #3 - Posted 2011-08-18 00:36:26 »

A small but important thing - you'll want to move your code for loading your background image to somewhere like your "BackFrame" constructor.  That way it's only loaded once.  As you have it now, the image will be loaded from disk each frame, which obviously isn't necessary.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #4 - Posted 2011-08-18 17:45:57 »

A small but important thing - you'll want to move your code for loading your background image to somewhere like your "BackFrame" constructor.  That way it's only loaded once.  As you have it now, the image will be loaded from disk each frame, which obviously isn't necessary.
Yeah very good point, I should have noticed that as well. Smiley

Also jamesovine, I would maybe spend some time reading tutorials or a programming book just to learn a bit more about Object-Oriented design principles. Your code is a bit all over the place in that regard, and a strong understanding of that will really help you.

See my work:
OTC Software
Offline jamesovine

Junior Newbie





« Reply #5 - Posted 2011-08-29 21:41:43 »

I would maybe spend some time reading tutorials or a programming book just to learn a bit more about Object-Oriented design principles. Your code is a bit all over the place in that regard, and a strong understanding of that will really help you.

Hi again.
Yeah i thought it was.... Do you have any books or tutorials you could recommend?
Thanks
James
Offline Z-Man
« Reply #6 - Posted 2011-08-29 23:37:54 »

At the top of the page there is a link to JGOs Java Gaming Resources. You can also click here to get to it.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (22 views)
2014-09-19 03:14:18

Dwinin (35 views)
2014-09-12 09:08:26

Norakomi (62 views)
2014-09-10 13:57:51

TehJavaDev (88 views)
2014-09-10 06:39:09

Tekkerue (43 views)
2014-09-09 02:24:56

mitcheeb (65 views)
2014-09-08 06:06:29

BurntPizza (47 views)
2014-09-07 01:13:42

Longarmx (35 views)
2014-09-07 01:12:14

Longarmx (40 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!