猫史档案馆


【新作(改模板)】

用户:CHN_流星CHN_流星查看:0 回复:1 评论:0 创建时间:2021-03-30T19:28:43


import pgzrun
from MyQR import myqr
from random import randint as ra
from time import sleep as sl
from math import floor as fl


WIDTH = 1280
HEIGHT = 720
ninja = [Actor('lucas_r', (200, 540)),
         Actor('roger_l', (1080, 540))]
dart = [Actor('dart', (-50, 100)),
        Actor('dart', (-50, 100))]
hp_bar = [Actor('hpl10', (370, 60)),
          Actor('hpr10', (910, 60))]
character = [Actor('ninja_head_l', (100, 70)),
             Actor('ninja_head_r', (1180, 70))]
heart = Actor('heart', (喵0, -2000))
mp_bar = [Actor('mpl3', (260, 90)),
          Actor('mpr3', (1020, 90))]
mp_plus = Actor('mp_plus', (ra(100, 1000), -2000))
sword_l = Actor('sword_red',(-50,100))
sword_r = Actor('sword_blue', (-50, 100))
sword = Actor('sword',(ra(100,1000),2000))


jump_speed = [10, 10]
dart_speed = [18, -18]
jump = [False, False]
shoot = [False, False]
init_pos = [(100, 100), (100, 100)]
hp = [10, 10]
game_state = 'start'
direction = ['R', 'L']
mp = [12, 12]
sk = [0,0]#技能
result = 0


def draw():
    global game_state, hp, mp
    if game_state == 'start':
        screen.blit('background', (0, 0))
        screen.blit('cover', (320, 210))
        if keyboard.space:
            game_state = 'play'
    elif game_state == 'play':
        screen.blit('background', (0, 0))
        for i in range(2):
            ninja[i].draw()
            dart[i].draw()
            hp_bar[i].draw()
            character[i].draw()
            mp_bar[i].draw()
        heart.draw()
        mp_plus.draw()
        sword.draw()
    else:
        screen.blit('background', (0, 0))
        for i in range(2):
            ninja[i].draw()
        screen.draw.text('GAME OVER!', (495, 270),
                         color='red', fontsize=70)
        winner = 'p1'
        if hp[1] > hp[0]:
            winner = 'p2'
        screen.draw.text(winner + 'wins',
                         (590, 340), fontsize=50)
        screen.draw.text('press SPACE to restart',
                         (520, 390), fontsize=36)
        screen.draw.text('press A to show you your result qrcode',
                         (450, 440), fontsize=36)
        if keyboard.a:
            if hp[1] > hp[0]:
                result = 'player 2 wins'
            elif hp[1] == hp[0]:
                result = 'it is a tie'
            else:
                result = 'player 1 wins'
            ver, level, qr_name = myqr.run(words=result)
            print(ver, level, qr_name)
            print('已将二维码保存至本.py文件的同一个目录下')

        if keyboard.space:
            game_state = 'play'
            hp = [10, 10]
            hp_bar[0].image = 'hpl10'
            hp_bar[1].image = 'hpr10'
            mp = [12, 12]
            mp_bar[0].image = 'mpl3'
            mp_bar[1].image = 'mpr3'


def attack(index):
    global shoot, init_pos, dart, direction, dart_speed
    init_pos[index] = ninja[index].x, ninja[index].y + 10
    dart[index].pos = init_pos[index]
    shoot[index] = True
    if direction[index] == 'R':
        dart_speed[index] = 18
    else:
        dart_speed[index] = -18


def collision_detect(ninj, dar, index):
    global hp
    if ninj.collidepoint(dar.pos):
        if hp[index] > 0:
            hp[index] -= 1
            sounds.hit.play()
        dar.x = WIDTH + 200
        dar.y = HEIGHT - 100
        if index == 0:
            hp_bar[index].image = 'hpl' + str(int(hp[index]))
        else:
            hp_bar[index].image = 'hpr' + str(int(hp[index]))


def update():
    global game_state, jump, init_pos, jump_speed, shoot, hp, \
        dart_speed, direction
    if game_state == 'play':
        if keyboard.d:
            ninja[0].x += 6
            ninja[0].image = 'lucas_r'
            direction[0] = 'R'
        elif keyboard.a:
            ninja[0].x -= 6
            ninja[0].image = 'lucas_l'
            direction[0] = 'L'
        if keyboard.right:
            ninja[1].x += 6
            ninja[1].image = 'roger_r'
            direction[1] = 'R'
        elif keyboard.left:
            ninja[1].x -= 6
            ninja[1].image = 'roger_l'
            direction[1] = 'L'

        if keyboard.w:
            jump[0] = True
        if keyboard.up:
            jump[1] = True

        if keyboard.v:
            dart[0].image = 'dart'
            attack(0)
        if keyboard.m:
            dart[1].image = 'dart'
            attack(1)

        if keyboard.r:
            if mp[0] >= 12:
                if sk[0] == 0:
                    dart[0].image = 'big_dart'
                    mp[0] = 0
                    mp_bar[0].image = 'mpl0'
                    attack(0)
                elif sk[0] == 1:
                    dart[0].image = 'sword_red'
                    mp[0] = 0
                    mp_bar[0].image = 'mpl0'
                    attack(0)

        if keyboard.j:
            if mp[1] >= 12:
                if sk[1] == 0:
                    dart[1].image = 'big_dart'
                    mp[1] = 0
                    mp_bar[1].image = 'mpl0'
                    attack(1)
                elif sk[1] == 1:
                    dart[1].image = 'sword_blue'
                    mp[1] = 0
                    mp_bar[1].image = 'mpl0'
                    attack(1)

        for i in range(2):
            if jump[i]:
                if ninja[i].y <= 540:
                    ninja[i].y -= jump_speed[i]
                    jump_speed[i] -= 0.3
                else:
                    ninja[i].y = 540
                    jump[i] = False
                    jump_speed[i] = 12
            if shoot[i]:
                if dart[i].x < WIDTH + 50 and dart[i].x > -50:
                    dart[i].x += dart_speed[i]
                    dart[i].angle += 8

            if ninja[i].x < 50:
                ninja[i].x = 50
            elif ninja[i].x > WIDTH - 50:
                ninja[i].x = WIDTH - 50

            if hp[i] == 0:
                game_state = 'game over'

        collision_detect(ninja[1], dart[0], 1)
        collision_detect(ninja[0], dart[1], 0)

        heart.y += 6
        if heart.y > HEIGHT + 50:
            heart.y = ra(-5000, -2000)

        mp_plus.y += 8
        if mp_plus.y > HEIGHT + 50:
            mp_plus.y = ra(-5000, -2000)

        sword.y += 10
        if sword.y > HEIGHT + 50:
            sword.y = ra(-5000, -2000)

        for i in range(2):
            if ninja[i].collidepoint(heart.pos):
                if hp[i] < 10:
                    hp[i] += 1
                    if i == 0:
                        hp_bar[i].image = 'hpl' + str(fl(hp[i]))
                    else:
                        hp_bar[i].image = 'hpr' + str(fl(hp[i]))
                    sounds.powerup.play()
                heart.y = ra(-5000, -2000)

            if mp[i] < 12:
                mp[i] += 0.01
                if i == 0:
                    mp_bar[i].image = 'mpl' + str(int(mp[i] // 4))
                else:
                    mp_bar[i].image = 'mpr' + str(int(mp[i] // 4))

        for i in range(2):
            if ninja[i].collidepoint(mp_plus.pos):
                if mp[i] < 12:
                    mp[i] = 12
                    if i == 0:
                        mp_bar[i].image = 'mpl' + str(fl(mp[i]) // 4)
                    else:
                        mp_bar[i].image = 'mpr' + str(fl(mp[i]) // 4)
                    sounds.powerup.play()
                mp_plus.y = ra(-5000, -2000)

        for i in range(2):
            if ninja[i].collidepoint(sword.pos):
                if sk[i] == 1:
                    sk[i] = 0
                else:
                    sk[i] = 1    
                sounds.powerup.play()
                sword.y = ra(-5000, -2000)

        if keyboard.f:
            if mp[0] >= 12:
                mp[0] = 0
                if direction[0] == 'L':
                    ninja[0].x -= 450
                else:
                    ninja[0].x += 450
        if keyboard.n:
            if mp[1] >= 12:
                mp[1] = 0
                if direction[1] == 'L':
                    ninja[1].x -= 450
                else:
                    ninja[1].x += 450

        if keyboard.q:
            if mp[0] >= 12:
                mp[0] = 0
                hp[0] += hp[0] * 0.8
                if hp[0] > 10:
                    hp[0] -= hp[0] - 10
                hp_bar[0].image = 'hpl' + str(int(fl(hp[0])))
        if keyboard.p:
            if mp[1] >= 12:
                mp[1] = 0
                hp[1] += hp[1] * 0.8
                if hp[1] > 10:
                    hp[1] -= hp[1] - 10
                hp_bar[1].image = 'hpl' + str(int(fl(hp[1])))


pgzrun.go()


回复

上一页1 页 / 共 1下一页
CHN_流星CHN_流星

要图片扣1

点赞0


评论