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

python pygame egypt brick埃及爆破砖
python pygame egypt brick埃及爆破砖

以下是部分代码预览:

"""
   一个创意打砖块游戏,用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

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))
   
        
class Explosion(pygame.sprite.Sprite):
    """爆炸类"""
    def __init__(self,images,position,group,screen):
        pygame.sprite.Sprite.__init__(self)
        self.images = images
               
def display_cover(cover_image):
    """显示封面"""
    cover_image = pygame.image.load(cover_image)
    clock = pygame.time.Clock()
    exit_game = False

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     # 砖块两边的到屏幕边缘的距离

     
def collision_check():
    
    "小球和砖块组的碰撞"    
    b =  pygame.sprite.spritecollideany(ball, brick_group)    # 球和砖块组的碰撞,返回碰到的砖块

         
if __name__ == "__main__":

  
    pygame.init()
    screen = pygame.display.set_mode((width,height))
    pygame.display.set_caption("埃及爆破砖_道具的设计")
    
    ball_image = pygame.image.load(ball_image).convert_alpha()
    brick_image = pygame.image.load(brick_image).convert_alpha()
    board_images = [pygame.image.load(image).convert_alpha() for image in board_images]
    level_images = [pygame.image.load(image).convert_alpha() for image in level_images]
    explosion_images = [pygame.image.load(image).convert_alpha() for image in explosion_images]

    prop_images = {}
    prop_images['life'] = ball_image
    prop_images['dead'] = pygame.image.load("resources/skeleton.png").convert_alpha()
    prop_images['speedup'] = pygame.image.load("resources/speedup.png").convert_alpha()
    prop_images['slowdown'] = pygame.image.load("resources/slowdown.png").convert_alpha()
    
    
    
    exit_game = display_cover(cover_image)                  # 按空格键开始游戏
    if exit_game :
        pygame.quit()
    else:
        brick_group = pygame.sprite.Group()     # 砖块组
        explosion_group = pygame.sprite.Group() # 爆炸组
        prop_group = pygame.sprite.Group()      # 道具组
        
        board= Board(board_images,screen)
        ball = Ball(ball_image,screen,board)
        exit_game1 = False
        exit_game2 = False
        clock = pygame.time.Clock()
        level_index = 0
        while level_index < level_amounts:      # 遍历关卡
            exit_game1 = start_level(level_index)
            if exit_game1 != True:                 
                exit_game2 = wait_press_any_key() # 没有在游戏中按关闭按钮,那就等待按任意键开始下一关
                level_index += 1
                if exit_game2:break
            else:
                break
         
        if exit_game1 == False and exit_game2 == False:
            "以下是显示结束时的画面"
            running = True
            while running:
                event = pygame.event.poll() 
                if event.type in ( QUIT,KEYDOWN,MOUSEBUTTONDOWN ):
                      running = False
                      break
                screen.blit(level_images[level_index-1],(0,0))
                pygame.display.update()
                clock.tick(10)
        pygame.quit()
        print("游戏结束!")
            
    

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

下载完整源代码与素材,请扫码付款。

VIP免费查看

已有1人支付

本站所有作品,教程等皆为原创,版权所有。只供个人及单位内部研究使用,对外展示或传播必需经本站同意,且注明来自本站。培训机构等用本站资源培训学生,需经本站授权。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。亦可直接向微信号scratch8付款购买。入住QQ群:225792826 和爱好者共同交流,并且能下载免费提供的Python资源(需提供真实姓名才可入群)
李兴球的博客_Python创意编程技术前沿_pygame » Python创意打砖块游戏埃及爆破砖
scratch

学本领,探索更大的世界!

李兴球博客 风火轮编程主页