pygame Sprite类(4)

pygame Sprite类(4)

开箱即用的打砖块游戏精灵类Demo版

# Pygame Sprite(4)
# coding: utf-8
# 作者:爱编程的章老师
# 创建:2021/1/27 2:53 下午 
# 邮箱:slxxf000@163.com
# 微信:slxxfl
# 微信公众号:A卫隆少儿编程
# 格言:给自己的生活增加一份向上的力,每都进步一点点
from random import randint

import pygame
from pygame.sprite import Sprite, Group

from sys import exit

'''运用Sprite类重写打砖块程序'''


# 砖快类
class Brick(Sprite):
    def __init__(self, left, top, width, height):
        super().__init__()
        self.rect = pygame.Rect(left, top, width, height)  # 位置矩形
        self.image = pygame.Surface((width, height))  # 画布
        pygame.draw.rect(self.image, "blue", (0, 0, width, height))  # 画到画布上


# 球类
class Ball(Sprite):
    def __init__(self, pos, radius):
        super().__init__()
        self.rect = pygame.Rect(pos, (2 * radius, 2 * radius))  # 位置矩形
        self.image = pygame.Surface((2 * radius, 2 * radius))  # 画布
        pygame.draw.circle(self.image, "red", (radius, radius), radius)  # 画到画布上

    def update(self, direct):
        DISTANCE = 5
        # 传入一个方向参数,用来决定向哪个方向移动
        # 分成四个方向:左上,右上,左下,右下
        if direct == 1:  # 左上
            self.rect.left -= DISTANCE
            self.rect.top -= DISTANCE
            return
        if direct == 2:  # 右上
            self.rect.left += DISTANCE
            self.rect.top -= DISTANCE
            return
        if direct == 3:  # 左下
            self.rect.left -= DISTANCE
            self.rect.top += DISTANCE
            return
        # 右下
        self.rect.left += DISTANCE
        self.rect.top += DISTANCE


# 挡板类
class Board(Sprite):
    def __init__(self, left, top, width, height):
        super().__init__()
        self.rect = pygame.Rect(left, top, width, height)  # 位置矩形
        self.image = pygame.Surface((width, height))  # 画布
        pygame.draw.rect(self.image, "green", (0, 0, width, height))  # 画到画布上

    # 挡版的移动
    def update(self, direct):
        # direct = 1 时往右, direct = -1 时往左, direct = 0 时不动
        self.rect.left += direct * 10


# 主程序
def main():
    pygame.init()
    screen = pygame.display.set_mode((800, 600))
    pygame.display.set_caption("打砖块程序V2.0")
    brick_width = 800 // 8 - 5
    brick_height = 15 - 5
    brick_group = Group()

    clock = pygame.time.Clock()

    for i in range(8):
        for j in range(5):
            # 往砖块组加入砖快
            brick_group.add(Brick(i * 100, j * 15, brick_width, brick_height))

    # 方向右上或者左上
    ball_direct = randint(1, 2)
    board_direct = 0
    # 第二种加入分组的方法
    # 板
    board_group = Group()
    board = Board(300, 570, 200, 20)
    board.add(board_group)
    # 球
    ball_group = Group()
    ball = Ball((400, 545), 25)
    ball.add(ball_group)

    # 主循环
    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
        screen.fill("black")
        # 画砖
        brick_group.update()
        brick_group.draw(screen)
        # 画球
        ball_group.update(ball_direct)
        ball_group.draw(screen)
        # 画板
        board_group.update(board_direct)
        board_group.draw(screen)

        # 按键检测
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                if board.rect.right < 800:
                    board_direct = 1
                else:
                    board_direct = 0
                    board.rect.right = 800
            elif event.key == pygame.K_LEFT:
                if board.rect.left > 0:
                    board_direct = -1
                else:
                    board_direct = 0
                    board.rect.left = 0
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
                board_direct = 0
        pygame.display.update()

        # 碰撞检测

        # 碰砖检测
        for brick in brick_group:
            if ball.rect.colliderect(brick.rect):
                if ball_direct == 1:
                    ball_direct = 3
                elif ball_direct == 2:
                    ball_direct = 4
                brick_group.remove(brick)
                break

        # 碰墙
        if ball.rect.left <= 0:  # 左墙
            if ball_direct == 1:
                ball_direct = 2
            elif ball_direct == 3:
                ball_direct == 4
        elif ball.rect.right >= 800:  # 右墙
            if ball_direct == 2:
                ball_direct = 1
            elif ball_direct == 4:
                ball_direct == 3
        elif ball.rect.top <= 0:  # 上墙
            if ball_direct == 1:
                ball_direct = 3
            elif ball_direct == 2:
                ball_direct = 4

        # 碰板
        if ball.rect.colliderect(board.rect):
            if ball_direct == 3:
                ball_direct = 1
            elif ball_direct == 4:
                ball_direct = 2
        
        if ball.rect.top >= 600:
            break

        # 设置帧率
        clock.tick(60)


if __name__ == "__main__":
    main()

说明

对Sprite()类作了一个简单的回顾,与增加了两个简单的函数说明

  • 精灵主动加入一个分组:sprite.add(group)
  • 一个精灵从组中删除:group.remove(sprite) sprite.remove(group)
  • 如果精灵属于一个唯一的分组,或者想从所有分组中删除自己.可以用sprite.kill()实现
    未举例应用的:碰撞消失(子弹击中对象,双双消灭)
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页