1

I decided to remake Pong in Visual Studio Code running on Mac by my pygame file wont open, the terminal wont ope. The Pygame symbol will appear on my doc but disappear afterwards and no window will open Code:

import pygame;

input()
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

HEIGHT = 600
WIDTH = 600

pygame.init()
game_font = pygame.font.SysFont('Ubuntu', 40)

delay = 30

paddle_speed = 20

paddle_width = 10 
paddle_height = 100

p1_x_pos = 10
p1_y_pos = HEIGHT / 2 - paddle_height / 2

p2_x_pos = WIDTH - paddle_width - 10
p2_y_pos = HEIGHT / 2 - paddle_height / 2

p1_score = 0
p2_score = 0

p1_up = False
p1_down = False
p2_up = False 
p2_down = False

ball_x_pos = WIDTH / 2
ball_y_pos = HEIGHT /2
ball_width = 10
ball_x_vel = -10
ball_y_vel = 0

# Screen
screen= pygame.display.set_mode((WIDTH, HEIGHT))

#dRAWING oBJECTS

def draw_objects():
    pygame.draw.rect(screen, WHITE, (int(p1_x_pos), int(p1_y_pos), paddle_width, paddle_height))
    pygame.draw.rect(screen, WHITE, (int(p2_x_pos), int(p2_y_pos), paddle_width, paddle_height))
    pygame.draw.circle(screen, WHITE, (ball_x_pos, ball_y_pos, ball_width))
    #Score
    score = game_font.render(f"{str(p1_score)}- {str(p2_score)}", False, WHITE)
    screen.blit(score, (WIDTH / 2, 30))

    # Player Movement
    def apply_player_movement():
        global p1_y_pos
        global p2_y_pos

        # Player 1
        if p1_up:
            p1_y_pos = max(p1_y_pos - paddle_speed, 0 )
        elif p1_down:
            p1_y_pos = min(p1_y_pos + paddle_speed, HEIGHT)
        # Player 2
        if p2_up:
            p2_y_pos = max(p2_y_pos - paddle_speed, 0 )
        elif p2_down:
            p2_y_pos = min(p2_y_pos + paddle_speed, HEIGHT)

            # Ball Movement
    def apply_ball_movement():
        global ball_x_pos
        global ball_y_pos
        global ball_x_vel
        global ball_y_vel
        global p1_score
        global p2_score

        if (ball_x_pos + ball_x_vel < p1_x_pos + paddle_width) and (p1_y_pos < ball_y_pos + ball_y_vel + ball_width < p1_y_pos + paddle_height) :
            ball_x_vel = - ball_x_vel
            ball_y_vel = (p1_y_pos + paddle_height / 2 - ball_y_pos) / 15  
            ball_y_vel =  - ball_y_vel

        elif ball_x_pos + ball_x_vel < 0:
            p2_score += 1
            ball_x_pos = WIDTH / 2
            ball_y_pos = HEIGHT / 2
            ball_x_vel = 10
            ball_y_vel = 0

        if (ball_x_pos + ball_x_vel > p2_x_pos - paddle_width) and (p2_y_pos < ball_y_pos + ball_y_vel + ball_width < p2_y_pos + paddle_height):
            ball_x_vel = -ball_x_vel
            ball_y_vel = (p2_y_pos + paddle_height / 2 - ball_y_pos) / 15
            ball_y_vel = - ball_y_vel

        elif ball_x_pos + ball_x_vel > HEIGHT:
            p1_score +=1
            ball_x_pos = WIDTH / 2
            ball_y_pos = HEIGHT / 2
            ball_x_vel = -10
            ball_y_vel = 0
        
        if ball_y_pos + ball_y_vel > HEIGHT or ball_y_pos + ball_y_vel < 0:
            ball_y_vel = - ball_y_vel

            ball_x_pos += ball_x_vel
            ball_y_pos += ball_y_vel

            pygame.display.set_caption("Pong Video Game")
            screen.fill(BLACK)
            pygame.display.flip()

            running = True
            while running:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        running = False
                        if event.type == pygame.KEYDOWN:
                            if event.key == pygame.K_ESCAPE:
                                running = False
                                if event.key == pygame.K_w:
                                    p1_up = True
                                    if event.key == pygame.K_s:
                                        p1_down = True
                                        if event.key == pygame.K_UP:
                                            P1_UP = True
                                            if event.key == pygame.K.DOWN:
                                                p2_down = True

                                                if event.type == pygame.KEYUP:
                                                         running = False
                                                if event.key == pygame.K_w:
                                                    p1_up = False
                                                if event.key == pygame.K_s:
                                                    p1_down = False
                                                if event.key == pygame.K_UP:
                                                    p1_up = False
                                                if event.key == pygame.K.DOWN:
                                                    p2_down = False
                
                screen.fill(BLACK)
                apply_player_movement()
                apply_ball_movement()
                draw_objects()
                pygame.display.flip()
                pygame.time.wait(delay)

I tried running the code on other laptops and still received the same error, I even attempted to run the file on windows and got the same error

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Mayhem
  • 11
  • 1
  • Please correct the indentation of your code. – Rabbid76 Feb 21 '23 at 16:56
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Feb 21 '23 at 17:37

1 Answers1

1

Please read about Indentation. After fixing the indentation of the code and all the typos, the application works fine. Note that input() statement at the beginning of the code prevents the application from starting until the RETURN key is pressed.

import pygame

input()
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

HEIGHT = 600
WIDTH = 600

pygame.init()
game_font = pygame.font.SysFont('Ubuntu', 40)

delay = 30

paddle_speed = 20

paddle_width = 10 
paddle_height = 100

p1_x_pos = 10
p1_y_pos = HEIGHT / 2 - paddle_height / 2

p2_x_pos = WIDTH - paddle_width - 10
p2_y_pos = HEIGHT / 2 - paddle_height / 2

p1_score = 0
p2_score = 0

p1_up = False
p1_down = False
p2_up = False 
p2_down = False

ball_x_pos = WIDTH / 2
ball_y_pos = HEIGHT /2
ball_width = 10
ball_x_vel = -10
ball_y_vel = 0

# Screen
screen= pygame.display.set_mode((WIDTH, HEIGHT))

#dRAWING oBJECTS

def draw_objects():
    pygame.draw.rect(screen, WHITE, (int(p1_x_pos), int(p1_y_pos), paddle_width, paddle_height))
    pygame.draw.rect(screen, WHITE, (int(p2_x_pos), int(p2_y_pos), paddle_width, paddle_height))
    pygame.draw.circle(screen, WHITE, (ball_x_pos, ball_y_pos), ball_width)
    #Score
    score = game_font.render(f"{str(p1_score)}- {str(p2_score)}", False, WHITE)
    screen.blit(score, (WIDTH / 2, 30))

# Player Movement
def apply_player_movement():
    global p1_y_pos
    global p2_y_pos

    # Player 1
    if p1_up:
        p1_y_pos = max(p1_y_pos - paddle_speed, 0 )
    elif p1_down:
        p1_y_pos = min(p1_y_pos + paddle_speed, HEIGHT)
    # Player 2
    if p2_up:
        p2_y_pos = max(p2_y_pos - paddle_speed, 0 )
    elif p2_down:
        p2_y_pos = min(p2_y_pos + paddle_speed, HEIGHT)

            # Ball Movement
def apply_ball_movement():
    global ball_x_pos
    global ball_y_pos
    global ball_x_vel
    global ball_y_vel
    global p1_score
    global p2_score

    if (ball_x_pos + ball_x_vel < p1_x_pos + paddle_width) and (p1_y_pos < ball_y_pos + ball_y_vel + ball_width < p1_y_pos + paddle_height) :
        ball_x_vel = - ball_x_vel
        ball_y_vel = (p1_y_pos + paddle_height / 2 - ball_y_pos) / 15  
        ball_y_vel =  - ball_y_vel

    elif ball_x_pos + ball_x_vel < 0:
        p2_score += 1
        ball_x_pos = WIDTH / 2
        ball_y_pos = HEIGHT / 2
        ball_x_vel = 10
        ball_y_vel = 0

    if (ball_x_pos + ball_x_vel > p2_x_pos - paddle_width) and (p2_y_pos < ball_y_pos + ball_y_vel + ball_width < p2_y_pos + paddle_height):
        ball_x_vel = -ball_x_vel
        ball_y_vel = (p2_y_pos + paddle_height / 2 - ball_y_pos) / 15
        ball_y_vel = - ball_y_vel

    elif ball_x_pos + ball_x_vel > HEIGHT:
        p1_score +=1
        ball_x_pos = WIDTH / 2
        ball_y_pos = HEIGHT / 2
        ball_x_vel = -10
        ball_y_vel = 0
    
    if ball_y_pos + ball_y_vel > HEIGHT or ball_y_pos + ball_y_vel < 0:
        ball_y_vel = - ball_y_vel

    ball_x_pos += ball_x_vel
    ball_y_pos += ball_y_vel

pygame.display.set_caption("Pong Video Game")
screen.fill(BLACK)
pygame.display.flip()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False
            if event.key == pygame.K_w:
                p1_up = True
            if event.key == pygame.K_s:
                p1_down = True
            if event.key == pygame.K_UP:
                p2_up = True
            if event.key == pygame.K_DOWN:
                p2_down = True

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_w:
                p1_up = False
            if event.key == pygame.K_s:
                p1_down = False
            if event.key == pygame.K_UP:
                p2_up = False
            if event.key == pygame.K_DOWN:
                p2_down = False
    
    screen.fill(BLACK)
    apply_player_movement()
    apply_ball_movement()
    draw_objects()
    pygame.display.flip()
    pygame.time.wait(delay)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174