基于Python+Pygame实现变异狗大战游戏

 更新时间:2023年03月03日 10:17:09   作者:木木子学python  
只有你想不到,没有我找不到写不了的好游戏!这篇文章就来和大家分享一下如何基于Python+Pygame实现变异狗大战游戏,感兴趣的可以了解一下

前言

只有你想不到,没有我找不到写不了的好游戏!

哈喽。我是你们的栗子同学啦~

今天小编去了我朋友家里玩儿,看到了一个敲可爱的小狗狗,是我朋友养的萨摩耶啦。心里羡慕一下下蛮。嘿嘿,但是我养肯定养不了滴~养狗狗的话要花费好多时间遛一遛的啦,小编除了代码就是代码,没这么多时间的啦~

嘿嘿,虽说我养不了狗,但是我们可以拥有一款专属的狗子游戏啊~

PS——

Python代码版本的狗子大战, 你值得拥有, 哈哈哈,可以放心的玩哦~

吐槽:这华丽突出丑的出奇的出场方式以及界面,**以至于我都不知道怎么吹了~你说呢?我觉得咳咳咳......主要是学习来的哈(学习编程知识,丑一点儿没关系哈,我突然良心通了一下下)。其实你可以给你的狗狗装饰一下也不是不行,换一只也行,创造一只专属于你的最强狗子吧!(其实确实有丑的出奇,23333~我自爆了。)

一、准备环境 

1)环境安装 

本文用到的环境如下——

 Python3、Pycharm社区版,pygame其他自带的库只要安装完 Python就可以直接使用了

 一般安装:pip install +模块名 

 镜像源安装:pip install -i pypi.douban.com/simple/+模块名…

 (之前有说过安装报错的几种方式跟解决方法,不会安装的可以去看下,还有很多国内镜像源 也有文章的) 

二、代码展示

1)导入库

import pygame, sys

from pygame.locals import *

2)主程序

def pygame_run():
    pygame.init()
    _display_surf = pygame.display.set_mode((480, 320))
    pygame.display.set_caption('py梦')
    _font_type = pygame.font.match_font('Microsoft YaHei')

    # 敌方精灵状态,文字显示
    _ord_pym_rect = pygame.Rect(-260, 0, 220, 50)
    # 敌方精灵名字,文字显示设置
    _ord_pym_name = pygame.font.Font(_font_type, 16)
    _ord_pym_name_surf_obj = _ord_pym_name.render("lv10:它狗", True, BLACK, None)
    _ord_pym_name_rect = _ord_pym_name_surf_obj.get_rect()
    _ord_pym_name_rect.left = -200
    _ord_pym_name_rect.top = 0
    # 敌方精灵血量,文字显示设置
    _ord_pym_blood = pygame.font.Font(_font_type, 16)
    _ord_pym_blood_surf_obj = _ord_pym_blood.render("血量:----------[69/69]", True, BLACK, None)
    _ord_pym_blood_rect = _ord_pym_blood_surf_obj.get_rect()
    _ord_pym_blood_rect.left = -200
    _ord_pym_blood_rect.top = 20
    # 敌方精灵贴图显示设置
    _ord_pym_img = pygame.image.load('dog1.png')
    _ord_pym_img_top = 20
    _ord_pym_img_left = 320+220

    # 我方精灵状态,文字显示设置
    _my_pym_rect = pygame.Rect(260, 170, 220, 50)
    # 我方精灵名字,文字显示设置
    _my_pym_name = pygame.font.Font(_font_type, 16)
    _my_pym_name_surf_obj = _my_pym_name.render("lv18:我狗", True, BLACK, None)
    _my_pym_name_rect = _my_pym_name_surf_obj.get_rect()
    _my_pym_name_rect.left = 480
    _my_pym_name_rect.top = 170
    # 我方精灵血量,文字显示设置
    _my_pym_blood = pygame.font.Font(_font_type, 16)
    _my_pym_blood_surf_obj = _my_pym_blood.render("血量:----------[99/99]", True, BLACK, None)
    _my_pym_blood_rect = _my_pym_blood_surf_obj.get_rect()
    _my_pym_blood_rect.left = 480
    _my_pym_blood_rect.top = 190
    # 我方精灵贴图显示设置
    _my_pym_img = pygame.image.load('dog2.png')
    _my_pym_img_top = 80
    _my_pym_img_left = 20-220

    # 对战面板,显示设置
    _select_rect = pygame.Rect(480, 220, 220, 95)
    # 战斗,文字显示设置
    _select_font_1 = pygame.font.Font(_font_type, 30)
    _select_font_1_surf_obj = _select_font_1.render("战斗", True, BLACK, None)
    _select_font_1_rect = _select_font_1_surf_obj.get_rect()
    _select_font_1_rect.left = 480
    _select_font_1_rect.top = 220
    # 道具,文字显示设置
    _select_font_2 = pygame.font.Font(_font_type, 30)
    _select_font_2_surf_obj = _select_font_2.render("道具", True, BLACK, None)
    _select_font_2_rect = _select_font_2_surf_obj.get_rect()
    _select_font_2_rect.left = 580
    _select_font_2_rect.top = 220
    # 精灵,文字显示设置
    _select_font_3 = pygame.font.Font(_font_type, 30)
    _select_font_3_surf_obj = _select_font_3.render("精灵", True, BLACK, None)
    _select_font_3_rect = _select_font_3_surf_obj.get_rect()
    _select_font_3_rect.left = 480
    _select_font_3_rect.top = 270
    # 逃跑,文字显示设置
    _select_font_4 = pygame.font.Font(_font_type, 30)
    _select_font_4_surf_obj = _select_font_4.render("逃跑", True, BLACK, None)
    _select_font_4_rect = _select_font_4_surf_obj.get_rect()
    _select_font_4_rect.left = 580
    _select_font_4_rect.top = 270
    while True:
        _display_surf.fill(WHITE)
        pygame.draw.rect(_display_surf, BLACK, _select_rect, 1)
        pygame.draw.rect(_display_surf, WHITE, _my_pym_rect, 0)
        _display_surf.blit(_ord_pym_img, (_ord_pym_img_left, _ord_pym_img_top))
        _display_surf.blit(_my_pym_img, (_my_pym_img_left, _my_pym_img_top))
        _display_surf.blit(_ord_pym_name_surf_obj, _ord_pym_name_rect)
        _display_surf.blit(_ord_pym_blood_surf_obj, _ord_pym_blood_rect)
        _display_surf.blit(_my_pym_name_surf_obj, _my_pym_name_rect)
        _display_surf.blit(_my_pym_blood_surf_obj, _my_pym_blood_rect)
        _display_surf.blit(_select_font_1_surf_obj, _select_font_1_rect)
        _display_surf.blit(_select_font_2_surf_obj, _select_font_2_rect)
        _display_surf.blit(_select_font_3_surf_obj, _select_font_3_rect)
        _display_surf.blit(_select_font_4_surf_obj, _select_font_4_rect)
        if _select_rect.left != 260:
            _select_rect.left = _select_rect.left - 5
            _select_font_1_rect.left = _select_font_1_rect.left - 5
            _select_font_2_rect.left = _select_font_2_rect.left - 5
            _select_font_3_rect.left = _select_font_3_rect.left - 5
            _select_font_4_rect.left = _select_font_4_rect.left - 5
            _my_pym_name_rect.left = _my_pym_name_rect.left - 5
            _my_pym_blood_rect.left = _my_pym_blood_rect.left - 5

            _ord_pym_name_rect.left = _ord_pym_name_rect.left + 5
            _ord_pym_blood_rect.left = _ord_pym_blood_rect.left + 5

            _ord_pym_img_left = _ord_pym_img_left - 5
            _my_pym_img_left = _my_pym_img_left + 5
        for _event in pygame.event.get():
            if _event.type == QUIT:
                pygame.quit()
                sys.exit()
        pygame.display.update()
        _fps_Clock.tick(FPS)


