Advertisement
Guest User

Untitled

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