Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Applet: A user controlled Image - Stuck! on: 2013-12-01 23:53:01
I need to have the user control the image via WASD keys to move around the screen. I have gotten this to work with a paint generated "rect" but can't seem to get it to work with a gif image. I am still a beginner and am not the best coder.

NOTE: I have the gif saved onto my desktop. Will that affect my code?

Here's my code:

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  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
package maze;


import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;


public class Maze extends JApplet implements KeyListener
{
   int x;
   int y;

   public void init()
   {
       
       
      setBackground(Color.black);
      x = 20;
      y = 20;
      addKeyListener(this);    
     
       Image img;
       img = getImage(getCodeBase(), "soldier.gif");
       setVisible(true);
   }

   public void keyPressed(KeyEvent e) {}
 
   public void keyReleased(KeyEvent e) {}
   
   public void keyTyped(KeyEvent e)
   {
      char c = e.getKeyChar();
     
      if (c == 'w')
      {
         y -= 20;
      }
      else if (c == 's')
      {
         y += 20;
      }
      else if (c == 'a')
      {
         x -= 20;
      }
      else if (c == 'd')
      {
         x += 20;
      }
     
      if (c != KeyEvent.CHAR_UNDEFINED)
      {
         repaint();
         e.consume();
      }
   }

   public void paint(Graphics g)
   {
        requestFocus();    // applet's initial focus

         g.drawImage(img,50,50, this);
         
        if (!collisionDetection())
        {
         g.setColor(Color.gray);
         g.drawLine(0, 100, 250, 100);

       
             
     }
     else
     {
          g.setColor(Color.yellow);
          g.drawString("LOSER", 130, 150);
     }
   }
   
   public boolean collisionDetection()
   {
        if (x < 250 && y < 100 && y + 20 > 100)      // line segment 1
      {
         return true;
      }
     
     
      return false;
   }

public class DisplayImage extends JApplet
{
   private Image img;

   public void init()
   {
       img = getImage(getDocumentBase(),"soldier.gif");
   }

   public void paint(Graphics g)
   {
       g.drawImage(img, 50, 50, this);
   }
}
}
   



The g.drawImage(img,50,50, this); above the collision if statement is giving me an error for "img".. Saying "Cannot find symbol" - "Symbol: variable img". Not sure what is the problem. Help much appreciated. Thanks!
2  Java Game APIs & Engines / Java 2D / Re-sizable Target Java Paint on: 2013-10-28 17:51:28
I need this target for an interface I'm working on.. It has to re-size and have several rings of alternating colors. I was able to get 1 circle that resizes but am unsure how to make more circles(obviously smaller and to some sort of ratio to make a target). I tried making another g.fillOval with different values but it wasn't making the target like pattern I wanted. Thanks in advance!

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  
package target;



import java.awt.*;

import javax.swing.*;

import java.applet.*;




public class Target extends JPanel         //Setting up a graphics window
{
   
   public Target()
   {
      setBackground(Color.BLACK);     //Color constant
      setPreferredSize(new Dimension(250,250));
   }
   
   public void paintComponent(Graphics g)
   {
       
              Dimension d = getSize();
              int x = d.width/2;
              int y = d.height/2;
              int radius = (int) ((d.width < d.height) ? 0.4 * d.width : 0.4 * d.height);
             
       int width = getWidth();        // width of window in pixels
        int height = getHeight();      // height of window in pixels
       

        super.paintComponent(g);       // call superclass to display the panel
       
       
        //Drawing code below
       
       
         g.setColor(Color.cyan);
         g.fillOval(x-radius, y-radius, 2*radius, 2*radius);
         //Here is where I'm not sure! If I do another g.fillOval, I have no clue what i'd change the values to be.

   
         
         
       
   }
   
   
   
   public static void main(String[] args)
    {
        Target panel = new Target();           // window for drawing
     
        JFrame application = new JFrame();           // the program itself
       
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   // set frame to exit when it is closed
        application.add(panel);          

        application.setTitle("Target Program");
        application.setSize(500, 500);  // window is 500 pixels wide, 400 high
        application.setVisible(true);          
    }


}
3  Game Development / Newbie & Debugging Questions / Re: New to Java Programming - Text Based RPG on: 2013-10-06 20:04:47
The reason i suggested an if statement is because i thought you had a game loop - you really should read up on them.

I will do that right now! Thank you.
4  Game Development / Newbie & Debugging Questions / Re: New to Java Programming - Text Based RPG on: 2013-10-06 20:01:45
It worked! Thank you! But I still have some questions.. Say I wanted to choose if I want to attack again (or possiblty use a spell attack, etc).. Would I use If's then, and would I need to continually copy and paste that battle sequence (with some minor changes) or could I make a method that I could call?
5  Game Development / Newbie & Debugging Questions / Re: New to Java Programming - Text Based RPG on: 2013-10-06 19:38:23
Just doing
1  
enemies[1].getHp();
doesn't do anything. It returns the hp, so you have to do something with that value, like
1  
hp = enemies[1].getHp();


Also, don't use a while, use an if - and instead of setting enemyAlive and playerAlive to true over and over again, just detect if the enemies[1].getHp() is <0 and then set them to false.

I understand the enemies[1].getHp(); now (I think). Thanks for clearing that up.

EDIT: Just saw your post. Let me try that.

