Upgrade to Pro — share decks privately, control downloads, hide ads and more …

pythonB0129029

B0129029
June 20, 2014
180

 pythonB0129029

B0129029

June 20, 2014
Tweet

Transcript

  1. 全域變數 FPS = 30 視窗寬度 = 640 視窗高度 = 480

    半視窗寬度 = int(視窗寬度 / 2) 半視窗高度 = int(視窗高度 / 2) 視窗黑屏 = 90 移動速率 = 9 跳出率 = 6 跳躍高度 = 30 開始尺寸 = 25 勝利尺寸 = 300 INVULNTIME = 2 遊戲結束次數 = 4 最大生命 = 3 草的數量 = 80 松鼠數量 = 30 松鼠最小速度 = 3 松鼠最大速度 = 7 變更DIR速度 = 2 左 = 'left' 右 = 'right‘ 草色 = (24, 255, 0) 白色 = (255, 255, 255) 紅色 = (255, 0, 0) 草的數量 = 80 松鼠數量 = 30 松鼠最小速度 = 3 松鼠最大速度 = 7 變更DIR速度 = 2 左 = 'left' 右 = 'right'
  2. 執行遊戲()-1 設定遊戲事件的觸發與執行內容。 先設定勝利訊息、game over訊息: 結束文字 = 基本字體.render('Game Over', True, 白色)

    結束大小 = 結束文字.get_rect() 結束大小.center = (半視窗寬度, 半視窗高度) 勝利文字 = 基本字體.render('You have achieved OMEGA SQUIRREL!', True, 白色) 勝利大小 = 勝利文字.get_rect() 勝利大小.center = (半視窗寬度, 半視窗高度) 勝利文字2 = 基本字體.render('(Press "r" to restart.)', True, 白色) 勝利大小2 = 勝利文字2.get_rect() 勝利大小2.center = (半視窗寬度, 半視窗高度 + 30)
  3. 執行遊戲()-2 設定視窗,建立草、松鼠、玩家 視窗x = 0 視窗y = 0 草設定 =

    [] 松鼠設定 = [] 玩家設定 = {'外表': pygame.transform.scale(向左松鼠圖案, (開始尺寸, 開始尺寸)), '面向': 左, '尺寸': 開始尺寸, 'x': 半視窗寬度, 'y': 半視窗高度, '跳':0, '生命': 最大生命} for i in range(10): 草設定.append(做出草(視窗x, 視窗y)) 草設定[i]['x'] = random.randint(0, 視窗寬度) 草設定[i]['y'] = random.randint(0, 視窗高度)
  4. 顯示生命條() def 顯示生命條(目前生命): for i in range(目前生命): pygame.draw.rect(遊戲畫面, 紅色, (15,

    5 + (10 * 最大生命) - i * 10, 20, 10)) for i in range(最大生命): pygame.draw.rect(遊戲畫面, 白色, (15, 5 + (10 * 最大生命) - i * 10, 20, 10), 1) 輸出圖形表示其生命,將顯有生命圖形疊於 最大生命圖形上表示。
  5. 獲得隨機視窗變量(): def 獲得隨機視窗變量(視窗x, 視窗y, 物件寬度, 物件高度): 視窗大小 = pygame.Rect(視窗x, 視窗y,

    視窗寬度, 視窗高度) while True: x = random.randint(視窗x - 視窗寬度, 視窗x + (2 * 視窗寬度)) y = random.randint(視窗y - 視窗高度, 視窗y + (2 * 視窗高度)) 物件大小 = pygame.Rect(x, y, 物件寬度, 物件高度) if not 物件大小.colliderect(視窗大小): return x, y 取得隨機的視窗座標。
  6. 做出松鼠()-1 def 做出松鼠(視窗x, 視窗y): sq = {} 一般尺寸 = random.randint(5,

    25) 倍率 = random.randint(1, 3) sq['width'] = (一般尺寸 + random.randint(0, 10)) * 倍率 sq['height'] = (一般尺寸 + random.randint(0, 10)) * 倍率 sq['x'], sq['y'] = 獲得隨機視窗變量(視窗x, 視窗y, sq['width'], sq['height']) sq['movex'] = 獲得隨機速度() sq['movey'] = 獲得隨機速度() if sq['movex'] < 0: sq['外表'] = pygame.transform.scale(向左松鼠圖案, (sq['width'], sq['height'])) else: sq['外表'] = pygame.transform.scale(向右松鼠圖案, (sq['width'], sq['height'])) sq['跳'] = 0 sq['跳出率'] = random.randint(10, 18) sq['跳躍高度'] = random.randint(10, 50) return sq
  7. 做出草() def 做出草(視窗x, 視窗y): gr = {} gr['grassImage'] = random.randint(0,

    len(草圖片) - 1) gr['width'] = 草圖片[0].get_width() gr['height'] = 草圖片[0].get_height() gr['x'], gr['y'] = 獲得隨機視窗變量(視窗x, 視窗y, gr['width'], gr['height']) gr['rect'] = pygame.Rect( (gr['x'], gr['y'], gr['width'], gr['height']) ) return gr 設定草的基本數據:寬度、高度、所使用的 圖片。