How to build a game like Battleship?

Get help using Construct 2

Post » Fri Nov 08, 2013 11:45 am

@nimos100

Thank you for all these explanations. Were very detailed , though some points were a bit confusing for me (I'm starting to program ) .

I had already changed the origin of the sprites to 0.0, as indicated in the previous post and I intend to keep it there.

My idea is to make a panel with buttons and a display to show the equation and, on the left side, display area of the game ( whith ships in hidden) . This area should be slightly smaller than the height of the panel , so I talked about creating a space around the area of ??the game . I still do not understand how I can do to determine the position of the playing area .

I also do not understand how to create random orientation to the ship (X / Y or left / right).

To prevent the ship stayed along the margin , I am using ( int ( random ( 0,9 ) ) * 40 +40 ) for the first part of it . This solves the problem or not ? The tests done so far with the first part of the ship, worked very well.

Another possibility to distribute the ships : if I create the sprites in the overall size of the ship ( eg 40px / 120 px) and then control it through overlapp event to know which object it is overlapping and how many pieces are still missing from this object through the local variables? This would be more complicated to do?
RenatoB2013-11-08 20:03:59
B
74
S
14
G
4
Posts: 1,016
Reputation: 8,135

Post » Fri Nov 08, 2013 8:41 pm

@nimos100

I created a CAPX in accordance with your instructions

https://www.dropbox.com/s/6ef019rjkvj6ptp/test.capx

Currently ships are overlapping in some times and the ship in vertical axis leaves at times of the playing area (sea sprite). I included a loop (repeat) to verify the overlap, but even so the boats are overlapping from time to time.

B
74
S
14
G
4
Posts: 1,016
Reputation: 8,135

Post » Fri Nov 08, 2013 8:55 pm

"I still do not understand how I can do to determine the position of the playing area ."

Well basically you decide that for yourself, and can place it where ever you want as long as the math you use to place the ships are correct. Its up to you to decide it, I just used 0,0 as a starting point because its easier to explain then.

But whether you add 40*1 to 0,0 or to 280,280 doesn't make any difference. The ratio of 40 is the same no matter what, because that's what you decided before hand.

And as long as you go from 0-9 (10 spaces).

But if you start at 0,0 you gaming area will be from 0-400 pixels. And if you started at 280,280 it will go from 280-680 pixels.

So if o are the playing area. And you want it to start at 0,0 your screen would look something like that.

oooooo------
oooooo------
oooooo------
oooooo------
------------
------------
------------

If you wanted it to start at 280,280, it would look something like this.

------------
---oooooo---
---oooooo---
---oooooo---
---oooooo---
------------
------------

(This is no where accurate :D)

But the factor are the same, no matter what starting point you choose.

for 0,0
(400 - 0) / 10 = 40px

for 280,280
(680 - 280) / 10 = 40px





I also do not understand how to create random orientation to the ship (X / Y or left / right).

Another possibility to distribute the ships : if I create the sprites in the overall size of the ship ( eg 40px / 120 px) and then control it through overlapp event to know which object it is overlapping and how many pieces are still missing from this object through the local variables? This would be more complicated to do? And if I separate the parts of the ship, but use a container to place it in the game and then go checking and destroying the parts separately? Which do you think would be the best (easy, quick, codeless) solution?


Dropping the grid all together is the easiest I think.

////// OK THIS ISNT CORRECTLY DESCRIBED //////////////
If you make the ships as separate objects, and in design mode you align them as you would like them to be in the game and add a pin behaviour to them, and then in "Start of layout" you pin all the ships correctly. And then you make a container for each ship that you need.

Then you just create one part of the ship, place it at a random point and give it a random angle, and if set up correctly the rest of the ship will appear as it should. And then you can just use overlapping on each part to see if they are hit. That I would think is the easiest way to do it.   
//////////////////////////////////////////////////////

Should have tested it first :D



nimos1002013-11-08 21:13:47
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,848

Post » Fri Nov 08, 2013 9:14 pm

I have modified your CAPX.

This is how you could do it.

Just press Q to spawn a ship.

Battleship test

To explain it:

I have added 2 image points to the middle part of the ship. One is called "Front" and other "End".

You can use these to place the rest of the parts correctly. Then you just pin these part to the middle part.nimos1002013-11-08 21:17:38
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,848

Post » Sat Nov 09, 2013 12:02 am

@nimos100

I think with the PIN behavior is more practical, but I don't understand why you changed the point of origin. Now the sprite appears on the line rather than within the squares. It's necessary because you are using PIN?

Two other important issues that it's very difficult for me: the problem of overlap and the fact that the ship is out of the playing area (sea sprite).

I tried to create a loop with Repeat event and not work 100%. Sometimes the ship appears overlapping other.

I also need to restrict the area to ensure that ships always appear on the sea sprite. Do you know how can I solve this?RenatoB2013-11-09 00:06:54
B
74
S
14
G
4
Posts: 1,016
Reputation: 8,135

Post » Sat Nov 09, 2013 3:48 am

I think with the PIN behavior is more practical, but I don't understand why you changed the point of origin. Now the sprite appears on the line rather than within the squares. It's necessary because you are using PIN?

Because you don't need the grid anymore. But you can keep the Origin at 0,0 you just have to adjust the "Front" and "End" image points.

Two other important issues that it's very difficult for me: the problem of overlap and the fact that the ship is out of the playing area (sea sprite).

You have to add "rules" for where to place the ship. A way you can do it is by adding some check sprites around the game area, if the ships are overlapping those then try a new location. For the overlap you just make a Sprite and tell it to follow your mouse cursor, and then when the player press the left button you check to see if that sprite is overlapping a ship part.

I tried to create a loop with Repeat event and not work 100%. Sometimes the ship appears overlapping other.
You cant really use repeat, as you don't know how many times you have to repeat it. So either you have to use a ship counter, or a while loop.

If you want to place 5 ships you make a counter like "Ship_placed = 0" and every time you successfully place a ship you add 1 to this, and every time you detect an overlap you subtract one. And then you let the event run as long "Ship_placed < 5".

I also need to restrict the area to ensure that ships always appear on the sea sprite. Do you know how can I solve this?
You can do that with if you place those sprites around the play area and check if the ship overlap one of those.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,848

Post » Sat Nov 09, 2013 11:16 am

Very nice, @nimos100. Thanks for the suggestions about overlapping issues. Ill create the sprite and put it around the game area to restrict it.

I just don't understand very well the identification of the ships. I beleive that I need to spread 100 sprites on the game area for when the player clicks on one of them, check if there's a part of the ship below. In this case, it first needs to know if any item of the family is there, isn't? If confirmed, then I need to check which part of the ship is, right?

I intend to create five different types of ships and include them in a single family. But in fact, will be different parts of ships separated. Can I relate all parties to a single ship, or a single name?

The same issue occurs when I have to check if a ship was included on the screen to see if it is overlapping some other ship. I can check if a piece of the ship was placed. But how can I check if the entire ship was included in the game? I think the solutions is: check each piece of the ship, relating the number of pieces to a variable and check whether the variable has a value compatible with the number of pieces? Is it? I beleive that you have a better solution.

I really want to thank you for your patience and dedication to this thread. I really appreciate this.
B
74
S
14
G
4
Posts: 1,016
Reputation: 8,135

Post » Sat Nov 09, 2013 12:11 pm

Ill create the sprite and put it around the game area to restrict it.

Yeah you still have to place the ship somewhat correctly when you create it.

So System->create Ship_mid_1 on X=int(Random(500)) and Y = int(Random(500))

This would create that ship somewhere on 0-500x, 0-500y.

So you have specify the range, like so:

System->create Ship_mid_1 on X=int(Random(200,500)) and Y = int(Random(200,500))

Then the ships will be placed between 200,500x and 200,500y

I just don't understand very well the identification of the ships. I beleive that I need to spread 100 sprites on the game area for when the player clicks on one of them, check if there's a part of the ship below.

You don't need to add 100 sprites. What you can do is to simply add two variable to the ship.family for instant (BE ADVISED YOU ONLY NEED TO ADD THESE VARIABLES IF YOU WANT TO DO SOMETHING SPECIAL FOR EACH PART OF THE SHIP, OTHERWISE YOU DONT NEED THEM):

Ship_number = 1
Ship_part = "Mid"

Then when you create the ships and lets say you know that you need 5 ships.

The middle part of the ship is what we use to control a ship. As its the part that hold the "Front" and "End" image points.

So it would look like this:

1. First check if Number_of_ship_placed < than 5

if that's the case, we need to create a ship:
2. System->create Ship_mid_1 on X=int(Random(200,500)) and Y = int(Random(200,500))

- Ship_mid_1.Ship_number = Number_of_ships_placed
- Ship_mid_1.Ship_part = "Mid"

So now you would have a ship like this:

-o- <- middle part of the ship :)

