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

B0129012_機率與統計

fankyou60
June 19, 2014
180

 B0129012_機率與統計

fankyou60

June 19, 2014
Tweet

Transcript

  1. 目前可用函數如下,可以進一步擴充 範圍= range 啟動= pygame.init 鐘類= pygame.time.Clock 幕設大小= pygame.display.set_mode 影像下載=

    pygame.image.load 轉換平滑尺度= pygame.transform.smoothscale 事件取得= pygame.event.get 結束= pygame.quit 離開= sys.exit 隨機選擇= random.choice 畫方形= pygame.draw.rect 事件取得= pygame.event.get 幕更新= pygame.display.update 系統離開= sys.exit 事件張貼= pygame.event.post 時間等待= pygame.time.wait '
  2. 程式 # Wormy (a Nibbles clone) # By Al Sweigart

    al@inventwithpython. # http://inventwithpython.com/pygame # Released under a "Simplified BSD" license import random, pygame, sys from pygame.locals import * 更新 = 15 寬 = 640 高 = 480 尺寸 = 20 assert 寬 % 尺寸 == 0, "Window width must be a multiple of cell size." assert 高 % 尺寸 == 0, "Window height must be a multiple of cell size." 寬度 = int(寬 / 尺寸) 高度 = int(高 / 尺寸) #R G B 白色的 = (255, 255, 255) 黑色的 = ( 0, 0, 0) 紅色的 = (255, 0, 0) 綠色的 = ( 0, 255, 0) 暗綠色 = ( 0, 155, 0) 暗灰色 = ( 40, 40, 40) 色彩 = 黑色的
  3. 大上 = '上' 大下 = '下' 大左 = '左' 大右

    = '右' 頭 = 0 # syntactic sugar: index of the worm's 頭 def main(): global 更新時間, 顯示, 基礎 pygame.init() 更新時間 = pygame.time.Clock() 顯示 = pygame.display.set_mode((寬, 高)) 基礎 = pygame.font.Font('freesansbold.ttf', 18) pygame.display.set_caption('Wormy') 開始幕() while True: 跑遊戲() 新遊戲()
  4. def 跑遊戲(): # Set a random start point. 開始1 =

    random.randint(5, 寬度 - 6) 開始2 = random.randint(5, 高度 - 6) 蟲長度 = [{'x': 開始1, 'y': 開始2}, {'x': 開始1 - 1, 'y': 開始2}, {'x': 開始1 - 2, 'y': 開始2}] 方向 = 大右 # Start the 食物 in a random place. 食物 = 食物位置() while True: # main game loop for event in pygame.event.get(): # event handling loop if event.type == QUIT: 停止()
  5. # check if the worm has hit itself or the

    edge if 蟲長度[頭]['x'] == -1 or 蟲長度[頭]['x'] == 寬度 or 蟲長度[頭]['y'] == -1 or 蟲長度[頭]['y'] == 高度: return # game over for wormBody in 蟲長度[1:]: if wormBody['x'] == 蟲長度[頭]['x'] and wormBody['y'] == 蟲長度[頭]['y']: return # game over # check if worm has eaten an apply if 蟲長度[頭]['x'] == 食物['x'] and 蟲長度[頭]['y'] == 食物['y']: # don't remove worm's tail segment 食物 = 食物位置() # set a new 食物 somewhere else: del 蟲長度[-1] # remove worm's tail segment elif event.type == KEYDOWN: if (event.key == K_LEFT or event.key == K_a) and 方向 != 大右: 方向 = 大左 elif (event.key == K_RIGHT or event.key == K_d) and 方向 != 大左: 方向 = 大右 elif (event.key == K_UP or event.key == K_w) and 方向 != 大下: 方向 = 大上 elif (event.key == K_DOWN or event.key == K_s) and 方向 != 大上: 方向 = 大下 elif event.key == K_ESCAPE: 停止()
  6. # move the worm by adding a segment in the

    方向 it is moving if 方向 == 大上: newHead = {'x': 蟲長度[頭]['x'], 'y': 蟲長度[頭]['y'] - 1} elif 方向 == 大下: newHead = {'x': 蟲長度[頭]['x'], 'y': 蟲長度[頭]['y'] + 1} elif 方向 == 大左: newHead = {'x': 蟲長度[頭]['x'] - 1, 'y': 蟲長度[頭]['y']} elif 方向 == 大右: newHead = {'x': 蟲長度[頭]['x'] + 1, 'y': 蟲長度[頭]['y']} 蟲長度.insert(0, newHead) 顯示.fill(色彩) 顯示格子() 蟲(蟲長度) 顯示食物(食物) 顯示分數(len(蟲長度) - 3) pygame.display.update() 更新時間.tick(更新) def 壓縮畫面(): 集合事件 = 基礎.render('Press a key to play.', True, 暗灰色) 接收事件 = 集合事件.get_rect() 接收事件.topleft = (寬 - 200, 高 - 30) 顯示.blit(集合事件, 接收事件)
  7. def 檢查(): if len(pygame.event.get(QUIT)) > 0: 停止() keyUpEvents = pygame.event.get(KEYUP)

    if len(keyUpEvents) == 0: return None if keyUpEvents[0].key == K_ESCAPE: 停止() return keyUpEvents[0].key def 開始幕(): 標題 = pygame.font.Font('freesansbold.ttf', 100) 標題破碎1 = 標題.render('Wormy!', True, 白色的, 暗綠色) 標題破碎2 = 標題.render('Wormy!', True, 綠色的) 速度1 = 0 速度2 = 0
  8. def 停止(): pygame.quit() sys.exit() def 食物位置(): return {'x': random.randint(0, 寬度

    - 1), 'y': random.randint(0, 高度 - 1)} def 新遊戲(): 結束遊戲 = pygame.font.Font('freesansbold.ttf', 150) 遊戲破碎 = 結束遊戲.render('Game', True, 白色的) 撞擊結束 = 結束遊戲.render('Over', True, 白色的) 接收遊戲 = 遊戲破碎.get_rect() 關閉遊戲 = 撞擊結束.get_rect() 接收遊戲.midtop = (寬 / 2, 10) 關閉遊戲.midtop = (寬 / 2, 接收遊戲.height + 10 + 25) 顯示.blit(遊戲破碎, 接收遊戲) 顯示.blit(撞擊結束, 關閉遊戲) 壓縮畫面() pygame.display.update() pygame.time.wait(500) 檢查() # clear out any key presses in the event queue while True: if 檢查(): pygame.event.get() # clear event queue return
  9.  def 顯示分數(分數): 分數消失 = 基礎.render('分數: %s' % (分數), True,

    白色的) 加分 = 分數消失.get_rect() 加分.topleft = (寬 - 120, 10) 顯示.blit(分數消失, 加分)  def 蟲(蟲長度): for 調動 in 蟲長度: x = 調動['x'] * 尺寸 y = 調動['y'] * 尺寸 成長 = pygame.Rect(x, y, 尺寸, 尺寸) pygame.draw.rect(顯示, 暗綠色, 成長) 真正成長 = pygame.Rect(x + 4, y + 4, 尺寸 - 8, 尺寸 - 8) pygame.draw.rect(顯示, 綠色的, 真正成長)
  10. DEF 顯示食物(調動): X = 調動['X'] * 尺寸 Y = 調動['Y']

    * 尺寸 接收食物 = PYGAME.RECT(X, Y, 尺寸, 尺寸) PYGAME.DRAW.RECT(顯示, 紅色的, 接收食物) DEF 顯示格子(): FOR X IN RANGE(0, 寬, 尺寸): # DRAW VERTICAL LINES PYGAME.DRAW.LINE(顯示, 暗灰色, (X, 0), (X, 高)) FOR Y IN RANGE(0, 高, 尺寸): # DRAW HORIZONTAL LINES PYGAME.DRAW.LINE(顯示, 暗灰色, (0, Y), (寬, Y)) IF __NAME__ == '__MAIN__': MAIN()