【二】巧用python编写打砖块游戏

一、安装模块

学习使用pygame模块,实现模块相应操作。

安装相应模块。

        在windows运行窗口输入cmd后回车(或者在pycharm的终端)输入:pip install pygame进行安装即可使用。

二、编写代码(代码已测试通过)

import pygame

pygame.init()
pygame.mixer.init()
sn_width = 600
sn_height = 600

sn = pygame.display.set_mode((sn_width, sn_height))
pygame.display.set_caption("打砖块")
#定义字体
font = pygame.font.SysFont('华文楷体', 30)

#定义颜色
bg = pygame.Color(pygame.Color("darkslategray"))

a = 10
#砖块的颜色
block_red = (242, 85, 96)
block_green = (86, 174, 87)
block_blue = (69, 153, 236)
block_black = (255,255,255)
#拍的颜色
paddle_color = (245, 235, 223)
paddle_outline = (200, 200, 200)
#字体颜色
text_color = (198, 222, 141)
text_color2 = (150, 200, 140)
cols = 60
rows = 30
#定义帧
clock = pygame.time.Clock()
fps = 60
live_ball = False
game_over = 0

#绘制字体
def draw_text(text, font, text_col, x, y):
    ing = font.render(text, True, text_col)
    sn.blit(ing, (x, y))
#设定砖块类
class Wall():
    def __init__(self):
        self.width = sn_width // cols
        self.height = 10
          self.blocks = []

        for row in range(rows):
            block_row = []
            for col in range(cols):
                block_individual = []
                block_x = col * self.width
                block_y = row * self.height
                rect = pygame.Rect(block_x, block_y, self.width, self.height)
                if row < 10:
                    strength = 3
                    lx = 1
                elif row < 20:
                    strength = 2
                    lx = 1
                elif row < 29:
                    strength = 1
                    lx = 1
                elif row == 29:
                    strength = 10
                    lx = 2

                if -1 < row < 30:
                    if 13 <= col < 16:
                        strength = 0
                        lx = 3
                if 8 < row < 11:
                    if 13 <= col < 60:
                        strength = 0
                        lx = 3
                block_individual = [rect, strength, lx]
                block_row.append(block_individual)
            self.blocks.append(block_row)
    #设定绘制砖块
    def draw_wall(self):
        for row in self.blocks:
            for block in row:
                if block[1] == 3:
                    block_color = block_blue
                elif block[1] == 2:
                    block_color = block_green
                elif block[1] == 1:
                    block_color = block_red
                if block[1] > 3:
                    block_color = block_black
                if block[2] != 3:
                    pygame.draw.rect(sn, block_color, block[0])
                    pygame.draw.rect(sn, bg, block[0], 1)
#设定拍
class Paddle():
    def __init__(self):
        self.reset()
    def move_paddle(self):
         self.direction = 0
         key = pygame.key.get_pressed()
         if key[pygame.K_LEFT] and self.rect.left > 0:
             self.rect.x -= self.speed
             self.direction = -1
         if key[pygame.K_RIGHT] and self.rect.right < sn_width:
             self.rect.x += self.speed
             self.direction = 1

    def draw_paddle(self):
        pygame.draw.rect(sn, paddle_color, self.rect)
        pygame.draw.rect(sn, paddle_outline, self.rect, 1)

    def reset(self):
        self.height = 10
        self.width = int (sn_width / cols) * a
        self.x = int ((sn_width / 2) - (self.width / 2))
        self.y = sn_height - (self.height * 2)
        self.speed = 10
        self.direction = 0
        self.rect = pygame.Rect (self.x, self.y, self.width, self.height)

class game_ball():
    def __init__(self, x, y):
        self.reset(x, y)

    def move(self):
        #碰撞
        collision_thresh = 5
        wall_destroyed = 1
        row_count = 0
        for row in wall.blocks:
            block_count = 0
            for block in row:
                if self.rect.colliderect(block[0]) and block[2] != 3:

                    if abs(self.rect.bottom - block[0].top) < collision_thresh and self.speed_y > 0:
                        self.speed_y *= -1
                    if abs(self.rect.top - block[0].bottom) < collision_thresh and self.speed_y < 0:
                        self.speed_y *= -1
                    if abs(self.rect.left - block[0].right) < collision_thresh and self.speed_x < 0:
                        self.speed_x *= -1
                    if abs(self.rect.right - block[0].left) < collision_thresh and self.speed_x > 0:
                        self.speed_x *= -1

                    if wall.blocks[row_count][block_count][1] > 1:
                        wall.blocks[row_count][block_count][1] -= 1
                    else:
                        wall.blocks[row_count][block_count][0] = (0, 0, 0, 0)
                if wall.blocks[row_count][block_count][0] != (0, 0, 0, 0):
                    wall_destroyed = 0
                #增加块的计数
                block_count += 1
            #增加行的计数
            row_count += 1

        #判断是否打完
        if wall_destroyed == 1:
            self.game_over = 1
        #判断球体
        if self.rect.left < 0 or self.rect.right > sn_width:
            self.speed_x *= -1
        if self.rect.top < 0:
            self.speed_y *= -1
        if self.rect.bottom > sn_height:
            self.game_over = -1

        if self.rect.colliderect(player_paddle):
            #运动分析方向
            if abs(self.rect.bottom - player_paddle.rect.top) < collision_thresh and self.speed_y > 0:
                self.speed_y *= -1
                self.speed_x += player_paddle.direction
                if self.speed_x > self.speed_max:
                    self.speed_x = self.speed_max
                elif self.speed_x < 0 and self.speed_x < -self.speed_max:
                    self.speed_x = -self.speed_max
            else:
                self.speed_x *= -1



        self.rect.x += self.speed_x
        self.rect.y += self.speed_y

        return self.game_over

    def draw(self):

        pygame.draw.circle(sn, paddle_color, (self.rect.x + self.ball_rad, self.rect.y + self.ball_rad),self.ball_rad)
        pygame.draw.circle(sn, paddle_outline, (self.rect.x + self.ball_rad, self.rect.y + self.ball_rad), self.ball_rad, 2)

    def reset(self, x, y):

        self.ball_rad = 5
        self.x = x - self.ball_rad
        self.y = y
        self.rect = pygame.Rect (self.x, self.y, self.ball_rad * 2, self.ball_rad * 2)
        self.speed_x = 4
        self.speed_y = -4
        self.speed_max = 5
        self.game_over = 0


#创造墙体
wall = Wall()
wall.create_wall()
#创造拍
player_paddle = Paddle()
#创造球

ball = game_ball(player_paddle.x + (player_paddle.width // 2), player_paddle.y - player_paddle.height)

#游戏循环
run = True
while run:

    clock.tick(fps)
    sn.fill(bg)

    wall.draw_wall()
    player_paddle.draw_paddle()
    player_paddle.move_paddle()

    ball.draw()
    if live_ball:
        player_paddle.move_paddle()
        game_over = ball.move()
        if game_over != 0:
            live_ball = False

    #文字提示
    if not live_ball:
        if game_over == 0:
            draw_text("Click anywhere to start the game!", font, text_color, 150, sn_height // 2 + 100)
        elif game_over == 1:
            draw_text("Game victory", font, text_color2, 240,sn_height // 2 + 50)
            draw_text("Click anywhere to start the game!", font, text_color, 150, sn_height // 2 + 100)
        elif game_over== -1:
            draw_text ("Game over", font, text_color2, 240, sn_height // 2 + 50)
            draw_text ("Click anywhere to start the game!", font, text_color, 150, sn_height // 2 + 100)




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

        if event.type == pygame.MOUSEBUTTONDOWN and live_ball == False:
            live_ball = True
            ball.reset(player_paddle.x + (player_paddle.width // 2), player_paddle.y - player_paddle.height)
            player_paddle.reset()
            wall.create_wall()
    pygame.display.update()
pygame.quit()

以上代码已测试通过,如有问题请加qq群(808738421)申请提供技术支持。

版权所有,违者必究。

五星工作室

2024年3月30日

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值