Advertisement
acclivity

PygameBlocksGame

Aug 13th, 2021
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  1. import pygame, sys, random, math
  2. from pygame import K_LEFT, K_RIGHT, K_SPACE
  3. # import time
  4.  
  5. pygame.init()
  6.  
  7. width = 700
  8. height = 600
  9. testing = False # MJK (set to True to get assistance with paddle positioning)
  10.  
  11. # bgm= pygame.mixer.Sound("Projectx/audio/bgm.mp3")
  12. # hit= pygame.mixer.Sound("Projectx/audio/hit.wav")
  13.  
  14. screen = pygame.display.set_mode((width, height))
  15.  
  16.  
  17. class Paddle(pygame.sprite.Sprite):
  18. def __init__(self, x, y, width, height, color):
  19. super().__init__()
  20. self.x = x
  21. self.y = y
  22. self.width = width
  23. self.height = height
  24. self.color = pygame.Color(color)
  25. self.left = self.x # MJK
  26. self.right = self.x + width # MJK
  27. self.dx = 3
  28. self.dy = 3
  29.  
  30.  
  31. def update(self):
  32. pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))
  33. keys = pygame.key.get_pressed()
  34. if keys[K_LEFT]:
  35. self.x -= self.dx
  36.  
  37. if keys[K_RIGHT]:
  38. self.x += self.dx
  39. self.left = self.x # MJK
  40. self.right = self.x + self.width # MJK
  41.  
  42. def iscollison(self, other):
  43. # self is the paddle, other is the ball
  44.  
  45. # x_collision = (math.fabs(self.x - other.x) * 2) < (self.width + other.width) # 100
  46. # y_collision = (math.fabs(self.y - other.y) * 2) < (self.height + other.height)
  47. # return (x_collision and y_collision)
  48. if ball.x > (self.left - ball.width / 2) and ball.x < (self.right + ball.width / 2):
  49. return ball.y > (paddle.y - paddle.height)
  50. return False
  51.  
  52.  
  53. class Block_(pygame.sprite.Sprite):
  54. def __init__(self, x, y, width, height):
  55. super().__init__()
  56. while True:
  57. r = random.randint(0, 255)
  58. g = random.randint(0, 255)
  59. b = random.randint(0, 255)
  60. if sum([r, g, b]) > 50:
  61. break
  62. self.x = x
  63. self.y = y
  64. self.width = width
  65. self.height = height
  66. self.left = self.x # MJK
  67. self.right = self.x + width # MJK
  68. self.top = self.y # MJK
  69. self.bottom = self.y + height # MJK
  70. self.color = (r, g, b)
  71. self.dx = 1
  72. self.dy = 1
  73.  
  74. def update(self):
  75. pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))
  76.  
  77. def iscollison(self, ball): # Detect ball colliding with a Block
  78. # x_collision = (math.fabs(self.x - other.x) * 2) < (self.width + other.width)
  79. # y_collision = (math.fabs(self.y - other.y) * 2) < (self.height + other.height)
  80.  
  81. # Does any part of the ball overlap any part of this block?
  82. # First check if the ball is within the x limits of this block
  83. if (ball.x + ball.width) > self.x and ball.x < self.right: # MJK
  84. # Now detect if the ball is within the y limits of this block
  85. return (ball.y + 20) > self.top and (ball.y) < self.bottom # MJK
  86.  
  87. return False
  88. # return (x_collision and y_collision)
  89.  
  90.  
  91. class Ball(pygame.sprite.Sprite):
  92. def __init__(self, x, y, width, height, color):
  93. super().__init__()
  94. self.x = x
  95. self.y = y
  96. self.width = width
  97. self.height = height
  98. self.color = pygame.Color(color)
  99. self.dx = 0.5
  100. self.dy = 0.5
  101.  
  102. def update(self):
  103. pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))
  104.  
  105.  
  106. ball = Ball(width / 2 - 10, height - 80 - 10, 20, 20, 'yellow')
  107.  
  108. all_blocks = []
  109. r = random.randint(0, 255)
  110. g = random.randint(0, 255)
  111. b = random.randint(0, 255)
  112.  
  113. paddle = Paddle(width / 2 - 40, height - 50 - 10, 80, 20, 'white')
  114.  
  115. # Draw all the blocks
  116. for i in range(50, 600, 80):
  117. for j in range(50, 300, 30):
  118. block_ = Block_(i, j, 80, 30)
  119. all_blocks.append(block_)
  120.  
  121. total_blocks = len(all_blocks)
  122. total_chances = 3
  123.  
  124. run = True
  125.  
  126.  
  127. def intro_screen():
  128. screen.fill((0, 0, 0))
  129. font1 = pygame.font.Font('freesansbold.ttf', 20)
  130. font2 = pygame.font.Font('freesansbold.ttf', 15)
  131. level_text = font1.render('Welcome to Block Breaker', True, (0, 255, 0), (0, 0, 0))
  132. level_text2 = font2.render("Press 'space' to start", True, (255, 0, 0), (0, 0, 0))
  133. screen.blit(level_text, (width / 2 - 120, height / 2))
  134. screen.blit(level_text2, (width / 2 - 60, height / 2 + 40))
  135.  
  136.  
  137. def game_engine():
  138. screen.fill((0, 0, 0))
  139. global total_chances, total_blocks
  140. for block in all_blocks:
  141. block.update()
  142.  
  143. if paddle.iscollison(ball):
  144. ball.dy *= -1 # Reverse y direction, keep x
  145.  
  146. for block in all_blocks:
  147. if block.x < 9000 and block.iscollison(ball): # MJK
  148. # pygame.mixer.Sound.play(hit)
  149. ball.dy *= -1 # Reverse y
  150. block.x = 12000
  151. total_blocks -= 1
  152. print("Blocks remaining = ", total_blocks)
  153.  
  154. if total_blocks == 0 or total_chances == 0:
  155. run = False
  156. pygame.mixer.music.stop()
  157.  
  158. if ball.x < 0 or ball.x >= width - 20: # MJK
  159. ball.dx *= -1 # Reverse x direction
  160.  
  161. if ball.y <= 0: # Top of screen. Reverse y direction
  162. ball.dy *= -1
  163.  
  164. # WHAT DOES THIS BLOCK OF CODE DO APART FROM LOSE A LIFE?
  165. if ball.y > height - 10:
  166. ball.y = height - 10
  167. ball.x = width / 2 - ball.width / 2
  168. ball.y = height / 2 - ball.height / 2
  169. total_chances -= 1
  170. print(total_chances)
  171. paddle.width -= 10 # WHY ???
  172.  
  173. if testing:
  174. # See if the paddle will be hit by the ball on its current trajectory
  175. paddle.color = "white"
  176. if ball.dy < 0: # if ball is descending (Weird!! Why is -ve descending?? should be +ve)
  177. # compute vertical distance to paddle
  178. vdist = paddle.y - ball.y
  179. # compute x position of ball after travelling vdist vertically
  180. hpos = ball.x - (vdist * ball.dx * 2) # WHY -ve? Why * 2?
  181. if (hpos + 20) >= paddle.left and hpos <= paddle.right:
  182. paddle.color = "red"
  183. # Draw a line showing the ball trajectory
  184. pygame.draw.line(screen, paddle.color, (ball.x + 10, ball.y + 10), (hpos + 10, paddle.y))
  185.  
  186. paddle.update()
  187.  
  188. ball.update()
  189.  
  190. ball.y -= ball.dy
  191. ball.x -= ball.dx
  192.  
  193. game_start = False
  194.  
  195. while run and total_blocks != 0 and total_chances != 0:
  196. intro_screen()
  197. for event in pygame.event.get():
  198. if event.type == pygame.QUIT:
  199. pygame.quit()
  200. sys.exit()
  201.  
  202. keys = pygame.key.get_pressed()
  203. if keys[K_SPACE]:
  204. game_start = True
  205.  
  206. if game_start:
  207. # pygame.mixer.Sound.play(bgm)
  208.  
  209. game_engine()
  210. else:
  211. intro_screen()
  212.  
  213. pygame.display.update()
  214.  
  215. print('Game Over')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement