pygame 笔记-5 模块化&加入敌人

上一节,已经用OOP方法,把几个类抽象出来了,但是都集中在一个.py文件中,代码显得很冗长,这一节复用模块化的思想,把这个大文件拆分成几个小文件:

先把主角Player单独放到一个文件player.py里:

import pygame


# 主角
class Player(object):

    def __init__(self, x, y, width, height, img_base_path):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.speed = 5
        self.left = False
        self.right = True
        self.isJump = False
        self.walkCount = 0
        self.t = 10
        self.speed = 5
        self.char = pygame.image.load(img_base_path + 'standing.png')
        # 向右走的图片数组
        self.walkRight = [pygame.image.load(img_base_path + 'actor/R1.png'),
                          pygame.image.load(img_base_path + 'actor/R2.png'),
                          pygame.image.load(img_base_path + 'actor/R3.png'),
                          pygame.image.load(img_base_path + 'actor/R4.png'),
                          pygame.image.load(img_base_path + 'actor/R5.png'),
                          pygame.image.load(img_base_path + 'actor/R6.png'),
                          pygame.image.load(img_base_path + 'actor/R7.png'),
                          pygame.image.load(img_base_path + 'actor/R8.png'),
                          pygame.image.load(img_base_path + 'actor/R9.png')]

        # 向左走的图片数组
        self.walkLeft = [pygame.image.load(img_base_path + 'actor/L1.png'),
                         pygame.image.load(img_base_path + 'actor/L2.png'),
                         pygame.image.load(img_base_path + 'actor/L3.png'),
                         pygame.image.load(img_base_path + 'actor/L4.png'),
                         pygame.image.load(img_base_path + 'actor/L5.png'),
                         pygame.image.load(img_base_path + 'actor/L6.png'),
                         pygame.image.load(img_base_path + 'actor/L7.png'),
                         pygame.image.load(img_base_path + 'actor/L8.png'),
                         pygame.image.load(img_base_path + 'actor/L9.png')]

    def draw(self, win):
        if self.walkCount >= 9:
            self.walkCount = 0

        if self.left:
            win.blit(self.walkLeft[self.walkCount % 9], (self.x, self.y))
            self.walkCount += 1
        elif self.right:
            win.blit(self.walkRight[self.walkCount % 9], (self.x, self.y))
            self.walkCount += 1
        else:
            win.blit(self.char, (self.x, self.y))

其次是子弹类:

import pygame


# 子弹类
class Bullet(object):

    def __init__(self, x, y, direction, img_base_path):
        self.x = x
        self.y = y
        self.facing = direction
        self.vel = 8 * direction
        self.width = 24
        self.height = 6
        self.bullet_right = pygame.image.load(img_base_path + 'r_bullet.png')
        self.bullet_left = pygame.image.load(img_base_path + 'l_bullet.png')

    def draw(self, win):
        # 根据人物行进的方向,切换不同的子弹图片
        if self.direction == -1:
            win.blit(self.bullet_left, (self.x - 35, self.y))
        else:
            win.blit(self.bullet_right, (self.x + 10, self.y))

做为一个射击类的小游戏,这一节我们再加入目标敌人的类:

import pygame


class Enemy(object):

    def __init__(self, x, y, width, height, end, img_base_path):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.path = [x, end]
        self.walkCount = 0
        self.vel = 3
        self.walkRight = [pygame.image.load(img_base_path + 'enemy/R1E.png'),
                          pygame.image.load(img_base_path + 'enemy/R2E.png'),
                          pygame.image.load(img_base_path + 'enemy/R3E.png'),
                          pygame.image.load(img_base_path + 'enemy/R4E.png'),
                          pygame.image.load(img_base_path + 'enemy/R5E.png'),
                          pygame.image.load(img_base_path + 'enemy/R6E.png'),
                          pygame.image.load(img_base_path + 'enemy/R7E.png'),
                          pygame.image.load(img_base_path + 'enemy/R8E.png'),
                          pygame.image.load(img_base_path + 'enemy/R9E.png'),
                          pygame.image.load(img_base_path + 'enemy/R10E.png'),
                          pygame.image.load(img_base_path + 'enemy/R11E.png')]

        self.walkLeft = [pygame.image.load(img_base_path + 'enemy/L1E.png'),
                         pygame.image.load(img_base_path + 'enemy/L2E.png'),
                         pygame.image.load(img_base_path + 'enemy/L3E.png'),
                         pygame.image.load(img_base_path + 'enemy/L4E.png'),
                         pygame.image.load(img_base_path + 'enemy/L5E.png'),
                         pygame.image.load(img_base_path + 'enemy/L6E.png'),
                         pygame.image.load(img_base_path + 'enemy/L7E.png'),
                         pygame.image.load(img_base_path + 'enemy/L8E.png'),
                         pygame.image.load(img_base_path + 'enemy/L9E.png'),
                         pygame.image.load(img_base_path + 'enemy/L10E.png'),
                         pygame.image.load(img_base_path + 'enemy/L11E.png')]

    def draw(self, win):
        self.move()
        if self.walkCount >= 11:
            self.walkCount = 0

        if self.vel > 0:
            win.blit(self.walkRight[self.walkCount % 11], (self.x, self.y))
            self.walkCount += 1
        else:
            win.blit(self.walkLeft[self.walkCount % 11], (self.x, self.y))
            self.walkCount += 1

    def move(self):
        if self.vel > 0:
            if self.x < self.path[1] + self.vel:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.x += self.vel
                self.walkCount = 0
        else:
            if self.x > self.path[0] - self.vel:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.x += self.vel
                self.walkCount = 0

这3个.py文件放在与主文件tutorial_6.py同一个目录下,如下图:

然后在主文件tutorial_6.py里,把这3个模块导进来:

import os
# 导入3个模块
from bullet import *
from player import *
from enemy import *

pygame.init()

WIN_WIDTH, WIN_HEIGHT = 500, 500

win = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption("first game")

img_base_path = os.getcwd() + '/img/'
bg = pygame.image.load(img_base_path + 'bg.jpg')

clock = pygame.time.Clock()


def redraw_game_window():
    win.blit(bg, (0, 0))
    man.draw(win)
    goblin.draw(win)
    for b in bullets:
        b.draw(win)
    pygame.display.update()


# main
man = Player(200, 410, 64, 64, img_base_path)
goblin = Enemy(100, 410, 64, 64, 400, img_base_path)
run = True
bullets = []
while run:
    clock.tick(24)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    for bullet in bullets:
        if WIN_WIDTH > bullet.x > 0:
            bullet.x += bullet.vel
        else:
            bullets.pop(bullets.index(bullet))

    keys = pygame.key.get_pressed()

    if keys[pygame.K_SPACE]:
        if man.left:
            facing = -1
        else:
            facing = 1

        if len(bullets) < 5:
            bullets.append(Bullet(round(man.x + man.width // 2), round(man.y + man.height // 2), facing, img_base_path))

    if keys[pygame.K_LEFT] and man.x > 0:
        man.x -= man.speed
        man.left = True
        man.right = False
    elif keys[pygame.K_RIGHT] and man.x < win.get_size()[0] - man.width:
        man.x += man.speed
        man.left = False
        man.right = True
    else:
        man.walkCount = 0

    if not man.isJump:
        if keys[pygame.K_UP]:
            man.isJump = True
            man.walkCount = 0
    else:
        if man.t >= -10:
            a = 1
            if man.t < 0:
                a = -1
            man.y -= 0.5 * a * (man.t ** 2)

            man.t -= 1
        else:
            man.isJump = False
            man.t = 10

    redraw_game_window()

pygame.quit()

效果:

 

 该出场的人物与道具都齐全了,下一节将讨论"碰撞检测"。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我来回答你的问题。 在 Pygame 中实现生命值/血条处理相对来说比较简单,我们可以使用矩形和颜色填充来实现。 首先,我们需要定义一个变量来表示生命值,可以是一个整数类型的变量。接着,我们需要在游戏界面中绘制一个矩形来表示血条,可以使用 Pygame 中的 draw.rect() 函数来实现。然后,我们可以根据生命值的变来改变血条的长度,可以使用颜色填充来实现这一点。 下面是一个简单的示例代码: ```python import pygame # 初始 Pygame pygame.init() # 设置游戏界面的大小和标题 screen_width, screen_height = 600, 400 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption(&#39;Life Bar Demo&#39;) # 定义生命值 life = 100 # 定义血条的位置和大小 bar_width, bar_height = 200, 20 bar_x, bar_y = (screen_width - bar_width) // 2, screen_height // 2 # 游戏主循环 while True: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # 绘制血条的背景 pygame.draw.rect(screen, (255, 0, 0), (bar_x, bar_y, bar_width, bar_height)) # 绘制血条的前景 bar_width_current = int(bar_width * life / 100) pygame.draw.rect(screen, (0, 255, 0), (bar_x, bar_y, bar_width_current, bar_height)) # 更新屏幕 pygame.display.update() ``` 在这个示例代码中,我们定义了一个生命值变量 `life`,并且设置了血条的位置和大小。在游戏主循环中,我们使用 `pygame.draw.rect()` 函数绘制了血条的背景和前景,前景的长度根据生命值的变而改变。最后,我们使用 `pygame.display.update()` 函数更新了屏幕。 你可以根据自己的需求来修改血条的颜色、大小和位置等参数。希望这个示例代码能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值