--- tags: 開源社 title: Pygame 總結 && 上傳到 Github --- # Pygame 總結 && 上傳到 Github 共筆: [匿名提問](https://app.sli.do/event/ps73jsw2) [前情提要](https://hackmd.io/isNp5KBZSvSj2UVt-rBwdw?both) 還記得上次我們做到什麼部分了嗎! 忘記得可以看一下上次的 教學筆記歐~ 這邊附上 上次完成的扣 -main.py ```python= import sys import pygame from pygame.locals import QUIT from Object import * def resetGame(): global game_mode,dx, dy game_mode = 0 dx = BALL_SPEED dy = -BALL_SPEED def isCollision(Rect1, Rect2): if(pygame.Rect.colliderect(Rect1, Rect2)): return True return False SCREEN_SIZEX = 800 SCREEN_SIZEY = 600 SCREEN_COLOR = (0,0,0) BALL_SPEED = 8 dx = BALL_SPEED dy = -BALL_SPEED clock = pygame.time.Clock() pygame.init() window_surface = pygame.display.set_mode((SCREEN_SIZEX,SCREEN_SIZEY)) window_surface.fill(SCREEN_COLOR) paddle_x = 0 paddle_y = (SCREEN_SIZEY - 48) paddle = Paddle(window_surface, [paddle_x, paddle_y, 100, 24]) ball_x = paddle_x ball_y = paddle_y ball = Ball(window_surface, [ball_x, ball_y], 8) pygame.display.update() # 滑鼠 鎖定 和 隱藏 pygame.event.set_grab(True) pygame.mouse.set_visible(False) resetGame() while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: game_mode = 1 if event.type == pygame.MOUSEMOTION: paddle.rect[0] = pygame.mouse.get_pos()[0] - 50 window_surface.fill(SCREEN_COLOR) if game_mode==0: ball.pos[0] = ball_x = paddle.rect[0] + ((paddle.rect[2] - ball.radius) >> 1) ball.pos[1] = ball_y = paddle.rect[1] - ball.radius else: if(isCollision(ball.rect, paddle.rect)): dy = -dy #判斷死亡. if(ball_y + dy > SCREEN_SIZEY - ball.radius): resetGame() # 右牆或左牆碰撞. if(ball_x + dx > SCREEN_SIZEX - ball.radius or ball_x + dx < ball.radius): dx = -dx # 上牆碰撞 if(ball_y + dy < -ball.radius): dy = -dy ball_x += dx ball_y += dy ball.pos[0] = ball_x ball.pos[1] = ball_y paddle.update() ball.update() pygame.display.update() clock.tick(60) ``` -Object.py ```python= import pygame class Ball(object): def __init__(self,canvas,pos, radius): self.pygame = pygame self.canvas = canvas self.pos = pos self.radius = radius self.color = (100,200,200) self.visible = True self.rect = self.pygame.draw.circle(self.canvas, self.color, self.pos, self.radius) def update(self): if(self.visible): self.rect = self.pygame.draw.circle(self.canvas, self.color, self.pos, self.radius) class Paddle(object): def __init__(self,canvas, rect): self.pygame = pygame self.canvas = canvas self.rect = rect self.color = (255, 255, 255) self.visible = True def update(self): if(self.visible): self.pygame.draw.rect(self.canvas, self.color, self.rect) ``` ## 這堂課 我們要來做磚塊的部分! ### 那我們先把這張圖下載下來吧! ![](https://i.imgur.com/Q70GAKT.png) >沒錯XD 一方面覺得只打磚塊太普通了, 一方面因為進度問題 我們今天來打 這隻娃娃吧~ 大家覺得他像誰呢 >大家把他下載下來之後 放到跟程式碼同樣的資料夾下喔! ## 在開始之前我們一樣來想想流程吧 今天的遊戲會像是這樣: 大家拿球球去打娃娃 打到五次之後就贏了 所以我們需要準備 1. 娃娃圖片的載入 1. 娃娃的碰撞反彈 1. 計分板的製作 ### 圖片的載入 在 pygame 套件中 我們想要實現 加載圖片這件事情 是非常簡單的 我們可以透過 ``pygame.image.load('要載入的圖片')`` 這個函數會載入圖片 並且回傳一個 Surface 物件 像這樣 ``` qq = pygame.image.load('要載入的圖片') ``` 然後因為這張圖片太大了 我們可以用 pygame內建的函數 快速的修改大小 像這樣 ``` qq = pygame.transform.scale(qq, (100, 130)) ``` 最後我們在 我們的遊戲迴圈內的一開始 繪製出這個娃娃 ```python qq_size = qq.get_size() qq_rect = window_surface.blit(qq,(SCREEN_SIZEX/2 - qq_size[0]/2,SCREEN_SIZEY/2 - qq_size[1]/2-100)) ``` ### 碰撞反彈 ```python= if(isCollision(qq_rect, ball.rect)): if ball.pos[1] > qq_rect[1]+qq_rect[3] or ball.pos[1] < qq_rect[1]: dy = - dy else: dx = - dx ``` ### 計分板 首先 我們會需要一個變數 來當分數 ``score = 0`` 再來就是在畫面上繪製計分板 這邊跟第一堂課教的 Hello World 一樣喔 先初始化 ``` pygame.font.init() ``` 然後建立 text 物件 ``text = pygame.font.SysFont("None", 25)`` 接著就是在遊戲迴圈內繪製 ```python #文字渲染成圖層 scoreboard = text.render("Score = " + str(score), 1, (255,25,255)) #在畫面上繪製 window_surface.blit(scoreboard,(10,10)) ``` 最後是分數的控制! 加分 ```python= #當球和娃娃發生碰撞 if(isCollision(qq_rect, ball.rect)): score+=1 ...........以下省略................ ``` 死掉歸零 ```python= #重置遊戲 def resetGame(): global game_mode,dx, dy,score score = 0 ...........以下省略................ ``` 5分時 結束遊戲 ```python= if game_mode == 0: .... elif score>=5: resetGame() else: .... ``` GitHub https://hackmd.io/@andy010629/BJB6pdg5w