Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import time
- import math
- pygame.init()
- pygame.mixer.pre_init(44100, 14, 5, 4096)
- BACKGROUND = (147, 245, 232)
- BACKGROUND2 = (200, 10, 10)
- GRAY = (230, 230, 230)
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- HOVER_RED = (255, 0, 0)
- RED = (150, 0, 0)
- HOVER_GREEN = (0, 255, 0)
- GREEN = (0, 200, 0)
- HOVER_YELLOW = (255, 255, 0)
- YELLOW = (230, 230, 0)
- DISPLAY_WIDTH = 800
- DISPLAY_HEIGHT = 600
- gameDisplay = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
- clock = pygame.time.Clock()
- titleFont = pygame.font.Font("freesansbold.ttf", 80)
- rulesFont = pygame.font.Font("freesansbold.ttf", 30)
- tinyFont = pygame.font.Font("freesansbold.ttf", 20)
- def messageDisplay(text, width, height, textfont):
- textSurf, textRect = textObjects(text, textfont)
- textRect.center = ((width), (height))
- gameDisplay.blit(textSurf, textRect)
- pygame.display.update()
- time.sleep(2)
- def textObjects(text, font):
- textSurface = font.render(text, True, WHITE)
- return textSurface, textSurface.get_rect()
- def firstMessage():
- messageDisplay("Girish's Pong Game", DISPLAY_WIDTH//2, DISPLAY_HEIGHT//4, titleFont)
- def instructions():
- gameDisplay.fill(BLACK)
- messageDisplay("How to play", DISPLAY_WIDTH//2, DISPLAY_HEIGHT//4, rulesFont)
- 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)
- messageDisplay("The goal is to gain more points than your opponent", DISPLAY_WIDTH//2, DISPLAY_HEIGHT//2, tinyFont)
- 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)
- def mainmenu():
- gameDisplay.fill(BLACK)
- menuExit = False
- while (not menuExit):
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- largeText = pygame.font.Font("freesansbold.ttf", 90)
- smallText = pygame.font.Font("freesansbold.ttf", 30)
- textSurf, textRect = textObjects("Main Menu", largeText)
- secondSurf, secondRect = textObjects("While in-game, pres ESC to return to main menu", smallText)
- textRect.center = ((DISPLAY_WIDTH/2), (DISPLAY_HEIGHT/2))
- gameDisplay.blit(textSurf, textRect)
- largeText = pygame.font.Font("freesansbold.ttf", 25)
- cursorclick = pygame.mouse.get_pressed()
- cursorpos = pygame.mouse.get_pos()
- pygame.draw.rect(gameDisplay, GREEN, (400,450,100,50))
- gameDisplay.blit(secondSurf, secondRect)
- if (500 > cursorpos[0] > 400 and 500 > cursorpos[1] > 450):
- pygame.draw.rect(gameDisplay, HOVER_GREEN, (400,450,100,50))
- if cursorclick[0] == 1:
- gameLoop()
- largeText = pygame.font.Font("freesansbold.ttf", 15)
- textSurf, textRect = textObjects("BEGIN", largeText)
- textRect.center = (450, 475)
- gameDisplay.blit(textSurf, textRect)
- pygame.display.update()
- clock.tick(15)
- class Paddle(pygame.Rect): #class for paddle
- def __init__(self, xPosition, colour):
- super(Paddle, self).__init__(xPosition, 300, 10, 100)
- self.colour = colour
- self.yVelocity = 0
- def draw(self, display):
- pygame.draw.rect(display, self.colour, self)
- def update(self):
- while (self.yVelocity != 0):
- if (self.y+self.yVelocity + 100 >= DISPLAY_HEIGHT or self.y + self.yVelocity < 0):
- self.yVelocity = 0
- self.move_ip(0, self.yVelocity)
- if (self.yVelocity > 0):
- self.yVelocity -= 1
- if (self.yVelocity < 0):
- self.yVelocity += 1
- ball = pygame.Rect(250, 250, 8, 8)
- ballXVelocity = 5
- ballYVelocity = 0
- leftPaddle = Paddle(10, WHITE)
- rightPaddle = Paddle(780, WHITE)
- paddles = [leftPaddle, rightPaddle]
- def sign(x):
- try:
- return abs(x)//x
- except ValueError:
- return 0
- keymap = {
- "LeftPaddleUp": pygame.K_w,
- "LeftPaddleDown": pygame.K_s,
- "RightPaddleUp": pygame.K_o,
- "RightPaddleDown": pygame.K_l,
- "Exit" : pygame.K_ESCAPE,
- }
- leftPlayerPoints = 0
- rightPlayerPoints = 0
- def gameLoop():
- global ballXVelocity, ballYVelocity, leftPlayerPoints, rightPlayerPoints
- gameExit = False
- while (not gameExit):
- yVelocity = 0
- for event in pygame.event.get():
- if (event.type == pygame.QUIT):
- pygame.quit()
- quit()
- keys = pygame.key.get_pressed()
- if (keys[keymap["Exit"]]): #if the user clicks escape key, program will go back to the main menu
- gameExit = True
- if (keys[keymap["LeftPaddleUp"]]):
- leftPaddle.yVelocity = -3
- if (keys[keymap["LeftPaddleDown"]]):
- leftPaddle.yVelocity = 3
- if (keys[keymap["RightPaddleUp"]]):
- rightPaddle.yVelocity = -3
- if (keys[keymap["RightPaddleDown"]]):
- rightPaddle.yVelocity = 3
- gameDisplay.fill(BLACK)
- for paddle in paddles:
- paddle.update()
- paddle.draw(gameDisplay)
- if (ball.top + ballYVelocity < 0 or ball.bottom + ballYVelocity > DISPLAY_HEIGHT):
- ballYVelocity *= -1
- ball.move_ip(ballXVelocity, ballYVelocity)
- ball.move_ip(ballXVelocity, ballYVelocity)
- for index, paddle in enumerate(paddles):
- if (ball.right + ballXVelocity >= rightPaddle.left or ball.left+ballXVelocity <= leftPaddle.right):
- if (ball.top > leftPaddle.top and ball.top < leftPaddle.bottom) or (ball.top > rightPaddle.top and ball.top < rightPaddle.bottom):
- angle = math.sin(math.pi*(ball.centery - paddle.centery)/100)
- ballXVelocity = -sign(ballXVelocity) * 5 * math.cos(angle)
- ballYVelocity = sign(ballXVelocity) * 5 * math.sin(angle)
- if (ball.right >= DISPLAY_WIDTH):
- leftPlayerPoints += 1
- time.sleep(1.2)
- ball.left = DISPLAY_WIDTH // 2
- elif (ball.left <= 0):
- rightPlayerPoints += 1
- time.sleep(1)
- ball.left = DISPLAY_WIDTH // 2
- #messageDisplay(str(leftPlayerPoints), 100, 100, tinyFont)
- #messageDisplay(str(rightPlayerPoints), 600, 100, tinyFont)
- pygame.draw.rect(gameDisplay, WHITE, ball)
- pygame.display.update()
- clock.tick(60)
- firstMessage()
- instructions()
- mainmenu()
- gameLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement