Esempio n. 1
0
def main():    
    pygame.init()
    pointcounter =0
    screensize = (640,480)
    screen = pygame.display.set_mode(screensize)
    clock = pygame.time.Clock()
    pong = Pong(screensize)
    paddle = Paddle(screensize)
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False
            if event.type == KEYDOWN:
                if event.key == K_LEFT:
                    paddle.direction = -1
                elif event.key == K_RIGHT:
                    paddle.direction = 1
            if event.type == KEYUP:
                if event.key == K_LEFT and paddle.direction == -1:
                    paddle.direction = 0
                elif event.key == K_RIGHT and paddle.direction == 1:
                    paddle.direction = 0
        paddle.update()
        pointcounter= pong.update(paddle,pointcounter)
        if pong.hit_edge_bottom:
            print ('Your Score ' + str(pointcounter))
            running = False
        screen.fill((0,0,0))
        paddle.render(screen)
        pong.render(screen)
        #Display scores:
        font = pygame.font.Font(None, 74)
        text = font.render(str(pointcounter), 1, white)
        screen.blit(text, (310,10))
        pygame.display.flip()        
        clock.tick(60)
    pygame.quit()
Esempio n. 2
0
ball = Ball()
scoreboard = ScoreBoard()

game_is_on = True
while game_is_on:
    time.sleep(ball.ball_speed)
    my_screen.update()
    ball.move()

    # Checking the user's choice input
    if user_choice == "singleplayer":
        r_paddle.move_auto()
        # change direction Right Paddle
        if r_paddle.ycor() > 280:
            r_paddle.direction = "Down"
        if r_paddle.ycor() < -280:
            r_paddle.direction = "Up"
    elif user_choice == "multiplayer":
        pass

    # Detect collision with wall
    if ball.ycor() > 280 or ball.ycor() < -280:
        # Needs to bounce back
        ball.bounce_y()

    # Detect collision with both paddles
    if ball.distance(r_paddle) < 50 and ball.xcor() > 320 or ball.distance(
            l_paddle) < 30 and ball.ycor() > -320:
        ball.bounce_x()