以下是部分代码预览:
""" 一个创意打砖块游戏,用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("游戏结束!")
下载完整源代码与素材,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)