Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # importing stuff
- import sys, pygame
- from pygame.locals import *
- # setup mixer to remove audio lag
- pygame.mixer.pre_init(44100, -16, 2, 2048)
- # starting pygame
- pygame.init()
- # defining basic colours
- white = (255, 255, 255)
- black = (0, 0, 0)
- red = (255, 0, 0)
- # set up the clock
- clock = pygame.time.Clock()
- # text and such
- tFont = pygame.font.SysFont("monospace", 15)
- # setting window res and setting display
- winX, winY = 600, 300
- window = pygame.display.set_mode((winX, winY))
- # setting the speed for on screen stuff
- playSpeed = 5 # player speed (p1 and p2)
- # counts points for each player
- # 1 2
- points = [0, 0]
- lastScored = 'playerOne'
- # tallies number of times FPS is counted and added to toal amount
- fpsCount = 0
- fpsTotal = 0
- class Ball(object):
- def __init__(self, speed, x, y):
- # set default ball position in screen centre
- self.ballX = x
- self.ballY = y
- self.ballSpeed = speed
- # setup default rect around ball
- self.rect = pygame.Rect(self.ballX - 3, self.ballY - 3, 6, 6)
- def move(self):
- if lastScored == 'playerOne': # if p1 scored last
- self.ballX -= self.ballSpeed # ball goes to p1's side
- elif lastScored == 'playerTwo': # ball goes to p2's side
- self.ballX += self.ballSpeed
- # draw ball in new position
- pygame.draw.circle(window, black, (self.ballX, self.ballY), 3)
- # draw rectangle in new position
- self.rect = pygame.Rect(self.ballX - 3, self.ballY - 3, 6, 6)
- pygame.draw.rect(window, red, self.rect, 1)
- class Paddle(object):
- # set the player number (1/2) and if it's an AI or real player
- def __init__(self, player, aiornot):
- if player == 1 and aiornot == False:
- self.coords = [[40, 130], [40, 160]]
- elif player == 2 and aiornot == False:
- self.coords = [[560, 130], [560, 160]]
- self.isAI = aiornot
- self.movement = 'stop' # sets default movement
- # setup default rect around paddle
- self.rect = pygame.Rect(self.coords[0][0] - 5, self.coords[0][1], 11, 31)
- def move(self):
- if self.movement == 'down' and self.coords[1][1] < 300:
- self.moveDown()
- elif self.movement == 'up' and self.coords[0][1] > 0:
- self.moveUp()
- elif self.movement == 'stop':
- self.stop()
- # draw the paddle in new position
- pygame.draw.line(window, black, self.coords[0], self.coords[1], 10)
- # draw the rectangle around paddle in new position
- self.rect = pygame.Rect(self.coords[0][0] - 5, self.coords[0][1], 11, 31)
- pygame.draw.rect(window, red, self.rect, 1)
- # movement functions, for direction and such
- def moveDown(self):
- self.coords[0][1] += playSpeed
- self.coords[1][1] += playSpeed
- def moveUp(self):
- self.coords[0][1] -= playSpeed
- self.coords[1][1] -= playSpeed
- def stop(self):
- self.coords[0][1] = self.coords[0][1]
- self.coords[1][1] = self.coords[1][1]
- ball = Ball(playSpeed, winX / 2, winY / 2)
- playerOne = Paddle(1, False)
- playerTwo = Paddle(2, False)
- while True: # main loop
- # event handling for exit
- for event in pygame.event.get():
- if event.type == QUIT:
- # print the average FPS
- print round(fpsTotal / fpsCount, 2), "fps (avg)"
- pygame.quit()
- sys.exit()
- # setting direction upon arrow key press
- elif event.type == KEYDOWN:
- # player 1 controls if not AI
- if event.key == K_DOWN:
- playerOne.movement = 'down'
- elif event.key == K_UP:
- playerOne.movement = 'up'
- # player 2 controls if not AI
- elif event.key == K_s and playerTwo.isAI == False:
- playerTwo.movement = 'down'
- elif event.key == K_w and playerTwo.isAI == False:
- playerTwo.movement = 'up'
- # when the key is released, stop moving
- elif event.type == KEYUP:
- if event.key == K_DOWN or event.key == K_UP:
- playerOne.movement = 'stop'
- elif event.key == K_s or event.key == K_w:
- playerTwo.movement = 'stop'
- print "player1:", playerOne.coords
- print "player2:", playerTwo.coords
- # if the balls rect collides with the paddles rect
- # then reverse direction at the same speed by generating
- # a new ball in the same position, but travelling the
- # opposite direction
- if ball.rect.colliderect(playerOne.rect):
- ball = Ball(-ball.ballSpeed, ball.ballX, ball.ballY)
- elif ball.rect.colliderect(playerTwo.rect):
- ball = Ball(-ball.ballSpeed, ball.ballX, ball.ballY)
- print ball.ballX, ball.ballY
- # fill the window
- window.fill(white)
- # redraw the bat with new position
- playerOne.move()
- playerTwo.move()
- # redraw the ball with new position
- ball.move()
- # set FPS to 65 (balances out at around 60)
- clock.tick(65)
- # for working out average FPS
- fpsCount += 1
- fpsTotal += clock.get_fps()
- # set window title
- pygame.display.set_caption("Long Pong")
- # render FPS to text, display text
- text = tFont.render(str(round(clock.get_fps(), 2)), 8, black)
- window.blit(text, (545, 5))
- # update display
- pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement