--- title: 10/27 社課共筆 tags: 社課 , MCU DSC 109 , python , pygame --- # 2020.10.27 社課共筆:PyGame * [課程講義](https://hackmd.io/@andy010629/BJY4bDEuP) * [提問](https://app.sli.do/event/18mhx5qs/live/questions) * [YouTube](https://youtu.be/6dufVQ6aCGc) ## Game loop 在遊戲的整個遊玩中,程式在一個不斷循環偵測的處理過程 ## Coding * 可以把球、反彈板等遊戲中的物件先設定好(初始化) ### 第一步 * `clock`:每秒畫面更新速率計時器 * `pygame.display.update()`: 更新畫面 ### 第二步 * 另外開一個檔案,寫球和發射板的程式 * `__init__`: 初始化函式 * 參數 * `self`: 我是哪個物件 * `canvas`: 所在的畫布 * `pos`: 所在的位置 * `radius`: 球的半徑 * 球跟反彈板的`update()`要放在`display.update()`前,因為要先繪製遊戲物件再更新畫面 ### 第三步 * 記得要先把上一步的兩行測試宣告刪除 * 在這裡物件位置還沒有在正確位置 ### 第四步 * 在進入遊戲迴圈之前。要先重設遊戲(透過`resetGame()`) * 當滑鼠移動時,讓反彈板跟著移動 ```python if event.type == pygame.MOUSEMOTION: paddle_x = pygame.mouse.get_pos()[0] - 50 ``` ``` ``` ### 第五步 當球碰到反彈板時,要進行碰撞偵測並處理 首先宣告碰撞偵測函數 ```python ``` ```python def isCollision(Rect1, Rect2): if(pygame.Rect.colliderect(Rect1, Rect2)): return True return False ``` 並且在適當位置放入碰撞判斷 ```python if(isCollision(ball.rect, paddle.rect)): dy = -dy ``` <style> i.fa.fa-file-text:after { content: " MCUOSC ·" } </style>
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
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
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.