How to build a game like Battleship?

Get help using Construct 2

Post » Sat Nov 09, 2013 3:23 pm

You `re the guy. Each post is a lesson. The best thread about a game construction that I participated so far. All very well explained.

I will now give you a rest :). I'll digest it all and put into practice. Please do not disappear. After implementing all you said, I will return to give you a position or clarify another question, if you allow it. Thank you.RenatoB2013-11-09 15:26:00
B
74
S
13
G
4
Posts: 1,000
Reputation: 8,041

Post » Sat Nov 09, 2013 3:27 pm

Sure no problem :) hope it works out for you
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,838

Post » Sat Nov 09, 2013 11:25 pm

@nimos100

I'm came back (sooner than I thought).

I've trouble with while loop. I did the following:

numberShips = 0
while
numberShips <3: Create object Ship1_mid

family is overlapping family: System subtract 1 to numberShips
or
family is overlapping margin (new sprite to restrict game area)

The overlapping still happens.

The other problem is with the other parts of the ship (front and end), leaving the restricted area and also overlaps

What's wrong?
RenatoB2013-11-09 23:27:28
B
74
S
13
G
4
Posts: 1,000
Reputation: 8,041

Post » Sat Nov 09, 2013 11:46 pm

Try to link the capx file
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,838

Post » Sun Nov 10, 2013 12:40 am

Here is an example of how to do it.

There are still overlapping, as this example only checks whether the mid part of the ship overlaps with the boundaries, and so you can see how its done.

So to check for overlapping you still have to check for the other things, such as if front or end part is overlapping etc.

Anyway I made some changes so i removed the While loop.

And instead use a Ship_placed boolean which can be 0 for false and 1 for true. This is because there can be some problems with newly created objects. So doing it this way will remove that.

So now it simply uses

Number_of_ships < 3


Then I have added a Overlap checker which will run as long as:

Number_of_ships < 3
Ship_placed = 0

So when all ships have been placed Ship_placed = 1 and the overlapping checker stops.

Secondly I have added a Identifier to the ships, based on the UID of the mid part. So each part ("Front", "End") can be selected through this and destroyed as well when an overlap is detected.


Battleship Ship placement overlapping testnimos1002013-11-10 00:41:53
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,838

Post » Sun Nov 10, 2013 12:35 pm

Nice solution, @nimos100

I found very difficult to use "while". I only had used REPEAT and FOR before.

As you said, the ships are still overlapping others ships. I thought to include the condition "Ship_1_mid is overlapping family" in the group "Ship overlapping checker"? Anyway to use "or" and "and" together in the same conditions block?

I had not commented, but I keep using the grid instead of creating ships in coordinates. I want that ships are included in the columns and rows. To create different angles, I'm using CHOOSE pointing angles 0/90/-90/180RenatoB2013-11-10 14:56:50
B
74
S
13
G
4
Posts: 1,000
Reputation: 8,041

Post » Sun Nov 10, 2013 5:15 pm

Avoid overlapping of ships with the border sprites is simple. However, to avoid overlap of ships of other ships, divided into several parts is very complicated, mainly because I will have to create 05 different types of ships. Perhaps the hardest part of the game.

I'll check the overlapping in "Ship_1_mid on created" event and destroy that part if overlapping is confirmed. If not, I'll create the other parts (end/front) after the "else" event.

Sometimes 03 ships appears, but after several refreshs, one or more ships disappear sometimes.RenatoB2013-11-10 19:11:41
B
74
S
13
G
4
Posts: 1,000
Reputation: 8,041

Post » Sun Nov 10, 2013 7:22 pm

I found very difficult to use "while". I only had used REPEAT and FOR before.

No worries you don't really need "while" anyway and in most cases its best to use a "for" loop instead anyway. And you can get it to work as a "while" loop as well.

As you said, the ships are still overlapping others ships. I thought to include the condition "Ship_1_mid is overlapping family" in the group "Ship overlapping checker"? Anyway to use "or" and "and" together in the same conditions block?

Yeah there shouldn't be any problems in modifying the code so it works for that as well. Its pretty much the same.

What you can do, which I personally like to do, when I have to work with complicated tasks, is to write in short text what I would like to do, before I even start coding anything.

For instant in your case it could be like this, you can just write it as comments (Q hotkey).

1. Spawn mid part of ship at random location.
2. Choose size of ship.
3. Give ship a fixed rotation (0,90,180, 270)
4. Spawn rest of the ship parts.
5. Check that ship is within game area, and is not overlapping boundary.
6. Check that ship is not overlapping another ship.

Whether it should be exactly like that for you I don't know, but it kind of give you some overview/guide of what needs to be done, and you can focus on one task at the time, and make sure that it works before moving to the next.

Avoid overlapping of ships with the border sprites is simple. However, to avoid overlap of ships of other ships, divided into several parts is very complicated, mainly because I will have to create 05 different types of ships. Perhaps the hardest part of the game.

Actually I don't think its a lot more complicated, since you can just test:

ship is overlapping ship
Ship UID not equal to the UID that you are currently checking

Then it will check all other ships, except the one you are currently checking for overlaps. It might sound complicated I agree, but if you think about it like this, if we assume that you want to check that a destroyer with a unique name of "King Paul" is not overlapping another ship:

Check if a ship named "King Paul" is overlapping another ship, but don't check against ships that are called "King Paul".

Then it doesn't sound that complicated, it actually sounds pretty straight forward. Since the UID of an object in C2 is unique, we can use that, its just a number rather than a name like "King Paul". :)

If you compare it to the boundary, and we assume that we only want to check the right boundary for whatever reason:

Check if a ship is overlapping a boundary. But the boundary must be on the right side.

Its pretty much the same, both have two conditions. If you compare that to the actual boundary check that you use, which only have one condition:

Check if a ship is overlapping a boundary

Its not really all that more complicated, you just have to check each ship part, that are "linked" to the ID of the ship. Like if "King Paul" is made up of 3 parts, you have to check all ship parts, that have the name "King Paul" in the Ship_ID variable, and you can do that with a loop, like is:

Ship_ID = "King Paul" (Will pick all ship part with that name)
For each Ship

[Then you do the checks]nimos1002013-11-10 19:33:21
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,838

Post » Sun Nov 10, 2013 8:10 pm

Before all, thanks for the tips and come here on Sunday to help.

I've noticed that the logical issues are very easy for you. For me, programming, even with so many facilitators as they have in Construct2, is more difficult. I know it's a matter of logic and commands are details. I think that I should invest in logic. Would you recommend something for this (book / website / etc). It has to be for beginners.

I think (just think) that I understood your explanations and I'll try to implement them. I hope that I can still consult you.


Thanks again.RenatoB2013-11-10 20:39:37
B
74
S
13
G
4
Posts: 1,000
Reputation: 8,041

Post » Sun Nov 10, 2013 9:14 pm

I use this:

family1 shipID = ship_1_mid.UID
for each family1
    family1 is overlapping family1: family Destroy
                                    System Subtract 1 from Nr_of_placed

but not work. Maybe the problem is the subevent that verify if overlap happens?
B
74
S
13
G
4
Posts: 1,000
Reputation: 8,041

PreviousNext

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 8 guests