Skip to content

Commit

Permalink
fix: bigger grid
Browse files Browse the repository at this point in the history
  • Loading branch information
Shibi Suriya committed Oct 2, 2023
1 parent 07432af commit 44cc29f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
31 changes: 16 additions & 15 deletions packages/game-client/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import { useDirection, useFood, useTicks, useSnakes, useInput, useSocket } from
import Grid from './Grid';
import styles from './app.module.css';

function App() {
const [mounted, setMounted] = useState(true);
return (
<div>
<div>
<button onClick={() => setMounted((prev) => !prev)}>Mount / Unmount</button>
</div>
<div>{mounted && <Game />}</div>
</div>
);
}
// function App() {
// const [mounted, setMounted] = useState(true);
// return (
// <div>
// <div>
// <button onClick={() => setMounted((prev) => !prev)}>Mount / Unmount</button>
// </div>
// <div>{mounted && <Game />}</div>
// </div>
// );
// }

function Game() {
function App() {
const [snakeId, setSnakeId] = useState(1);

// Keep the direction of the snakes inside useRef since we don't
Expand All @@ -28,7 +28,7 @@ function Game() {

useInput({ snakes, onUp, onDown, onLeft, onRight, snakeId });

const { food, getFood, removeFood, spawnFood } = useFood({ initialFoodState });
const { food, getFood, removeFood, spawnFood, isFood } = useFood({ initialFoodState });

const getSnakeCells = () => allSnakeCells();

Expand All @@ -45,19 +45,20 @@ function Game() {
getFood,
removeFood,
setDirection,
isFood,
});

useTicks({ updateSnake, snakes, food, spawnFood, getSnakeCells });

return (
<div className={styles.game}>
<select value={snakeId} onChange={(e) => setSnakeId(e.target.value)}>
{/* <select value={snakeId} onChange={(e) => setSnakeId(e.target.value)}>
{Object.keys(snakes).map((snakeId, index) => (
<option value={snakeId} key={index}>
{snakeId}
</option>
))}
</select>
</select> */}
<Grid snakes={snakes} food={food} />
</div>
);
Expand Down
4 changes: 2 additions & 2 deletions packages/game-client/src/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// in px (pixels)
const GRID_WIDTH = 30 * 30;
const GRID_HEIGHT = 30 * 10;
const GRID_WIDTH = 30 * 50;
const GRID_HEIGHT = 30 * 30;
const CELL_DIMENSION = 30;

if (GRID_HEIGHT % CELL_DIMENSION !== 0) {
Expand Down
2 changes: 1 addition & 1 deletion packages/game-client/src/grid.module.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.grid {
position: relative;
border: 3px solid black;
/* border: 3px solid black; */
}

.cell {
Expand Down
8 changes: 7 additions & 1 deletion packages/game-client/src/hooks/useSnakes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { generateKey } from '../utils';
import cloneDeep from 'lodash/cloneDeep';
import { DIRECTIONS, NUMBER_OF_ROWS, NUMBER_OF_COLUMNS, defaultDirections } from '../constants';

const useSnakes = ({ initialSnakesState, getDirection, getFood, removeFood, setDirection }) => {
const useSnakes = ({ initialSnakesState, getDirection, getFood, removeFood, setDirection, isFood }) => {
const [snakes, setSnakes] = useState(initialSnakesState);
const snakesRef = useRef(snakes);

Expand Down Expand Up @@ -31,6 +31,12 @@ const useSnakes = ({ initialSnakesState, getDirection, getFood, removeFood, setD
setDirection(snakeId, defaultDirections[snakeId]); // Set to the default initial direction.
snakesRef.current = { ...snakesRef.current, [snakeId]: initialSnakesState[snakeId] };
setSnakes(snakesRef.current);
for (const snakeCell of Object.values(snakesRef.current[snakeId].hash)) {
const { x, y } = snakeCell;
if (isFood(x, y)) {
removeFood(x, y);
}
}
};

const updateSnake = () => {
Expand Down

0 comments on commit 44cc29f

Please sign in to comment.