Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, sys, random, math
- from pygame import K_LEFT, K_RIGHT, K_SPACE
- # import time
- pygame.init()
- width = 700
- height = 600
- testing = False # MJK (set to True to get assistance with paddle positioning)
- # bgm= pygame.mixer.Sound("Projectx/audio/bgm.mp3")
- # hit= pygame.mixer.Sound("Projectx/audio/hit.wav")
- screen = pygame.display.set_mode((width, height))
- class Paddle(pygame.sprite.Sprite):
- def __init__(self, x, y, width, height, color):
- super().__init__()
- self.x = x
- self.y = y
- self.width = width
- self.height = height
- self.color = pygame.Color(color)
- self.left = self.x # MJK
- self.right = self.x + width # MJK
- self.dx = 3
- self.dy = 3
- def update(self):
- pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))
- keys = pygame.key.get_pressed()
- if keys[K_LEFT]:
- self.x -= self.dx
- if keys[K_RIGHT]:
- self.x += self.dx
- self.left = self.x # MJK
- self.right = self.x + self.width # MJK
- def iscollison(self, other):
- # self is the paddle, other is the ball
- # x_collision = (math.fabs(self.x - other.x) * 2) < (self.width + other.width) # 100
- # y_collision = (math.fabs(self.y - other.y) * 2) < (self.height + other.height)
- # return (x_collision and y_collision)
- if ball.x > (self.left - ball.width / 2) and ball.x < (self.right + ball.width / 2):
- return ball.y > (paddle.y - paddle.height)
- return False
- class Block_(pygame.sprite.Sprite):
- def __init__(self, x, y, width, height):
- super().__init__()
- while True:
- r = random.randint(0, 255)
- g = random.randint(0, 255)
- b = random.randint(0, 255)
- if sum([r, g, b]) > 50:
- break
- self.x = x
- self.y = y
- self.width = width
- self.height = height
- self.left = self.x # MJK
- self.right = self.x + width # MJK
- self.top = self.y # MJK
- self.bottom = self.y + height # MJK
- self.color = (r, g, b)
- self.dx = 1
- self.dy = 1
- def update(self):
- pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))
- def iscollison(self, ball): # Detect ball colliding with a Block
- # x_collision = (math.fabs(self.x - other.x) * 2) < (self.width + other.width)
- # y_collision = (math.fabs(self.y - other.y) * 2) < (self.height + other.height)
- # Does any part of the ball overlap any part of this block?
- # First check if the ball is within the x limits of this block
- if (ball.x + ball.width) > self.x and ball.x < self.right: # MJK
- # Now detect if the ball is within the y limits of this block
- return (ball.y + 20) > self.top and (ball.y) < self.bottom # MJK
- return False
- # return (x_collision and y_collision)
- class Ball(pygame.sprite.Sprite):
- def __init__(self, x, y, width, height, color):
- super().__init__()
- self.x = x
- self.y = y
- self.width = width
- self.height = height
- self.color = pygame.Color(color)
- self.dx = 0.5
- self.dy = 0.5
- def update(self):
- pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))
- ball = Ball(width / 2 - 10, height - 80 - 10, 20, 20, 'yellow')
- all_blocks = []
- r = random.randint(0, 255)
- g = random.randint(0, 255)
- b = random.randint(0, 255)
- paddle = Paddle(width / 2 - 40, height - 50 - 10, 80, 20, 'white')
- # Draw all the blocks
- for i in range(50, 600, 80):
- for j in range(50, 300, 30):
- block_ = Block_(i, j, 80, 30)
- all_blocks.append(block_)
- total_blocks = len(all_blocks)
- total_chances = 3
- run = True
- def intro_screen():
- screen.fill((0, 0, 0))
- font1 = pygame.font.Font('freesansbold.ttf', 20)
- font2 = pygame.font.Font('freesansbold.ttf', 15)
- level_text = font1.render('Welcome to Block Breaker', True, (0, 255, 0), (0, 0, 0))
- level_text2 = font2.render("Press 'space' to start", True, (255, 0, 0), (0, 0, 0))
- screen.blit(level_text, (width / 2 - 120, height / 2))
- screen.blit(level_text2, (width / 2 - 60, height / 2 + 40))
- def game_engine():
- screen.fill((0, 0, 0))
- global total_chances, total_blocks
- for block in all_blocks:
- block.update()
- if paddle.iscollison(ball):
- ball.dy *= -1 # Reverse y direction, keep x
- for block in all_blocks:
- if block.x < 9000 and block.iscollison(ball): # MJK
- # pygame.mixer.Sound.play(hit)
- ball.dy *= -1 # Reverse y
- block.x = 12000
- total_blocks -= 1
- print("Blocks remaining = ", total_blocks)
- if total_blocks == 0 or total_chances == 0:
- run = False
- pygame.mixer.music.stop()
- if ball.x < 0 or ball.x >= width - 20: # MJK
- ball.dx *= -1 # Reverse x direction
- if ball.y <= 0: # Top of screen. Reverse y direction
- ball.dy *= -1
- # WHAT DOES THIS BLOCK OF CODE DO APART FROM LOSE A LIFE?
- if ball.y > height - 10:
- ball.y = height - 10
- ball.x = width / 2 - ball.width / 2
- ball.y = height / 2 - ball.height / 2
- total_chances -= 1
- print(total_chances)
- paddle.width -= 10 # WHY ???
- if testing:
- # See if the paddle will be hit by the ball on its current trajectory
- paddle.color = "white"
- if ball.dy < 0: # if ball is descending (Weird!! Why is -ve descending?? should be +ve)
- # compute vertical distance to paddle
- vdist = paddle.y - ball.y
- # compute x position of ball after travelling vdist vertically
- hpos = ball.x - (vdist * ball.dx * 2) # WHY -ve? Why * 2?
- if (hpos + 20) >= paddle.left and hpos <= paddle.right:
- paddle.color = "red"
- # Draw a line showing the ball trajectory
- pygame.draw.line(screen, paddle.color, (ball.x + 10, ball.y + 10), (hpos + 10, paddle.y))
- paddle.update()
- ball.update()
- ball.y -= ball.dy
- ball.x -= ball.dx
- game_start = False
- while run and total_blocks != 0 and total_chances != 0:
- intro_screen()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- keys = pygame.key.get_pressed()
- if keys[K_SPACE]:
- game_start = True
- if game_start:
- # pygame.mixer.Sound.play(bgm)
- game_engine()
- else:
- intro_screen()
- pygame.display.update()
- print('Game Over')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement