Advertisement
ada1711

Untitled

Mar 15th, 2023
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. import pygame
  2.  
  3. from Platform import Platform
  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. # platform object
  22. platform = Platform()
  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. # platform controls
  37. pressed_keys = pygame.key.get_pressed()
  38. if pressed_keys[pygame.K_a]:
  39. platform.move_platform(-1)
  40. if pressed_keys[pygame.K_d]:
  41. platform.move_platform(1)
  42.  
  43. # ball update
  44. ball.update(platform)
  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. platform.reset_position()
  53.  
  54. # platform update
  55. platform.update()
  56.  
  57. # display background
  58. display.blit(background_image, (0, 0))
  59.  
  60. # display the player and the ball
  61. display.blit(platform.image, platform.position)
  62. display.blit(ball.image, ball.position)
  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.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement