|
| 1 | +import pygame |
| 2 | + |
| 3 | +BLACK = (0,0,0) |
| 4 | +WHITE = (255,255,255) |
| 5 | +RED = (255,0,0) |
| 6 | +GREEN = (0,255,0) |
| 7 | +BLUE = (0,0,255) |
| 8 | + |
| 9 | +pygame.init() |
| 10 | + |
| 11 | +#Initializing the display window |
| 12 | +size = (800,600) |
| 13 | +screen = pygame.display.set_mode(size) |
| 14 | +pygame.display.set_caption("The Pong Game") |
| 15 | + |
| 16 | +#Starting coordinates of the paddle |
| 17 | +rect_x = 400 |
| 18 | +rect_y = 580 |
| 19 | + |
| 20 | +#initial speed of the paddle |
| 21 | +rect_change_x = 0 |
| 22 | +rect_change_y = 0 |
| 23 | + |
| 24 | +#initial position of the ball |
| 25 | +ball_x = 50 |
| 26 | +ball_y = 50 |
| 27 | + |
| 28 | +#speed of the ball |
| 29 | +ball_change_x = 5 |
| 30 | +ball_change_y = 5 |
| 31 | + |
| 32 | +score = 0 |
| 33 | + |
| 34 | +#draws the paddle. Also restricts its movement between the edges of the window. |
| 35 | + |
| 36 | +def drawrect(screen,x,y): |
| 37 | + if x <= 0: |
| 38 | + x = 0 |
| 39 | + if x >= 699: |
| 40 | + x = 699 |
| 41 | + |
| 42 | + pygame.draw.rect(screen,RED,[x,y,100,20]) |
| 43 | + |
| 44 | +#game's main loop |
| 45 | +done = False |
| 46 | +clock=pygame.time.Clock() |
| 47 | +while not done: |
| 48 | + for event in pygame.event.get(): |
| 49 | + if event.type == pygame.QUIT: |
| 50 | + done = True |
| 51 | + elif event.type == pygame.KEYDOWN: |
| 52 | + if event.key == pygame.K_LEFT: |
| 53 | + rect_change_x = -6 |
| 54 | + elif event.key == pygame.K_RIGHT: |
| 55 | + rect_change_x = 6 |
| 56 | + |
| 57 | + #elif event.key == pygame.K_UP: |
| 58 | + |
| 59 | + #rect_change_y = -6 |
| 60 | + |
| 61 | + #elif event.key == pygame.K_DOWN: |
| 62 | + |
| 63 | + #rect_change_y = 6''' |
| 64 | + |
| 65 | + elif event.type == pygame.KEYUP: |
| 66 | + if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: |
| 67 | + rect_change_x = 0 |
| 68 | + elif event.key == pygame.K_UP or event.key == pygame.K_DOWN: |
| 69 | + rect_change_y = 0 |
| 70 | + |
| 71 | + screen.fill(BLACK) |
| 72 | + rect_x += rect_change_x |
| 73 | + rect_y += rect_change_y |
| 74 | + |
| 75 | + ball_x += ball_change_x |
| 76 | + ball_y += ball_change_y |
| 77 | + |
| 78 | + #this handles the movement of the ball. |
| 79 | + if ball_x<0: |
| 80 | + ball_x=0 |
| 81 | + ball_change_x = ball_change_x * -1 |
| 82 | + elif ball_x>785: |
| 83 | + ball_x=785 |
| 84 | + ball_change_x = ball_change_x * -1 |
| 85 | + elif ball_y<0: |
| 86 | + ball_y=0 |
| 87 | + ball_change_y = ball_change_y * -1 |
| 88 | + elif ball_x>rect_x and ball_x<rect_x+100 and ball_y==565: |
| 89 | + ball_change_y = ball_change_y * -1 |
| 90 | + score = score + 1 |
| 91 | + elif ball_y>600: |
| 92 | + ball_change_y = ball_change_y * -1 |
| 93 | + score = 0 |
| 94 | + |
| 95 | + pygame.draw.rect(screen,WHITE,[ball_x,ball_y,15,15]) |
| 96 | + |
| 97 | + #drawball(screen,ball_x,ball_y) |
| 98 | + drawrect(screen,rect_x,rect_y) |
| 99 | + |
| 100 | + #score board |
| 101 | + font= pygame.font.SysFont('Calibri', 15, False, False) |
| 102 | + text = font.render("Score = " + str(score), True, WHITE) |
| 103 | + screen.blit(text,[600,100]) |
| 104 | + |
| 105 | + pygame.display.flip() |
| 106 | + clock.tick(60) |
| 107 | + |
| 108 | +pygame.quit() |
0 commit comments