赞
踩
冬日当然要和心爱的人一起去滑雪,
徜徉在雪白的世界,
浪漫又刺激!唯有爱和滑雪不可辜负!
不但风景绝美,而且还超!会!玩!
现在还不是时候 但秋天已过半动冬天还会远吗?
既然不能现在去滑雪,但是小编可以先让大家身临其境~
带大家做一款超好玩的滑雪小游戏,还可以练习滑雪姿势哦~
环境安装部分(1):
Python版本:3.6
pygame模块;
pip install pygame
以及一些Python自带的模块。
游戏图片素材(2):可自行修改。
设置号各种音乐、字体、图片路径(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。
滑雪者参赛人员设置:拥有向左向右偏移等能力,如下图:
向左: 向右:直线:左偏:右偏:
操作玩家滑雪,拥有不同方向的偏移能力,那还需要什么嘞?当然是障碍物啦。
'''创建障碍物''' 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)
游戏效果图:
好啦!这款滑雪小游戏就写完啦,大家来一展身手叭~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。