0

I'm attempting to write a program using pygame to create a class called Dot() that allows me to implement Dot objects, which will be small, 2 pixel radius circles. I'm trying to create them within my main game loop, but I keep getting the error message "name 'x' is not defined". I'm not sure why that would be the case. If it were going to throw such an error I would expect it to first say that color isn't defined, since it's listed first in the list of parameters. I'm not sure if my error is caused by the way I wrote the class itself or if it's something in my implementation of the class (I'm almost positive I did this wrong, but I've tried it a few different ways as well and keep getting the same error), or if it's or both.

#!/usr/bin/env python

import random, pygame, sys
from random import randint 
from pygame.locals import *

pygame.init()

FPS = 30
fpsClock = pygame.time.Clock()

DISPLAYSURF = pygame.display.set_mode((700, 700), 0, 32)
pygame.display.set_caption('Version02')

WHITE = (255, 255, 255)
RED      = (255,   0,   0)
GREEN    = (  0, 255,   0)
BLUE     = (  0,   0, 255)

class Dot():
    def __init__(self, color, x, y):
        self = pygame.draw.circle(DISPLAYSURF, color, (x, y), 2, 0)
        self.color = getDotColor()
        self.x = getDotX()
        self.y = getDotY() 

    def getDotColor():
        color = random.choice([RED,  GREEN, BLUE])
        return color

    def getDotX():
        x = randint(0, 700)
        return x

    def getDotY():
        y = randint(0, 700)
        return y   

while True: #main game loop    
    DISPLAYSURF.fill(WHITE)

    dot = Dot(color, x, y)#I'm not exactly sure how to implement this correctly  

    for event in pygame.event.get(): 
        if event.type == QUIT:
            pygame.quit() 
            sys.exit()

    pygame.display.update()
    fpsClock.tick(FPS)
5
  • dot = Dot(color, x, y) - what are x and y here? Where do they come from? That color is somehow defined may be because you used from pygame.locals import *, and this imported the name color. Commented Nov 25, 2018 at 14:54
  • They're supposed to be the x and y coordinates for the location of the circle, and color would be the color of the circle. Supposed to be, though I know this is not the correct syntax. I'm just not sure what would be. Commented Nov 25, 2018 at 14:59
  • This is correct syntax. The problem happens during execution, because, as the error message tells you, name x is not defined. Where is x defined? Can you output its value with print(x)? (Spoiler: you can't, for the same reason). Commented Nov 25, 2018 at 15:02
  • I thought I had defined x, y, and color in the class Dot(), which would be taken when I created an object of that class. Commented Nov 25, 2018 at 15:38
  • 1
    No, you didn't define them: you defined self.x, self.y, which are different. Anyway, when you do Dot(color, x, y) and get this error, it happens before the attempt to instantiate Dot and, thus, before the call to Dot.__init__. Try this: create a new blank Python file and write print(x). Then run this script. Why do you get this exact error message? What is x here? (Spoiler: you cannot know, and nor can the Python interpreter, because this name is not defined) Commented Nov 25, 2018 at 15:43

1 Answer 1

1

dot = Dot(color, x, y)#I'm not exactly sure how to implement this correctly

You do not define a x or y value before. Your error comes from here. Same for the color. If you want color and dot coordinates to be randomly created each time you create a Dot object (at least I understand this is what you are looking for) you have to rewrite the __init__ method:

class Dot():
    def __init__(self):
        self.color = getDotColor()
        self.x = getDotX()
        self.y = getDotY()
        self = pygame.draw.circle(DISPLAYSURF, self.color, (self.x, self.y), 2, 0)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.