1. 程式人生 > >pygame 筆記-4 程式碼封裝&發射子彈

pygame 筆記-4 程式碼封裝&發射子彈

繼續之前的內容,隨著遊戲的內容越來越複雜,有必要把程式碼優化一下,可以參考OOP的做法,把人物類抽象出來,弄成一個單獨的類,這們便於程式碼維護,同時我們給小人兒,加個發射子彈的功能,程式碼如下:(看上去略長,但是絕大多數,都是上節的程式碼)

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

效果: