当颜色改变时将物体从墙上反弹

2024-04-29 00:10:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着给一张快乐的脸编上代码,让它从墙上弹下来,并在掉到墙上时随机改变颜色。目前,我已经设置了颜色和代码,使其从墙上反弹并改变颜色。但是,我的代码并没有按照它所说的“NameError:name'xPos'未定义”工作,即使我已经定义了它

color = GREEN 
color2 = BLUE
color3 = RED

# funtion to draw a the "happy face"
# it has 4 parameters passed to it xPos, yPos, radius, and colour
# notice all the shapes are drawn "relative" to the xPos and yPos and the radius
def drawHappy(xPos,yPos,r,colour):
    pygame.draw.circle(screen,colour,(xPos,yPos),r,1)
    eyeRadius = int(1/6*r)
    eyeX = int(xPos-1/3*r)
    eyeY = int(yPos- 1/3*r)
    pygame.draw.circle(screen,colour,(eyeX,eyeY),eyeRadius,1)
    eyeX = int(xPos + 1/3*r)
    pygame.draw.circle(screen,colour,(eyeX,eyeY),eyeRadius,1)
    wMouth = 1.5*r
    xMouth = xPos - 3/4*r
    yMouth = yPos - 3/4*r
    pygame.draw.arc(screen,colour,(xMouth,yMouth,wMouth,wMouth),math.pi,2*math.pi,1)

def random_color():
    random_number = random.randint(1,3)
    if random_number == 1:
            return GREEN
    elif random_number ==2:
            return BLUE
    else:
            return RED

# set up clock to control frames per second
clock = pygame.time.Clock()
FPS = 120

# set main loop to True so it will run
main = True
# main loop
while main:
    for event in pygame.event.get():  # check for any events (i.e key press, mouse click etc.)
        if event.type == pygame.QUIT:  # check to see if it was "x" at top right of screen
            main = False # set the "main" variable to False to exit while loop

        xPos = xPos + dxPos
        yPos = yPos + dyPos

        if x >= 750:
                dx = -abs(dx)
                color = random_color()
        elif x <=50:
                dx = abs(dx)
                color = random_color()

        if y <= 50:
                dy = abs(dy)
                color = random_color()
        elif y >=550:
                dy = -abs(dy)
                color = random_color()

        if x2 >= 775:
                dx2 = -abs(dx2)
                color2 = random_color()
        elif x2 <= 25:
                dx2 = abs(dx2)

        if   y2 <= 25:
                dy2 = abs(dy2)
                color2 = random_color()
        elif y2 >= 575:
                dy2 = -abs(dy2)
                color2 = random_color()

        if   x3 >=700:
                dx3 = -abs(dx3)
                color3 = random_color()
        elif x3 <= 100:
                dx3 = abs(dx3)
                color3 = random_color()

        if   y3 <= 100:
                dy3 = abs(dy3)
                color3 = random_color()
        elif y3 >= 500:
                dy3 = -abs(dy3)
                color3 = random_color()

Tags: thetoifmainrandomabsscreenpygame
3条回答

当语句xPos = xPos + dxPosyPos = yPos + dyPos时,首先读取变量xPosyPos,然后添加dxPosdyPos,最后将结果分配给xPosyPos
第一步,读取失败,因为变量在读取之前没有定义。在主应用程序循环之前定义变量。e、 g:

xPos, yPos = 100, 100 

main = True
# main loop
while main:
    # [...]

此外,您的应用程序中存在Indentation问题。您必须在应用程序循环而不是事件循环中执行逻辑。
此外,您必须添加dxdy,而不是dxPosdyPos

# set main loop to True so it will run
main = True
# main loop
while main:
    for event in pygame.event.get():  # check for any events (i.e key press, mouse click etc.)
        if event.type == pygame.QUIT:  # check to see if it was "x" at top right of screen
            main = False # set the "main" variable to False to exit while loop

    # INDENTATION 
    #< | 
    xPos = xPos + dx
    yPos = yPos + dy

    if x >= 750:
            dx = -abs(dx)
            color = random_color()
    elif x <=50:
            dx = abs(dx)
            color = random_color()

    if y <= 50:
            dy = abs(dy)
            color = random_color()
    elif y >=550:
            dy = -abs(dy)
            color = random_color()

    if x2 >= 775:
            dx2 = -abs(dx2)
            color2 = random_color()
    elif x2 <= 25:
            dx2 = abs(dx2)

    if   y2 <= 25:
            dy2 = abs(dy2)
            color2 = random_color()
    elif y2 >= 575:
            dy2 = -abs(dy2)
            color2 = random_color()

    if   x3 >=700:
            dx3 = -abs(dx3)
            color3 = random_color()
    elif x3 <= 100:
            dx3 = abs(dx3)
            color3 = random_color()

    if   y3 <= 100:
            dy3 = abs(dy3)
            color3 = random_color()
    elif y3 >= 500:
            dy3 = -abs(dy3)
            color3 = random_color()

当它到达xPos = xPos + dxPos时。您从未放置过xPos = 0。因此,您所需要做的就是将xPos = 0或任何您希望它在循环之前等于的值。这与yPos相同

这可能是因为在主while循环的第一次迭代中,对xPos的第一个引用是xPos = xPos + dxPos,其中xPos尚未定义

相关问题 更多 >