(Then you know that this ship is number 1 ship, and its the middle part of it)

3. Then you spawn the rest of the ship parts.

So first we add the front part:

- On event Ship_mid_1 created
Ship_mid_1 spawn new object "Ship_front_1" at imagepoint "Front"
Ship_front_1.Ship_number = ship_mid_1.Ship_number (Now it have the same ship number as the middle part)
Ship_front_1.ship_part = "Front" (And now you know its the front part)

So now the ship looks like this:

<-o-

Then we spawn the last part exactly the same way:

Ship_mid_1 spawn new object "Ship_end_1" at imagepoint "End"
Ship_end_1.Ship_number = ship_mid_1.Ship_number (Now it have the same ship number as the middle part)
Ship_end_1.ship_part = "End" (And now you know its the end part)

And finally the ship looks like this:

<-o-)

Then we pin these parts to the middle part, so we only have to work with that piece when we have to manipulate it.

Ship_front.pin to object -> Ship_mid_1
Ship_end.pin to object -> Ship_mid_1

And finally you change the rotation of the middle part, to add the random rotation.

Ship_mid_1.angle = random(360)

If you in the pin behaviour have put it to "Position and angle" the other parts will follow the middle part correctly.

To check for hit

1. If Crosshair is overlapping Ship.family

Since it can overlap more than one part, you just use pick nearest as well, so it only picks 1.

