Advertisement
Guest User

Peng Pong

a guest
Aug 12th, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.81 KB | None | 0 0
  1. # importing stuff
  2. import sys, pygame
  3. from pygame.locals import *
  4.  
  5. # setup mixer to remove audio lag
  6. pygame.mixer.pre_init(44100, -16, 2, 2048)
  7. # starting pygame
  8. pygame.init()
  9.  
  10. # defining basic colours
  11. white = (255, 255, 255)
  12. black = (0, 0, 0)
  13. red = (255, 0, 0)
  14.  
  15. # set up the clock
  16. clock = pygame.time.Clock()
  17.  
  18. # text and such
  19. tFont = pygame.font.SysFont("monospace", 15)
  20.  
  21. # setting window res and setting display
  22. winX, winY = 600, 300
  23. window = pygame.display.set_mode((winX, winY))
  24.  
  25. # setting the speed for on screen stuff
  26. playSpeed = 5 # player speed (p1 and p2)
  27.  
  28. # counts points for each player
  29. #         1   2
  30. points = [0, 0]
  31. lastScored = 'playerOne'
  32.  
  33. # tallies number of times FPS is counted and added to toal amount
  34. fpsCount = 0
  35. fpsTotal = 0
  36.  
  37. class Ball(object):
  38.     def __init__(self, speed, x, y):
  39.         # set default ball position in screen centre
  40.         self.ballX = x
  41.         self.ballY = y
  42.         self.ballSpeed = speed
  43.  
  44.         # setup default rect around ball
  45.         self.rect = pygame.Rect(self.ballX - 3, self.ballY - 3, 6, 6)
  46.  
  47.     def move(self):
  48.         if lastScored == 'playerOne': # if p1 scored last
  49.             self.ballX -= self.ballSpeed # ball goes to p1's side
  50.         elif lastScored == 'playerTwo': # ball goes to p2's side
  51.             self.ballX += self.ballSpeed
  52.  
  53.         # draw ball in new position
  54.         pygame.draw.circle(window, black, (self.ballX, self.ballY), 3)
  55.         # draw rectangle in new position
  56.         self.rect = pygame.Rect(self.ballX - 3, self.ballY - 3, 6, 6)
  57.         pygame.draw.rect(window, red, self.rect, 1)
  58.  
  59. class Paddle(object):
  60.     # set the player number (1/2) and if it's an AI or real player
  61.     def __init__(self, player, aiornot):
  62.         if player == 1 and aiornot == False:
  63.             self.coords = [[40, 130], [40, 160]]
  64.         elif player == 2 and aiornot == False:
  65.             self.coords = [[560, 130], [560, 160]]
  66.  
  67.         self.isAI = aiornot
  68.         self.movement = 'stop' # sets default movement
  69.         # setup default rect around paddle
  70.         self.rect = pygame.Rect(self.coords[0][0] - 5, self.coords[0][1], 11, 31)
  71.  
  72.     def move(self):
  73.         if self.movement == 'down' and self.coords[1][1] < 300:
  74.             self.moveDown()
  75.  
  76.         elif self.movement == 'up' and self.coords[0][1] > 0:
  77.             self.moveUp()
  78.  
  79.         elif self.movement == 'stop':
  80.             self.stop()
  81.  
  82.         # draw the paddle in new position
  83.         pygame.draw.line(window, black, self.coords[0], self.coords[1], 10)
  84.         # draw the rectangle around paddle in new position
  85.         self.rect = pygame.Rect(self.coords[0][0] - 5, self.coords[0][1], 11, 31)
  86.         pygame.draw.rect(window, red, self.rect, 1)
  87.  
  88.     # movement functions, for direction and such
  89.     def moveDown(self):
  90.         self.coords[0][1] += playSpeed
  91.         self.coords[1][1] += playSpeed
  92.  
  93.     def moveUp(self):
  94.         self.coords[0][1] -= playSpeed
  95.         self.coords[1][1] -= playSpeed
  96.  
  97.     def stop(self):
  98.         self.coords[0][1] = self.coords[0][1]
  99.         self.coords[1][1] = self.coords[1][1]
  100.  
  101. ball = Ball(playSpeed, winX / 2, winY / 2)
  102. playerOne = Paddle(1, False)
  103. playerTwo = Paddle(2, False)
  104.  
  105.  
  106. while True: # main loop
  107.     # event handling for exit
  108.     for event in pygame.event.get():
  109.         if event.type == QUIT:
  110.             # print the average FPS
  111.             print round(fpsTotal / fpsCount, 2), "fps (avg)"
  112.             pygame.quit()
  113.             sys.exit()
  114.         # setting direction upon arrow key press
  115.         elif event.type == KEYDOWN:
  116.             # player 1 controls if not AI
  117.             if event.key == K_DOWN:
  118.                 playerOne.movement = 'down'
  119.             elif event.key == K_UP:
  120.                 playerOne.movement = 'up'
  121.             # player 2 controls if not AI
  122.             elif event.key == K_s and playerTwo.isAI == False:
  123.                 playerTwo.movement = 'down'
  124.             elif event.key == K_w and playerTwo.isAI == False:
  125.                 playerTwo.movement = 'up'
  126.         # when the key is released, stop moving
  127.         elif event.type == KEYUP:
  128.             if event.key == K_DOWN or event.key == K_UP:
  129.                 playerOne.movement = 'stop'
  130.             elif event.key == K_s or event.key == K_w:
  131.                 playerTwo.movement = 'stop'
  132.             print "player1:", playerOne.coords
  133.             print "player2:", playerTwo.coords
  134.  
  135.     # if the balls rect collides with the paddles rect
  136.     # then reverse direction at the same speed by generating
  137.     # a new ball in the same position, but travelling the
  138.     # opposite direction
  139.     if ball.rect.colliderect(playerOne.rect):
  140.             ball = Ball(-ball.ballSpeed, ball.ballX, ball.ballY)
  141.            
  142.     elif ball.rect.colliderect(playerTwo.rect):
  143.             ball = Ball(-ball.ballSpeed, ball.ballX, ball.ballY)
  144.  
  145.     print ball.ballX, ball.ballY
  146.            
  147.     # fill the window
  148.     window.fill(white)
  149.     # redraw the bat with new position
  150.     playerOne.move()
  151.     playerTwo.move()
  152.     # redraw the ball with new position
  153.     ball.move()
  154.     # set FPS to 65 (balances out at around 60)
  155.     clock.tick(65)
  156.     # for working out average FPS
  157.     fpsCount += 1
  158.     fpsTotal += clock.get_fps()
  159.     # set window title
  160.     pygame.display.set_caption("Long Pong")
  161.     # render FPS to text, display text
  162.     text = tFont.render(str(round(clock.get_fps(), 2)), 8, black)
  163.     window.blit(text, (545, 5))
  164.     # update display
  165.     pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement