This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Making my first attempt at a 2D two-player game in Java, but without using any external libraries. The issue I am running into is I am trying to split the window in half so two people can play the game against each other. I have successfully been able to create a second window using GridLayout, but the second screen is all white. This seems like it could work, but it creates two windows when I would rather it be just one. Otherwise, the window displays fine and has all my GameObjects displayed when it is not split and I was able to implement a camera object to track player 1. I have provided both Game() and Window() for reference. I have also read about getSubimage, is that a better way to achieve my goal?
public void tick() {
for (int i=0; i<handler.object.size(); i ) {
if (handler.object.get(i).getId() == ID.Player) {
camera.tick(handler.object.get(i));
}
}
handler.tick();
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
Graphics2D g2d = (Graphics2D) g;
//////////////////////////////////////////////
//// This is where graphics is displayed /////
//g.setColor(Color.red);
//g.fillRect(0,0, 1000, 563);
g2d.translate(-camera.getX(), -camera.getY());
for (int xx=0; xx<30*72; xx =32) {
for (int yy=0; yy<30*72; yy =32) {
g.drawImage(floor, xx, yy,null);
}
}
handler.render(g);
g2d.translate(camera.getX(), camera.getY());
//hp Player 1 display
g.setColor(Color.gray);
g.fillRect(5,5,200,32);
g.setColor(Color.green);
g.fillRect(5,5,hpPlayer1*2,32);
g.setColor(Color.black);
g.drawRect(5,5,200,32);
//ammo display player 1
g.setColor(Color.white);
g.drawString("Ammo: " ammoPlayer1,10,50);
//Player 1 total points
g.setColor(Color.GREEN);
g.drawString("Points: " pointsPlayer1, 110, 50);
//hp Player 2 display
g.setColor(Color.gray);
g.fillRect(780,5,200,32);
g.setColor(Color.green);
g.fillRect(780,5,hpPlayer2*2,32);
g.setColor(Color.black);
g.drawRect(780,5,200,32);
//ammo display player 2
g.setColor(Color.white);
g.drawString("Ammo: " ammoPlayer2,790,50);
//Player 2 total points
g.setColor(Color.GREEN);
g.drawString("Points: " pointsPlayer2, 890, 50);
///// End of Graphics Displayed /////
/////////////////////////////////////
g.dispose();
bs.show();
}
//Loading level
private void loadLevel(BufferedImage image) {
int w = image.getWidth();
int h = image.getHeight();
for (int xx=0; xx<w; xx ) {
for (int yy=0; yy<h; yy ) {
int pixel = image.getRGB(xx, yy);
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
// wall
if (red == 255 && green == 0 && blue == 0)
handler.addObject(new Block(xx*32,yy*32, ID.Block, ss));
// Breakable Wall
if (red == 255 && green == 128 && blue == 0)
handler.addObject(new BreakableBlock(xx*32,yy*32,ID.BreakableBlock,handler,ss));
// player 1
if (red == 0 && green == 0 && blue == 255)
handler.addObject(new Tank(xx*32,yy*32, ID.Player, handler,this, ss));
// player 2
if (red == 255 && green == 255 && blue == 0)
handler.addObject(new Tank(xx*32,yy*32, ID.Player2, handler,this, ss));
// enemy
if (red == 0 && green == 255 && blue == 0)
handler.addObject(new MovableEnemy(xx*32,yy*32,
ID.MovableEnemy,handler,this,ss));
//if (red == 125 && green == 125 && blue == 0)
//handler.addObject(new FollowEnemy(xx*32,yy*32, ID.FollowEnemy, handler,this, ss));
// crate
if (red == 0 && green == 255 && blue == 255)
handler.addObject(new Crate(xx*32,yy*32, ID.Crate, ss));
// overshield
if (red == 200 && green == 0 && blue == 200)
handler.addObject(new Overshield(xx*32,yy*32, ID.Overshield, ss));
}
}
}
public class Window {
public Window(int width, int height, String title, Game game) {
JFrame frame = new JFrame(title);
// size of frame
frame.setPreferredSize(new Dimension(width, height));
frame.setMaximumSize(new Dimension(width, height));
frame.setMinimumSize(new Dimension(width, height));
/*frame.setSize(new Dimension(width, height));
GridLayout layout = new GridLayout(2,1);
frame.setLayout(layout);*/
frame.add(game); //add in game class to frame
//frame.add(game);
frame.pack();
frame.setResizable(false); //cannot resize window
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); //when game started box will start center of window
frame.setVisible(true); //lets us see window
}
}
Subreddit
Post Details
- Posted
- 5 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/javahelp/co...