前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pygame 笔记-4 代码封装&发射子弹

pygame 笔记-4 代码封装&发射子弹

作者头像
菩提树下的杨过
发布2018-12-27 17:46:06
5390
发布2018-12-27 17:46:06
举报

继续之前的内容,随着游戏的内容越来越复杂,有必要把代码优化一下,可以参考OOP的做法,把人物类抽象出来,弄成一个单独的类,这们便于代码维护,同时我们给小人儿,加个发射子弹的功能,代码如下:(看上去略长,但是绝大多数,都是上节的代码)

代码语言:javascript
复制
import pygame
import os

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')
char = pygame.image.load(img_base_path + 'standing.png')
bullet_right = pygame.image.load(img_base_path + 'r_bullet.png')
bullet_left = pygame.image.load(img_base_path + 'l_bullet.png')

clock = pygame.time.Clock()


# 将主角人物抽象成1个类
class Player(object):
    # 向右走的图片数组
    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')]

    # 向左走的图片数组
    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 __init__(self, x, y, width, height):
        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

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

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


# 子弹类
class Bullet(object):
    global man

    def __init__(self, x, y, radius, color, facing):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.facing = facing
        self.vel = 8 * facing

    def draw(self, win):
        # 根据人物的朝向,要切换不同的子弹图片
        if man.left:
            win.blit(bullet_left, (self.x, self.y))
        else:
            win.blit(bullet_right, (self.x, self.y))


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


# main
man = Player(200, 410, 64, 64)
run = True
bullets = []
while run:
    clock.tick(27)

    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:
            # 子弹不足5个时,自动填充
            bullets.append(Bullet(round(man.x + man.width // 2), round(man.y + man.height // 2), 6, (0, 0, 0), facing))

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

效果:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-12-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档