Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.34 KB | None | 0 0
  1.  
  2. import pygame
  3. import time
  4. import math
  5. pygame.init()
  6. pygame.mixer.pre_init(44100, 14, 5, 4096)
  7. BACKGROUND = (147, 245, 232)
  8. BACKGROUND2 = (200, 10, 10)
  9. GRAY = (230, 230, 230)
  10. BLACK = (0, 0, 0)
  11. WHITE = (255, 255, 255)
  12. HOVER_RED = (255, 0, 0)
  13. RED = (150, 0, 0)
  14. HOVER_GREEN = (0, 255, 0)
  15. GREEN = (0, 200, 0)
  16. HOVER_YELLOW = (255, 255, 0)
  17. YELLOW = (230, 230, 0)
  18. DISPLAY_WIDTH = 800
  19. DISPLAY_HEIGHT = 600
  20.  
  21. gameDisplay = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
  22. clock = pygame.time.Clock()
  23. titleFont = pygame.font.Font("freesansbold.ttf", 80)
  24. rulesFont = pygame.font.Font("freesansbold.ttf", 30)
  25. tinyFont = pygame.font.Font("freesansbold.ttf", 20)
  26. def messageDisplay(text, width, height, textfont):
  27.         textSurf, textRect = textObjects(text, textfont)
  28.         textRect.center = ((width), (height))
  29.         gameDisplay.blit(textSurf, textRect)
  30.         pygame.display.update()
  31.         time.sleep(2)
  32.  
  33.  
  34. def textObjects(text, font):
  35.         textSurface = font.render(text, True, WHITE)
  36.         return textSurface, textSurface.get_rect()
  37.  
  38. def firstMessage():
  39.         messageDisplay("Girish's Pong Game", DISPLAY_WIDTH//2, DISPLAY_HEIGHT//4, titleFont)
  40.  
  41.  
  42. def instructions():
  43.         gameDisplay.fill(BLACK)
  44.         messageDisplay("How to play", DISPLAY_WIDTH//2, DISPLAY_HEIGHT//4, rulesFont)
  45.         messageDisplay("Left Player uses 'w' and 's' to move up and down, Right Player uses 'o' and 'l' ",  DISPLAY_WIDTH//2, DISPLAY_HEIGHT//3, tinyFont)
  46.         messageDisplay("The goal is to gain more points than your opponent", DISPLAY_WIDTH//2, DISPLAY_HEIGHT//2, tinyFont)
  47.         messageDisplay("Points are awarded when one fails to return the ball to the other, similar to tennis.", DISPLAY_WIDTH//2, DISPLAY_HEIGHT//2+20, tinyFont)
  48. def mainmenu():
  49.     gameDisplay.fill(BLACK)    
  50.     menuExit = False
  51.     while (not menuExit):
  52.         for event in pygame.event.get():
  53.             if event.type == pygame.QUIT:
  54.                 pygame.quit()
  55.                 quit()
  56.         largeText = pygame.font.Font("freesansbold.ttf", 90)
  57.         smallText = pygame.font.Font("freesansbold.ttf", 30)
  58.         textSurf, textRect = textObjects("Main Menu", largeText)
  59.         secondSurf, secondRect = textObjects("While in-game, pres ESC to return to main menu", smallText)
  60.         textRect.center = ((DISPLAY_WIDTH/2), (DISPLAY_HEIGHT/2))
  61.         gameDisplay.blit(textSurf, textRect)
  62.         largeText = pygame.font.Font("freesansbold.ttf", 25)
  63.         cursorclick = pygame.mouse.get_pressed()
  64.         cursorpos = pygame.mouse.get_pos()
  65.         pygame.draw.rect(gameDisplay, GREEN, (400,450,100,50))
  66.         gameDisplay.blit(secondSurf, secondRect)
  67.  
  68.         if (500 > cursorpos[0] > 400 and 500 > cursorpos[1] > 450):
  69.             pygame.draw.rect(gameDisplay, HOVER_GREEN, (400,450,100,50))
  70.             if cursorclick[0] == 1:
  71.                 gameLoop()
  72.      
  73.         largeText = pygame.font.Font("freesansbold.ttf", 15)
  74.         textSurf, textRect = textObjects("BEGIN", largeText)
  75.         textRect.center = (450, 475)
  76.         gameDisplay.blit(textSurf, textRect)  
  77.    
  78.         pygame.display.update()
  79.         clock.tick(15)
  80.  
  81.  
  82. class Paddle(pygame.Rect): #class for paddle
  83.         def __init__(self, xPosition, colour):
  84.                 super(Paddle, self).__init__(xPosition, 300, 10, 100)
  85.                 self.colour = colour           
  86.                 self.yVelocity = 0
  87.  
  88.         def draw(self, display):
  89.                 pygame.draw.rect(display, self.colour, self)
  90.  
  91.         def update(self):
  92.                 while (self.yVelocity != 0):
  93.                         if (self.y+self.yVelocity + 100 >= DISPLAY_HEIGHT or self.y + self.yVelocity < 0):
  94.                                 self.yVelocity = 0
  95.                         self.move_ip(0, self.yVelocity)
  96.                         if (self.yVelocity > 0):
  97.                                 self.yVelocity -= 1
  98.                         if (self.yVelocity < 0):
  99.                                 self.yVelocity += 1
  100.  
  101. ball = pygame.Rect(250, 250, 8, 8)
  102. ballXVelocity = 5
  103. ballYVelocity = 0
  104.  
  105. leftPaddle = Paddle(10, WHITE)
  106. rightPaddle = Paddle(780, WHITE)
  107. paddles = [leftPaddle, rightPaddle]
  108.  
  109.  
  110. def sign(x):
  111.         try:
  112.                 return abs(x)//x
  113.         except ValueError:
  114.                 return 0
  115.  
  116. keymap = {
  117.         "LeftPaddleUp":    pygame.K_w,
  118.         "LeftPaddleDown":  pygame.K_s,
  119.         "RightPaddleUp":   pygame.K_o,
  120.         "RightPaddleDown": pygame.K_l,
  121.         "Exit" : pygame.K_ESCAPE,
  122. }
  123.  
  124.  
  125. leftPlayerPoints = 0
  126. rightPlayerPoints = 0
  127.  
  128. def gameLoop():
  129.         global ballXVelocity, ballYVelocity, leftPlayerPoints, rightPlayerPoints
  130.         gameExit = False
  131.         while (not gameExit):
  132.                 yVelocity = 0
  133.                 for event in pygame.event.get():
  134.                         if (event.type == pygame.QUIT):
  135.                                 pygame.quit()
  136.                                 quit()
  137.                
  138.                 keys = pygame.key.get_pressed()
  139.                 if (keys[keymap["Exit"]]): #if the user clicks escape key, program will go back to the main menu
  140.                         gameExit = True
  141.  
  142.                 if (keys[keymap["LeftPaddleUp"]]):
  143.                         leftPaddle.yVelocity = -3
  144.                 if (keys[keymap["LeftPaddleDown"]]):
  145.                         leftPaddle.yVelocity = 3
  146.                 if (keys[keymap["RightPaddleUp"]]):
  147.                         rightPaddle.yVelocity = -3
  148.                 if (keys[keymap["RightPaddleDown"]]):
  149.                         rightPaddle.yVelocity = 3
  150.                
  151.                 gameDisplay.fill(BLACK)
  152.  
  153.                 for paddle in paddles:
  154.                         paddle.update()
  155.                         paddle.draw(gameDisplay)
  156.                 if (ball.top + ballYVelocity < 0 or ball.bottom + ballYVelocity > DISPLAY_HEIGHT):
  157.                         ballYVelocity *= -1
  158.                 ball.move_ip(ballXVelocity, ballYVelocity)
  159.                
  160.                 ball.move_ip(ballXVelocity, ballYVelocity)
  161.                 for index, paddle in enumerate(paddles):
  162.                         if (ball.right + ballXVelocity >= rightPaddle.left or ball.left+ballXVelocity <= leftPaddle.right):
  163.                                 if (ball.top > leftPaddle.top and ball.top < leftPaddle.bottom) or (ball.top > rightPaddle.top and ball.top < rightPaddle.bottom):
  164.                                         angle = math.sin(math.pi*(ball.centery - paddle.centery)/100)
  165.                                         ballXVelocity = -sign(ballXVelocity) * 5 * math.cos(angle)
  166.                                         ballYVelocity = sign(ballXVelocity) * 5 * math.sin(angle)
  167.  
  168.                 if (ball.right >= DISPLAY_WIDTH):
  169.                         leftPlayerPoints += 1
  170.                         time.sleep(1.2)
  171.                         ball.left = DISPLAY_WIDTH // 2
  172.                 elif (ball.left <= 0):
  173.                         rightPlayerPoints += 1
  174.                         time.sleep(1)
  175.                         ball.left = DISPLAY_WIDTH // 2
  176.                 #messageDisplay(str(leftPlayerPoints), 100, 100, tinyFont)
  177.                 #messageDisplay(str(rightPlayerPoints), 600, 100, tinyFont)
  178.                 pygame.draw.rect(gameDisplay, WHITE, ball)                
  179.                 pygame.display.update()
  180.                
  181.                 clock.tick(60)
  182. firstMessage()
  183. instructions()
  184. mainmenu()
  185. gameLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement