1
# Creates a Dot Instance.
class Dot(object):
    velx=0
    vely=0
    def __init__(self, xloc=0, yloc=0,color=(0,7,0)):
        self.color=color
        self.xloc=xloc
        self.yloc=xloc

    def entity(self):
     for event in pygame.event.get():
        pygame.draw.rect(gameDisplay, (self.color), (self.xloc,self.yloc, 50, 50))
        pygame.display.update()


GameExit=False

# Main Function
def Main():
    global x,y,gameDisplay
    Red=Dot(50,50,color=(0,0,255))
    Blue=Dot(150,150,color=(255,0,0))
    Red.entity()
    Blue.entity()

    pygame.display.update()
    while not GameExit:
        if GameExit==False:
            pygame.QUIT

Main()

Im trying to create a second class instance that will display a Red Dot but it doesn't seem to be appearing. The first class instance works and it creates a Blue Dot on the Display. What is it that I am doing wrong?

3
  • Name your variable with lowercase please : Red, Blue Commented Nov 19, 2019 at 21:37
  • As a better suggestion - follow python.org/dev/peps/pep-0008 Commented Nov 19, 2019 at 21:39
  • @BłażejMichalik no, you are correct, I just read it incorrectly, did not see the assignment statements, speaking of PEP8 formatting... Commented Nov 19, 2019 at 21:46

1 Answer 1

1

Try this:

def entity(self):
    pygame.draw.rect(gameDisplay, self.color, (self.xloc, self.yloc, 50, 50))
    pygame.display.update()

See it in online REPL with pyGame: https://repl.it/repls/ActiveMisguidedApplescript


The problem stems from implementation of .entity().

In your code:

for event in pygame.event.get():
    ...

You are essentially drawing stuff only if there's an event in the queue. So the call to Red.entity() exhausts this queue. The call to Blue.entity() doesn't even enter the aforementioned for loop, as there's nothing to iterate on at that particular moment - pygame.event.get() returns an empty list.


From pygame.event.get() docs:

This will get all the messages and remove them from the queue.


Also, your event loop looks wrong. It should be (in Main):

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            return
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.