首頁/ 時尚/ 正文

Python做一款超好玩的滑雪大冒險小遊戲,超!會!玩!「附原始碼」

導語

冬日當然要和心愛的人一起去滑雪,

徜徉在雪白的世界,

不但風景絕美,而且還超!會!玩!

現在還不是時候 但秋天已過半動冬天還會遠嗎?

既然不能現在去滑雪,但是小編可以先讓大家身臨其境~

帶大家做一款超好玩的滑雪小遊戲,還可以練習滑雪姿勢哦~

Python做一款超好玩的滑雪大冒險小遊戲,超!會!玩!「附原始碼」

正文

環境安裝部分(1):

Python版本:

3。6

pygame模組;

pip install pygame

以及一些Python自帶的模組。

遊戲圖片素材(2):可自行修改。

Python做一款超好玩的滑雪大冒險小遊戲,超!會!玩!「附原始碼」

Python做一款超好玩的滑雪大冒險小遊戲,超!會!玩!「附原始碼」

設定號各種音樂、字型、圖片路徑(3):

‘’‘圖片路徑’‘’SKIER_IMAGE_PATHS = [ os。path。join(os。getcwd(), ‘resources/images/skier_forward。png’), os。path。join(os。getcwd(), ‘resources/images/skier_right1。png’), os。path。join(os。getcwd(), ‘resources/images/skier_right2。png’), os。path。join(os。getcwd(), ‘resources/images/skier_left2。png’), os。path。join(os。getcwd(), ‘resources/images/skier_left1。png’), os。path。join(os。getcwd(), ‘resources/images/skier_fall。png’)]OBSTACLE_PATHS = { ‘tree’: os。path。join(os。getcwd(), ‘resources/images/tree。png’), ‘flag’: os。path。join(os。getcwd(), ‘resources/images/flag。png’)}‘’‘背景音樂路徑’‘’BGMPATH = os。path。join(os。getcwd(), ‘resources/music/bgm。mp3’)‘’‘字型路徑’‘’FONTPATH = os。path。join(os。getcwd(), ‘resources/font/FZSTK。TTF’)

玩家透過“AD”鍵或者“←→”操控前進中的滑雪者,努力避開路上的樹,儘量撿到路上的小旗。

如果碰到樹,則得分減50,如果撿到小旗子,則得分加10。

滑雪者參賽人員設定:擁有向左向右偏移等能力,如下圖:

向左:

Python做一款超好玩的滑雪大冒險小遊戲,超!會!玩!「附原始碼」

Python做一款超好玩的滑雪大冒險小遊戲,超!會!玩!「附原始碼」

向右:

Python做一款超好玩的滑雪大冒險小遊戲,超!會!玩!「附原始碼」

Python做一款超好玩的滑雪大冒險小遊戲,超!會!玩!「附原始碼」

Python做一款超好玩的滑雪大冒險小遊戲,超!會!玩!「附原始碼」

直線:

Python做一款超好玩的滑雪大冒險小遊戲,超!會!玩!「附原始碼」

Python做一款超好玩的滑雪大冒險小遊戲,超!會!玩!「附原始碼」

左偏:

Python做一款超好玩的滑雪大冒險小遊戲,超!會!玩!「附原始碼」

Python做一款超好玩的滑雪大冒險小遊戲,超!會!玩!「附原始碼」

右偏:

Python做一款超好玩的滑雪大冒險小遊戲,超!會!玩!「附原始碼」

Python做一款超好玩的滑雪大冒險小遊戲,超!會!玩!「附原始碼」

操作玩家滑雪,擁有不同方向的偏移能力,那還需要什麼嘞?當然是障礙物啦。

‘’‘建立障礙物’‘’def createObstacles(s, e, num=10): obstacles = pygame。sprite。Group() locations = [] for i in range(num): row = random。randint(s, e) col = random。randint(0, 9) location = [col*64+20, row*64+20] if location not in locations: locations。append(location) attribute = random。choice(list(cfg。OBSTACLE_PATHS。keys())) img_path = cfg。OBSTACLE_PATHS[attribute] obstacle = ObstacleClass(img_path, location, attribute) obstacles。add(obstacle) return obstacles

遊戲中剪刀路上的小旗子肯定會加分,碰到樹減分,所以設定了分數顯示:

‘’‘顯示分數’‘’ def showScore(screen, score, pos=(10, 10)): font = pygame。font。Font(cfg。FONTPATH, 30) score_text = font。render(“Score: %s” % score, True, (0, 0, 0)) screen。blit(score_text, pos)

附程式碼遊戲主程式:

‘’‘主程式’‘’ def main(): # 遊戲初始化 pygame。init() pygame。mixer。init() pygame。mixer。music。load(cfg。BGMPATH) pygame。mixer。music。set_volume(0。4) pygame。mixer。music。play(-1) # 設定螢幕 screen = pygame。display。set_mode(cfg。SCREENSIZE) pygame。display。set_caption(‘滑雪遊戲 原始碼基地:#959755565 ’) # 遊戲開始介面 ShowStartInterface(screen, cfg。SCREENSIZE) # 例項化遊戲精靈 # ——滑雪者 skier = SkierClass() # ——建立障礙物 obstacles0 = createObstacles(20, 29) obstacles1 = createObstacles(10, 19) obstaclesflag = 0 obstacles = AddObstacles(obstacles0, obstacles1) # 遊戲clock clock = pygame。time。Clock() # 記錄滑雪的距離 distance = 0 # 記錄當前的分數 score = 0 # 記錄當前的速度 speed = [0, 6] # 遊戲主迴圈 while True: # ——事件捕獲 for event in pygame。event。get(): if event。type == pygame。QUIT: pygame。quit() sys。exit() if event。type == pygame。KEYDOWN: if event。key == pygame。K_LEFT or event。key == pygame。K_a: speed = skier。turn(-1) elif event。key == pygame。K_RIGHT or event。key == pygame。K_d: speed = skier。turn(1) # ——更新當前遊戲幀的資料 skier。move() distance += speed[1] if distance >= 640 and obstaclesflag == 0: obstaclesflag = 1 obstacles0 = createObstacles(20, 29) obstacles = AddObstacles(obstacles0, obstacles1) if distance >= 1280 and obstaclesflag == 1: obstaclesflag = 0 distance -= 1280 for obstacle in obstacles0: obstacle。location[1] = obstacle。location[1] - 1280 obstacles1 = createObstacles(10, 19) obstacles = AddObstacles(obstacles0, obstacles1) for obstacle in obstacles: obstacle。move(distance) # ——碰撞檢測 hitted_obstacles = pygame。sprite。spritecollide(skier, obstacles, False) if hitted_obstacles: if hitted_obstacles[0]。attribute == “tree” and not hitted_obstacles[0]。passed: score -= 50 skier。setFall() updateFrame(screen, obstacles, skier, score) pygame。time。delay(1000) skier。setForward() speed = [0, 6] hitted_obstacles[0]。passed = True elif hitted_obstacles[0]。attribute == “flag” and not hitted_obstacles[0]。passed: score += 10 obstacles。remove(hitted_obstacles[0]) # ——更新螢幕 updateFrame(screen, obstacles, skier, score) clock。tick(cfg。FPS)

效果圖:

Python做一款超好玩的滑雪大冒險小遊戲,超!會!玩!「附原始碼」

Python做一款超好玩的滑雪大冒險小遊戲,超!會!玩!「附原始碼」

Python做一款超好玩的滑雪大冒險小遊戲,超!會!玩!「附原始碼」

總結

好啦!這款滑雪小遊戲就寫完啦,大家來一展身手叭~

原始碼基地企鵝群:#私信小編即可免費# 完整資料更多遊戲程式碼群免費分享滴!!

Python做一款超好玩的滑雪大冒險小遊戲,超!會!玩!「附原始碼」

Python做一款超好玩的滑雪大冒險小遊戲,超!會!玩!「附原始碼」

相關文章

頂部