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

Pygame立方体左侧可反弹右侧触碰消失,求故障排查与修复

Pygame立方体右侧反弹消失问题分析与修复

问题代码

import pygame
import sys

def main():
    x=375
    y=400
    y_velocity = 0
    acceleration = 0.1
    bg_color=(183,255,183)
    shape_color=(0,255,255)
    x_minus = None
    pygame.init()
    DISPLAY = pygame.display.set_mode((800,800))
    icon = pygame.image.load("assets/firedash.ico")
    pygame.display.set_caption("pygame window")
    pygame.display.set_icon(icon)
    DISPLAY.fill(bg_color)
    pygame.display.update()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    y_velocity+=-5
        cube = pygame.draw.rect(DISPLAY,shape_color,(x,y,50,50))
        y+= y_velocity
        y_velocity+=acceleration
        if x_minus == None:
            x+=-2
        if x_minus == False:
            x+=2
        if x_minus == True:
            x+=-2
        if y > 800:
            y_velocity = -abs(y_velocity)
        if x < 0:
            x*=-1
            x_minus = False
        if x > 750:
            x*=-1
            x_minus = True
        pygame.time.delay(10)
        pygame.display.update()
        DISPLAY.fill(bg_color)
main()

问题原因

  • 右侧反弹逻辑错误:当x > 750时,执行x *= -1会让x直接变成负数(例如x=760时,结果为-760),立方体瞬间跑到屏幕左侧外
  • 后续x_minus = True会让立方体继续向左移动,彻底离开可视区域,导致看起来消失
  • 左侧反弹能正常工作是因为x原本是极小的负数(如x=-5),乘-1后变为正数5,仍在屏幕内,后续向右移动正常

修复方案

修改右侧反弹的逻辑,将x修正到屏幕内侧,而不是直接取反:

# 替换原右侧反弹代码
if x > 750:
    # 计算超出距离,反弹回屏幕内
    x = 750 - (x - 750)
    x_minus = True

也可以简化为直接将x重置到边缘位置:

if x > 750:
    x = 750
    x_minus = True

修复后完整代码

import pygame
import sys

def main():
    x=375
    y=400
    y_velocity = 0
    acceleration = 0.1
    bg_color=(183,255,183)
    shape_color=(0,255,255)
    x_minus = None
    pygame.init()
    DISPLAY = pygame.display.set_mode((800,800))
    icon = pygame.image.load("assets/firedash.ico")
    pygame.display.set_caption("pygame window")
    pygame.display.set_icon(icon)
    DISPLAY.fill(bg_color)
    pygame.display.update()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    y_velocity+=-5
        cube = pygame.draw.rect(DISPLAY,shape_color,(x,y,50,50))
        y+= y_velocity
        y_velocity+=acceleration
        if x_minus == None:
            x+=-2
        if x_minus == False:
            x+=2
        if x_minus == True:
            x+=-2
        if y > 800:
            y_velocity = -abs(y_velocity)
        if x < 0:
            x = -x
            x_minus = False
        if x > 750:
            x = 750 - (x - 750)
            x_minus = True
        pygame.time.delay(10)
        pygame.display.update()
        DISPLAY.fill(bg_color)
main()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 04:35:28