Wednesday 27 January 2021

Game No. 5 : PING - PONG Menu Based game to select level and paddle color by S P SHARMA CLASSES

 

Game No. 5 : PING - PONG Menu Based game to select level and paddle color by S P SHARMA CLASSES



#PING - PONG Menu Based game to select level and paddle color by Sachin Kumar, 9910707562

#sachinpgt2019@gmail.com

import pygame

import random,sys

 

pygame.init()

clock = pygame.time.Clock()

 

sw = 800

sh = 600

screen = pygame.display.set_mode((sw,sh))

pygame.display.set_caption("PING--PONG BY SACHIN KUMAR")

bg_color = pygame.Color('red')

game_font = pygame.font.Font('freesansbold.ttf', 60)

font1 = pygame.font.SysFont("comicsansms", 45)

p_color="red"

b_color="blue"

level = 1

opponent_speed = 6

 

score_time = None


def c_color(OS1):

    global p_color,b_color

    sw = 800

    sh = 600

 

    ball = pygame.Rect(sw//2 - 15, sh//2 - 15, 30, 30)

    player = pygame.Rect(sw-20, sh//2 - 60, 10, 120)

    opponent = pygame.Rect(10, sh//2 - 60, 10, 120)

 

    bg_color = pygame.Color('yellow')


    running  = True

    while running:

        screen.fill(bg_color)

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                welcomscreen = False

                pygame.quit()

                sys.exit(0)

     

            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_SPACE:

                    welcomscreen = False

                    StartGame(OS1)

                if event.key == pygame.K_1:

                    p_color="red"

                    b_color="blue"

     

                elif event.key == pygame.K_2:

                    p_color="black"

                    b_color="red"

     

                elif event.key == pygame.K_3:

                    p_color="blue"

                    b_color="red"


        if p_color=="red":

            pygame.draw.rect(screen, (250,0,0), pygame.Rect(sw//2-190, sh-410, 350, 70), 2)

        elif p_color=="black":

            pygame.draw.rect(screen, (250,0,0), pygame.Rect(sw//2-190, sh-310, 350, 70), 2)

        elif p_color=="blue":

            pygame.draw.rect(screen, (250,0,0), pygame.Rect(sw//2-190, sh-210, 350, 70), 2)

 


        Welcome_message = font1.render("PING PONG BY SACHIN KUMAR", True, (250,0,0))

        screen.blit(Welcome_message, (sw//2 - 320, 20))

     

        Select_level = font1.render("SELECT PADDLE COLOR", True, (250,0,0))

        screen.blit(Select_level, (sw//2 - 230, sh-500))

     

        Easy = game_font.render("RED", True, (250,0,0))

        screen.blit(Easy, (sw//2 - 90, sh-400))

     

        Medium = game_font.render("BLACK", True, (250,0,0))

        screen.blit(Medium, (sw//2-130, sh-300))

     

        Hard = game_font.render("BLUE", True, (250,0,0))

        screen.blit(Hard, (sw//2 - 90, sh-200))

     

        Start_game = font1.render("PRESS SPACE TO START", True, (250,0,0))

        screen.blit(Start_game, (sw//2-260, sh-100))

        clock.tick(30)

        pygame.display.update()


def StartGame(OS):

    global score_time

    sw = 800

    sh = 600

 

    ball = pygame.Rect(sw//2 - 15, sh//2 - 15, 30, 30)

    player = pygame.Rect(sw-20, sh//2 - 60, 10, 120)

    opponent = pygame.Rect(10, sh//2 - 60, 10, 120)

 

    bg_color = pygame.Color('yellow')

 

    ball_speed_x = 6*random.choice((-1,1))

    ball_speed_y = 6*random.choice((-1,1))

 

    player_speed = 0

    opponent_speed = OS

 

    player_score = 0

    opponent_score = 0

 

    game_font = pygame.font.Font('freesansbold.ttf', 32)

 

    score_time = None

    def ball_restart():

        global score_time

        ball.center = (sw//2 + 7, sh//2)

        #ball.x = sw//2 - 15

        #ball.y = sh//2 - 15

        current_time = pygame.time.get_ticks()

        if current_time - score_time <= 2000:

            ball_speed_x = 0

            ball_speed_y = 0

 

        if current_time - score_time > 2000:

            ball_speed_x = 6*random.choice((-1,1))

            ball_speed_y = 6*random.choice((-1,1))

            score_time = None

 

    running  = True

    while running:

        screen.fill(bg_color)

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                running = False

                pygame.quit()

                sys.exit(0)

 

            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_UP: player_speed -= 7

                if event.key == pygame.K_DOWN: player_speed += 7

 

            if event.type == pygame.KEYUP:

                if event.key == pygame.K_UP: player_speed += 7

                if event.key == pygame.K_DOWN: player_speed -= 7

        

 

        #SCORE

        if ball.left <= 0:

            score_time = pygame.time.get_ticks()

            player_score += 1

            ball_restart()

 

        if ball.right >= sw:

            score_time = pygame.time.get_ticks()

            opponent_score += 1

            ball_restart()

 

        if score_time:

            ball_restart()

 

        #COLLISION

        if ball.colliderect(player) or ball.colliderect(opponent):

            ball_speed_x *= -1

 

        #BALL MOVEMENT

        ball.x += ball_speed_x

        ball.y += ball_speed_y

 

        if ball.top <= 0 or ball.bottom >= sh:

            ball_speed_y *= -1

        

        if ball.left <= 0 or ball.right >= sw:

            ball_speed_x *= -1

 

        #PLAYER MOVEMENT

        player.y += player_speed

        if player.top <= 0:

            player.top = 0

        if player.bottom >= sh:

            player.bottom = sh

 

        #OPPONENT MOVEMENT

        if opponent.bottom < ball.y:

            opponent.bottom += opponent_speed

        if opponent.top > ball.y:

            opponent.top -= opponent_speed

 

        pygame.draw.rect(screen, p_color, player)

        pygame.draw.rect(screen, p_color, opponent)

        pygame.draw.ellipse(screen, b_color, ball)

        pygame.draw.aaline(screen, (200,200,200), (sw//2,0), (sw//2,sh))

 

        player_text = game_font.render(str(player_score), True, (250,0,0))

        screen.blit(player_text, (sw//2 + 20, sh//2 - 16))

 

        opponent_text = game_font.render(str(opponent_score), True, (250,0,0))

        screen.blit(opponent_text, (sw//2 - 42, sh//2 - 16))

 

        clock.tick(30)

        pygame.display.update()

 

 

welcomscreen = True

while welcomscreen:

    screen.fill(bg_color)

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            welcomscreen = False

            pygame.quit()

            sys.exit(0)

 

        if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_SPACE:

                welcomscreen = False

                #pygame.quit()

                #sys.exit(0)

                c_color(opponent_speed)

                #StartGame(opponent_speed)

            if event.key == pygame.K_1:

                level = 1

                opponent_speed = 6

 

            elif event.key == pygame.K_2:

                level = 2

                opponent_speed = 10

 

            elif event.key == pygame.K_3:

                level = 3

                opponent_speed = 13

 

    #pygame.draw.rect(screen, (250,0,0), pygame.Rect(sw//2, sh-140, 350, 70), 2)

 

    if level == 1:

        pygame.draw.rect(screen, (0,0,250), pygame.Rect(sw//2-190, sh-410, 350, 70), 2)

    elif level == 2:

        pygame.draw.rect(screen, (0,0,250), pygame.Rect(sw//2-190, sh-310, 350, 70), 2)

    elif level == 3:

        pygame.draw.rect(screen, (0,0,250), pygame.Rect(sw//2-190, sh-210, 350, 70), 2)

 

    

    Welcome_message = font1.render("PING PONG BY SACHIN KUMAR", True, (250,250,250))

    screen.blit(Welcome_message, (sw//2 - 320, 20))

 

    Select_level = game_font.render("SELECT LEVEL", True, (250,250,250))

    screen.blit(Select_level, (sw//2 - 200, sh-500))

 

    Easy = game_font.render("EASY", True, (250,250,250))

    screen.blit(Easy, (sw//2 - 90, sh-400))

 

    Medium = game_font.render("MEDIUM", True, (250,250,250))

    screen.blit(Medium, (sw//2-130, sh-300))

 

    Hard = game_font.render("HARD", True, (250,250,250))

    screen.blit(Hard, (sw//2 - 90, sh-200))

    

    Start_game = font1.render("PRESS SPACE TO SELECT COLOR", True, (250,250,250))

    screen.blit(Start_game, (sw//2-360, sh-100))

 

    clock.tick(30)

    pygame.display.update()



 


No comments:

Post a Comment