You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac/Windows下VS Code运行Pygame版Pong无响应问题求助

问题分析与修复方案

你的代码无法正常运行主要是由以下几个关键错误导致的:

  • 缩进逻辑混乱:apply_player_movement、apply_ball_movement函数被错误嵌套在draw_objects内部,主游戏循环甚至被放在apply_ball_movement里,导致代码永远无法进入游戏循环。
  • 图形绘制参数错误:pygame.draw.circle的参数格式不正确,正确用法需要传入圆心坐标(二元组)和半径,而非三元组。
  • 事件处理嵌套错误:键盘按键事件被嵌套在窗口关闭事件的判断分支中,导致按键输入完全无法被捕获。
  • 变量与常量拼写错误:存在pygame.K.DOWN(应为pygame.K_DOWN)、P1_UP(应为p2_up)这类低级拼写错误。
  • 球的移动逻辑错误:球的坐标更新语句被错误缩进,仅在碰撞上下边界时才会移动,而非每帧更新位置。
  • 边界判断错误:判断球是否超出右边界时误用了HEIGHT(垂直高度),应该使用WIDTH(水平宽度)。
  • paddle移动边界错误:paddle向下移动时没有考虑自身高度,会导致部分paddle超出屏幕。

修复后的完整代码

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 = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong Video Game")

# 绘制游戏对象
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))
    # 修复draw.circle的参数格式
    pygame.draw.circle(screen, WHITE, (int(ball_x_pos), int(ball_y_pos)), ball_width)
    # 绘制分数
    score = game_font.render(f"{p1_score} - {p2_score}", False, WHITE)
    # 调整分数位置使其居中
    screen.blit(score, (WIDTH/2 - score.get_width()/2, 30))

# 玩家paddle移动逻辑
def apply_player_movement():
    global p1_y_pos
    global p2_y_pos

    # 玩家1移动(使用独立if,避免按键冲突)
    if p1_up:
        p1_y_pos = max(p1_y_pos - paddle_speed, 0 )
    if p1_down:
        p1_y_pos = min(p1_y_pos + paddle_speed, HEIGHT - paddle_height)
    # 玩家2移动
    if p2_up:
        p2_y_pos = max(p2_y_pos - paddle_speed, 0 )
    if p2_down:
        p2_y_pos = min(p2_y_pos + paddle_speed, HEIGHT - paddle_height)

# 球的移动与碰撞逻辑
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

    # 碰撞玩家1 paddle
    if (ball_x_pos + ball_x_vel < p1_x_pos + paddle_width) and (p1_y_pos < ball_y_pos < 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

    # 球超出左边界,玩家2得分
    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

    # 碰撞玩家2 paddle
    if (ball_x_pos + ball_x_vel > p2_x_pos) and (p2_y_pos < ball_y_pos < 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

    # 球超出右边界,玩家1得分(修复为WIDTH)
    elif ball_x_pos + ball_x_vel > WIDTH:
        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 - ball_width or ball_y_pos + ball_y_vel < ball_width:
        ball_y_vel = -ball_y_vel

    # 每帧更新球的位置(修复缩进)
    ball_x_pos += ball_x_vel
    ball_y_pos += ball_y_vel

# 主游戏循环
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)

# 退出pygame
pygame.quit()

额外优化说明

  1. 移除了不必要的input(),若你需要调试时暂停可自行恢复。
  2. 调整了分数的绘制位置,使其在屏幕顶部居中显示。
  3. 改进了paddle移动的边界判断,确保paddle不会超出屏幕范围。
  4. 优化了球与paddle的碰撞检测逻辑,只检测球的中心是否在paddle范围内,避免误判。
  5. 在代码末尾添加了pygame.quit(),确保程序退出时正确释放资源。

内容的提问来源于stack exchange,提问作者Mayhem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 14:27:24