import pygame import sys
pygame 是我们用来制作游戏的模块。它为我们提供了图形、声音等工具。
sys 是 python 中的一个模块,可以帮助我们与 python 解释器交互。
pygame.init()
初始化所有 pygame 模块并使其可供使用。
#dimensions width, height=800,600 #frame rate fps=60 #the paddles at the side of ping pong paddle_width, paddle_height=15,90 #the balls radius ball_radius=15 #the color of the ball and paddle white=(255, 255, 255)
screen=pygame.display.set_mode((width,height))
pygame.display.set_caption("ping pong")
您将有一个名为ping pong 的窗口,并指定了宽度和高度

立即学习“Python免费学习笔记(深入)”;
left_paddle=pygame.rect(50, height//2 - paddle_height //2, paddle_width, paddle_height) right_paddle=pygame.rect(width - 50 - paddle_width, height //2- paddle_height //2, paddle_width, paddle_height) ball=pygame.rect(width //2 - ball_radius, height //2 - ball_radius, ball_raduis *2, ball_radius *2)

在 pygame 中,屏幕的左上角代表坐标 (0,0)。
pygame.rect(x, y, width, height)
pygame.rect(50, height//2 - paddle_height //2, paddle_width, paddle_height)
首先,我们将左桨定位在距左侧 50px 处。
然后我们执行 height//2 - paddle_height //2 因为如果你只执行 height//2 它看起来就像图片中的样子。它从屏幕上下来。为了使其居中,我们这样做 - paddle_height //2

这就是我们为右桨使其居中所做的事情。
right_paddle=pygame.rect(width - 50 - paddle_width, height //2- paddle_height //2, paddle_width, paddle_height)
ball=pygame.rect(width //2 - ball_radius, height //2 - ball_radius, ball_raduis *2, ball_radius *2)
为了使球居中,我们减去半径。
ball_speed_x=7 ball_speed_y=7 paddle_speed=10
ball_speed_x 和 ball_speed_y 控制球的水平和垂直速度。
paddle_speed:控制桨的移动速度。
left_score=0 right_score=0 font=pygame.font.sysfont(none,55)
def draw(): screen.fill((0,0,0)) #fill the screen with black pygame.draw.rect(screen, white, left_paddle) pygame.draw.rect(screen, white, right_paddle) pygame.draw.ellipse(screen, white, ball)
pygame.draw.aaline(screen, white, (width //2, 0), (width //2, height))
left_text=font.render(str(left_score),true, white) screen.blit(left_text, (width // 4 - left_text.get_width() // 2, 20)) right_text=font.render(str(right_score), true, white) screen.blit(right_text, (width * 3 // 4 - right_text.get_width() //2, 20))
渲染双方玩家的分数并将其放置在屏幕上。
pygame.display.flip()
使用最新更改更新显示。
#main game loop while true:
让游戏无限期地运行。
for event in pygame.event.get():
if event.type == pygame.quitt:
pygame.quit()
sys.exit()
这将遍历 pygame 中可能发生的所有事件,如果其中一个事件正在关闭窗口,则退出 pygame 并关闭窗口。
#paddle controls keys pygame.key.get_pressed() if keys [pygame.k_w] and left_paddle.top > 0: left_paddle.y-=paddle_speed if keys [pygame.k_s] and left_paddle.bottom < height: left_paddle.y += paddle_speed if keys [pygame.k_up] and right_paddle.top > 0: right_paddle.y -= paddle_speed if keys [pygame.k_down] and right_paddle.bottom < height: right_paddle.y += paddle_speed 66
检测按键:
ball.x += ball_speed_x ball.y + ball_speed_y
通过将球的速度添加到当前位置来移动球
if ball.top <= 0 or ball.bottom >= height: ball_speed_y=-ball_speed_y
如果球击中屏幕顶部或底部,则反转球的垂直方向
if ball.colliderect(left_paddle) or ball.colliderect(right_paddle): ball_speed_x = -ball_speed_x
如果球与球拍碰撞,则反转球的水平方向。
pygame.time.clock().tick (fps)
限制游戏运行速度最高为60帧/秒,确保游戏流畅。
import pygame
import sys
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 600
FPS = 60
PADDLE_WIDTH, PADDLE_HEIGHT = 15, 90
BALL_RADIUS = 15
WHITE = (255, 255, 255)
# Setup screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong")
# Paddles and ball setup
left_paddle = pygame.Rect(50, HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT)
right_paddle = pygame.Rect(WIDTH - 50 - PADDLE_WIDTH, HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT)
ball = pygame.Rect(WIDTH // 2 - BALL_RADIUS, HEIGHT // 2 - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2)
# Speeds
ball_speed_x = 7
ball_speed_y = 7
paddle_speed = 10
# Score variables
left_score = 0
right_score = 0
font = pygame.font.SysFont(None, 55)
# Function to draw everything
def draw():
screen.fill((0, 0, 0)) # Fill screen with black
pygame.draw.rect(screen, WHITE, left_paddle)
pygame.draw.rect(screen, WHITE, right_paddle)
pygame.draw.ellipse(screen, WHITE, ball)
# Draw the center line
pygame.draw.aaline(screen, WHITE, (WIDTH // 2, 0), (WIDTH // 2, HEIGHT))
# Draw scores
left_text = font.render(str(left_score), True, WHITE)
screen.blit(left_text, (WIDTH // 4 - left_text.get_width() // 2, 20))
right_text = font.render(str(right_score), True, WHITE)
screen.blit(right_text, (WIDTH * 3 // 4 - right_text.get_width() // 2, 20))
pygame.display.flip()
# Main game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Paddle controls
keys = pygame.key.get_pressed()
if keys[pygame.K_w] and left_paddle.top > 0:
left_paddle.y -= paddle_speed
if keys[pygame.K_s] and left_paddle.bottom < HEIGHT:
left_paddle.y += paddle_speed
if keys[pygame.K_UP] and right_paddle.top > 0:
right_paddle.y -= paddle_speed
if keys[pygame.K_DOWN] and right_paddle.bottom < HEIGHT:
right_paddle.y += paddle_speed
# Ball movement
ball.x += ball_speed_x
ball.y += ball_speed_y
# Ball collision with top and bottom walls
if ball.top <= 0 or ball.bottom >= HEIGHT:
ball_speed_y = -ball_speed_y
# Ball collision with paddles
if ball.colliderect(left_paddle) or ball.colliderect(right_paddle):
ball_speed_x = -ball_speed_x
# Scoring
if ball.left <= 0:
right_score += 1
ball = pygame.Rect(WIDTH // 2 - BALL_RADIUS, HEIGHT // 2 - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2)
ball_speed_x = -ball_speed_x
if ball.right >= WIDTH:
left_score += 1
ball = pygame.Rect(WIDTH // 2 - BALL_RADIUS, HEIGHT // 2 - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2)
ball_speed_x = -ball_speed_x
draw()
pygame.time.Clock().tick(FPS)

以上就是Pygame python 中的乒乓球游戏的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号