Duongw8l
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # ĐỌC HIỂU CODE MÔN MÃ NGUỒN MỞ - NGUYỄN MINH DƯƠNG_725105048 # Đoạn code chính thực thi trờ chơi # 1. **Lớp `Component` - Thành phần cơ bản của trò chơi** Lớp này định nghĩa các thuộc tính chung của một đối tượng trong trò chơi như kích thước, màu sắc, vị trí và phương thức cập nhật (vẽ lên canvas). ## **Thuộc tính** - `x, y`: Tọa độ của đối tượng trên canvas. - `width, height`: Kích thước của đối tượng. - `color`: Màu sắc của đối tượng (áp dụng nếu không phải ảnh). - `gameArea`: Đối tượng chứa thông tin khu vực chơi. - `gameContext`: Lấy đối tượng `context` từ `canvas`, cho phép vẽ lên nó. - `speedX, speedY`: Tốc độ di chuyển của đối tượng. - `gravity, gravitySpeed`: Ảnh hưởng của trọng lực lên đối tượng. ## **Phương thức** ### `1.Hàm update()`: - Vẽ hình chữ nhật lên `canvas` dựa vào tọa độ, màu sắc, kích thước. - ![Uploading file..._8bqk2lbiv]() - Đây là phương thức `update()` trong lớp `Component`. Nó có nhiệm vụ vẽ một hình chữ nhật đại diện cho đối tượng trên `canvas`. --- ##### **Phân tích từng dòng** ```javascript this.gameContext.fillStyle = this.color; ``` - Đặt màu nền (`fillStyle`) cho hình chữ nhật bằng giá trị `this.color`. - `this.color` được truyền vào khi tạo đối tượng, ví dụ: `"red"`, `"#2CB01A"` --- ```javascript this.gameContext.fillRect(this.x, this.y, this.width, this.height); ``` - Vẽ một hình chữ nhật màu `this.color` lên `canvas` với: - `this.x`, `this.y`: Tọa độ góc trên bên trái. - `this.width`, `this.height`: Kích thước của hình chữ nhật. - Đây là phần nền của đối tượng. --- ```javascript this.gameContext.strokeStyle = 'black'; ``` - Thiết lập màu viền (`strokeStyle`) của hình chữ nhật thành **màu đen**. --- ```javascript this.gameContext.lineWidth = 10; ``` - Đặt độ dày của viền là **10px**. --- ```javascript this.gameContext.strokeRect(this.x, this.y, this.width, this.height); ``` - Vẽ viền màu đen quanh hình chữ nhật với độ dày 10px. --- ##### **Tóm tắt hoạt động của `update()`** 1. Vẽ một hình chữ nhật màu (`fillRect`). 2. Vẽ một viền đen xung quanh hình chữ nhật (`strokeRect`). 3. Hình chữ nhật này sẽ đại diện cho các đối tượng như chướng ngại vật. --- ##### **Tại sao cần `strokeRect`?** - Nếu chỉ có `fillRect()`, hình sẽ không có viền, trông kém nổi bật. - `strokeRect()` giúp dễ nhìn thấy đối tượng hơn, đặc biệt khi nền có màu tương tự đối tượng. 📝 **Tóm lại**: Phương thức này giúp hiển thị một đối tượng hình chữ nhật có màu nền và viền đen trên `canvas`. 🚀 **--------------------------------------------------------------------------------------------------------------------------------------------------------** ..................................................................... ### `2.Hàm crashWith(otherObj)`: - Kiểm tra xem đối tượng hiện tại có va chạm với `otherObj` không bằng cách so sánh tọa độ biên của hai đối tượng. - Đây là **hàm khởi tạo (`constructor`)** của lớp `Component`. Nó chịu trách nhiệm thiết lập các thuộc tính ban đầu cho một đối tượng game. --- #### Phân tích từng dòng ##### 2.1 Nhận tham số đầu vào ```javascript constructor({ gameArea, width, height, color, x, y }) { ``` - Hàm khởi tạo nhận một **đối tượng** chứa các thuộc tính: - `gameArea`: Khu vực trò chơi (`GameArea`). - `width`, `height`: Kích thước đối tượng. - `color`: Màu sắc. - `x`, `y`: Tọa độ ban đầu trên canvas. **Lưu ý**: Cú pháp `{ gameArea, width, height, ... }` là **destructuring** để trích xuất các giá trị từ một object. --- ##### 2.2 Gán các giá trị đầu vào cho thuộc tính của đối tượng ```javascript this.x = x; this.y = y; this.width = width; this.color = color; this.height = height; ``` - Gán các tham số đầu vào cho đối tượng `this` (đối tượng đang được tạo ra). - `this.x`, `this.y`: Xác định vị trí trên canvas. - `this.width`, `this.height`: Xác định kích thước đối tượng. - `this.color`: Xác định màu sắc của đối tượng. --- ##### 2.3 Lưu tham chiếu đến `GameArea` ```javascript this.gameArea = gameArea; this.gameContext = gameArea.canvas.getContext('2d'); ``` - `this.gameArea = gameArea;`: Lưu tham chiếu đến `GameArea` để sử dụng sau này. - `this.gameContext = gameArea.canvas.getContext('2d');`: - Lấy **context 2D** của `canvas`, cho phép vẽ lên canvas. - Được sử dụng trong `update()` để vẽ hình. --- ##### 2.4 Khởi tạo các thuộc tính liên quan đến chuyển động ```javascript this.score = 0; this.speedX = 0; this.speedY = 0; this.gravity = 0; this.gravitySpeed = 0; ``` - `this.score = 0;`: Điểm số ban đầu là `0`. - `this.speedX = 0;`: Tốc độ di chuyển theo trục `X`. - `this.speedY = 0;`: Tốc độ di chuyển theo trục `Y`. - `this.gravity = 0;`: Trọng lực mặc định là `0`, sau này có thể thay đổi. - `this.gravitySpeed = 0;`: Tốc độ rơi do trọng lực, khởi tạo bằng `0`. -> **Giải thích về trọng lực (`gravity`)** - Nếu `gravity > 0`: Đối tượng rơi xuống dần theo thời gian. - Nếu `gravity < 0`: Đối tượng bay lên (như khi nhấn phím **Space**). - `gravitySpeed` sẽ tăng dần, tạo hiệu ứng rơi tự nhiên. -> **Ví dụ sử dụng gravity trong game:** ```javascript this.gravity = 0.05; // Trọng lực nhỏ để rơi từ từ. this.gravitySpeed += this.gravity; this.y += this.gravitySpeed; ``` -> Mỗi lần cập nhật, `gravitySpeed` tăng lên, khiến nhân vật rơi xuống nhanh hơn. --- ##### Tóm tắt - Hàm `constructor` **khởi tạo** một đối tượng với: - **Kích thước**, **màu sắc**, **vị trí** trên `canvas`. - **Tham chiếu đến `GameArea`** để vẽ lên canvas. - **Tốc độ di chuyển** (`speedX`, `speedY`) và **trọng lực** (`gravity`, `gravitySpeed`). 🚀 **Tóm lại**: Đây là bước đầu tạo ra một **thực thể (entity) di chuyển được** trong trò chơi! 🎮 --- # 2. **Lớp `MyGamePiece` - Đối tượng điều khiển chính (chim)** Lớp này kế thừa từ `Component` nhưng thay đổi cách hiển thị từ hình chữ nhật sang hình ảnh con chim. ### **Phương thức** - `constructor(obj)`: Thay vì vẽ một hình chữ nhật, nó tải hình ảnh (`/static/img/convit.png`) vào `this.img`. - `update()`: Dùng `drawImage` để vẽ hình ảnh thay vì hình chữ nhật. - `newPos()`: Cập nhật vị trí mới của chim dựa vào trọng lực và tốc độ. - `checkHitTop()` và `checkHitBottom()`: Kiểm tra xem chim có vượt quá viền trên hoặc viền dưới màn hình không. --- # 3. **Lớp `TextComponent` - Hiển thị văn bản (điểm số)** Lớp này kế thừa `Component` nhưng thay vì vẽ hình chữ nhật, nó hiển thị văn bản lên màn hình. - `update(text)`: Dùng `fillText` để hiển thị điểm số. --- # 4. **Lớp `GameArea` - Quản lý toàn bộ trò chơi** Lớp này điều khiển các thành phần trong game, cập nhật màn hình và xử lý logic trò chơi. ### **Thuộc tính** - `gameRunning`: Trạng thái trò chơi (đang chạy hay dừng). - `canvas`: Phần tử HTML `canvas` để vẽ trò chơi. - `context`: Đối tượng để vẽ lên `canvas`. - `frameNo`: Đếm số frame đã chạy (dùng để tạo chướng ngại vật). - `myPiece`: Đối tượng chim mà người chơi điều khiển. - `myScore`: Đối tượng hiển thị điểm số. - `myObstacles`: Mảng chứa các chướng ngại vật. ### **Phương thức** #### `start()` - Kiểm tra nếu trò chơi đã chạy thì không làm gì. - Ẩn phần giới thiệu (`bird-running`), tắt nhạc intro. - Chạy nhạc nền (`maplestory_playing.mp3`). - Ẩn nút `START GAME`, thêm `canvas` vào trang. - Chạy hàm `updateGameArea()` liên tục mỗi 10ms. #### `checkCrash()` - Duyệt qua mảng `myObstacles` để kiểm tra xem chim có va chạm với bất kỳ chướng ngại vật nào không. #### `updateGameArea()` - Xóa màn hình (`clearRect`). - Nếu đủ số frame, tạo một chướng ngại vật mới. - Cập nhật vị trí của tất cả chướng ngại vật và vẽ lại. - Cập nhật điểm số. - Cập nhật vị trí và vẽ lại chim. - Nếu chim va vào chướng ngại vật, gọi `loseGame()`. #### `loseGame()` - Dừng nhạc nền, xóa canvas, ẩn hướng dẫn. - Hiển thị điểm số của người chơi. - Gửi điểm lên backend bằng hàm `submitPoint(score)`. --- # 5. **Xử lý phím (`onkeyup`, `onkeydown`)** - Khi **bấm phím cách (SPACE)**: Chim bay lên (`gravity = -0.2`). - Khi **nhả phím cách**: Chim rơi xuống (`gravity = 0.05`). --- # 6. **Giao diện HTML** - **Nút "START GAME"**: Gọi `myGameArea.start()`. - **Hướng dẫn chơi**: Hiển thị cách điều khiển. - **Phần hiển thị điểm khi thua**: `text-losegame`. --- ### **Tóm tắt chức năng chính** - **Nhấn SPACE** để chim bay lên. - **Tự động rơi xuống** khi không nhấn. - **Tránh chướng ngại vật** để đạt điểm cao. - Khi **va chạm**, hiển thị điểm và dừng trò chơi.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully