游戏简介:

没有美术,画面简洁(懒得做)。玩家控制小球躲避敌人(上下左右,闪避),敌人体积越大速度越慢,随机生成道具球(目前只有生命球),靠近道具球可拾取。

未来展望:

1. 添加其他道具球

2. 添加攻击手段,目前只能闪避。

3. 添加耐力条

4. 添加更多属性

核心代码

玩家移动

def player_move(space_down, player):
    """
    控制玩家移动处理
    :param space_down: 是否按下空格
    :param player: 玩家
    :return:
    """
    global monitor
    if space_down:
        speed = player.dodge_speed
    else:
        speed = player.speed

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        player.x -= speed
    if keys[pygame.K_RIGHT]:
        player.x += speed
    if keys[pygame.K_UP]:
        player.y -= speed
    if keys[pygame.K_DOWN]:
        player.y += speed

    if player.x < 0:
        player.x = monitor.width + player.x
    elif player.x > monitor.width:
        player.x = player.x - monitor.width

    if player.y < 0:
        player.y = monitor.height + player.y
    elif player.y > monitor.height:
        player.y = player.y - monitor.height

生成小怪

def make_enemy():
    """
    生成小怪
    :return:
    """
    global monitor, score
    boss = False
    if score % 20000 == 0 and score != 0:
        boss = True
    sizeList = [10, 30, 50, 70, 100, 130, 150, 180, 200, 250]
    random_int = random.randint(1, 10) if boss is False else 20
    enemy = Enemy(
        atc=random_int,
        max_health=random_int,
        defense=0,
        speed=(11 - random_int) * 1.5 if boss is False else 1.5,
        attribute=0,
        x=random.uniform(0.1, 1) * monitor.width,
        y=random.uniform(0.1, 1) * monitor.height,
        size=sizeList[random_int - 1] if boss is False else 500
    )
    return enemy

道具球处理

def propBall_handle(propBall_list, window, player):
    """
    道具球处理
    :param propBall_list:
    :param window:
    :param player:
    :return:
    """
    count = 0
    for propBall in propBall_list:
        pygame.draw.circle(window, propBall.color, (propBall.x, propBall.y), propBall.size)
        propBall.moveToPlayer(player.x, player.y)
        if detectIntersect(player, propBall):
            propBall_function(player, propBall)
            del propBall_list[count]
        count += 1
    if score % 200 == 0:
        propBall = generate_propBall()
        if propBall is not None:
            propBall_list.append(propBall)
    return propBall_list

游戏主要逻辑

def main():
    global is_running, is_playing, font, pass_time, pass_time_made, score, start_time, monitor
    propBall_list = []
    window = pygame.display.set_mode((monitor.width, monitor.height))
    pygame.display.set_caption("demo")
    player = init_player()
    health_bal = pygame.Rect(20, 20, player.health * 20, 20)
    enemyList = [make_enemy()]
    button_playAgain = pygame.Rect(monitor.width // 2 - button_width // 2, monitor.height * 0.6,
                                   button_width, button_height)
    button_quit = pygame.Rect(monitor.width // 2 - button_width // 2,
                              monitor.height * 0.6 + 30 + button_height,
                              button_width, button_height)
    buttonList = [button_playAgain, button_quit]

    while is_running:
        score += 1
        window.fill(color_dict['black'])  # 填充黑色
        health_bal.width = player.health * 20
        space_down = False
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                is_running = False
            elif event.type == KEYDOWN:
                if event.key == K_SPACE:
                    space_down = True
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()
                button_serialNum = button_clicked(buttonList, mouse_pos)
                if button_serialNum == 0:
                    is_playing = True
                    player = init_player()
                    enemyList = [make_enemy()]
                elif button_serialNum == 1:
                    is_running = False

        if is_playing:
            if pass_time_made is False:
                pass_time = 0
                start_time = time.perf_counter()
                pass_time_made = True

            if player.health == 0:
                is_playing = False

            if score % 400 == 0:
                enemyList.append(make_enemy())

            propBall_list = propBall_handle(propBall_list, window, player)
            player_move(space_down, player)  # 玩家移动
            player_twinkle(player, window)  # 玩家绘制
            draw_healthBar(window, player, health_bal)  # 血条更新
            make_enemyThreading(enemyList, window, player)  # 小怪更新
            draw_score(window)
            pass_time = int(time.perf_counter() - start_time)
        else:
            draw_scoreTitle(window)
            draw_button(buttonList, window)
            pass_time_made = False
            propBall_list = []
            enemyList = []

        pygame.display.flip()  # 刷新屏幕
        time.sleep(0.01)
    pygame.quit()

游戏画面

完整代码

有需要者自取,盘内还有打包好的exe文件

链接:https://pan.baidu.com/s/1rZ1xNZJYtvyXPIG9Rgh5Hw
提取码:iq6l

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部