奔马小游戏

from Walimaker import *
setup(1024, 512)
bgmusic('./snd/绿色回忆.mp3')
bg_far1 = Character('./Background/scroll_bg_far.png')
bg_far2 = Character('./Background/scroll_bg_far.png')
bg_far2.x = 2048
hills1 = Character('./Background/hills-scroll.png')
hills1.y = -50
hills2 = Character('./Background/hills-scroll.png')
hills2.y = -50
hills2.x = 2048
bg_grass1 = Character('./Background/bg_grass.png')
bg_grass2 = Character('./Background/bg_grass.png')
bg_grass2.x = 2048
horse_run_img = ['./Sprite/Horse/horse-run-00.png',
                 './Sprite/Horse/horse-run-01.png',
                 './Sprite/Horse/horse-run-02.png',
                 './Sprite/Horse/horse-run-03.png',
                 './Sprite/Horse/horse-run-04.png',
                 './Sprite/Horse/horse-run-05.png',
                 './Sprite/Horse/horse-run-06.png']
horse_jump_img = ['./Sprite/Horse/horse-jump-00.png',
                  './Sprite/Horse/horse-jump-01.png',
                  './Sprite/Horse/horse-jump-02.png',
                  './Sprite/Horse/horse-jump-03.png',
                  './Sprite/Horse/horse-jump-04.png',
                  './Sprite/Horse/horse-jump-05.png',
                  './Sprite/Horse/horse-jump-06.png']
horse_img = {'run':horse_run_img,
             'jump':horse_jump_img}
horse = Character(horse_img)
horse.state = 'run' # state 状态
horse.play_anim() # play播放 animation动画
# horse.dt = 0.08 # time时间 delta极小的
horse.set_dt('run', 0.05) # set 设置
horse.set_dt('jump', 0.15)
# horse.set_next_state('jump', 'run')
horse.goto(-400, -100)
horse.vy = 0
horse.ay = -0.9

# 金币
# spritesheet sprite动画精灵 sheet清单
coin_img = SpriteSheet('./Sprite/coin_gold.png', 8, 1)
coin1 = Character(coin_img)
coin1.scale(3)
coin1.play_anim()
coin1.x = 1500
coin1.y = -100
coin2 = Character(coin_img)
coin2.scale(3)
coin2.play_anim()
coin2.x = 3000

# 蝙蝠
bat_img = SpriteSheet('./Sprite/bat.png', 4, 4)
bat1 = Character(bat_img[13:])
bat1.play_anim()
bat1.scale(2.5)
bat1.x = 1800
bat1.y = -120
bat2 = Character(bat_img[13:])
bat2.play_anim()
bat2.scale(2.5)
bat2.x = 3800
v = 5

text = TextBox(100)
text.print('分数:0')
text.goto(0, 150)
score = 0 # 分数
while True:
    v += 0.3
    bg_far1.backward(v)
    bg_far2.backward(v)
    hills1.backward(v * 2)
    hills2.backward(v * 2)
    bg_grass1.backward(v * 3)
    bg_grass2.backward(v * 3)
    if bg_far1.x < -2048:
        bg_far1.x = bg_far2.x + 2048
    if bg_far2.x < -2048:
        bg_far2.x = bg_far1.x + 2048
    if hills1.x < -2048:
        hills1.x = hills2.x + 2048
    if hills2.x < -2048:
        hills2.x = hills1.x + 2048
    if bg_grass1.x < -2048:
        bg_grass1.x = bg_grass2.x + 2048
    if bg_grass2.x < -2048:
        bg_grass2.x = bg_grass1.x + 2048
    # 马的运动
    horse.vy += horse.ay
    if horse.collide(bg_grass1) or horse.collide(bg_grass2):
        horse.state = 'run'
        horse.vy = max(0, horse.vy) # max 最大  
        if key_pressed(K_SPACE):
            horse.state = 'jump'
            horse.vy = 15  
    horse.y += horse.vy


    # 金币
    coin1.backward(v * 2)
    coin2.backward(v * 2)
    if coin1.x < -1500:
        coin1.x = 1500
    if coin2.x < -1500:
        coin2.x = 1500
    if coin1.collide(horse):
        coin1.play_snd('./snd/金币.wav')
        coin1.x = 1500
        score += 1
        text.print(f'分数:{score}')
    if coin2.collide(horse):
        coin2.play_snd('./snd/金币.wav')
        coin2.x = 1500
        score += 1
        text.print(f'分数:{score}')

    # 蝙蝠
    bat1.backward(v * 3)
    bat2.backward(v * 3)
    if bat1.x < -1500:
        bat1.x = 1500
    if bat2.x < -1500:
        bat2.x = 1500
    if horse.collide(bat1) or horse.collide(bat2):
        text.print("Game Over")
        while True:
            if key_pressed(K_p):
                bat1.x = 1800
                bat2.x = 3800
                score = 0
                text.print(f'分数:{score}')              
                break
            update()
    update()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值