首頁 > 軟體

Python Pygame實戰之打磚塊小遊戲

2022-02-21 13:03:56

導語

嘿!前不久剛剛給大家過一款反彈球的小遊戲嘛!

不知道大家還記得不?不記得可以看下往期的內容呢,在上一期的基礎上升級了這款打磚塊的小遊戲,介面的話也挺簡單的,經典配色原汁原味哈哈哈。

大家好,我是木木子,一個上的程式設計下的廳堂的女碼農!今天帶大家編寫一款經典的打磚塊兒小遊戲!

小科普:

打磚塊最早是由雅達利公司開發的一款獨立遊戲,也是無數人的童年記憶。

在谷歌圖片中搜尋“atari breakout”(雅利達打磚塊遊戲),搜尋結果就會變成這款遊戲。把所有磚塊都清除後,還能繼續進入下一輪挑戰。

大家也可以試試 哈哈哈~希望給大家介紹更多程式設計方各種知識!

一、準備中

1)遊戲規則:

把所有磚塊都清除後,還能繼續進入下一輪挑戰

初始化每個玩家2次機會,打完所有磚塊兒即可勝利,否則失敗遊戲不過關!

(關卡素材、背景音樂等比較少也不展示了需要的主頁原始碼基地見哈)

2)環境安裝

本文用到的環境:Python3、Pycharm、Pygame模組以及部分自帶。

環境安裝:pip install -i https://pypi.douban.com/simple/ +模組名

二、開始敲程式碼

1)組態檔

import os
 
'''遊戲介面一些數值'''
SCREENWIDTH = 640
SCREENHEIGHT = 480
BRICKWIDTH = 10
BRICKHEIGHT = 10
PADDLEWIDTH = 60
PADDLEHEIGHT = 12
BALLRADIUS = 8
'''遊戲素材路徑'''
FONTPATH = os.path.join(os.getcwd(), 'resources/font/font.TTF')
HITSOUNDPATH = os.path.join(os.getcwd(), 'resources/audios/hit.wav')
BGMPATH = os.path.join(os.getcwd(), 'resources/audios/bgm.mp3')
LEVELROOTPATH = os.path.join(os.getcwd(), 'resources/levels')
LEVELPATHS = [os.path.join(LEVELROOTPATH, '%s.level' % str(i+1)) for i in range(len(os.listdir(LEVELROOTPATH)))]
'''一些顏色'''
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
PINK = (212, 149, 174) 
PURPLE = (168, 152, 191)
YELLOW = (245, 237, 162)
BLUE  = (51, 170, 230)
AQUA = (182, 225, 225)

2)定義一些類

import random
import pygame
 
 
'''板子'''
class Paddle(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height, SCREENWIDTH, SCREENHEIGHT, **kwargs):
        pygame.sprite.Sprite.__init__(self)
        self.init_state = [x, y, width, height]
        self.rect = pygame.Rect(x, y, width, height)
        self.base_speed = 10
        self.SCREENWIDTH = SCREENWIDTH
        self.SCREENHEIGHT = SCREENHEIGHT
    '''移動板子'''
    def move(self, direction):
        if direction == 'left':
            self.rect.left = max(0, self.rect.left-self.base_speed)
        elif direction == 'right':
            self.rect.right = min(self.SCREENWIDTH, self.rect.right+self.base_speed)
        else:
            raise ValueError('Paddle.move.direction unsupport %s...' % direction)
        return True
    '''繫結到螢幕上'''
    def draw(self, screen, color):
        pygame.draw.rect(screen, color, self.rect)
        return True
    '''重置'''
    def reset(self):
        self.rect = pygame.Rect(self.init_state[0], self.init_state[1], self.init_state[2], self.init_state[3])
        return True
 
 
'''球'''
class Ball(pygame.sprite.Sprite):
    def __init__(self, x, y, radius, SCREENWIDTH, SCREENHEIGHT, **kwargs):
        pygame.sprite.Sprite.__init__(self)
        self.init_state = [x, y, radius*2, radius*2]
        self.rect = pygame.Rect(x, y, radius*2, radius*2)
        self.base_speed = [5, 5]
        self.direction = [random.choice([1, -1]), -1]
        self.radius = radius
        self.SCREENWIDTH = SCREENWIDTH
        self.SCREENHEIGHT = SCREENHEIGHT
    '''移動球'''
    def move(self):
        self.rect.left += self.direction[0] * self.base_speed[0]
        self.rect.top += self.direction[1] * self.base_speed[1]
        if self.rect.left <= 0:
            self.rect.left = 0
            self.direction[0] = -self.direction[0]
        elif self.rect.right >= self.SCREENWIDTH:
            self.rect.right = self.SCREENWIDTH
            self.direction[0] = -self.direction[0]
        if self.rect.top <= 0:
            self.rect.top = 0
            self.direction[1] = -self.direction[1]
        elif self.rect.bottom >= self.SCREENHEIGHT:
            return False
        return True
    '''改變運動速度和方向(與拍相撞時)'''
    def change(self):
        self.base_speed = [random.choice([4, 5, 6]), random.choice([4, 5, 6])]
        self.direction = [random.choice([1, -1]), -1]
        return True
    '''繫結到螢幕上'''
    def draw(self, screen, color):
        pygame.draw.circle(screen, color, (self.rect.left+self.radius, self.rect.top+self.radius), self.radius)
        return True
    '''重置'''
    def reset(self):
        self.rect = pygame.Rect(self.init_state[0], self.init_state[1], self.init_state[2], self.init_state[3])
        return True
 
 
'''磚塊'''
class Brick(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height, **kwargs):
        pygame.sprite.Sprite.__init__(self)
        self.init_state = [x, y, width, height]
        self.rect = pygame.Rect(x, y, width, height)
    '''繫結到螢幕上'''
    def draw(self, screen, color):
        pygame.draw.rect(screen, color, self.rect)
        return True
    '''重置'''
    def reset(self):
        self.rect = pygame.Rect(self.init_state[0], self.init_state[1], self.init_state[2], self.init_state[3])
        return True

3)定義開始、結束介面

 '''開始介面'''
    def __startInterface(self):
        clock = pygame.time.Clock()
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                    pygame.quit()
                    sys.exit(-1)
                if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
                    return
            self.screen.fill(self.cfg.AQUA)
            text1 = 'Press <Enter> to start the game'
            text2 = 'Press <Esc> to quit the game'
            text_render1 = self.font_big.render(text1, False, self.cfg.BLUE)
            text_render2 = self.font_big.render(text2, False, self.cfg.BLUE)
            self.screen.blit(text_render1, ((self.cfg.SCREENWIDTH-text_render1.get_rect().width)//2, (self.cfg.SCREENHEIGHT-text_render1.get_rect().height)//4))
            self.screen.blit(text_render2, ((self.cfg.SCREENWIDTH-text_render2.get_rect().width)//2, (self.cfg.SCREENHEIGHT-text_render2.get_rect().height)//2))
            pygame.display.flip()
            clock.tick(30)
    '''結束介面'''
    def __endInterface(self, is_win):
        if is_win:
            text1 = 'Congratulations! You win!'
        else:
            text1 = 'Game Over! You fail!'
        text2 = 'Press <R> to restart the game'
        text3 = 'Press <Esc> to quit the game.'
        clock = pygame.time.Clock()
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                    pygame.quit()
                    sys.exit(-1)
                if event.type == pygame.KEYDOWN and event.key == pygame.K_r:
                    return
            self.screen.fill(self.cfg.AQUA)
            text_render1 = self.font_big.render(text1, False, self.cfg.BLUE)
            text_render2 = self.font_big.render(text2, False, self.cfg.BLUE)
            text_render3 = self.font_big.render(text3, False, self.cfg.BLUE)
            self.screen.blit(text_render1, ((self.cfg.SCREENWIDTH-text_render1.get_rect().width)//2, (self.cfg.SCREENHEIGHT-text_render1.get_rect().height)//4))
            self.screen.blit(text_render2, ((self.cfg.SCREENWIDTH-text_render2.get_rect().width)//2, (self.cfg.SCREENHEIGHT-text_render2.get_rect().height)//2))
            self.screen.blit(text_render3, ((self.cfg.SCREENWIDTH-text_render3.get_rect().width)//2, (self.cfg.SCREENHEIGHT-text_render2.get_rect().height)//1.5))
            pygame.display.flip()
            clock.tick(30)

4)定義遊戲

'''打磚塊遊戲'''
class breakoutClone():
    def __init__(self, cfg, **kwargs):
        pygame.init()
        pygame.display.set_caption('Breakout clone ')
        pygame.mixer.init()
        self.screen = pygame.display.set_mode((cfg.SCREENWIDTH, cfg.SCREENHEIGHT))
        self.font_small = pygame.font.Font(cfg.FONTPATH, 20)
        self.font_big = pygame.font.Font(cfg.FONTPATH, 30)
        self.hit_sound = pygame.mixer.Sound(cfg.HITSOUNDPATH)
        pygame.mixer.music.load(cfg.BGMPATH)
        pygame.mixer.music.play(-1, 0.0)
        self.cfg = cfg
    '''執行遊戲'''
    def run(self):
        while True:
            self.__startInterface()
            for idx, levelpath in enumerate(self.cfg.LEVELPATHS):
                state = self.__runLevel(levelpath)
                if idx == len(self.cfg.LEVELPATHS)-1:
                    break
                if state == 'win':
                    self.__nextLevel()
                else:
                    break
            if state == 'fail':
                self.__endInterface(False)
            else:
                self.__endInterface(True)
    '''執行某關卡'''
    def __runLevel(self, levelpath):
        score = 0
        num_lives = 2
        # running: 遊戲正在進行, fail: 遊戲失敗, win: 遊戲成功.
        state = 'running'
        paddle = Paddle((self.cfg.SCREENWIDTH-self.cfg.PADDLEWIDTH)/2, self.cfg.SCREENHEIGHT-self.cfg.PADDLEHEIGHT-10, self.cfg.PADDLEWIDTH, self.cfg.PADDLEHEIGHT, self.cfg.SCREENWIDTH, self.cfg.SCREENHEIGHT)
        ball = Ball(paddle.rect.centerx-self.cfg.BALLRADIUS, paddle.rect.top-self.cfg.BALLRADIUS*2, self.cfg.BALLRADIUS, self.cfg.SCREENWIDTH, self.cfg.SCREENHEIGHT)
        brick_sprites = pygame.sprite.Group()
        brick_positions = loadLevel(levelpath)
        for bp in brick_positions:
            brick_sprites.add(Brick(bp[0]*self.cfg.BRICKWIDTH, bp[1]*self.cfg.BRICKHEIGHT, self.cfg.BRICKWIDTH, self.cfg.BRICKHEIGHT))
        clock = pygame.time.Clock()
        while True:
            if state != 'running':
                return state
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit(-1)
            keys_pressed = pygame.key.get_pressed()
            if keys_pressed[pygame.K_LEFT]:
                paddle.move('left')
            elif keys_pressed[pygame.K_RIGHT]:
                paddle.move('right')
            self.screen.fill(self.cfg.AQUA)
            is_alive = ball.move()
            # 判斷有沒有接住球
            if not is_alive:
                ball.reset()
                paddle.reset()
                num_lives -= 1
                if num_lives == 0:
                    state = 'fail'
            # 球和磚塊碰撞檢測
            num_bricks = pygame.sprite.spritecollide(ball, brick_sprites, True)
            score += len(num_bricks)
            # 球和拍碰撞檢測
            if pygame.sprite.collide_rect(ball, paddle):
                ball.change()
            # 判斷磚塊是否已經打完
            if len(brick_sprites) == 0:
                state = 'win'
            # 將遊戲精靈繫結到螢幕
            paddle.draw(self.screen, self.cfg.PURPLE)
            ball.draw(self.screen, self.cfg.WHITE)
            for brick in brick_sprites:
                brick.draw(self.screen, self.cfg.YELLOW)
            text_render = self.font_small.render('SCORE: %s, LIVES: %s' % (score, num_lives), False, self.cfg.BLUE)
            self.screen.blit(text_render, (10, 10))
            pygame.display.flip()
            clock.tick(50)
    '''關卡切換'''
    def __nextLevel(self):
        clock = pygame.time.Clock()
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit(-1)
                if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
                    return
            self.screen.fill(self.cfg.AQUA)
            text = 'Press <Enter> to enter the next level'
            text_render = self.font_big.render(text, False, self.cfg.BLUE)
            self.screen.blit(text_render, ((self.cfg.SCREENWIDTH-text_render.get_rect().width)//2, (self.cfg.SCREENHEIGHT-text_render.get_rect().height)//3))
            pygame.display.flip()
            clock.tick(30)

5)主函數與執行介面

import cfg
from modules import breakoutClone
 
'''主函數'''
def main():
    game = breakoutClone(cfg)
    game.run()
 
'''run'''
if __name__ == '__main__':
    main()

三、效果展示

1)視訊效果展示——

視訊連結

帶你回憶經典:原生Python開發一款打磚塊兒小遊戲~

2)截圖效果展示——

遊戲開始介面——

​執行介面——

以上就是Python Pygame實戰之打磚塊小遊戲的詳細內容,更多關於Python Pygame打磚塊遊戲的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com