Пример #1
0
    def testPaddleMoveDown(self):
        print('Describe: test Paddle Class Movement Down')
        x = 27
        i = 0
        p = Paddle(x)
        while i < 200:
            p.down()
            i += 1

        #move up
        self.assertGreater(p.rect.y, 160, 'y loc is less than 160')
        self.assertGreater(p.rect.y, 200, 'y loc is less than 200')
        self.assertEqual(p.rect.y, 390, 'y loc is screenHeight - p.height')
Пример #2
0
clock = pygame.time.Clock()  # Used to adjust screen updates

while is_running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            is_running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_x:
                is_running = False

    # --- User Input ----
    keys = pygame.key.get_pressed()
    if keys[pygame.K_w] and paddle_one.rect.y > 0:
        paddle_one.up(5)
    if keys[pygame.K_s] and paddle_one.rect.y < window_height - paddle_height:
        paddle_one.down(5)
    if keys[pygame.K_UP] and paddle_two.rect.y > 0:
        paddle_two.up(5)
    if keys[pygame.
            K_DOWN] and paddle_two.rect.y < window_height - paddle_height:
        paddle_two.down(5)

    # --- Game Logic ----
    sprite_list.update()

    # Ball Collision
    if ball.rect.x >= window_width - 10:
        score_player_one += 1
        ball.velocity[0] = -ball.velocity[0]
    if ball.rect.x <= 0:
        score_player_two += 1
Пример #3
0
sprite_list.add(bot)
sprite_list.add(ball)

#game loop
playing = True
while playing:
    #event listener
    for event in pygame.event.get():  #pygame window events
        if event.type == pygame.QUIT:
            playing = False

    kb = pygame.key.get_pressed()
    if kb[pygame.K_UP]:
        player.up()
    if kb[pygame.K_DOWN]:
        player.down()

    #game logic
    sprite_list.update()
    #timeee
    # timeFunc("Ball.move()", ball.move)
    # timeFunc("Ball.hitPlayer(player)", ball.hitPlayer, player)
    # timeFunc("Score.playerScored()", playerScore.playerScored, ball, botScore)

    ball.move()
    ball.hitPlayer(player)
    ball.hitBot(bot)
    bot.moveBot(ball)
    playerScore.playerScored(ball, botScore)
    botScore.botScored(ball, playerScore)
Пример #4
0
            BALL_DOWN = False
            BALL_LEFT = False
        else:
            BALL_UP = True
            BALL_LEFT = True
            BALL_DOWN = False
            BALL_RIGHT = False
        time.sleep(0.1)

    else:
        if playerTwo.tail.ycor() <= -520:
            playerTwo.up()
            AI_UP = True
            AI_DOWN = False
        elif playerTwo.head.ycor() >= 520:
            playerTwo.down()
            AI_DOWN = True
            AI_UP = False
        else:
            if AI_UP:
                playerTwo.up()
            else:
                playerTwo.down()

        if ball.ycor() <= -520:
            if ball.xcor() < 0:
                BALL_LEFT = True
                BALL_RIGHT = False
                ball.moveUpLeft()
            elif ball.xcor() > 0:
                BALL_RIGHT = True
Пример #5
0
    # if right_paddle.ycor() > -250

    # Detect collision with top and bottom wall
    if ball.ycor() > 290 or ball.ycor() < -290:
        ball.bounce_y()

    # Detect collision with paddles
    if ball.distance(right_paddle) <= 50 and ball.xcor() >= 330 or \
            ball.distance(left_paddle) <= 50 and ball.xcor() <= -330:
        ball.bounce_x()
        ball.increase_speed()

    # If the ball is missed, then increase the score
    if ball.xcor() > 410:
        ball.reset_game()
        scoreboard.increase_score_left()
        ball.xmove = 10
    if ball.xcor() < -410:
        ball.reset_game()
        scoreboard.increase_score_right()
        ball.xmove = 10

    # AI Player
    if game_mode == "a":
        if right_paddle.ycor() < ball.ycor():
            right_paddle.up()
        if right_paddle.ycor() > ball.ycor():
            right_paddle.down()

screen.exitonclick()
Пример #6
0
def pong():
    #INIT
    pygame.init()

    paddleSound = pygame.mixer.Sound(
        'C:\\Users\\cqwhi\\PycharmProjects\\Pong\\venv\\pongblip.wav')
    # pongMusic = pygame.mixer.music.load('C:\\Users\\cqwhi\\PycharmProjects\\Pong\\venv\\ambient-piano-mix.wav')

    clock = pygame.time.Clock()

    menuState = True
    winState = False
    aiState = False

    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    RED = (199, 0, 57)
    BLUE = (44, 150, 239)

    screenWidth = 700
    screenHeight = 500

    surf = pygame.display.set_mode((screenWidth, screenHeight))
    pygame.display.set_caption("PyPong!")

    player1 = Paddle(RED, 10, 100)
    player1.rect.x = 20
    player1.rect.y = 200

    player2 = Paddle(BLUE, 10, 100)
    player2.rect.x = 670
    player2.rect.y = 200

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

    spiritGroup = pygame.sprite.Group()
    spiritGroup.add(player1)
    spiritGroup.add(player2)
    spiritGroup.add(ball)

    p1Score = 0
    p2Score = 0

    done = False
    #Game Loop
    while not done:
        #Event loop
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
        if menuState == True:
            keys = pygame.key.get_pressed()
            if (keys[pygame.K_e]):
                menuState = False
            if (keys[pygame.K_ESCAPE]):
                done = True
            if (keys[pygame.K_f]):
                menuState = False
                aiState = True
            surf.fill((BLACK))
            font = pygame.font.Font(None, 50)
            text0 = font.render("by Connor White", 10, WHITE)
            surf.blit(text0, (375, 450))
            text = font.render("Press E to play PyPong! 2 Player", 10, WHITE)
            surf.blit(text, (75, 150))
            text2 = font.render("Press F to play PyPong! 1 Player", 10, WHITE)
            surf.blit(text2, (75, 250))
            pygame.display.update()
        elif winState == True:
            keys = pygame.key.get_pressed()
            if (keys[pygame.K_e]):
                winState = False
            if (keys[pygame.K_ESCAPE]):
                done = True
            surf.fill((BLACK))
            font = pygame.font.Font(None, 50)
            winText = font.render(winner + " Wins!", 10, WHITE)
            surf.blit(winText, (150, 100))
            text = font.render("Press E to play again!", 10, WHITE)
            surf.blit(text, (150, 200))
            #reset the board
            p1Score = 0
            p2Score = 0
            player1.rect.x = 20
            player1.rect.y = 200
            player2.rect.x = 670
            player2.rect.y = 200
            pygame.display.update()
        else:
            keys = pygame.key.get_pressed()
            if (keys[pygame.K_w]):
                player1.up(5)
            if (keys[pygame.K_s]):
                player1.down(5)
            if (keys[pygame.K_UP] and aiState == False):
                player2.up(5)
            if (keys[pygame.K_DOWN] and aiState == False):
                player2.down(5)
            if (keys[pygame.K_ESCAPE]):
                done = True

            if aiState == True:
                if (ball.rect.y < player2.rect.y):
                    player2.up(3)
                elif (ball.rect.y > player2.rect.y):
                    player2.down(3)

            #Game logic/update
            spiritGroup.update()

            if ball.rect.x >= 690:
                p1Score += 1
                ball.rect.x = 345
                ball.rect.y = 195
                ball.velocity[0] = -ball.velocity[0]
            if ball.rect.x <= 0:
                p2Score += 1
                ball.rect.x = 345
                ball.rect.y = 195
                ball.velocity[0] = -ball.velocity[0]
            if ball.rect.y > screenHeight:
                paddleSound.play()
                ball.velocity[1] = -ball.velocity[1]
            if ball.rect.y < 0:
                paddleSound.play()
                ball.velocity[1] = -ball.velocity[1]

            if pygame.sprite.collide_mask(
                    ball, player1) or pygame.sprite.collide_mask(
                        ball, player2):
                paddleSound.play()
                ball.bounce()
                #Game Draw
            surf.fill((BLACK))
            pygame.draw.line(surf, WHITE, [349, 0], [349, 500], 5)
            spiritGroup.draw(surf)

            font = pygame.font.Font(None, 74)
            text = font.render(str(p1Score), 1, WHITE)
            surf.blit(text, (250, 10))
            text = font.render(str(p2Score), 1, WHITE)
            surf.blit(text, (420, 10))

            if (p1Score >= 2):
                winState = True
                winner = "Player 1"
            elif (p2Score >= 2):
                winState = True
                winner = "Player 2"

            pygame.display.update()

            clock.tick(30)
    pygame.quit()
Пример #7
0
screen = pygame.display.set_mode(SCREEN_SIZE)
clock = pygame.time.Clock(
)  # The clock will be used to control how fast the screen updates

# Runthe main loop of the game until window is closed
running = True

while running:
    screen.fill(BACKGROUND_COLOUR)
    pressed_keys = pygame.key.get_pressed()

    # Move the paddles
    if pressed_keys[K_w]:
        paddle_A.up(PADDLE_SPEED)
    if pressed_keys[K_s]:
        paddle_A.down(PADDLE_SPEED)
    if pressed_keys[K_UP]:
        paddle_B.up(PADDLE_SPEED)
    if pressed_keys[K_DOWN]:
        paddle_B.down(PADDLE_SPEED)

    if pressed_keys[K_v]:
        reset_ball()

    # Move the ball
    ball.rect.x += ball.velocity[0]
    ball.rect.y += ball.velocity[1]

    # Bounce the ball
    # Change y-direction if ball reaches top or bottom of screen
    if ball.rect.y <= 0 or ball.rect.y + (2 * BALL_SIZE) >= SCREEN_SIZE[1]:
Пример #8
0
l_score = Score((-100, 200))
r_score = Score((100, 200))

# Listeners
screen.listen()
set_key_binds()

# Loop principal
running = True
while running:
    time.sleep(0.01)
    # Revisar estado de teclas presionadas y responder adecuadamente
    if keys_pressed["w"]:
        l_paddle.up()
    if keys_pressed["s"]:
        l_paddle.down()
    if keys_pressed["Up"]:
        r_paddle.up()
    if keys_pressed["Down"]:
        r_paddle.down()
    screen.update()
    ball.move()

    # Detección de colisión con las paredes
    if ball.ycor() > 290 or ball.ycor() < -290:
        ball.bounce_y()

    # Detección de colisión con las paletas
    if (ball.distance(r_paddle) < 55 and ball.xcor() > 330) or (ball.distance(l_paddle) < 55 and ball.xcor() < -330):
        ball.bounce_x()
Пример #9
0
                                             )

        print("Found {0} faces!".format(len(faces)))
        if len(faces) > 0:
            error.face()
            (x, y, w, h) = faces[0]

            print(y + h / 2)

            is_up = 480 / 4

            if (y + h / 2 < 480 / 3):
                paddle_1.up()
                print("up")
            elif (y + h / 2 > 480 * 2 / 3):
                paddle_1.down()
                print("down")
        else:
            error.noface()

        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

        pygame.time.delay(5)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
Пример #10
0
Файл: main.py Проект: sjk39/pong
while game_on:
    sc.update()
    #time.sleep(0.001)

    if ball.distance(paddle_one.pos()) < 40 or ball.distance(
            paddle_two.pos()) < 40:
        ball.move(1)
        #prevents ball getting 'stuck' to paddle
        ball.move(0)
    else:
        ball.move(0)
    #if ball y cor different to comp y cor, move to coords
    if ball.ycor() > paddle_two.ycor() and ball.xcor() > 200:
        paddle_two.up()
    elif ball.ycor() < paddle_two.ycor() and ball.xcor() > 200:
        paddle_two.down()
    #if ball goes out left add one to comp score and replace ball and vice versa
    if ball.xcor() < -400:
        scoreboard.increase_score_two()
        ball.color("black")
        ball.__init__()
    elif ball.xcor() > 400:
        scoreboard.increase_score_one()
        ball.color("black")
        ball.__init__()
    #if a player exceeds score limit end game
    if scoreboard.score_one > 9 or scoreboard.score_two > 9:
        scoreboard.game_over()
        game_on = False

sc.exitonclick()
Пример #11
0
                    FUN = False
                    print("No Fun")
                else:
                    FUN = True
                    print("Fun")
            # if event.key == game.K_c:
            #     change_color()

    # Event handlers
    pressed_key = game.key.get_pressed()
    if TWO_P_MODE and FUN:
        p1_ai.move(ball)
    elif pressed_key[game.K_w]:
        player1.up(paddle_speed)
    elif pressed_key[game.K_s]:
        player1.down(paddle_speed)
    if TWO_P_MODE:
        p2_ai.move(ball)
    elif pressed_key[game.K_UP]:
        player2.up(paddle_speed)
    elif pressed_key[game.K_DOWN]:
        player2.down(paddle_speed)

    # ___Logic___
    all_sprites_list.update()

    # Collision between ball and walls
    if ball.rect.x <= border[0]:  # Player 2 scored
        score2 += 1
        ball.reset()
    if ball.rect.x >= border[2] - ball.size:  # Player 1 scored