GERMANYDOV

arcanoid

Feb 21st, 2020
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 KB | None | 0 0
  1. """
  2. Sample Breakout Game
  3.  
  4. Sample Python/Pygame Programs
  5. Simpson College Computer Science
  6. http://programarcadegames.com/
  7. http://simpson.edu/computer-science/
  8. """
  9.  
  10. # --- Import libraries used for this program
  11.  
  12. import math
  13. import pygame
  14.  
  15. # цвета объектов
  16. black = (255, 255, 255)
  17. white = (0, 0, 0)
  18. blue = (0, 0, 255)
  19.  
  20. # Size of break-out blocks
  21. block_width = 23
  22. block_height = 15
  23.  
  24.  
  25. class Block(pygame.sprite.Sprite):
  26. """This class represents each block that will get knocked out by the ball
  27. It derives from the "Sprite" class in Pygame """
  28.  
  29. def __init__(self, color, x, y):
  30. """ Constructor. Pass in the color of the block,
  31. and its x and y position. """
  32.  
  33. # Call the parent class (Sprite) constructor
  34. super().__init__()
  35.  
  36. # Create the image of the block of appropriate size
  37. # The width and height are sent as a list for the first parameter.
  38. self.image = pygame.Surface([block_width, block_height])
  39.  
  40. # Fill the image with the appropriate color
  41. self.image.fill(color)
  42.  
  43. # Fetch the rectangle object that has the dimensions of the image
  44. self.rect = self.image.get_rect()
  45.  
  46. # Move the top left of the rectangle to x,y.
  47. # This is where our block will appear..
  48. self.rect.x = x
  49. self.rect.y = y
  50.  
  51.  
  52. class Ball(pygame.sprite.Sprite):
  53. """ This class represents the ball
  54. It derives from the "Sprite" class in Pygame """
  55.  
  56. # скорость мячика
  57. speed = 11.0
  58.  
  59. # Floating point representation of where the ball is
  60. x = 0.0
  61. y = 180.0
  62.  
  63. # Direction of ball (in degrees)
  64. direction = 200
  65.  
  66. width = 10
  67. height = 10
  68.  
  69. # Constructor. Pass in the color of the block, and its x and y position
  70. def __init__(self):
  71. # Call the parent class (Sprite) constructor
  72. super().__init__()
  73.  
  74. # Create the image of the ball
  75. self.image = pygame.Surface([self.width, self.height])
  76.  
  77. # Color the ball
  78. self.image.fill(white)
  79.  
  80. # Get a rectangle object that shows where our image is
  81. self.rect = self.image.get_rect()
  82.  
  83. # Get attributes for the height/width of the screen
  84. self.screenheight = pygame.display.get_surface().get_height()
  85. self.screenwidth = pygame.display.get_surface().get_width()
  86.  
  87. def bounce(self, diff):
  88. """ This function will bounce the ball
  89. off a horizontal surface (not a vertical one) """
  90.  
  91. self.direction = (180 - self.direction) % 360
  92. self.direction -= diff
  93.  
  94. def update(self):
  95. """ Update the position of the ball. """
  96. # Sine and Cosine work in degrees, so we have to convert them
  97. direction_radians = math.radians(self.direction)
  98.  
  99. # Change the position (x and y) according to the speed and direction
  100. self.x += self.speed * math.sin(direction_radians)
  101. self.y -= self.speed * math.cos(direction_radians)
  102.  
  103. # Move the image to where our x and y are
  104. self.rect.x = self.x
  105. self.rect.y = self.y
  106.  
  107. # Do we bounce off the top of the screen?
  108. if self.y <= 0:
  109. self.bounce(0)
  110. self.y = 1
  111.  
  112. # Do we bounce off the left of the screen?
  113. if self.x <= 0:
  114. self.direction = (360 - self.direction) % 360
  115. self.x = 1
  116.  
  117. # Do we bounce of the right side of the screen?
  118. if self.x > self.screenwidth - self.width:
  119. self.direction = (360 - self.direction) % 360
  120. self.x = self.screenwidth - self.width - 1
  121.  
  122. # Did we fall off the bottom edge of the screen?
  123. if self.y > 600:
  124. return True
  125. else:
  126. return False
  127.  
  128.  
  129. class Player(pygame.sprite.Sprite):
  130. """ This class represents the bar at the bottom that the
  131. player controls. """
  132.  
  133. def __init__(self):
  134. """ длинна и ширина платформы """
  135. # Call the parent's constructor
  136. super().__init__()
  137.  
  138. self.width = 75
  139. self.height = 15
  140. self.image = pygame.Surface([self.width, self.height])
  141. self.image.fill((white))
  142.  
  143. # Make our top-left corner the passed-in location.
  144. self.rect = self.image.get_rect()
  145. self.screenheight = pygame.display.get_surface().get_height()
  146. self.screenwidth = pygame.display.get_surface().get_width()
  147.  
  148. self.rect.x = 0
  149. self.rect.y = self.screenheight-self.height
  150.  
  151. def update(self):
  152. """ Update the player position. """
  153. # Get where the mouse is
  154. pos = pygame.mouse.get_pos()
  155. # Set the left side of the player bar to the mouse position
  156. self.rect.x = pos[0]
  157. # Make sure we don't push the player paddle
  158. # off the right side of the screen
  159. if self.rect.x > self.screenwidth - self.width:
  160. self.rect.x = self.screenwidth - self.width
  161.  
  162. # Call this function so the Pygame library can initialize itself
  163. pygame.init()
  164.  
  165. # Create an 800x600 sized screen
  166. screen = pygame.display.set_mode([800, 600])
  167.  
  168. # Set the title of the window
  169. pygame.display.set_caption('Breakout')
  170.  
  171. # Enable this to make the mouse disappear when over our window
  172. pygame.mouse.set_visible(0)
  173.  
  174. # This is a font we use to draw text on the screen (size 36)
  175. font = pygame.font.Font(None, 36)
  176.  
  177. # Create a surface we can draw on
  178. background = pygame.Surface(screen.get_size())
  179.  
  180. # Create sprite lists
  181. blocks = pygame.sprite.Group()
  182. balls = pygame.sprite.Group()
  183. allsprites = pygame.sprite.Group()
  184.  
  185. # Create the player paddle object
  186. player = Player()
  187. allsprites.add(player)
  188.  
  189. # Create the ball
  190. ball = Ball()
  191. allsprites.add(ball)
  192. balls.add(ball)
  193.  
  194. # The top of the block (y position)
  195. top = 80
  196.  
  197. # Number of blocks to create
  198. blockcount = 32
  199.  
  200. # --- Create blocks
  201.  
  202. # Five rows of blocks
  203. for row in range(5):
  204. # 32 columns of blocks
  205. for column in range(0, blockcount):
  206. # Create a block (color,x,y)
  207. block = Block(blue, column * (block_width + 2) + 1, top)
  208. blocks.add(block)
  209. allsprites.add(block)
  210. # Move the top of the next row down
  211. top += block_height + 2
  212.  
  213. # Clock to limit speed
  214. clock = pygame.time.Clock()
  215.  
  216. # Is the game over?
  217. game_over = False
  218.  
  219. # Exit the program?
  220. exit_program = False
  221.  
  222. # Main program loop
  223. while not exit_program:
  224.  
  225. # Limit to 30 fps
  226. clock.tick(30)
  227.  
  228. # Clear the screen
  229. screen.fill(black)
  230.  
  231. # Process the events in the game
  232. for event in pygame.event.get():
  233. if event.type == pygame.QUIT:
  234. exit_program = True
  235.  
  236. # Update the ball and player position as long
  237. # as the game is not over.
  238. if not game_over:
  239. # Update the player and ball positions
  240. player.update()
  241. game_over = ball.update()
  242.  
  243. # If we are done, print game over
  244. if game_over:
  245. text = font.render("Game Over", True, white)
  246. textpos = text.get_rect(centerx=background.get_width()/2)
  247. textpos.top = 300
  248. screen.blit(text, textpos)
  249.  
  250. # See if the ball hits the player paddle
  251. if pygame.sprite.spritecollide(player, balls, False):
  252. # The 'diff' lets you try to bounce the ball left or right
  253. # depending where on the paddle you hit it
  254. diff = (player.rect.x + player.width/2) - (ball.rect.x+ball.width/2)
  255.  
  256. # Set the ball's y position in case
  257. # we hit the ball on the edge of the paddle
  258. ball.rect.y = screen.get_height() - player.rect.height - ball.rect.height - 1
  259. ball.bounce(diff)
  260.  
  261. # Check for collisions between the ball and the blocks
  262. deadblocks = pygame.sprite.spritecollide(ball, blocks, True)
  263.  
  264. # If we actually hit a block, bounce the ball
  265. if len(deadblocks) > 0:
  266. ball.bounce(0)
  267.  
  268. # Game ends if all the blocks are gone
  269. if len(blocks) == 0:
  270. game_over = True
  271.  
  272. # Draw Everything
  273. allsprites.draw(screen)
  274.  
  275. # Flip the screen and show what we've drawn
  276. pygame.display.flip()
  277.  
  278. pygame.quit()
Add Comment
Please, Sign In to add comment