coding_giants

lesson 12 project

Mar 17th, 2023 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2.  
  3. from paddle import Paddle
  4. from ball import Ball
  5.  
  6. # height and width of the screen
  7. SCREEN_WIDTH = 1024
  8. SCREEN_HEIGHT = 800
  9. lives = 3
  10.  
  11. # pygame settings
  12. pygame.init()
  13. pygame.font.init()
  14.  
  15. # font, display, clock and background objects
  16. font = pygame.font.SysFont('Comic Sans MS', 24)
  17. display = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
  18. clock = pygame.time.Clock()
  19. background_image = pygame.image.load('images/background.png')
  20.  
  21. # paddle object
  22. paddle = Paddle()
  23. # ball object
  24. ball = Ball()
  25.  
  26. # main game loop
  27. game_on = True
  28. while game_on:
  29. for event in pygame.event.get():
  30. if event.type == pygame.KEYDOWN:
  31. if event.key == pygame.K_ESCAPE:
  32. game_on = False
  33. elif event.type == pygame.QUIT:
  34. game_on = False
  35.  
  36. # paddle controls
  37. pressed_keys = pygame.key.get_pressed()
  38. if pressed_keys[pygame.K_a]:
  39. paddle.move_paddle(-1)
  40. if pressed_keys[pygame.K_d]:
  41. paddle.move_paddle(1)
  42.  
  43. # ball update
  44. ball.update(paddle)
  45.  
  46. # checking if ball has touched the lower edge of the screen
  47. if ball.lost:
  48. lives -= 1
  49. if lives <= 0:
  50. break
  51. ball.reset_position()
  52. paddle.reset_position()
  53.  
  54. # paddle update
  55. paddle.update()
  56.  
  57. # display background
  58. display.blit(background_image, (0, 0))
  59.  
  60. # display the player and the ball
  61. display.blit(paddle.image, paddle.rect)
  62. display.blit(ball.image, ball.rect)
  63.  
  64. # render score
  65. text = font.render(
  66. f'Lives: {lives}', False, (255, 0, 255))
  67. display.blit(text, (16, 16))
  68.  
  69. pygame.display.flip()
  70. clock.tick(30)
  71.  
  72. pygame.quit()
  73.  
Add Comment
Please, Sign In to add comment