Guest User

Untitled

a guest
May 20th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. import sys
  2. import pygame
  3.  
  4. SCREEN_SIZE = 640,480
  5.  
  6. # Object dimensions
  7. BRICK_WIDTH = 60
  8. BRICK_HEIGHT = 15
  9. PADDLE_WIDTH = 60
  10. PADDLE_HEIGHT = 12
  11. BALL_DIAMETER = 16
  12. BALL_RADIUS = BALL_DIAMETER / 2
  13.  
  14. MAX_PADDLE_X = SCREEN_SIZE[0] - PADDLE_WIDTH
  15. MAX_BALL_X = SCREEN_SIZE[0] - BALL_DIAMETER
  16. MAX_BALL_Y = SCREEN_SIZE[1] - BALL_DIAMETER
  17.  
  18. # Paddle Y coordinate
  19. PADDLE_Y = SCREEN_SIZE[1] - PADDLE_HEIGHT - 10
  20.  
  21. # Color constants
  22. BLACK = (0,0,0)
  23. WHITE = (255,255,255)
  24. BLUE = (0,0,255)
  25. BRICK_COLOR = (200,200,0)
  26.  
  27. # State constants
  28. STATE_BALL_IN_PADDLE = 0
  29. STATE_PLAYING = 1
  30. STATE_WON = 2
  31. STATE_GAME_OVER = 3
  32.  
  33. class Bricka:
  34.  
  35. def __init__(self):
  36. pygame.init()
  37.  
  38. self.screen = pygame.display.set_mode(SCREEN_SIZE)
  39. pygame.display.set_caption("bricka (a breakout clone by codeNtronix.com)")
  40.  
  41. self.clock = pygame.time.Clock()
  42.  
  43. if pygame.font:
  44. self.font = pygame.font.Font(None,30)
  45. else:
  46. self.font = None
  47.  
  48. self.init_game()
  49.  
  50.  
  51. def init_game(self):
  52. self.lives = 3
  53. self.score = 0
  54. self.state = STATE_BALL_IN_PADDLE
  55.  
  56. self.paddle = pygame.Rect(300,PADDLE_Y,PADDLE_WIDTH,PADDLE_HEIGHT)
  57. self.ball = pygame.Rect(300,PADDLE_Y - BALL_DIAMETER,BALL_DIAMETER,BALL_DIAMETER)
  58.  
  59. self.ball_vel = [5,-5]
  60.  
  61. self.create_bricks()
  62.  
  63.  
  64. def create_bricks(self):
  65. y_ofs = 35
  66. self.bricks = []
  67. for i in range(7):
  68. x_ofs = 35
  69. for j in range(8):
  70. self.bricks.append(pygame.Rect(x_ofs,y_ofs,BRICK_WIDTH,BRICK_HEIGHT))
  71. x_ofs += BRICK_WIDTH + 10
  72. y_ofs += BRICK_HEIGHT + 5
  73.  
  74. def draw_bricks(self):
  75. for brick in self.bricks:
  76. pygame.draw.rect(self.screen, BRICK_COLOR, brick)
  77.  
  78. def check_input(self):
  79. keys = pygame.key.get_pressed()
  80.  
  81. if keys[pygame.K_LEFT]:
  82. self.paddle.left -= 5
  83. if self.paddle.left < 0:
  84. self.paddle.left = 0
  85.  
  86. if keys[pygame.K_RIGHT]:
  87. self.paddle.left += 5
  88. if self.paddle.left > MAX_PADDLE_X:
  89. self.paddle.left = MAX_PADDLE_X
  90.  
  91. if keys[pygame.K_SPACE] and self.state == STATE_BALL_IN_PADDLE:
  92. self.ball_vel = [5,-5]
  93. self.state = STATE_PLAYING
  94. elif keys[pygame.K_RETURN] and (self.state == STATE_GAME_OVER or self.state == STATE_WON):
  95. self.init_game()
  96.  
  97. def move_ball(self):
  98. self.ball.left += self.ball_vel[0]
  99. self.ball.top += self.ball_vel[1]
  100.  
  101. if self.ball.left <= 0:
  102. self.ball.left = 0
  103. self.ball_vel[0] = -self.ball_vel[0]
  104. elif self.ball.left >= MAX_BALL_X:
  105. self.ball.left = MAX_BALL_X
  106. self.ball_vel[0] = -self.ball_vel[0]
  107.  
  108. if self.ball.top < 0:
  109. self.ball.top = 0
  110. self.ball_vel[1] = -self.ball_vel[1]
  111. elif self.ball.top >= MAX_BALL_Y:
  112. self.ball.top = MAX_BALL_Y
  113. self.ball_vel[1] = -self.ball_vel[1]
  114.  
  115. def handle_collisions(self):
  116. for brick in self.bricks:
  117. if self.ball.colliderect(brick):
  118. self.score += 3
  119. self.ball_vel[1] = -self.ball_vel[1]
  120. self.bricks.remove(brick)
  121. break
  122.  
  123. if len(self.bricks) == 0:
  124. self.state = STATE_WON
  125.  
  126. if self.ball.colliderect(self.paddle):
  127. self.ball.top = PADDLE_Y - BALL_DIAMETER
  128. self.ball_vel[1] = -self.ball_vel[1]
  129. elif self.ball.top > self.paddle.top:
  130. self.lives -= 1
  131. if self.lives > 0:
  132. self.state = STATE_BALL_IN_PADDLE
  133. else:
  134. self.state = STATE_GAME_OVER
  135.  
  136. def show_stats(self):
  137. if self.font:
  138. font_surface = self.font.render("SCORE: " + str(self.score) + " LIVES: " + str(self.lives), False, WHITE)
  139. self.screen.blit(font_surface, (205,5))
  140.  
  141. def show_message(self,message):
  142. if self.font:
  143. size = self.font.size(message)
  144. font_surface = self.font.render(message,False, WHITE)
  145. x = (SCREEN_SIZE[0] - size[0]) / 2
  146. y = (SCREEN_SIZE[1] - size[1]) / 2
  147. self.screen.blit(font_surface, (x,y))
  148.  
  149.  
  150. def run(self):
  151. while 1:
  152. for event in pygame.event.get():
  153. if event.type == pygame.QUIT:
  154. sys.exit
  155.  
  156. self.clock.tick(50)
  157. self.screen.fill(BLACK)
  158. self.check_input()
  159.  
  160. if self.state == STATE_PLAYING:
  161. self.move_ball()
  162. self.handle_collisions()
  163. elif self.state == STATE_BALL_IN_PADDLE:
  164. self.ball.left = self.paddle.left + self.paddle.width / 2
  165. self.ball.top = self.paddle.top - self.ball.height
  166. self.show_message("PRESS SPACE TO LAUNCH THE BALL")
  167. elif self.state == STATE_GAME_OVER:
  168. self.show_message("GAME OVER. PRESS ENTER TO PLAY AGAIN")
  169. elif self.state == STATE_WON:
  170. self.show_message("YOU WON! PRESS ENTER TO PLAY AGAIN")
  171.  
  172. self.draw_bricks()
  173.  
  174. # Draw paddle
  175. pygame.draw.rect(self.screen, BLUE, self.paddle)
  176.  
  177. # Draw ball
  178. pygame.draw.circle(self.screen, WHITE, (self.ball.left + BALL_RADIUS, self.ball.top + BALL_RADIUS), BALL_RADIUS)
  179.  
  180. self.show_stats()
  181.  
  182. pygame.display.flip()
  183.  
  184. if __name__ == "__main__":
  185. Bricka().run()
Add Comment
Please, Sign In to add comment