Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
pythonB0129029
Search
B0129029
June 20, 2014
210
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
pythonB0129029
B0129029
June 20, 2014
Featured
See All Featured
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Crafting Experiences
bethany
1
170
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.5k
How to Think Like a Performance Engineer
csswizardry
28
2.6k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.8k
Odyssey Design
rkendrick25
PRO
2
690
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
140
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
560
Producing Creativity
orderedlist
PRO
348
40k
Technical Leadership for Architectural Decision Making
baasie
3
400
Optimizing for Happiness
mojombo
378
71k
Un-Boring Meetings
codingconduct
0
310
Transcript
Python期末報告 B0129029王睿陞
全域變數 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'
main 設定基本數值FPS時間、 DISPLAYSURF、 基本字體、向 左松鼠圖案、 向右松鼠圖案、 草圖片,並執行遊戲。
函式
執行遊戲()-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)
執行遊戲()-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, 視窗高度)
執行遊戲()-3 設定按鍵觸發事件: 控制鍵有四個,為方向鍵上下左右,根 據按鍵觸發角色動作,改變突騙集所在位置。 設定物件碰撞觸發事件: 當操作角色碰撞期他松鼠時,判斷是否 受傷(生命-1並無敵閃爍),或是成長。
執行遊戲()-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) 輸出圖形表示其生命,將顯有生命圖形疊於 最大生命圖形上表示。
終止() def 終止(): pygame.quit() sys.exit() 終止程式。
取得跳躍值() def 取得跳躍值(當前跳躍值, 跳出率, 跳躍高度): return int(math.sin( (math.pi / float(跳出率))
* 當前跳躍值 ) * 跳躍高度)
獲得隨機速度() def 獲得隨機速度(): speed = random.randint(松鼠最小速度, 松鼠最大速度) if random.randint(0, 1)
== 0: return speed else: return –speed
獲得隨機視窗變量(): 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 取得隨機的視窗座標。
做出松鼠()-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
做出松鼠()-2 設定一隻松鼠的基本數據:寬度、高度、 移動速度XY、跳躍高度、跳躍頻率、外表所 使用的圖片。
做出草() 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 設定草的基本數據:寬度、高度、所使用的 圖片。