Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
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  
  2d Simplistic Bullets  (Read 1547 times)
0 Members and 1 Guest are viewing this topic.
Offline SquidNig

Senior Newbie





« Posted 2012-05-22 04:21:17 »

Hello everybody, im new to this site as well as Java! I have this game, well its not really a game, i'm just testing out things in java so see if i can do them, compile my knowledge, and then later on make an awesome game. So more than likely i will be coming back with a lot of question because most things in java i cant get unless i see and example or is personally taught to me.  Grin

Well, i have this code right here, and i know this is VERY bad for a 2D game (with the for loop) but i mean, the logic to me sounds perfectly sound:
but prior to this i have no 2D Java gaming knowledge what so ever. ANY HELP WOULD BE USEFUL.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
if (drawRect) {
           
            g.setColor(Color.BLUE);
            g.fillRect(dx, dy, 10, 10);
           
            for (int x= 0; x < 500; x++) {
               
               dx += 3;
               
            }
           
           
            repaint();
           
         }


I have this inside my paint method. And to my understanding i set a keyListener to the Space bar to toggle drawRect to True. I would believe that whenever my space bar toggles to true it would create a blue rectangle, and start to change the x position dynamically by 3 on the x axis? Resulting in what looks like... a bullet? It just does not work. It does spawn the bullet? It just doesnt move. All help is accepted. I really need to to learn how to do this stuff... This is what i want to do later on in life. I just dont know where to start...

Thanks in advance,
     - SquidNig
Offline 65K
« Reply #1 - Posted 2012-05-22 06:25:40 »

Whats that inner loop for ? Is it for moving in steps of three, cause now it always adds up a big step.
So you get the rectangle on a fixed position ?

Offline SquidNig

Senior Newbie





« Reply #2 - Posted 2012-05-22 07:22:57 »

Actually, i was hoping for it to act the same way i have my WASD set up. You know:

y++;
y--;
x++;
x--;

I was hoping for me to transfer over the same idea, but in increments of 3? To make the rectangle move faster. It just, doesn't seem to work.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ciaran54

Junior Member


Medals: 4



« Reply #3 - Posted 2012-05-22 08:29:32 »

Do you also have squidNig as a twitter username? hi again Tongue

from your code there, it looks like as soon as you press space, you will draw your box 1500 places to the right of where it initially spawned, because it is only ever drawn after this has run:
ie. you will only ever see it after it has moved.

1  
2  
3  
4  
5  
for (int x= 0; x < 500; x++) {
               
               dx += 3;
               
            }


(500*3 = 1500)

If you want to see it animate, you would need to redraw after every step (and also sleep the thread so that the animation is slow enough to see)

What happens when you press the spacebar twice?
I think we might need to see your listener code...
Offline aazimon
« Reply #4 - Posted 2012-05-22 21:30:28 »

You will need a separate class to store the bullets current position. Each update cycle, you will then update the bullet's position, and use it's current position to determine where to display (draw) the bullet.
Offline SquidNig

Senior Newbie





« Reply #5 - Posted 2012-05-24 03:49:27 »

Thank you! Cheesy See that was the "Simplistic" version i was looking for. Now, since im just a beginner, It will completely alright to draw on another class using Graphics g and it will still appear on my main JFrame?
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-05-24 04:48:23 »

Of course! Pass the Graphics instance anywhere and it will still 'work' Tongue

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #7 - Posted 2012-05-24 14:41:53 »

What I like to do for being able to render in all of classes is have my graphics object sent to the static Screen class at the beginning of the render, then render everything in my other classes by calling my Screen.draw method, like this:

Render method

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
private void render() {
      BufferStrategy bs = getBufferStrategy();
      if (bs == null) {
         this.createBufferStrategy(3);
         return;
      }
     
      g = image.getGraphics();
     
      g.setColor(new Color(0x000000));
      g.fillRect(0, 0, getWidth(), getHeight());
      g.setColor(Color.WHITE);
      Screen.setGraphics(g);
     
      menu.render();
     
      g = Screen.getGraphics();
      g.dispose();
      g = bs.getDrawGraphics();
      g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
      g.dispose();
      bs.show();
   }


Drawing methods in screen class

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  
public static void draw(int x, int y, int sprite, boolean flipX, boolean flipY) {
      BufferedImage img = sheet.sprites[sprite];
     
      if (flipX) {
         AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
         tx.translate(-16, 0);
         AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
         img = op.filter(img, null);
      }
      if (flipY) {
         AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
         tx.translate(0, -16);
         AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
         img = op.filter(img, null);
      }
     
      draw(img, x, y, 16, 16);
   }
   
   public static void draw(BufferedImage img, int x, int y, int width, int height) {
      g.drawImage(img, x, y, width, height, null);
   }
   
   public static void drawRect(Rectangle r, boolean fill, Color col) {
      g.setColor(col);
      if (fill) g.fillRect(r.x, r.y, r.width, r.height);
      else g.drawRect(r.x, r.y, r.width, r.height);
   }


Example of player rendering method

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
Font.draw("Coords:" + cx + "," + cy, 1, 1, Col.get(0x808080));
     
      if (using) {
         if (dir == UP) Screen.draw(x - xoffs - 2, y - yoffs - 16, Inventory.equipped.sprite, false, false);
         if (dir == DOWN) Screen.draw(x - xoffs - 2, y - yoffs + 16, Inventory.equipped.sprite, false, false);
         if (dir == LEFT) Screen.draw(x - xoffs - 18, y - yoffs, Inventory.equipped.sprite, false, false);
         if (dir == RIGHT) Screen.draw(x - xoffs + 14, y - yoffs, Inventory.equipped.sprite, false, false);
      }
     
      Screen.drawRect(new Rectangle(listener.px - xoffs, listener.py - yoffs, 16, 16), false, Col.get(0x404040));
      if (cx != tileX || cy != tileY) Screen.drawRect(new Rectangle(tileX * 16 - xoffs, tileY * 16 - yoffs, 16, 16), false, Col.get(0xAAAAAA));
     
      Screen.draw(x - xoffs, y - yoffs, sprite, false, false);


Maybe you can get something useful out of that, haha.

-Nate

Check out my website!
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2012-05-24 17:17:09 »

No need to draw to an image and then draw that image to the BufferStrategy. Draw directly onto the BufferStrategy. That should speed things up.

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.

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

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

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

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

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

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

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

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

Longarmx (31 views)
2014-09-07 01:10:19

mitcheeb (39 views)
2014-09-04 23:08:59
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!