Ok so it worked (kinda). I have this now:

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  
answer = input.nextInt();
     
      if(answer == 1)
      {
         
         if(enemies[1].getHp() >= 0)
         {
         enemyAlive = true;
         playerAlive = true;
         int d;
         d = enemies[1].getHp() - user.getDamage();
         enemies[1].setHp(d);
         
         System.out.println(enemies[1].getName() +  " has " + enemies[1].getHp() + " HP remaining.");
            if (enemies[1].getHp() <= 0)
            {
               System.out.println("enemy died");
            }
         }
      }
     
      else if (answer == 2)
      {
         System.out.println("You try to run, but fall into a pit of spikes and die!");
     
      }


It does the math and prints out
"The Goblin has 85 HP remaining."

So now I have 2 questions. Now if I prompt the user again if they want to attack again or not and they choose to attack, do I need to put all that code (pretty much same as above) in again for the next attack?

And also, what if I wanted the entire fight to happen right there? Like my character keeps attacking the goblin.
6  Game Development / Newbie & Debugging Questions / Re: New to Java Programming - Text Based RPG on: 2013-10-06 19:27:40
You never update the enemy Hp. You have this:

1  
2  
int d;
d = enemies[1].getHp() - user.getDamage();


Which does calculate the new Hp, but you never assign it to the enemy. You'll need something like

1  
enemies[1].setHp(d);


That makes sense. So I would do that in the while loop, correct? Like this:

1  
2  
3  
4  
5  
6  
7  
while(enemies[1].getHp() > 0)
      {
         enemyAlive = true;
         playerAlive = true;
         int d;
         d = enemies[1].getHp() - user.getDamage();
         enemies[1].setHp(d);



Will enemies[1].getHp() in the loop declaration get the new Hp value then (which is d now) ? And also I think something may be wrong with my if statement because when I type 1, nothing happens and the program now stops.

@wreed12345

Now that I look at it, I don't think it was necessary at all, but I also don't think it harms anything?
7  Game Development / Newbie & Debugging Questions / New to Java Programming - Text Based RPG on: 2013-10-06 19:07:40
I just started taking a Java Programming Class at my school, and am definitely not the strongest programming. I'm trying to lay out the ground work for a VERY basic, and linear, text based rpg. I got stuck when I got to the battle sequences.. I can't get the code to produce what I want it too, and am looking for some advice/tips on this stuff. When I type "1" as my answer to attack, nothing happens but the program keeps running. I want to damage the goblin. Thanks!

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  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
package videogameenemy;
import java.util.Scanner;
public class VideoGameEnemy {
   
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);
      int answer = 0;
     
      boolean enemyAlive;
      boolean playerAlive;
     
      Enemy Goblin = new Enemy();
      Goblin.setName("Goblin");
      Goblin.setHp(100);
      Enemy Wolf = new Enemy();
      Wolf.setName("Wolf");
      Wolf.setHp(80);
     
      Player user = new Player();
      user.setDamage(15);
      user.setHp(150);
     
     
      Enemy[] enemies = new Enemy[2];
      enemies[1] = new Enemy();
      enemies[1] = Goblin;
      enemies[1].getHp();
      enemies[0] = new Enemy();
      enemies[0] = Wolf;
      enemies[0].getHp();
   
     
     
     
     
     
      System.out.println("A " + enemies[1].getName() + " has appeared!");
     
      System.out.println(enemies[1].getName() +  " has " + enemies[1].getHp() + " HP remaining.");
      System.out.println("Will you attack the " + enemies[1].getName() + "?");
      System.out.println("Enter 1 to attack, or 2 to flee!");
      System.out.println("\t[1 - Attack]");
      System.out.println("\t[2 - Flee]");
     
      answer = input.nextInt();
     
      if(answer == 1)
      {
         
      while(enemies[1].getHp() > 0)
      {
         enemyAlive = true;
         playerAlive = true;
         int d;
         d = enemies[1].getHp() - user.getDamage();
         
         if (enemyAlive = false || playerAlive == false)
         {
            break;
         }
      }
      }
      else if (answer == 2)
      {
         System.out.println("You run and fall.");
     
      }
     
   }
}






/////////
class Enemy
{
   private String name;
   private int Hp;
   
   public Enemy()
   // A Enemy is created with name set to an empty string
   {
     
   }
   
   public Enemy(String name, int Hp)
   {
      name = "";
      Hp = 0;
   }
   
   
   
   public void setName(String newName)
   // Pre:  Assigned(name)
   // Post: class member name has been set to name
   {
      name = newName;
   }
   
   
   public String getName()
   // Post: Returns the name
   {
      return name;
   }
   
   public void setHp(int newHp)
   // Pre:  Assigned(hp amount)
   // Post: class member hp has been set to newHp
   {
      Hp = newHp;
   }
   
   public int getHp()
   {
      return Hp;
   }
   

   
   
}

class Player extends Enemy
{
   private int damage;
   private int hp;
   
   public Player()
   // Default constructor
   {
      damage = 0;
      hp = 0;
     
   }
   
   public void setDamage(int newDamage)
   {
      damage = newDamage;
   }
   
   public int getDamage()
   {
      return damage;
   }
   
   public void setHp(int newHp)
   {
      hp = newHp;  
   }
   
   
   public int getHp()
   {
      return hp;
   }
   
}
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

theagentd (18 views)
2014-10-25 15:46:29

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (45 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (74 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!