Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Brick Breaker
- # Bobby Clarke
- # version 1.2
- #Imports
- import pygame, easygui, pickle, random, sys, os
- from time import sleep
- from pygame.locals import *
- pygame.init()
- # # # # #
- screen = pygame.display.set_mode((500, 600))
- clock = pygame.time.Clock()
- black = (0, 0, 0)
- class paddle():
- """The paddle, player controlled"""
- def __init__(self):
- self.sprite = pygame.image.load("Images/paddle.png")
- self.x = 200
- self.left = False
- self.right = False
- def move(self):
- """move the paddle"""
- if self.left and self.x >= 0:
- self.x -= 2
- elif self.right and self.x <= 400:
- self.x += 2
- class ball():
- """The ball"""
- def __init__(self, x = 242, y = 440):
- self.sprite = pygame.image.load("Images/ball.png")
- self.x = x
- self.y = y
- self.up = False
- self.down = True
- self.left = False
- self.right = False
- self.lives = 3
- def move(self, paddle, start_x, start_y):
- """Move the ball according to it's direction and surroundings"""
- if self.up:
- self.y -= 5
- elif self.down:
- self.y += 5
- if self.right:
- self.x += 2
- elif self.left:
- self.x -= 2
- if self.y < 0:
- self.down = True
- self.up = False
- elif self.x < 0:
- self.right = True
- self.left = False
- elif self.x > 484:
- self.left = True
- self.right = False
- elif self.y > 616:
- self.x = start_x
- self.y = start_y
- self.up = True
- self.down = False
- self.left = False
- self.right = False
- self.lives -= 1
- if self.x in range(paddle.x - 16, paddle.x + 100):
- if self.y in range(534, 580):
- self.up = True
- self.down = False
- if self.x in range(paddle.x - 16, paddle.x + 29):
- self.left = True
- self.right = False
- elif self.x in range(paddle.x + 65, paddle.x + 100):
- self.left = False
- self.right = True
- else:
- self.left = False
- self.right = False
- class brick():
- """The bricks, perameters are "broken", indicating how broken the brick is
- from 1 to 5, x and y axis and unbreaking (bool) (broken, x, y, unbreaking = False),
- broken must be 0 for unbreaking bricks"""
- def __init__(self, broken, x, y, unbreaking = False):
- self.broken = broken
- self.x = x
- self.y = y
- self.unbreaking = unbreaking
- self.sprite = pygame.image.load("Images/brick{0}.png".format(self.broken))
- def update(self, ball, paddle):
- broken_once = False
- heal = False
- heal_x = None
- heal_y = None
- """Check for changes with the brick, and act accordingly"""
- if ball.x in range(self.x - 16, self.x + 60):
- if ball.y in range(self.y - 16, self.y + 40):
- if not broken_once:
- if not self.unbreaking:
- self.broken += 1
- broken_once = True
- try:
- self.sprite = pygame.image.load("Images/brick{0}.png".format(self.broken))
- except:
- healchance = random.randint(1, 10)
- if healchance == 10:
- heal = True
- heal_x = self.x + 22
- heal_y = self.y + 12
- self.x = -60
- self.y = -40
- if ball.left and (ball.x in range(self.x - 16, self.x) or ball.x in range(self.x + 55, self.x + 60)):
- ball.left = False
- ball.right = True
- elif ball.right and (ball.x in range(self.x - 16, self.x) or ball.x in range(self.x + 55, self.x + 60)):
- ball.right = False
- ball.left = True
- if ball.up:
- ball.up = False
- ball.down = True
- elif ball.down:
- ball.down = False
- ball.up = True
- ball.move(paddle, ball.start_x, ball.start_y)
- return heal, heal_x, heal_y
- class menu():
- """The menu / title screen"""
- def run():
- """Run the menu and return a string"""
- end = False
- screen.blit(pygame.image.load("Images/menu.png"), (0, 0))
- pygame.display.update()
- while not end:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit()
- elif event.type == MOUSEBUTTONDOWN:
- x, y = pygame.mouse.get_pos()
- if x in range(200, 300) and y in range(200, 250):
- output = "play"
- end = True
- elif x in range(150, 370) and y in range(320, 360):
- output = "newlevel"
- end = True
- elif x in range(135, 390) and y in range(440, 480):
- output = "editlevel"
- end = True
- return output
- class level():
- def build(brickclass, ball, bricks = [], levelname = ""):
- """User-Friendly level builder"""
- click = False
- broken = 1
- x = 0
- y = 0
- unbreaking = False
- end = False
- drag = False
- delete = False
- new = True
- while not end:
- clock.tick(60)
- screen.fill(black)
- if click:
- for brick in bricks:
- if (x in range(brick.x, brick.x + 60) and y in range(brick.y, brick.y + 40)) or (x in range(ball.x - 8, ball.x + 24) and y in range(ball.y - 8, ball.y + 24)):
- if delete:
- bricks.remove(brick)
- new = False
- else:
- drag = True
- new = False
- if new:
- if broken == 0:
- unbreaking = True
- bricks.append(brickclass(broken, x - 30, y - 20, unbreaking))
- x = 0
- y = 0
- unbreaking = False
- click = False
- if drag:
- for brick in bricks:
- x, y = pygame.mouse.get_pos()
- if x in range(brick.x, brick.x + 60) and y in range(brick.y, brick.y + 40):
- brick.x = x - 30
- brick.y = y - 20
- elif x in range(ball.x - 8, ball.x + 24) and y in range(ball.y - 8, ball.y + 24):
- ball.x = x - 8
- ball.y = y - 8
- for brick in bricks:
- screen.blit(brick.sprite, (brick.x, brick.y))
- screen.blit(ball.sprite, (ball.x, ball.y))
- pygame.display.update()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit()
- elif event.type == MOUSEBUTTONDOWN:
- click = True
- x, y = pygame.mouse.get_pos()
- elif event.type == MOUSEBUTTONUP:
- drag = False
- new = True
- elif event.type == KEYDOWN:
- if event.key == K_0:
- broken = 0
- elif event.key == K_1:
- broken = 1
- elif event.key == K_2:
- broken = 2
- elif event.key == K_3:
- broken = 3
- elif event.key == K_4:
- broken = 4
- elif event.key == K_5:
- broken = 5
- elif event.key == K_SPACE:
- if delete:
- delete = False
- else:
- delete = True
- elif event.key == K_RETURN:
- save = easygui.boolbox("Do you want to save?", "Level Builder", ["Yes", "no"])
- if save:
- levelname = easygui.enterbox("What is this level's name?", "Level Builder", default = levelname)
- if levelname:
- levelfile = open("Levels/{0}.lvl".format(levelname), "wb")
- for brick in bricks:
- del(brick.sprite)
- del(ball.sprite)
- pickle.dump([bricks, ball], levelfile)
- end = True
- def load(levelfile, brickclass, ballclass):
- levelfile = open(levelfile, "rb")
- level = pickle.load(levelfile)
- bricks = level[0]
- ball = level[1]
- ball = ballclass(ball.x, ball.y)
- newbricks = []
- for brick in bricks:
- newbricks.append(brickclass(brick.broken, brick.x, brick.y, brick.unbreaking))
- levelfile.close()
- return newbricks, ball
- def browse():
- selfdir = os.path.dirname(sys.argv[0])
- levels = os.listdir("{0}{1}".format(selfdir, "/Levels"))
- newlevels = []
- for level in levels:
- if ".lvl" in level:
- level = level[0 : len(level) - 4]
- newlevels.append(level)
- level = easygui.choicebox("Which level do you want to load?", "Brick Breaker", newlevels)
- return level
- def randgen(brickclass):
- bricks = []
- numbricks = random.randint(5, 20)
- for i in range(0, numbricks):
- x = random.randint(0, 440)
- y = random.randint(60, 500)
- broken = random.randint(0, 5)
- if broken == 0:
- unbreaking = True
- else:
- unbreaking = False
- for brick in bricks:
- while x in range (brick.x - 60, brick.x + 60) and y in range(brick.y- 40, brick.y + 40):
- x = random.randint(0, 500)
- y = random.randint(0, 500)
- bricks.append(brickclass(broken, x, y, unbreaking))
- return bricks
- def play(paddle, ball, bricks):
- ball.start_x = ball.x
- ball.start_y = ball.y
- heals = []
- livesimg = pygame.image.load("Images/lives.png")
- gameover = pygame.image.load("Images/gameover.png")
- youwin = pygame.image.load("Images/youwin.png")
- healimg = pygame.image.load("Images/heal.png")
- unbreaking_bricks = 0
- for brick in bricks:
- if brick.unbreaking:
- unbreaking_bricks += 1
- """Play the game"""
- while ball.lives != 0 and len(bricks) != unbreaking_bricks:
- clock.tick(60)
- screen.fill(black)
- paddle.move()
- ball.move(paddle, ball.start_x, ball.start_y)
- for brick in bricks:
- heal, heal_x, heal_y = brick.update(ball, paddle)
- if brick.x == -60 and brick.y == -40:
- bricks.remove(brick)
- if heal:
- heals.append([heal_x, heal_y, random.randint(1, 4)])
- for heal in heals:
- screen.blit(healimg, (heal[0], heal[1]))
- heal[1] += heal[2]
- if heal[0] in range(paddle.x - 16, paddle.x + 100) and heal[1] in range(534, 580):
- heals.remove(heal)
- if ball.lives < 9:
- ball.lives += 1
- # Blits
- screen.blit(paddle.sprite, (paddle.x, 550))
- screen.blit(ball.sprite, (ball.x, ball.y))
- for brick in bricks:
- screen.blit(brick.sprite, (brick.x, brick.y))
- screen.blit(livesimg, (0, 0))
- try:
- screen.blit(pygame.image.load("Images/{0}.png".format(ball.lives)), (100, 0))
- except:
- screen.blit(gameover, (0, 0))
- if len(bricks) == unbreaking_bricks:
- screen.blit(youwin, (0, 0))
- # # # # #
- pygame.display.update()
- if ball.lives == 0 or len(bricks) == unbreaking_bricks:
- sleep(3)
- # Event Loop
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit()
- elif event.type == KEYDOWN:
- #If left or right is pressed, move the paddle
- if event.key == K_RIGHT:
- paddle.right = True
- elif event.key == K_LEFT:
- paddle.left = True
- elif event.type == KEYUP:
- #If left or right is depressed, stop moving
- if event.key == K_RIGHT:
- paddle.right = False
- elif event.key == K_LEFT:
- paddle.left = False
- ball.lives = 3
- def main(paddle, ballclass, brick, menu, levelclass):
- pygame.display.set_caption("Brick Breaker")
- while True:
- choice = menu.run()
- if choice == "play":
- levelname = levelclass.browse()
- level = "Levels/{0}.lvl".format(levelname)
- if levelname:
- bricks, ball = levelclass.load(level, brick, ballclass)
- play(paddle(), ball, bricks)
- elif choice == "newlevel":
- levelclass.build(brick, ballclass(), bricks = [])
- elif choice == "editlevel":
- levelname = levelclass.browse()
- level = "Levels/{0}.lvl".format(levelname)
- if levelname:
- bricks, ball = levelclass.load(level, brick, ballclass)
- levelclass.build(brick, ball, bricks, levelname = levelname)
- main(paddle, ball, brick, menu, level)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement