Workshop › Battleship › Step 32 of 35
Fire with Enter, play again
What we are doing
Enter or Space fires at the cursor. After the game, Enter, Space or a click starts a new one.
```js
} else if (event.key === 'Enter' || event.key === ' ') {
if (state !== 'playing') reset()
else playerShoots(cursor.r, cursor.c)
if (state !== 'playing') return reset()
```
What it means
- While playing, Enter or Space shoots the cursor's square; otherwise it resets.
- In
pointerdown, return reset() resets and stops, so the restarting click does not also shoot.
Your turn
- In
keydown, write the Enter/Space branch between the arrow block and the H line.
- Make
if (state !== 'playing') return reset() the first line of the pointerdown listener.
Skills: Keyboard and mouse input, Game states
← Move with the arrows Your best game →
All steps
- A 10 by 10 sea
- A function for any sea
- Your own sea
- Draw again and again
- Grids and ships as lists
- A random fleet
- Two fleets
- Which ship is here?
- Fire at a square
- Show the shots
- Your shot
- Tap to fire
- A message line
- Hit or miss?
- See your own fleet
- Sinking ships
- Darker hits on a sunk ship
- Outline sunk ships
- Win in as few shots as you can
- A record of their shots
- Take turns
- The computer shoots back
- You can lose too
- A heat map
- Shoot the hottest square
- Hunt around a hit
- See the heat map
- H for heat
- Show their ships at the end
- A keyboard cursor
- Move with the arrows
- Fire with Enter, play again
- Your best game
- A side panel
- Build it yourself: ships apart