猴小子冲关_横版过关游戏

"""
   猴小子冲关.py
"""
from sprites import *
try:
    import pygame
except:
    import sys
    print("本程序需要pygame混音器支持以便播放音效。\n请先用pip install pygame命令在cmd窗口下安装。")
    input()
    sys.exit()

title = '猴小子冲关_Python精灵模块版本'
pygame.mixer.init()
pygame.mixer.music.load('audio/Jetpack Joyride.wav')
pygame.mixer.music.play(-1,0)
dead_sound = pygame.mixer.Sound("audio/死.wav")
collect_sound = pygame.mixer.Sound("audio/collect.wav")

bgi = 0
bgs = [f'bgs/{index}.png' for index in range(12)]  # 12张背景图
width,height = 480,360
screen = Screen()
screen.setup(width,height)
screen.bgpic(bgs[bgi])
screen.title(title)

weapons = ['im/剑.png','im/匕首.png','im/戟.png','im/环.png','im/锤.png',
           'im/盾4.png','im/箭.png','im/菜刀.png','im/血滴子.png','im/cat.png']

_help = Sprite('im/title.png',visible=False)
_help.sety(120)
_help.stamp(3)                     # 3秒后此图章被清除
_help.shape('im/help.png')
_help.sety(60)
_help.stamp(5)                     # 5秒后此图章被清除


# 障碍物
obstacle = Sprite(shape=random.choice(weapons),visible=False)  # 障碍物
x = random.randint(width,width*2)
y = random.randint(-height//2,height//2)
obstacle.goto(x,y)
# obstacle不断地旋转
def auto_rotate():
    obstacle.left(30)
    screen.ontimer(auto_rotate,80)
auto_rotate()

# 金币
coins = [f"im/g{index}.png" for index in range(8)]
c_index = 0
c_amounts = len(coins)
coin = Sprite(shape=coins[0],visible=False)
coin.scale(0.5)
x = random.randint(width,width*2)
y = random.randint(-height//2,height//2)
coin.goto(x,y)
coin.show()
# coin不断地换造型
def coin_rotate():
    global c_index
    c_index += 1
    c_index %= c_amounts
    coin.shape(coins[c_index])
    screen.ontimer(coin_rotate,100)
coin_rotate()

# 猴小子
costumes = ['im/造型1.gif','im/造型2.gif']
index = 0
monkey = Sprite(shape=costumes[index],pos=(-200,0))
monkey.scale(0.5)
monkey.alive = True
monkey.right(90)
monkey.saycolor('cyan')
monkey.saybordercolor('yellow')
monkey.say("冲啊!!",wait=False)
# monkey不断地变换造型
def alt_costume():
    global index
    index = 1 - index
    monkey.shape(costumes[index])
    screen.ontimer(alt_costume,50)
alt_costume()

akey = Key('a')                     # 实例化a按键 
dkey = Key('d')                     # 实例化d按键
wkey = Key('w')                     # 实例化w按键 
skey = Key('s')                     # 实例化s按键
screen.listen()                     # 监听按键 
clock = Clock()
speed = 10                          # 背景移动速度 
distance = 0                        # 路程
counter = 0                         # 接到的金币计数器
while speed>0:    
  if monkey.alive == True:
     if akey.down(): monkey.addx(-4)   # 按左键往左移
     if dkey.down(): monkey.addx(4)    # 按右键往右移
     if wkey.down(): monkey.addy(4)    # 按上键往上移
     if skey.down(): monkey.addy(-4)   # 按下键往下移 
  else:
     monkey.move(0,-5)              # 猴死下坠
     if monkey.ycor()<-height//2-500:speed = 0
     

以下代码省略......

下载完整源代码与素材,请扫码付款。

VIP免费查看

已有0人支付

本站所有作品,教程等皆为原创,版权所有。只供个人及单位内部研究使用,对外展示或传播必需经本站同意,且注明来自本站。培训机构等用本站资源培训学生,需经本站授权。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。亦可直接向微信号scratch8付款购买。入住QQ群:225792826 和爱好者共同交流,并且能下载免费提供的Python资源(需提供真实姓名才可入群)
李兴球的博客_Python创意编程技术前沿_pygame » 猴小子冲关_横版过关游戏
scratch

学本领,探索更大的世界!

李兴球博客 风火轮编程主页