Python Pygame 实现宝可梦对战场面

宝可梦游戏是小时候最喜欢的掌机游戏之一,印象最深刻的是宝可梦(黄)、宝可梦(金、银)还有红蓝宝石。游戏的设计很出色,画面精致,可以说是是掌机时代的代表了。

本篇文章使用Pygame实现宝可梦的战斗场景,重温经典。

Python Pygame 实现宝可梦对战场面

目录

一、游戏效果展示

二、使用Pygame画矩形

三、制作矩形移动动画

四、使用Pygame显示游戏文本

五、使用Pygame加载图片

六、完整源码

一、游戏效果展示

Python Pygame 实现宝可梦对战场面

二、使用Pygame画矩形

战斗画面需要用到三个矩形,分别用于显示战斗选择面板、敌方精灵状态面板(等级、血量)、我方精灵状态面板(等级、血量)。我们可以先使用 Pygame_的 pygame.Rect(260,220,220,95)创建一个矩形,并设置矩形的top,left,长,款四个参数。然后使用 pygame.draw.rect(_display_surf,BLACK,_select_rect,1)_显示在画板中。

画矩形的代码如下:

import pygame, sys
from pygame.locals import *

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
FPS = 30
_fps_Clock = pygame.time.Clock()

def pygame_run():
    # 初始化游戏
    pygame.init()
    # 设置游戏画面长、宽
    _display_surf = pygame.display.set_mode((480, 320))
    pygame.display.set_caption('py梦')
    # 对战面板,显示设置
    _select_rect = pygame.Rect(260, 220, 220, 95)
    while True:
        # 填充背景为白色
        _display_surf.fill(WHITE)
        # 画矩形
        pygame.draw.rect(_display_surf, BLACK, _select_rect, 1)
        # 退出游戏条件
        for _event in pygame.event.get():
            if _event.type == QUIT:
                pygame.quit()
                sys.exit()
        pygame.display.update()
        # 让游戏按FPS帧率运行,FPS默认为30帧
        _fps_Clock.tick(FPS)

if __name__ == '__main__':
    pygame_run()

效果如下:

Python Pygame 实现宝可梦对战场面

三、制作矩形移动动画

好了,矩形画出来了,但矩形现在还是静止的,怎么才能动起来了?这就需要在Pygame的循环当中不断的改变矩形的位置,也就是矩形对象的 _top,left_值。

不断改变矩形位置的代码如下,其中 pygame.Rect(260,220,220,95)_改成 pygame.Rect(480_

220,220,95)_让矩形一开始处于右侧画面之外,接着在循环中加入 if _select_rect.left != 260判断语句,当向左移动至left=260像素处跳出循环停止移动。还没到260就执行 _select_rect.left =_

__select_rect.left – 5_每帧向左移动5个像素点。

对战面板,显示设置
_select_rect = pygame.Rect(480, 220, 220, 95)
while True:
    # 填充背景为白色
    _display_surf.fill(WHITE)
    # 画矩形
    pygame.draw.rect(_display_surf, BLACK, _select_rect, 1)
    #控制矩形向左移动
    if _select_rect.left != 260:
        _select_rect.left = _select_rect.left - 5

效果如下:

Python Pygame 实现宝可梦对战场面

四、使用Pygame显示游戏文本

有了矩形,我们还需要在矩形上面显示文字,比如选择面板就需要显示战斗、背包、精灵、逃跑四组文件。这里使用Pygame的font类,设置字体类型、字体大小、字体显示位置,最后显示在面板中。

Pygame显示文本的代码如下,其中

import pygame, sys
from pygame.locals import *

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

战斗,文字显示设置
_font_type = pygame.font.match_font('Microsoft YaHei')
_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 = 260
_select_font_1_rect.top = 220
while True:
    # 填充背景为白色
    _display_surf.fill(WHITE)
    #
    _display_surf.blit(_select_font_1_surf_obj, _select_font_1_rect)
    #
    pygame.display.update()

效果如下:

Python Pygame 实现宝可梦对战场面

五、使用Pygame加载图片

选择面板和状态面板效果做好了,现在就差精灵的贴图了,战斗画面有两个地方需要图片,确定好位置和图片大小后我们就可以使用Pygame的…加载图片,显示图片

Pygame加载图片、显示图片的代码如下

import pygame, sys
from pygame.locals import *

WHITE = (255, 255, 255)

敌方精灵贴图显示设置
_ord_pym_img = pygame.image.load('dog1.png')
_ord_pym_img_top = 20
_ord_pym_img_left = 320

while True:
    # 填充背景为白色
    _display_surf.fill(WHITE)
    #
    _display_surf.blit(_ord_pym_img, (_ord_pym_img_left, _ord_pym_img_top))
    #
    pygame.display.update()

效果如下:

Python Pygame 实现宝可梦对战场面

六、完整源码

好了最后补上血条和动画就OK了,下一篇尝试制作攻击动画,有兴趣的可以一起研究一下。

源码下载↓↓↓

PythonPygame实现宝可梦对战场面-Python文档类资源-CSDN下载宝可梦游戏是小时候最喜欢的掌机游戏之一,印象最深刻的是宝可梦(黄)、宝可梦(金、银)还有红蓝宝石。游更多下载资源、学习资料请访问CSDN下载频道.Python Pygame 实现宝可梦对战场面https://download.csdn.net/download/qq616491978/86404819 ;

import pygame, sys
from pygame.locals import *

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
FPS = 1
_fps_Clock = pygame.time.Clock()

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()

Original: https://blog.csdn.net/qq616491978/article/details/126403406
Author: 清远小阮
Title: Python Pygame 实现宝可梦对战场面

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/779698/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球