Hey everybody,
Im new here, I
m new to programming with Java and new to Processing. I want to code a Battleships game. It should look something like this in the end. I apologize for my bad english, but I hope you can understand the most I`ve written.
That`s the design I made for now.
The game should be following functions:
- create two boards with a arraylist - works partly
(here should be other pictures of my game - but as new user I can upload one picture only)
so when I want place my ships it works, but I can place the ships under the board and right from the board too.
My first thought about that was to include a collision detection. That could has the benefit of a kind of “hover” effect and that I could place it maybe better into the limits of the board. But I dont know if that
s the right way for that. I don`t know how to write “edges” or “limits” for the ships.
In my mind I had a idea with setting the ships:
- I want place exactly four ships for now
- it should begin with a a ship with a length of 5 fields and should end with a ship of a length with 2 fields - works
- with the left mouseclick you could set it vertical and with the right mouseclick you can set it horizontal on the battlefield - works
At this point it looks like this:
(here should be other pictures of my game - but as new user I can upload one picture only)
As decribed above I have problems with setting the ships within the edges of the board.
following button activities:
- “New Game”-Button to start and restart the game - works
- “Exit Game”-Button - works
- “Quick Guide”-Button that opens a second window with rules etc. - tried some things but it never worked
If these problems are gone, I want to write a simple AI for the computer opponent. When I placed all my ships I can set the first hit on the opponent`s battlefield. After I set the first hit, my opponent should set a hit at random at my battlefield.
The game is over when the user or the computer has 11 hits on the ships from his or her opponent (5 + 4 + 3 +2 = 11).
A kind of highscore system is imagined for the future.
And here is the code for now. I`m sure that this code is chaotic and hard to understand. I taken the basic code of this from: https:
//forum.processing.org/two/discussion/15952/just-a-little-question-to-how-to-make-the-game-battleships
// ArrayList fĂĽr battlefield
ArrayList<Sea> seas = new ArrayList();
// Logo Header
PImage battleshipLogo;
PImage screenLogo1;
PImage screenLogo2;
// Font
PFont font;
// Ship Settings
int shipSizeXY = 250;
// Buttons
Button newGameButton;
Button quickGuideButton;
Button exitGameButton;
//Quick Guide
void setup() {
size(1060, 810);
background(0);
battleshipLogo = loadImage("battleship_logo2.jpg");
screenLogo1 = loadImage("ScreenLogo_part1-3.jpg");
screenLogo2 = loadImage("ScreenLogo_part2-3.jpg");
image(screenLogo1, 20, 100, 500, 500);
image(screenLogo2, 540, 100, 500, 500);
//for (int i=0; i<10; i++) {
// for (int j=0; j<10; j++) {
// seas.add(new Sea(20+50*i, 100+50*j, false) );
// seas.add(new Sea(540+50*i, 100+50*j, true) );
// }
//}
// Button
font = loadFont("space_font.vlw");
textFont(font);
textSize(26);
fill(104,240,211);
quickGuideButton = new Button(20, 620, 1020, 100, "QUICK GUIDE", 128,128,255);
newGameButton = new Button(20, 740, 500, 50, "NEW GAME", 128,128,255);
exitGameButton = new Button(540, 740, 500, 50, "EXIT GAME", 128,128,255);
}
void draw() {
// position for the sketch window
if(1==frameCount) surface.setLocation(width/4,0);
//image(screenLogo1, 20, 100, 500, 500);
//image(screenLogo2, 540, 100, 500, 500);
imageMode(CENTER);
image(battleshipLogo, width/2, 50);
for ( int i = 0; i < seas.size(); i++) {
seas.get(i).draw();
}
noFill();
stroke(152,20,152);
strokeWeight(3);
rect(20,100,500,500);
rect(540,100,500,500);
if(newGameButton.isClicked()) {
//setup();
for (int i=0; i<10; i++) {
for (int j=0; j<10; j++) {
seas.add(new Sea(20+50*i, 100+50*j, false) );
seas.add(new Sea(540+50*i, 100+50*j, true) );
}
}
}
if(quickGuideButton.isClicked()) {
}
if(exitGameButton.isClicked()) {
exit();
}
newGameButton.update();
newGameButton.render();
quickGuideButton.update();
quickGuideButton.render();
exitGameButton.update();
exitGameButton.render();
}
void mousePressed() {
for ( int i = 0; i < seas.size (); i++) {
seas.get(i).setShip();
}
}
class Sea {
boolean has_ship;
boolean secret;
boolean clicked;
int x, y;
int shipSizeXY = 250;
Sea(int ix, int iy, boolean isecret) {
secret = isecret;
has_ship = secret && random(1)<.2;//false;
clicked = false;
x = ix;
y = iy;
}
void draw() {
pushMatrix();
translate(x, y);
fill(128, 128, 255);
strokeWeight(1.5);
stroke(51, 242, 170);
rect(0, 0, 50, 50);
// ships
if ( !secret && has_ship ) {
fill(255,244,139);
stroke(255, 84, 167);
rect(5, 5, 40, 40);
}
// if ship is not hit
if ( !has_ship && clicked ) {
fill(255);
stroke(0);
ellipse(25, 25, 30, 30);
}
// if ship is hit
if ( has_ship && clicked ) {
fill(255, 0, 0);
stroke(0);
ellipse(25, 25, 30, 30);
}
popMatrix();
}
void setShip() {
if(mousePressed && (mouseButton == LEFT)) {
if ( mouseX > x && mouseX < x+shipSizeXY && mouseY > y && mouseY < y+50 ) {
if( ship_place_state && !secret ){
has_ship = !has_ship;
if( secret && !ship_place_state ){
clicked = true;
computers_turn();
}
}
}
}
else if(mousePressed && (mouseButton == RIGHT)) {
if ( mouseX > x && mouseX < x+50 && mouseY > y && mouseY < y+shipSizeXY ) {
if( ship_place_state && !secret ){
has_ship = !has_ship;
}
else if( secret && !ship_place_state ){
clicked = true;
computers_turn();
}
}
}
// four ships in ascending size: 5, 4, 3, 2
shipSizeXY -= 50;
if(shipSizeXY == 50) {
shipSizeXY = 0;
}
}
void computers_turn(){
for(int i = 0; i < seas.size(); i++){
if( !seas.get(i).secret && !seas.get(i).clicked ){
seas.get(i).clicked = true;
return;
}
}
}
boolean ship_place_state = true;
}
and here is my button-class:
class Button {
PVector Pos = new PVector(0,0);
float Width = 0;
float Height = 0;
color ButtonColor;
color TextColor;
String Text;
boolean Pressed = false;
boolean Clicked = false;
// constructor for creating buttons
Button(int x, int y, int w, int h, String t, int r, int g, int b) {
Pos.x = x;
Pos.y = y;
Width = w;
Height = h;
ButtonColor = color(r,g,b);
Text = t;
TextColor = color(r,g,b);
}
//
void update() {
if(mousePressed == true && mouseButton == LEFT && Pressed == false) {
Pressed = true;
if(mouseX >= Pos.x && mouseX <= Pos.x+Width && mouseY >= Pos.y && mouseY <= Pos.y+Height) {
Clicked = true;
}
} else {
Clicked = false;
}
if(mousePressed != true) {
Pressed = false;
}
}
//
void render() {
fill(ButtonColor);
rect(Pos.x, Pos.y, Width, Height);
textAlign(CENTER, CENTER);
fill(66,210,149);
text(Text, Pos.x+(Width/2), Pos.y+(Height/2));
}
//
boolean isClicked() {
return Clicked;
}
}
Ok these are my problems for now. Maybe there is anyone that can help me in a way.
Thank you very much for reading this.