if __name__ == '__main__':
    pygame_run()

三、效果展示

1)出场方式第一步

2)出场方式第二步

3)出场方式第三步

到此这篇关于基于Python+Pygame实现变异狗大战游戏的文章就介绍到这了,更多相关Python Pygame变异狗游戏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • pytorch之Resize()函数具体使用详解

    pytorch之Resize()函数具体使用详解

    这篇文章主要介绍了pytorch之Resize()函数具体使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02
  • 简单分析python的类变量、实例变量

    简单分析python的类变量、实例变量

    在本篇文章中小编给大家整理的是关于python类变量、实例变量的知识点内容,有需要的朋友们可以学习下。
    2019-08-08
  • python获取当前用户的主目录路径方法(推荐)

    python获取当前用户的主目录路径方法(推荐)

    下面小编就为大家带来一篇python获取当前用户的主目录路径方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • python向MySQL数据库插入数据的操作方法

    python向MySQL数据库插入数据的操作方法

    这篇文章主要介绍了python向MySQL数据库插入数据,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-11-11
  • 使用python检测网页文本内容屏幕上的坐标

    使用python检测网页文本内容屏幕上的坐标

    在 Web 开发中,经常需要对网页上的文本内容进行处理和操作,有时候,我们可能需要知道某个特定文本在屏幕上的位置,以便进行后续的操作,所以本文将介绍如何使用 Python 中的 Selenium 和 BeautifulSoup 库来检测网页文本内容在屏幕上的坐标,需要的朋友可以参考下
    2024-04-04
  • PyCharm+PyQt5+QtDesigner配置详解

    PyCharm+PyQt5+QtDesigner配置详解

    这篇文章主要介绍了PyCharm+PyQt5+QtDesigner配置详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • 详解Flask开发技巧之异常处理

    详解Flask开发技巧之异常处理

    Flask是一个微型的Python开发的Web框架,基于Werkzeug WSGI工具箱和Jinja2 模板引擎。Flask使用BSD授权。Flask也被称为“microframework”,因为它使用简单的核心,用extension增加其他功能。本文主要介绍了它的异常处理机制
    2021-06-06
  • Python Des加密解密如何实现软件注册码机器码

    Python Des加密解密如何实现软件注册码机器码

    这篇文章主要介绍了Python Des加密解密如何实现软件注册码机器码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01
  • 浅析Python中getattr和getattribute的调用

    浅析Python中getattr和getattribute的调用

    在Python中,getattr和getattribute是两个用于属性访问的重要函数,它们可以在运行时动态地获取对象的属性或自定义属性访问行为,下面我们就来学习一下它们的具体用法吧
    2023-11-11
  • python里面单双下划线的区别详解

    python里面单双下划线的区别详解

    本文主要介绍了python里面单双下划线的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04

最新评论