And then you can now just check for ship number and ship part if you want to do something special to these parts. Or if you just want to destroy them you can just add:

- On event Mouse.left click object -> ship_family
Pick nearest ship to crosshair.

- Ship -> destroy
Show equation somewhere on the screen.

Here is an example of the crosshair and overlapping functionality.

Just spawn a ship with Q again and left click on a ship.

Battleship crosshair testnimos1002013-11-09 12:21:51
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,848

Post » Sat Nov 09, 2013 1:59 pm

But when the ship has 2, 4 or 5 pieces? In these cases, I can have 3, 2 or no piece in the middle of the ship. When you have 3 or 2 middle parts, all have the points of origin "front" and "end"? And the ship with two parts?
B
74
S
14
G
4
Posts: 1,016
Reputation: 8,135

Post » Sat Nov 09, 2013 3:06 pm

But when the ship has 2, 4 or 5 pieces? In these cases, I can have 3, 2 or no piece in the middle of the ship. When you have 3 or 2 middle parts, all have the points of origin "front" and "end"? And the ship with two parts?

You just modify each ship part so they fit. And you can freely decide which part of the ship you want to manipulate to control the ship. The size of the ship doesn't matter.

Some examples.

<) 2 part ship.
(Just add an image point to the "End" part called "front" and spawn the front part to that one or the other way around, doesn't really matter)

<--) 4 part ship.
(You could choose the 2 part, and there you add a "Front" and a "Mid_part_2", and you spawn the second mid part to that. And in the second mid part you just add one called "End" and spawn the end to that one. Then you pin all of them to your control part, which in this case is "Mid_part_1")

<---) 5 part ship
(Exactly the same as a 4 part ship, you just add a "Mid_part_3" to second mid part. And in the third mid part you add the "End" part)

Then in your code you can just make a "Ship creator" as I did in the example for each ship. And depending on which ship you want to create you use the correct "Ship creator".

As you create the different ships in design mode, you just make sure that the image points fits what you need.

So the "End" part used for a 2 part ship is not necessarily the same used for a 5 part ship. You could do it like that, but that's up to you to decide. You just have to modify the code accordingly, and not use the image points for some sizes ships that you would use for others.

To explain it a bit more clear:

If you only have 3 ship parts ("Front", "Mid", "End") you can make any size ship with these if you want. Doesn't matter whether its 4 or 100 long. The only thing is that if you want to use a 2 part ship, you have to add a front image point to the "End" part as well, which you only use for 2 part ships.

And to the mid part you have added "Front" and "End" image points.
Then you can in principal add whatever sprite you want to these image points. The names "Front" and "End" could just as well have been called "Snow" and "Rain" it wouldn't make any different, they are just names of the image points, that you have told construct 2 that you want them to be.

(Just some information about why to use names: The reason its always good to name things like "image points" and "layers" for instant, is if you don't and just refer to them with there index number. Like if you look at your layers, they have an index number starting at 0, and if you add another layer it will be 1. You can add objects such as your ships to a certain layer if you want, by saying "Spawn ship on layer 0" however if you instead of using the index number use the name of the layer. like "Spawn ship on layer "Playing area". You can move these layers around as you please, and then you don't screw up things. Imagine that you use the index number 0, and suddenly figure out that it would be pretty nice if the "Water" was on its own layer below where the "Ships" are. If you do that and you don't use the name of the layer. You have to go through all your code an correct all the index numbers, because now the "Water" is actually on the layer with index 0 and the "Ships" should be on layer 1, but if you use names you can just drag the layers as you please, because the name of the layer doesn't change.....anyway just a short explanation :))

So back on track.

Since "Front" and "End" are just names, you can just spawn another mid part on "End" and then get the new "Mid" part to spawn the "End" part of the ship instead.

nimos1002013-11-09 15:20:29
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,848

PreviousNext

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 5 guests