2d Simplistic Bullets

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. ;D

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.

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

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 ?

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.

Do you also have squidNig as a twitter username? hi again :stuck_out_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.

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…

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.

Thank you! :smiley: 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?

Of course! Pass the Graphics instance anywhere and it will still ‘work’ :stuck_out_tongue:

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

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

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

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

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.