Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module #2 (Nowshin Owishi) #29

Open
wants to merge 11 commits into
base: module-2
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const Direction = {
Bottom: { x: 0, y: 1 },
};

const Cell = ({ x, y, type }) => {
const Cell = ({ x, y, type, remaining }) => {
const getStyles = () => {
switch (type) {
case CellType.Snake:
Expand All @@ -33,10 +33,11 @@ const Cell = ({ x, y, type }) => {

case CellType.Food:
return {
backgroundColor: "darkorange",
backgroundColor: "tomato",
borderRadius: 20,
width: 32,
height: 32,
transform: `scale(${0.5 + remaining / 20})`,
};

case CellType.Poison:
Expand All @@ -45,6 +46,7 @@ const Cell = ({ x, y, type }) => {
borderRadius: 20,
width: 32,
height: 32,
transform: `scale(${0.5 + remaining / 20})`,
};

default:
Expand All @@ -61,7 +63,9 @@ const Cell = ({ x, y, type }) => {
height: Config.cellSize,
}}
>
<div className={styles.cell} style={getStyles()}></div>
<div className={styles.cell} style={getStyles()}>
{remaining}
</div>
</div>
);
};
Expand Down Expand Up @@ -207,8 +211,8 @@ const UseSnake = () => {
UseInterval(() => addObject("food"), 3000);
UseInterval(() => addObject("poison"), 1000);
UseInterval(runSingleStep, 300);
UseInterval(() => removeObject("food"),50);
UseInterval(()=> removeObject("poison"), 100);
UseInterval(() => removeObject("food"), 50);
UseInterval(() => removeObject("poison"), 100);

const changeDir = (checkDir, newDir) => {
setDirection((direction) => {
Expand Down Expand Up @@ -241,28 +245,36 @@ const UseSnake = () => {

return () => window.removeEventListener("keydown", handleNavigation);
}, []);

return { score, isFood, isSnake, isPoison };
};

//view
const Snake = () => {
const { score, isFood, isSnake, isPoison } = UseSnake();
const cells = [];
for (let x = 0; x < Config.width; x++) {
for (let y = 0; y < Config.height; y++) {
let type = CellType.Empty;
let type = CellType.Empty,
remaining = undefined;
if (isFood({ x, y })) {
type = CellType.Food;
remaining =
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@royantar0311 I forgot to add the remaining time to the food, so I tried it out today. But the time shows undefined. I can't figure out the problem :")

10 -
Math.round(
(Date.now() -
foods.find((food) => food.x === x && food.y === y).createdAt) /
Copy link

@MahdiMurshed MahdiMurshed Apr 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as your cell looks like

{
x: Math.floor(Math.random() * Config.width),
  y: Math.floor(Math.random() * Config.width),
  start: Date.now()
}

.createdAt should be

 poisons.find((poison) => poison.x === x && poison.y === y)
                .start) /
              1000

@owishiboo

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, bro @MahdiMurshed

1000
);
} else if (isSnake({ x, y })) {
type = CellType.Snake;
} else if (isPoison({ x, y })) {
type = CellType.Poison;
}
cells.push(<Cell key={`${x}-${y}`} x={x} y={y} type={type} />);
cells.push(
<Cell key={`${x}-${y}`} x={x} y={y} type={type} remaining={remaining} />
);
}
}
return { score, isFood, isSnake, isPoison, cells };
};

//view
const Snake = () => {
const { score, cells } = UseSnake();
return (
<div className={styles.container}>
<div
Expand Down