python小游戏——躲避球(可当课设)

游戏简介:

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

未来展望:

  1. 添加其他道具球

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

  3. 添加耐力条

  4. 添加更多属性

核心代码

玩家移动

python 复制代码
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

生成小怪

python 复制代码
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

道具球处理

python 复制代码
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

游戏主要逻辑

python 复制代码
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

相关推荐
小_太_阳17 分钟前
Scala_【2】变量和数据类型
开发语言·后端·scala·intellij-idea
直裾20 分钟前
scala借阅图书保存记录(三)
开发语言·后端·scala
老刘莱国瑞25 分钟前
STM32 与 AS608 指纹模块的调试与应用
python·物联网·阿里云
唐 城41 分钟前
curl 放弃对 Hyper Rust HTTP 后端的支持
开发语言·http·rust
一只敲代码的猪1 小时前
Llama 3 模型系列解析(一)
大数据·python·llama
Hello_WOAIAI2 小时前
批量将 Word 文件转换为 HTML:Python 实现指南
python·html·word
winfredzhang2 小时前
使用Python开发PPT图片提取与九宫格合并工具
python·powerpoint·提取·九宫格·照片
矩阵推荐官hy147622 小时前
短视频矩阵系统种类繁多,应该如何对比选择?
人工智能·python·矩阵·流量运营
测试19982 小时前
外包干了2年,技术退步明显....
自动化测试·软件测试·python·功能测试·测试工具·面试·职场和发展
码银2 小时前
【python】银行客户流失预测预处理部分,独热编码·标签编码·数据离散化处理·数据筛选·数据分割
开发语言·python