📜  pong (1)

📅  最后修改于: 2023-12-03 15:18:37.302000             🧑  作者: Mango

Pong

Pong is a classic arcade game that was released in 1972. It was one of the first video games to gain widespread popularity and has since become a cultural icon.

How to Play

The game is played on a two-dimensional rectangular field. Each player controls a paddle that can move vertically up and down on its side of the field. The objective of the game is to use the paddle to hit a small ball back and forth between the two players. Each time the ball is missed by a player, the other player scores a point.

Implementing Pong

To implement Pong, you will need to create a two-dimensional field and draw the paddles and ball. You will also need to handle user input to move the paddles and update the position of the ball based on collisions with the paddles and the boundaries of the field.

Here is an example implementation of Pong in Python using the Pygame library:

import pygame

# Initialize Pygame
pygame.init()

# Set up the screen
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Pong')

# Set up game objects
paddle_width = 10
paddle_height = 100
paddle_speed = 5
left_paddle = pygame.Rect(0, screen_height/2-paddle_height/2, paddle_width, paddle_height)
right_paddle = pygame.Rect(screen_width-paddle_width, screen_height/2-paddle_height/2, paddle_width, paddle_height)
ball_speed = 5
ball_radius = 10
ball = pygame.Rect(screen_width/2-ball_radius/2, screen_height/2-ball_radius/2, ball_radius, ball_radius)
ball_direction = pygame.math.Vector2(1, 1).normalize() * ball_speed

# Run the game loop
running = True
while running:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False
    keys = pygame.key.get_pressed()
    if keys[pygame.K_w] and left_paddle.top > 0:
        left_paddle.move_ip(0, -paddle_speed)
    if keys[pygame.K_s] and left_paddle.bottom < screen_height:
        left_paddle.move_ip(0, paddle_speed)
    if keys[pygame.K_UP] and right_paddle.top > 0:
        right_paddle.move_ip(0, -paddle_speed)
    if keys[pygame.K_DOWN] and right_paddle.bottom < screen_height:
        right_paddle.move_ip(0, paddle_speed)

    # Update game state
    ball.move_ip(ball_direction)
    if ball.left < 0:
        running = False  # Left player loses
    elif ball.right > screen_width:
        running = False  # Right player loses
    if ball.top < 0 or ball.bottom > screen_height:
        ball_direction.y *= -1
    if ball.colliderect(left_paddle) or ball.colliderect(right_paddle):
        ball_direction.x *= -1

    # Draw the screen
    screen.fill((0, 0, 0))
    pygame.draw.rect(screen, (255, 255, 255), left_paddle)
    pygame.draw.rect(screen, (255, 255, 255), right_paddle)
    pygame.draw.circle(screen, (255, 255, 255), ball.center, ball_radius)
    pygame.display.flip()

# Clean up
pygame.quit()
Conclusion

Pong is a fun and challenging game that is relatively simple to implement. By following the steps above, you can create your own version of Pong and even add your own features and modifications to make it unique.