---
title: "Discussion on \"02 Game Loops\" | Hashnode"
description: "Discussion on \"02 Game Loops\". JavaScript Game Development"
url: "https://hashnode.com/posts/02-game-loops/624efb667f832067321ea09a"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [02 Game Loops](https://sudarshanrai.hashnode.dev/02-game-loops?utm_source=hashnode&utm_medium=feed)

JavaScript Game Development

Apr 7, 2022 · 5 min read · sudarshanrai.hashnode.dev  
Updated Jun 29  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#game-development](https://hashnode.com/tag/game-development)
-   [#html5](https://hashnode.com/tag/html5)
-   [#gamedev](https://hashnode.com/tag/gamedev)
-   [#canvas](https://hashnode.com/tag/canvas)

## Written by

[Sudarshan Rai](https://hashnode.com/@sudarshanrai)

@sudarshanrai · Sydney, Australia

  
[View profile](https://hashnode.com/@sudarshanrai)

Software Developer

I'm Sudarshan Rai, a seasoned Fullstack Developer based in Nepal, with 6+ years of experience in delivering high-performance applications. My technical foundation—spanning frameworks, libraries, and runtime environments. I proactively research emerging technologies to ensure the solutions I build are modern, scalable, and future-proof.

## Responses 3

Markdown supported · ⌘↵ to post

[Gunaraj Khatri](https://hashnode.com/@gunaraaj)

[Apr 8, 2022](https://hashnode.com/posts/02-game-loops/624efb667f832067321ea09a/comment/624fce367f832067321eb1c9)

Challenge of bounce to-fro: modify in update() method

```
 if(this.x===0){
      this.count--;
    }
    if(this.x===canvas.width){
      this.count++;
    }
    if(this.count===0){
    this.x += this.speedX;
    this.y += this.speedY;
    }
    else{
       this.x -= this.speedX;
    this.y -= this.speedY;
    }
```

_initially count is supposed to be 1_

[Sudarshan Rai](https://hashnode.com/@sudarshanrai)

[Apr 8, 2022](https://hashnode.com/posts/02-game-loops/624efb667f832067321ea09a/comment/624fce367f832067321eb1c9#reply-624fd4fbbd2d386758329dfb)

[Gunaraj Khatri](https://hashnode.com/@gunaraaj)Nice one, Also You can add rect width to rect x position to get rect right side of the for bounce.

```
if(this.x + this.width === canvas.width)
```

[Gunaraj Khatri](https://hashnode.com/@gunaraaj)

[Apr 8, 2022](https://hashnode.com/posts/02-game-loops/624efb667f832067321ea09a/comment/624fce367f832067321eb1c9#reply-624fea2c7f832067321eb4a9)

[Sudarshan Rai](https://hashnode.com/@sudarshanrai) oh, yeah!!

[Prajwal Pradhan](https://hashnode.com/@ctrlxaltf4)

[Apr 7, 2022](https://hashnode.com/posts/02-game-loops/624efb667f832067321ea09a/comment/624f0edfbd2d386758329088)

Waiting for more...

[Shanti Moktan](https://hashnode.com/@myu906)

[Apr 7, 2022](https://hashnode.com/posts/02-game-loops/624efb667f832067321ea09a/comment/624f042f7f832067321ea2b7)

Awesome and clear explaination.

[Sudarshan Rai](https://hashnode.com/@sudarshanrai)

[Apr 7, 2022](https://hashnode.com/posts/02-game-loops/624efb667f832067321ea09a/comment/624f042f7f832067321ea2b7#reply-624f15dcbd2d386758329116)

Thank you [Shanti Moktan](https://hashnode.com/@myu906)
