Python创意打砖块游戏埃及爆破砖

Python创意打砖块游戏埃及爆破砖

"""
   一个创意打砖块游戏,用Python的pygame模块,这个版本实现了道具的设计。
   dead死亡道具,骷髅头,skeleton.png,不小心接到它后ball.lives减一,出现机率大
   life生命道具,15.png 核桃图形,接到后ball.lives加一
   speedup加速道具,speedup.png,接到它后球的速度翻倍
   slowdown减速道具,slowdown.png,接到它后球的速度减倍。

"""

import time
import pygame
from pygame.locals import *
from random import choice,randint
from brick import *                        # 导入Brick类
from prop import *                         # 导入Prop类
from board import *                        # 导入Board类 
 
class Ball(pygame.sprite.Sprite):
    """弹球类"""
    def __init__(self,image,screen,board):
        pygame.sprite.Sprite.__init__(self)
        self.image = image
        self.rect = self.image.get_rect()
        self.screen = screen
        self.screen_width = screen.get_width()
        self.screen_height = screen.get_height()
        self.board = board           # 球能访问board
        self.dx = 0
        self.dy = 0
        self.shoot_flag = 0
        self.lives = 5                       # 球的数量
        
    def shoot(self):
        if self.shoot_flag == 0:             # 只有小球呆在拦板上才可以发射,否则在空中也能发射
           self.dx = choice([-10,-9,-8,-7,-6,-5,5,6,7,8,9,10])
           self.dy = choice([-10,-9,-8,-7,-6,-5])
           self.shoot_flag = 1
        
    def update(self):
        if self.shoot_flag == 0 :            
            self.rect.bottom =  self.board.rect.top
            self.rect.centerx = self.board.rect.centerx
        else:       
            self.rect.move_ip(self.dx,self.dy)
            if self.rect.top > self.screen_height + 200: # 弹球丢失,重新开始或结束游戏
               self.lives -= 1                         # 数量减去1
               self.shoot_flag = 0                     # 发射标志
               self.board.lost_ball()                  # 变短拦板
               pygame.display.set_caption("埃及爆破砖:" + str(self.lives))
            if self.rect.left <= 0 or self.rect.right >= self.screen_width: # 碰到左右边缘反弹
               self.dx = -self.dx
               if abs(self.dy) < 0.1 :                       # 特殊情况可能dy为0,这时球永远不会下来
                   self.dy = randint(1,2)
            if self.rect.top <= 0:                     # 碰到上边缘反弹
               self.dy = -self.dy                
            
    def draw(self):
        self.screen.blit(self.image,self.rect)
        
class Explosion(pygame.sprite.Sprite):
    """爆炸类"""
    def __init__(self,images,position,group,screen):
        pygame.sprite.Sprite.__init__(self)
        self.images = images
        self.index = 0
        self.amounts = len(images)
        self.interval = 0.1               # 造型切换时间(秒)
        self.begin_time = time.time()     # 起始时间
        self.image = self.images[0]       # 最初的图形对象
        self.rect = self.image.get_rect() # 矩形对象
        self.rect.center = position       # 爆炸坐标
        self.group = group
        self.group.add(self)
        self.screen = screen
    def update(self):
        if self.index < self.amounts:
            if time.time() - self.begin_time > self.interval: # 超时换造型
                oldcenter = self.rect.center                
                self.image = self.images[self.index]
                self.index = self.index + 1
                self.rect = self.image.get_rect()
                self.rect.center = oldcenter
                self.begin_time = time.time()
        else:
            self.group.remove(self)
            
def display_cover(cover_image):
    """显示封面"""
    cover_image = pygame.image.load(cover_image)
    clock = pygame.time.Clock()
    exit_game = False
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False
                exit_game = True
                break
            if event.type == KEYDOWN:
                if event.key == K_SPACE:
                    running = False
                    break
        screen.blit(cover_image,(0,0))
        pygame.display.update()
        clock.tick(10)
    return exit_game

def start_level(level_index):
    """生成砖块,启动这一关"""
    rows,cols = level_design[level_index]
    all_bricks_width = cols * 100 + (cols-1) * 20  # 所有砖块的宽度
    left_pad = (width - all_bricks_width) // 2     # 砖块两边的到屏幕边缘的距离
    up_pad = 50                                    # 顶端距离
    for r in range(rows):                          # 摆砖块
        for c in range(cols):
            x = left_pad + c * 120
            y = up_pad +r * 60
            Brick(brick_image,(x,y),brick_group)
            
    exit_game = False 
    while len(brick_group) > 0 and exit_game ==False:
        for event in pygame.event.get():
            if event.type == QUIT: 
                exit_game = True
                break
            if event.type == MOUSEMOTION:
                board.update(event.pos)
            if event.type == MOUSEBUTTONUP:
                ball.shoot()
                print(ball.shoot_flag)
        
        if ball.lives == 0:
            exit_game = True
            break
        ball.update()
        brick_group.update()
        explosion_group.update()
        prop_group.update()
        
        collision_check()            #这个函数是检测所有对象之间的碰撞检测
        
        screen.fill((0,0,0))
        board.draw()
        ball.draw()
        brick_group.draw(screen)
        explosion_group.draw(screen)
        prop_group.draw(screen)
        pygame.display.update()
        clock.tick(60)
    print("退出本关")
    "关卡完后,弹球归位"
    explosion_group.empty()       # 爆炸组清空
    prop_group.empty()            # 道具组清空
    ball.shoot_flag = 0
    return exit_game
     

You must log in to read the rest of this article. Please log in or register as a user.