Ejemplo n.º 1
0
class Player:
    def __init__(self, no, name):
        self.no = no
        self.name = name
        self.score = 0
        self.createPaddle()
        self.setKeys()
        self.setScorepos()

    def setScorepos(self):
        self.namepos = (30, 0) if self.no == 1 else (WIDTH - 100, 0)
        self.scorepos = (30, 50) if self.no == 1 else (WIDTH - 100, 50)

    def createPaddle(self):
        self.paddle = Paddle(10) if self.no == 1 else Paddle(WIDTH - 2 * 10)

    def setKeys(self):
        self.upKey = pygame.K_w if self.no == 1 else pygame.K_UP
        self.downKey = pygame.K_s if self.no == 1 else pygame.K_DOWN

    def movePaddle(self):
        keys = pygame.key.get_pressed()
        self.paddle.moveUp() if keys[self.upKey] else ''
        self.paddle.moveDown() if keys[self.downKey] else ''

    def usePaddle(self, ball):
        self.movePaddle()
        self.paddle.show()
        self.paddle.checkforBall(ball)
        self.paddle.checkforEdges()

    def showName(self):
        nameLabel = gameFont.render(str(self.name), True, WHITE, BLACK)
        SCREEN.blit(nameLabel, self.namepos)

    def showScore(self):
        scoreLabel = gameFont.render(str(self.score), True, WHITE, BLACK)
        SCREEN.blit(scoreLabel, self.scorepos)
Ejemplo n.º 2
0
    # --- Main event loop
    for event in pygame.event.get():  # User did something
        if event.type == pygame.QUIT:  # If user clicked close
            carryOn = False  # Flag that we are done so we exit this loop
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:  # Pressing the x Key will quit the game
                carryOn = False
        elif scoreA == 15 or scoreB == 15:
            carryOn = False

    # Moving the paddles when the use uses the arrow keys (player A) or "W/S" keys (player B)
    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]:
        paddleA.moveUp(5)
    if keys[pygame.K_s]:
        paddleA.moveDown(5)
    if keys[pygame.K_p]:
        paddleB.moveUp(5)
    if keys[pygame.K_l]:
        paddleB.moveDown(5)
    if keys[pygame.K_BACKSPACE]:
        paused()
        # --- Game logic should go here
    all_sprites_list.update()

    # Check if the ball is bouncing against any of the 4 walls:
    if ball.rect.x >= 690:
        scoreA += 1
        ball.velocity[0] = -ball.velocity[0]
        ball.velocity[1] = 0
        ball.scored()
Ejemplo n.º 3
0
def gameloop(isHumanPlaying, isComputerPlaying):

    height = 100
    PaddleA = Paddle(WHITE, 10, height)
    PaddleB = Paddle(WHITE, 10, height)

    PaddleA.rect.x = 20
    PaddleA.rect.y = 200

    PaddleB.rect.x = 670
    PaddleB.rect.y = 200

    ball = Ball(WHITE, 10, 10)
    ball.rect.x = 345
    ball.rect.y = 195

    all_sprites_list = pygame.sprite.Group()
    all_sprites_list.add(PaddleA)
    all_sprites_list.add(PaddleB)
    all_sprites_list.add(ball)

    scoreA = 0
    scoreB = 0
    multiplierA = 0
    multiplierB = 0

    carryOn = True

    #---- MAIN PROGRAM LOOP ----
    while carryOn:
        frame_count = 0
        # ---- MAIN EVENT LOOP ----
        for event in pygame.event.get():  # User did something
            if event.type == pygame.QUIT:  # If user clicked close
                carryOn = False  # Exit the loop

        # ---- GAME LOGIC ------------------
        keys = pygame.key.get_pressed()

        if keys[pygame.K_w]:
            PaddleA.moveUp(5)
        if keys[pygame.K_s]:
            PaddleA.moveDown(5)

        if keys[pygame.K_UP]:
            if (isHumanPlaying):
                PaddleB.moveUp(5)
        if keys[pygame.K_DOWN]:
            if (isHumanPlaying):
                PaddleB.moveDown(5)

        if (isComputerPlaying):
            if (ball.rect.y > PaddleB.rect.y + 1 / 2 * height):
                PaddleB.moveDown(rand.randint(0, 7))
            if (ball.rect.y < PaddleB.rect.y + 1 / 2 * height):
                PaddleB.moveUp(rand.randint(0, 7))

        all_sprites_list.update()

        if ball.rect.x >= 690:
            multiplierA += 1
            multiplierB = 0

            if (multiplierA <= 3):
                scoreA += 1
            elif (5 > multiplierA > 3):
                scoreA += 2
            else:
                scoreA += 5

            ball.reset()
            ball.velocity[0] = -ball.velocity[0]

        if ball.rect.x <= 0:
            multiplierB += 1
            multiplierA = 0

            if (multiplierB <= 3):
                scoreB += 1
            elif (5 > multiplierB > 3):
                scoreB += 2
            else:
                scoreB += 5

            ball.reset()
            ball.velocity[0] = -ball.velocity[0]
        if ball.rect.y > 490:
            ball.velocity[1] = -ball.velocity[1]
        if ball.rect.y < 0:
            ball.velocity[1] = -ball.velocity[1]

        if pygame.sprite.collide_mask(
                ball, PaddleA) or pygame.sprite.collide_mask(ball, PaddleB):
            ball.bounce()

        # ---- DRAWING THE CODE ------------
        screen.fill(BLACK)
        pygame.draw.line(screen, WHITE, [349, 0], [349, 500], 5)
        all_sprites_list.draw(screen)

        if (multiplierA <= 3):
            colorA = WHITE
        elif (5 > multiplierA > 3):
            colorA = YELLOW
        elif (multiplierA >= 5):
            colorA = RED

        if (multiplierB <= 3):
            colorB = WHITE
        elif (5 > multiplierB > 3):
            colorB = YELLOW
        elif (multiplierB >= 5):
            colorB = RED

        font = pygame.font.Font(None, 74)
        text = font.render(str(scoreA), 1, colorA)
        screen.blit(text, (250, 10))
        text = font.render(str(scoreB), 1, colorB)
        screen.blit(text, (420, 10))

        # Update the screen with what we have drawn
        pygame.display.flip()

        if (frame_count == 60):
            frame_count = 0
        else:
            frame_count += 1

        # Limit to 60 frames per second
        clock.tick(60)
    pygame.quit()
Ejemplo n.º 4
0
# -------- Main Program Loop --------
while carryOn:
    # ----- Main event Loop -----
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            carryOn = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_x:
                carryOn = False

        keys = pygame.key.get_pressed()
        if keys[pygame.K_w]:
            paddleA.moveUp(30)
        if keys[pygame.K_s]:
            paddleA.moveDown(30)
        if keys[pygame.K_UP]:
            paddleB.moveUp(30)
        if keys[pygame.K_DOWN]:
            paddleB.moveDown(30)
    # ----- Game Logic -----

    all_sprites_list.update()

    # -- Check if the ball hits any of the sides of the screen
    if ball.rect.x >= 690:
        scoreA += 1
        ball.velocity[0] = -ball.velocity[0]
    if ball.rect.x <= 0:
        scoreB += 1
        ball.velocity[0] = -ball.velocity[0]
Ejemplo n.º 5
0
def game_loop():
    paddleA = Paddle(constants.BLACK, 10, 100)
    paddleA.rect.x = 20 
    paddleA.rect.y = 200 

    paddleB = Paddle(constants.BLACK, 10, 100)
    paddleB.rect.x = 670 
    paddleB.rect.y = 200 

    ball = Ball(constants.BLACK,10,10)
    ball.rect.x = 345
    ball.rect.y = 195

    all_sprites_list = pygame.sprite.Group()

    all_sprites_list.add(paddleA)
    all_sprites_list.add(paddleB)
    all_sprites_list.add(ball)

    carryOn = True

    scoreA = 0 
    scoreB = 0 

    while carryOn:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                carryOn = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_x: 
                    carryOn=False 
            if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                while True:
                    event = pygame.event.wait()
                    if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                        break

        keys = pygame.key.get_pressed()
        if keys[pygame.K_w]:
            paddleA.moveUp(constants.MOVE_SPEED)
        if keys[pygame.K_s]:
            paddleA.moveDown(constants.MOVE_SPEED)
        if keys[pygame.K_UP]:
            paddleB.moveUp(constants.MOVE_SPEED)
        if keys[pygame.K_DOWN]:
            paddleB.moveDown(constants.MOVE_SPEED)

        all_sprites_list.update()

        if ball.rect.x>=690:
            scoreA+=1
            HelperFunction.playSFX(constants.BOOP)
            ball.rect.x = 345
            ball.rect.y = 195
            ball.velocity[0] = -ball.velocity[0]
        if ball.rect.x<=0:
            HelperFunction.playSFX(constants.BOOP)
            scoreB+=1
            ball.rect.x = 345
            ball.rect.y = 195
            ball.velocity[0] = -ball.velocity[0]
        if ball.rect.y>490:
            ball.velocity[1] = -ball.velocity[1]
        if ball.rect.y<0:
            ball.velocity[1] = -ball.velocity[1]

        if pygame.sprite.collide_mask(ball, paddleA) or pygame.sprite.collide_mask(ball, paddleB):
            ball.bounce()

        constants.screen.blit(game_background, (0,0))

        pygame.draw.line(constants.screen, constants.BLACK, [349, 0], [349,500], 5)

        all_sprites_list.draw(constants.screen)

        font = pygame.font.Font(None, 74)
        text = font.render(str(scoreA), 1, constants.BLACK)
        constants.screen.blit(text, (250,10))
        text = font.render(str(scoreB), 1, constants.BLACK)
        constants.screen.blit(text,(420,10))

        pygame.display.flip()

        clock.tick(30)