0

I am trying to code a happy face to bounce off walls and randomly change colour as it hits off a wall. Currently, I have set the colours and the code to cause it to bounce off the wall and change colour. But, my code isn't working as it says "NameError: name 'xPos' is not defined" even tho I did define it.

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()
i'm sorry
  • 53
  • 5

3 Answers3

1

This is probably because on the first iteration of your main while loop the first reference to xPos is xPos = xPos + dxPos, where xPos has not been defined yet!

dspencer
  • 4,297
  • 4
  • 22
  • 43
Caleb Irwin
  • 398
  • 2
  • 5
  • What would I define it as? I defined it here "def drawHappy(xPos,yPos,r,colour)" – i'm sorry Apr 09 '20 at 02:35
  • `xPos` was only defined in the function `drawHappy()`'s local scope, not the global. Define `xPos` above the main loop as the X coordinate you want your happy face to start at. – Caleb Irwin Apr 10 '20 at 15:02
0

When it gets to xPos = xPos + dxPos. You have never put xPos = 0. So all you need to do is put xPos = 0 or whatever you want it to equal before the loop. This is the same for yPos.

The Big Kahuna
  • 2,097
  • 1
  • 6
  • 19
0

When the statements xPos = xPos + dxPos, yPos = yPos + dyPos, then first the variables xPos and yPos are read, then dxPos and dyPos are added and finally the result is assigned to xPos and yPos.
The 1st step, the reading fails, because the the variables are not defined before they are read. Define the variables before the main application loop. e.g:

xPos, yPos = 100, 100 

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

Furthermore, there is an Indentation issue in your application. You have to execute the logic in the application loop rather than the event loop.
Further you have to add dx and dy rather than dxPos and dyPos.

# 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()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • ok so I did everything you guys said but the happy face still just stays in the middle and isn't bouncing off walls or changing colour. It is able to run now but it's not following any of the code written.. – i'm sorry Apr 09 '20 at 15:29
  • @i'msorry Your question was *"NameError: name 'xPos' is not defined"*. Is that a new question? I am not a clairvoyant. The snippet in your question is not a [Minimal, **Complete, and Verifiable example**](https://stackoverflow.com/help/mcve). – Rabbid76 Apr 09 '20 at 15:30
  • @i'msorry I just can guess. Add `dx` and `dy` rather than `dxPos` and `dyPos`. e.g.: `xPos = xPos + dx` – Rabbid76 Apr 09 '20 at 15:33