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

Pygame中菱形立方体不可见及脚本运行异常问题求助

问题描述

我无法看到由菱形组成的立方体。时隔数月打开该脚本后,发现它无法正常运行。我记不清上次修改后代码是否能正常工作。这是一个复杂的脚本,附上了完整代码。我没有任何错误细节,因为未出现任何报错信息。

预期效果示意图

我的问题不只是无法看到立方体。


原始代码
import pygame as pg
import random
import time
pg.init()

BG_COLOR = pg.Color('black')
WHITE = pg.Color('white')
ORANGE = pg.Color('orange')
BLUE = pg.Color('blue')
RED = pg.Color('red')
PURPLE = pg.Color('purple')
BALL_COOR_X = 240

screen = pg.display.set_mode((500, 800))
clock = pg.time.Clock()


bg = pg.image.load("bg.png")


def rhumbus(screen,color,plus):
    point1=(170, 500-plus)
    point2=(350, 500-plus)
    point3=(300, 550-plus)
    point4=(120, 550-plus)
    points=[point1, point2, point3, point4]
    return pg.draw.polygon(screen,color, points)

def colors():
    collist = []
    old_col = None
    for x in range(0,10):
        col = random.choice([RED,ORANGE,RED,BLUE,WHITE])
        if col != old_col:

            collist.append(col)
            old_col = col
        else:
            collist.append(PURPLE)
            old_col = col


    return collist

def cube(collist, plus):
    index = -1
    for x in range(0,10):
        rhumbus(screen,collist[x],plus)
        if ball_coor_y >= 500-plus and ball_coor_y <= 550-plus and BALL_COOR_X >= 120+plus and BALL_COOR_X <= 300+plus:
            index = x
            break
    return index
            
def ball(color,x,y):
    pg.draw.circle(screen, color, (x,y),15,)

def plus(y):
    y = y +1
    return y

def minus(y):
    y = y -1
    return y

def move_rhumbus(plus, keys):
    if keys[pg.K_LEFT]:
        if plus > 0:
            plus -= 5
    if keys[pg.K_RIGHT]:
        if plus < 500:
            plus += 5
    if event.type == pg.FINGERDOWN:
        start_x = event.x
    if event.type == pg.FINGERUP:
        if event.x > start_x:
            if plus < 500:
                plus += 5
        else:
            if plus > 0:
                plus -= 5
    return plus


def check_collision(plus, index):
    end = False
    if plus > 0 and plus < 500:
        if colorlist[ballcol] != colorlist[index]:
            end = True 
    return end

def ball_color(colorlist,):
    li = [0,1,2,3,4]
    ballcol = random.choice(li)
    return ballcol



# this is a just a 1 level 


done = False
colorlist = colors()
ballcol = ball_color(colorlist)
ball_coor_y = 300
constant_of_px = 516
end = False
restart = False
plus_x = 0
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        
    keys = pg.key.get_pressed()
    #plus_x = move_rhumbus(plus_x , keys)
    print(f"This is a plus_x {plus_x}")
    index = cube(colorlist, plus_x)
    end = check_collision(plus_x , index)
    screen.fill(BG_COLOR)
    ball(colorlist[ballcol],BALL_COOR_X,ball_coor_y)


    
    # ball movement 
    if ball_coor_y < constant_of_px:
        ball_coor_y = plus(ball_coor_y)
        

    else:

        ball_coor_y = minus(ball_coor_y)
        constant_of_px = 0
        if colorlist[ballcol] != colorlist[index]:
            end = True 

        if ball_coor_y == 300:
            constant_of_px = 516
    
    
    #if end == True:
        #screen.blit(bg,(0,0))
        
    
    pg.display.flip()
    clock.tick(60)

问题修复说明
  1. 修正绘制顺序:把screen.fill(BG_COLOR)移到cube()和ball()绘制之前,避免刚画的菱形被背景涂黑覆盖,这样就能看到菱形组成的立方体了。
  2. 启用菱形移动功能:取消plus_x = move_rhumbus(plus_x , keys)的注释,恢复菱形左右移动的交互功能。
  3. 修复事件处理bug:修改move_rhumbus函数,将事件列表作为参数传入,避免函数内直接访问循环外的event变量导致的报错。
  4. 完善碰撞检测逻辑:在判断颜色匹配前先检查index是否为-1(球未到达菱形区域的状态),避免索引越界错误;同时将colorlist和ballcol作为参数传入check_collision函数,提升代码封装性。
  5. 启用结束界面:取消if end == True:块的注释,触发结束条件时显示背景图。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 22:08:12