国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現簡單flappy bird

2020-02-16 00:18:24
字體:
來源:轉載
供稿:網友

本文實例為大家分享了python實現flappy bird的簡單代碼,供大家參考,具體內容如下

import pygamefrom pygame.locals import *from sys import exitimport random # 屏幕寬度SCREENWIDTH = 288# 屏幕高度SCREENHEIGHT = 512IMAGES = {}# 背景圖片地址BACKGROUND_PATH = 'back_ground.png'PIPE_PATH = 'pipe.png'BASE_PATH = 'base.png'PLAYER_PATH = (  'bird2_0.png',  'bird2_1.png',  'bird2_2.png',)# 初始化pygame.init()# 創建窗口SCREEN = pygame.display.set_mode((SCREENHEIGHT, SCREENHEIGHT))# 設置窗口標題pygame.display.set_caption("Flappy Bird") # 加載圖片,透明用convert_alpha,不透明用convertIMAGES['background'] = pygame.image.load(BACKGROUND_PATH).convert()IMAGES['base'] = pygame.image.load(BASE_PATH).convert_alpha()IMAGES['bird'] = (  pygame.image.load(PLAYER_PATH[0]).convert_alpha(),  pygame.image.load(PLAYER_PATH[1]).convert_alpha(),  pygame.image.load(PLAYER_PATH[2]).convert_alpha(),)IMAGES['pipe'] = (  pygame.transform.rotate(pygame.image.load(PIPE_PATH).convert_alpha(), 180),  pygame.image.load(PIPE_PATH).convert_alpha() )BASEY = SCREENHEIGHT * 0.82 # 設置幀率FPS = 30FPSCLOCK = pygame.time.Clock() PIPE_WIDTH = IMAGES['pipe'][0].get_width()PIPE_HEIGHT = IMAGES['pipe'][0].get_height()PLAYER_WIDTH = IMAGES['bird'][0].get_width()PLAYER_HEIGHT = IMAGES['bird'][0].get_height()  PIPEGAPSIZE = 100 # 兩個水管之間的距離x = SCREENWIDTH//2y = SCREENHEIGHT//2move_x = 0move_y = 0 flap = 0 # 小鳥初始狀態pipeVelX = -4 # 管道x方向的速度playerVelY = 0 # 小鳥y方向的初速度playerMaxVelY = 10 # 小鳥y方向的最大速度playerMinVelY = -8 # 小鳥y方向的最小速度playerAccY = 2 # 小鳥y方向的下降加速度playerFlapAcc = -3 # 小鳥y方向的上升加速度playerFLapped = False # 當小鳥飛的時候為真playery = int((SCREENHEIGHT - PLAYER_HEIGHT)/2)  # 隨機移動柱子def getRandomPipe():  # 兩個水管之間的距離有如下變量  gapYs = [20, 30, 40, 50, 60, 70, 80, 90]  index = random.randint(0, len(gapYs) - 1)  gapY = gapYs[index]   gapY += int(BASEY * 0.2)  # 水管x坐標  pipeX = SCREENWIDTH + 10   return [    {'x': pipeX, 'y': gapY - PIPE_HEIGHT},  # 上面水管的左上角位置    {'x': pipeX, 'y': gapY + PIPEGAPSIZE},  # 下面水管的左上角位置  ]    newPipel = getRandomPipe() upperPipes = [  {'x': SCREENWIDTH, 'y':newPipel[0]['y']}]lowerPipes = [  {'x': SCREENWIDTH, 'y':newPipel[1]['y']}] while True:   for event in pygame.event.get():    if event.type == QUIT:      exit()    elif event.type == KEYDOWN:      if event.key == K_LEFT:        move_x = -3      elif event.key == K_RIGHT:        move_x = 3      elif event.key == K_UP:        move_y = -3      elif event.key == K_DOWN:        move_y = 3    elif event.type == KEYUP:      move_x = 0      move_y = 0   x = x + move_x  y = y + move_y   # 防止沖出邊界  if x > SCREENWIDTH:    x = 0  elif x < 0:    x = SCREENWIDTH   if y > SCREENHEIGHT:    y = 0  elif y < 0:    y = SCREENHEIGHT   # 貼圖在左上角  SCREEN.blit(IMAGES['background'], (0, 0)) # 背景  # 顯示水管  for uPipe, lPipe in zip(upperPipes, lowerPipes):    SCREEN.blit(IMAGES['pipe'][0], (uPipe['x'], uPipe['y']))    SCREEN.blit(IMAGES['pipe'][1], (lPipe['x'], lPipe['y']))     # 放小鳥  SCREEN.blit(IMAGES['bird'][flap], (x, y))  flap = flap + 1   if flap % 3 == 0:    flap = 0    for uPipe, lPipe in zip(upperPipes, lowerPipes):    uPipe['x'] += pipeVelX    lPipe['x'] += pipeVelX    # 當水管移動到某一位置的時候,生成新的水管   if 0 < upperPipes[0]['x'] < 5:    newPipe = getRandomPipe()    upperPipes.append(newPipe[0])    lowerPipes.append(newPipe[1])     # 如果水管從右往左移動到邊緣,則摧毀水管  if upperPipes[0]['x'] < -PIPE_WIDTH:    # 隊列頭出隊    upperPipes.pop(0)    lowerPipes.pop(0)    # 刷新畫面  pygame.display.update()  FPSCLOCK.tick(FPS)            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台东市| 冷水江市| 涞水县| 桦川县| 游戏| 南充市| 内黄县| 苏尼特左旗| 旬阳县| 瓮安县| 霸州市| 辽中县| 安陆市| 定边县| 贵溪市| 将乐县| 宁安市| 荃湾区| 拉孜县| 襄城县| 石河子市| 南溪县| 卫辉市| 岑溪市| 寿阳县| 蓬安县| 开封县| 天水市| 商洛市| 通渭县| 呼图壁县| 航空| 罗甸县| 南漳县| 平顶山市| 启东市| 台安县| 逊克县| 宣城市| 毕节市| 神池县|