Advertisement
Techmo

PONG

Apr 21st, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.09 KB | None | 0 0
  1. import pygame
  2. import sys, time, random
  3. pygame.init()
  4. pygame.mixer.init()
  5. x = 50
  6. y = 50
  7. y1 = 0
  8. y2 = 0
  9. pSpeed = 6
  10. speed = 5
  11. aiSpeed = 5
  12. score = 0
  13. aiScore = 0
  14. windowSize = pygame.display.Info()
  15. f = 1
  16. flag = True
  17. screen = pygame.display.set_mode((windowSize.current_w, windowSize.current_h), pygame.FULLSCREEN)
  18. ballHit = pygame.mixer.Sound("ballHit.wav")
  19. ballBounce =  pygame.mixer.Sound("ballBounce.wav")
  20. myriadProFont = pygame.font.SysFont("Myriad Pro", 48)
  21. directionX = 1
  22. directionY = 1
  23. clock = pygame.time.Clock()
  24. pygame.key.set_repeat(1, 10)
  25. pygame.mouse.set_visible(0)
  26. while flag:
  27.     clock.tick(100)
  28.     scoreCount = myriadProFont.render(str(score), 1, (255, 255, 255), (0, 0, 0))
  29.     aiScoreCount = myriadProFont.render(str(aiScore), 1, (255, 255, 255), (0, 0, 0))
  30.     keystate = pygame.key.get_pressed()
  31.     for event in pygame.event.get():
  32.         if event.type == pygame.QUIT:
  33.             flag = False
  34.     if y2 < y and y2 <= windowSize.current_h - 80:
  35.         y2 += aiSpeed
  36.     if y < y2 and y2 >= 0:
  37.         y2 -= aiSpeed
  38.     if keystate[pygame.K_w] and y1 >= 0:
  39.         y1 -= pSpeed
  40.     if keystate[pygame.K_s] and y1 <= windowSize.current_h - 80:
  41.         y1 += pSpeed
  42.     if keystate[pygame.K_r] and speed == 0:
  43.         x = 50
  44.         y = 50
  45.         y1 = 0
  46.         y2 = 0
  47.         score = 0
  48.         f = 1
  49.         flag = True
  50.         speed = 5
  51.         directionX = 1
  52.         directionY = 1
  53.         aiScore = 0
  54.     if keystate[pygame.K_e] and speed == 0:
  55.         flag = False
  56.     screen.fill((0, 0, 0))
  57.     pygame.draw.rect(screen, (255, 255, 255), (screen.get_size()[0] / 2 - 5/2, 0, 5, windowSize.current_w))
  58.     pygame.draw.rect(screen, (255, 0, 0), (x, y, 10, 10))
  59.     pygame.draw.rect(screen, (255, 255, 255), (0, y1, 10, 80))
  60.     pygame.draw.rect(screen, (255, 255, 255), (windowSize.current_w - 10, y2 - 30, 10, 80))
  61.  
  62.     size = (20, 20)
  63.     x += speed * directionX
  64.     y += speed * directionY
  65.     if x == 10 and  y > y1 and y < y1 + 80:
  66.         directionX *= -1
  67.         score += 1
  68.         ballHit.play()
  69.     if x < 0:
  70.         screen.fill((0, 0, 0))
  71.         finalScore = myriadProFont.render("Final Score: " + str(score), 1, (255, 255, 255), (0, 0, 0))
  72.         retry = myriadProFont.render("Press 'r' to retry", 1, (255, 255, 255), (0, 0, 0))
  73.         end = myriadProFont.render("Press 'e' to exit", 1, (255, 255, 255), (0, 0, 0))
  74.         screen.blit(finalScore, (screen.get_size()[0] / 2 - finalScore.get_size()[0] / 2, 200))
  75.         screen.blit(retry, (screen.get_size()[0] / 2 - retry.get_size()[0] / 2, 250))
  76.         screen.blit(end, (screen.get_size()[0] / 2 - end.get_size()[0] / 2, 300))
  77.         speed = 0
  78.     if x >= windowSize.current_w - 10:
  79.         directionX *= -1
  80.         aiScore += 1
  81.         ballHit.play()
  82.  
  83.     if y + size[1] > windowSize.current_h or y <= 0:
  84.         directionY *= -1
  85.         ballBounce.play()
  86.     screen.blit(scoreCount, (screen.get_size()[0] / 2 - scoreCount.get_size()[0] / 2-20, 0))
  87.     screen.blit(aiScoreCount, (screen.get_size()[0] / 2 - aiScoreCount.get_size()[0] / 2+20, 0))
  88.     pygame.display.update()
  89. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement