diff --git a/Pong_7_Video_2 b/Pong_7_Video_2 new file mode 100644 index 0000000..3ae4bcc --- /dev/null +++ b/Pong_7_Video_2 @@ -0,0 +1,148 @@ +from pygame import draw +import pygame +import sys +import random + + +def ball_animation(): + global ball_speed_x, ball_speed_y, player_score, opponent_score, score_timer + + ball.x += ball_speed_x + ball.y += ball_speed_y + + if ball.top <= 0 or ball.bottom >= screen_height: + ball_speed_y *= -1 + + if ball.left <= 0: + player_score += 1 + + score_timer = pygame.time.get_ticks() + + if ball.right >= screen_width: + opponent_score += 1 + score_timer = pygame.time.get_ticks() + + if ball.colliderect(player) or ball.colliderect(opponent): + ball_speed_x *= -1 + + +def player_animation(): + player.y += player_speed + + if player.top <= 0: + player.top = 0 + if player.bottom >= screen_height: + player.bottom = screen_height + +def opponent_ai(): + if opponent.top < ball.y: + opponent.y += opponent_speed + if opponent.bottom > ball.y: + opponent.y -= opponent_speed + + if opponent.top <= 0: + opponent.top = 0 + if opponent.bottom >= screen_height: + opponent.bottom = screen_height + +def ball_start(): + global ball_speed_x, ball_speed_y, score_timer + + current_timer = pygame.time.get_ticks() + ball.center = (screen_width/2, screen_height/2) + + if current_timer - score_timer < 700: + number_three = game_font.render('3', False, light_grey) + screen.blit(number_three, (screen_width/2 -10, screen_height/2 + 20)) + + if 700 < current_timer - score_timer < 1400: + number_two = game_font.render('2', False, light_grey) + screen.blit(number_two, (screen_width/2 -10, screen_height/2 + 20)) + + if 1400 < current_timer - score_timer < 2100: + number_one = game_font.render('1', False, light_grey) + screen.blit(number_one, (screen_width/2 -10, screen_height/2 + 20)) + + if current_timer - score_timer < 2100: + ball_speed_x, ball_speed_y = 0,0 + + else: + ball_speed_y = 7 * random.choice((1,-1)) + ball_speed_x = 7 * random.choice((1,-1)) + score_timer = None + +# General setup +pygame.init() +clock = pygame.time.Clock() + +# Main Window +screen_width = 1080 +screen_height = 760 +screen = pygame.display.set_mode((screen_width,screen_height)) +pygame.display.set_caption('Pong') + +# Colors +light_grey = (200,200,200) +bg_color = pygame.Color('grey12') + +# Game Rectangles +ball = pygame.Rect(screen_width / 2 - 15, screen_height / 2 - 15, 30, 30) +player = pygame.Rect(screen_width - 20, screen_height / 2 - 70, 10,140) +opponent = pygame.Rect(10, screen_height / 2 - 70, 10,140) + +# Game Variables +ball_speed_x = 7 * random.choice((1,-1)) +ball_speed_y = 7 * random.choice((1,-1)) +player_speed = 0 +opponent_speed = 7 + +#Txt Var +player_score = 0 +opponent_score = 0 +game_font = pygame.font.Font('freesansbold.ttf', 32) + +#Score timer +score_timer = None + +while True: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + sys.exit() + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_UP: + player_speed -= 6 + if event.key == pygame.K_DOWN: + player_speed += 6 + if event.type == pygame.KEYUP: + if event.key == pygame.K_UP: + player_speed += 6 + if event.key == pygame.K_DOWN: + player_speed -= 6 + + #Game Logic + ball_animation() + player_animation() + opponent_ai() + + # Visuals + screen.fill(bg_color) + pygame.draw.rect(screen, light_grey, player) + pygame.draw.rect(screen, light_grey, opponent) + pygame.draw.ellipse(screen, light_grey, ball) + pygame.draw.aaline(screen, light_grey, (screen_width / 2, 0),(screen_width / 2, screen_height)) + + if score_timer: + ball_start() + + player_text = game_font.render(f"{player_score}", False, light_grey) + screen.blit(player_text, (640, 470)) + + #player_text = game_font.render(f"{player_score}",False, light_grey) + #screen.blit(player_text, (660, 470)) + + opponent_text = game_font.render(f"{opponent_score}", False, light_grey) + screen.blit(opponent_text, (440, 470)) + + pygame.display.flip() + clock.tick(60)