GDG Logo

Game Dev with React Workshop

Obstacle Component

The Obstacle component represents obstacles that appear on the game board, which the player's spaceship must avoid. Below, we break down its implementation.

1. Import Statements

import React from 'react';

Imports the React library, which is essential for creating the Obstacle component.

2. Functional Component Declaration

const Obstacle = ({ x, y }) => { ... };
      return();

The `Obstacle` component is a functional component that takes `x` and `y` as props. These props determine the position of the obstacle on the game board.

3. Dynamic Styling for Positioning


        <div 
          className="obstacle"
          style={{ 
            left: `${x}px`,
            top: `${y}px`
          }}
        />
      

The `style` prop dynamically positions the obstacle using the `x` and `y` values passed as props. The `left` and `top` CSS properties ensure the obstacle appears at the correct location on the game board.

4. Export Statement

export default Obstacle;

Exports the `Obstacle` component so it can be used in other parts of the application, such as the game board.