Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from paddle import Paddle
- from ball import Ball
- # height and width of the screen
- SCREEN_WIDTH = 1024
- SCREEN_HEIGHT = 800
- lives = 3
- # pygame settings
- pygame.init()
- pygame.font.init()
- # font, display, clock and background objects
- font = pygame.font.SysFont('Comic Sans MS', 24)
- display = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
- clock = pygame.time.Clock()
- background_image = pygame.image.load('images/background.png')
- # paddle object
- paddle = Paddle()
- # ball object
- ball = Ball()
- # main game loop
- game_on = True
- while game_on:
- for event in pygame.event.get():
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_ESCAPE:
- game_on = False
- elif event.type == pygame.QUIT:
- game_on = False
- # paddle controls
- pressed_keys = pygame.key.get_pressed()
- if pressed_keys[pygame.K_a]:
- paddle.move_paddle(-1)
- if pressed_keys[pygame.K_d]:
- paddle.move_paddle(1)
- # ball update
- ball.update(paddle)
- # checking if ball has touched the lower edge of the screen
- if ball.lost:
- lives -= 1
- if lives <= 0:
- break
- ball.reset_position()
- paddle.reset_position()
- # paddle update
- paddle.update()
- # display background
- display.blit(background_image, (0, 0))
- # display the player and the ball
- display.blit(paddle.image, paddle.rect)
- display.blit(ball.image, ball.rect)
- # render score
- text = font.render(
- f'Lives: {lives}', False, (255, 0, 255))
- display.blit(text, (16, 16))
- pygame.display.flip()
- clock.tick(30)
- pygame.quit()
Add Comment
Please, Sign In to add comment